C++ data structure-algorithm ideas for graph-related operations

table of Contents

1. Judgment of the loop:

2. Prim-minimum spanning tree

3. Kruskal-minimum spanning tree

4. Dijkstra-shortest path

5. The longest path of the graph (do not take the repeated path)

6. The critical path of a directed acyclic graph


1. Judgment of the loop:

Using depth-first traversal, in the directed graph, if the visited point is repeatedly visited during the traversal process, then the directed graph is a loop; if it is an undirected graph, the ancestor node is repeatedly visited, then the undirected graph There is a loop.

2. Prim-minimum spanning tree

You need to enter a starting node and create a new object array arr [size] (nodeIndex,lowcost), where nodeIndex refers to the array subscript of the node in the adjacency matrix or adjacency table, where arr[ i] represents the value of nodeIndex to i The shortest path, the cost is low cost.

To traverse the edge of the newly added node, compare arr[i], if the new edge is smaller than arr[i], then re-cover the value of arr[i], and then rotate the point with the smallest lowcost, and then proceed Join. Perform this operation repeatedly.

 

1. Assuming that the input is A at the beginning, then traverse the edges of A, initialize the minimum value, and get the smallest edge (A, C). Then add C to the set

2 Traverse all the edges of the new node C, where (C, B) is smaller than the original (A, B); (C, E), (C, F) are smaller than the original (A, E) (A ,F) To be small, cover the value, and repeat step 2 until it is traversed n times (total number of nodes)

Times\node A B C D E F Points added Minimum node Remarks
1 (null) (A,6) (A,1) (A,5) (A,∞) (A,∞) (A) C Add C to the set
2 (null) (C,5) (null) (A,5) (C,6) (C,4) (A,C) F (C,B) is smaller than the original (A,B), covering
3 (null) (C,5) (null) (F,2) (C,6) (null) (A,C,F) D (F,D) is smaller than (A,D)
4 (null) (C,5) (null) (null) (C,6) (null) (A,C,F,D) B D’s side is not as small as the original
5 (null) (null) (null) (null) (B,3) (null) (A,C,F,D,B) E (B,E) is smaller than (C,E)
6 (null) (null) (null) (null) (null) (null) (A,C,F,D,B,E) end A total of 6 nodes, a total of 6 traversal

3. Kruskal-minimum spanning tree

  At the beginning, each node is a set of connected components. The shortest edge is selected among the connected components, and the connected components are merged with the shortest edge, and iteratively until all the nodes are in the same connected component and end, or there is no connectable edge. When the shortest edge is added, a loop appears, then the next edge is selected

Sort the edges from smallest to largest:

(A,C,1)(D,F,2)(B,E,3)(C,F,4)(A,D,5)(B,C,5)(C,D,5)(A,B,6)(C,E,6)(E,F,6)

Initial connected component group A B C D E F
Small side 1 (A, C) (A,C) B D E F
The second small side (D, F) (A,C) B (D,F) E
Small side 3 (B, E) (A,C) (B,E) (D,F)
Small side 4 (C, F) (A,C,D,F) (B,E)
The 5th small side (A, D), already in the same set, forms a loop (A,C,D,F) (B,E)
Small side 6 (B, C) (A,C,D,F,B,E)
All nodes are in the same set, end            

4. Dijkstra-shortest path

    Goal: Enter a starting node, find the shortest path and cost from the starting node to other nodes. The adjacency matrix adjacencyMatrix_ needs to be initialized in advance.

Suppose you enter the starting point A, the current path A (also the visited point),

When the object element of the object array (the minimum cost lowcost_, and the shortest path pathIndex_)

1. Find the cost of all points (A,C,10) (A,E,30),(A,F,100), and select the direct target node currentPathIndex(C) with the smallest cost. Adding to the current path currentPath (A,C) costs the lowestCost of 10.

2. That is

currentPath= lowestPaths[currentPathIndex].pathIndex_  + currentPathIndex ;
lowestCost=lowestPaths[currentPathIndex].lowcost_

3. Traverse LowestPaths[0....j...n] (exclude points in CurrentPath)

if (lowestCost+ adjacencyMatrix_[currentPathIndex][j] <lowestPaths[j].lowcost_) {
    lowestPaths[j].lowcost_= lowestCost+ adjacencyMatrix_[currentPathIndex][j] ;
    lowestPaths[j].pathIndex_=currentPath;
}

  Repeat 2, 3

Solution steps:

 


5. The longest path of the graph (do not take the repeated path)

     Regardless of whether it is a directed graph or an undirected graph, the recursive method is applicable. It is recommended to use the adjacency matrix data structure. When the graph is a directed acyclic graph, the critical path is sought. The main idea is: when the longest path from the source node to the predecessor node of the target node is known , the longest path from the source node to the target node can be obtained..

Join to find the longest path from node V0 to Vn. Then first list the predecessor node set of Vn (Vxi, Vxi...) 

There is a recursive function fun(V0,Vn) that represents the longest path from V0 to Vn. Its value =Max(fun(V0,Vxi)+arc[Vxi][Vn]). Find the longest path through this method. You can also refer to this method to find the shortest path

Function format double fun( src, target, vector<int> & hasVisits)

Initialize the adjacency matrix arc[][] in advance.

0. If the source node and target node are the same, return 0.

1. The visited node set hasVisits is a reference parameter, and the target node target is added to the set.

2. Find the predecessor node of the target node that has not been visited yet.

3. Determine whether the adjacency matrix arc[source node][target node] is ∞, if it is true, initialize the maximum path maxValue as the smallest floating point value, if it is false, maxValue is set to arc[source node][target node] point].

4. Copy hasVisits to make a copy, traverse the set of predecessor nodes,

value = fun (source node, a predecessor node, a copy of hasVisits reference) + arc[a predecessor node][target node]

If value is greater than maxvalue, maxvalue is assigned to value and hasVisit is assigned to a copy of hasVisits. At the end of the recursion, hasVisits is the maximum path, and the return value is the maximum cost.

6. The critical path of a directed acyclic graph

The main idea is to find the node where the earliest start time and the latest end time are equal

The earliest start time of the start node is 0, and the cost is regarded as time.

The earliest start time of each node = max (the earliest start time of the predecessor node + arc[predecessor node][current node])

The latest end time of the end node = the earliest start time of the end node.

The latest end time of each node = min((The latest end time of the successor node-arc[current node][successor node]) 

The node where the earliest start time and the latest end time are equal is the critical node, and the path is the critical path

Guess you like

Origin blog.csdn.net/superSmart_Dong/article/details/108805855