TJUNGBLUT Math: Introduction

[Title] Introduction to the technical principles of the diagram theory algorithm in the Java class library [Introduction] The theory is one of the important mathematical branches in computer science, which is used to study the nature and algorithm of the map.The Java class library provides various graphic algorithms that can solve various problems such as the shortest path, minimum generating tree, and topology sorting.This article will introduce the technical principles of the diagraming algorithm in the Java class library, and deepen the explanation through the Java code example. 【text】 1. Graphological foundation The figure is a collection of a set of vertices (nodes) and a set of edges (line segments connected to the vertex).In the figure, the node represents the entity, and the relationship between nodes is represented.The diagram can be divided into a direction diagram and an un or -. There is directional characteristics in the direction diagram, and the side of the unwanted diagram has no directionality. 2. The representation of the graph The Java class library provides a variety of graphic ways, common adjacent matrix and adjacent tables. -The adjacent matrix: The adjacent matrix is a two -dimensional array. The lines and columns of the array represent the nodes in the figure, and the elements in the array indicate the edges between the nodes.The adjacent matrix is very efficient when looking for the weight of the edge and edge between the two nodes. -The adjacent table: The adjacent table uses a set of linked lists to represent the nodes and edges in the graph.Each node corresponds to a linked list, and the element in the linked list indicates the neighbor nodes and its corresponding edges adjacent to the node.The adjacent table saves more space when storing the sparse map. 3. Graphic algorithm (1) The shortest circuit diameter algorithm -Dijkstra's Algorithm: It is used to solve the shortest path problem of the single source.Based on greedy strategy, the algorithm gradually updates the minimum path to the source node through the ideological planning of dynamic planning.Example code: import org.jgrapht.Graph; import org.jgrapht.alg.shortestpath.DijkstraShortestPath; // Create a picture with a weight Graph<String, DefaultWeightedEdge> graph = new SimpleDirectedWeightedGraph<>(DefaultWeightedEdge.class); graph.addVertex("A"); graph.addVertex("B"); graph.addEdge("A", "B"); DefaultWeightedEdge edge = graph.getEdge("A", "B"); graph.setEdgeWeight(edge, 10.0); // Use the Dijkstra algorithm to calculate the shortest path DijkstraShortestPath<String, DefaultWeightedEdge> shortestPath = new DijkstraShortestPath<>(graph); Double distance = shortestPath.getPathWeight("A", "B"); System.out.println ("The shortest path distance is" + distance); (2) Minimum generating tree algorithm -Prim's Algorithm: It is used to solve the minimum generic tree of the inferior graph.The algorithm starts from any one -apex, and gradually adds the closest to the generated tree to the generated tree until the generated tree contains all nodes.Example code: import org.jgrapht.Graph; import org.jgrapht.alg.spanning.PrimMinimumSpanningTree; // Create an unlimited chart with weight Graph<String, DefaultWeightedEdge> graph = new SimpleWeightedGraph<>(DefaultWeightedEdge.class); graph.addVertex("A"); graph.addVertex("B"); graph.addEdge("A", "B"); DefaultWeightedEdge edge = graph.getEdge("A", "B"); graph.setEdgeWeight(edge, 10.0); // Use the Prim algorithm to calculate the minimum generation tree PrimMinimumSpanningTree<String, DefaultWeightedEdge> mst = new PrimMinimumSpanningTree<>(graph); Graph<String, DefaultWeightedEdge> minimumSpanningTree = mst.getSpanningTree(); System.out.println ("The minimum set of the borders of the tree:" + minimumspanningtree.edgeset ()); (3) Tourism sorting algorithm -Depth first search (DFS): used to sort the topology of a ringless diagram.The algorithm starts from a node and traverses the adjacent nodes of the node recursively until all nodes are accessed.Example code: import org.jgrapht.Graph; import org.jgrapht.alg.cycle.CycleDetector; import org.jgrapht.alg.dag.TopologicalOrderIterator; // Create a direction without a loop diagram Graph<String, DefaultEdge> dag = new DefaultDirectedGraph<>(DefaultEdge.class); dag.addVertex("A"); dag.addVertex("B"); dag.addVertex("C"); dag.addEdge("A", "B"); dag.addEdge("B", "C"); // Define whether the diagram has a ring CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<>(dag); boolean hasCycle = cycleDetector.detectCycles(); if (!hasCycle) { // Use the topology sorting algorithm to obtain the sequence of the vertex of the apex without a ring graph TopologicalOrderIterator<String, DefaultEdge> iterator = new TopologicalOrderIterator<>(dag); System.out.print ("Topology Sorting Result:"); while (iterator.hasNext()) { System.out.print(iterator.next() + " "); } } [Conclusion] This article introduces the technical principles of the diagram algorithm in the Java class library, including the foundation foundation, the representation of the map, and the common graph theory algorithm.Through the Java code example, readers can better understand the implementation and application of these algorithms.The illustration algorithm is widely used in various fields, and mastering these algorithms is of great significance for developers.