Searching a graph accesses all the points along the graph edge in an orderly manner. The graph search algorithm allows us to find a lot of graph structure information. Graph search technology is the core of Graph Algorithm neighborhood.
1. Two computer representations of the image
1. Adjacent table: This method indicates that the sparse graph is concise and compact.
Typedef struct {int adjvex; // struct arcnode * Next; int weight; // Edge Weight} arcnode; typedef struct {vertextype data; arcnode * firstarc ;} vnode, adjlist [max_vertex_num];
A. the vertex in the adjacent table is stored in any order, and the adjacent vertex of each vertex is stored in any order.
B. The number of vertices in the adjacent table is the number of nodes in the graph. If G is an undirected graph, the length of all the adjacent tables is 2E. If G is a directed graph, the length of all adjacent tables is E.
C. Whether directed graph or undirected graph, the required storage capacity is O (V + E ).
D. Deficiency: to determine whether an edge exists, you need to search all vertices in the vertex's adjacent table.
2. Adjacent matrix method: This method is suitable for dense graphs and can quickly determine whether two vertices are adjacent.
The adjacent matrix first numbers the vertices in the graph. 1... | v |, after the number, A | v | x | v | matrix is used to represent the graph. Whether the element AIJ in the matrix is 0 indicates whether there is an edge between VI and vj, the storage space of the matrix is O (| v | ^ 2), independent of the number of edges.
A. In some undirected graph applications, only the upper and lower triangle matrices can be stored to save storage space;
B. Edge weights can be stored with matrix elements;
C. Use a binary bit to indicate whether an edge exists or not in a non-weighted graph to save storage space.
2. Graphic search algorithm with breadth-first priority
Given a graph G = (V, E) and the Source Vertex S, the breadth-first search algorithm systematically explores all the edges of G and finds all vertices reachable from S, calculate the distance from S to all these vertices (minimum number of edges ). This algorithm generates a breadth-first tree with a root of S and contains all reachable vertices. For any node departing from S, the shortest path from S to V in the breadth-first tree corresponds to the path from S to V in G. The algorithm is equally effective for Directed Graphs and undirected graphs. From beginning to end, the algorithm expands outward through the boundary between the located vertex and the unfound vertex.
1. Execution Process: The breadth-first search is colored for each vertex (white gray and black). It is white at the beginning. The first time we encounter a vertex, we say that the vertex is discovered. At this time, the vertex changes from white to non-white. Gray and black vertices are the discovered vertices. All vertices adjacent to the Black vertex are discovered. Gray vertices can be adjacent to some white vertices, representing the existing and undiscovered boundary.
2. breadth-first search creates a breadth-first tree. When a white vertex is found, the vertex and related edge are added to the tree.
3. Additional Data Structure: stores the color array color, the PI of the parent node, And the d that stores the shortest distance from the Source Vertex s to the vertex, and stores the first-in-first-out queue Q of the gray node.
BFS(G, s)1 for each vertex v in {G}-{s}2 do color[v]<--white3 d[v]<--04 pi[v]<--05 color[s]<--gray6 d[s]<--07 pi[s]<--nil8 Q<--NULL9 Enqueue(Q, s)10while Q != NULL11do u<--Dequeue(Q)12for each v in Adj[u]13do if color[v]<--white14then color[v]<--gray15d[v]<--d[u]+116pi[v]<--u17Enqueue(Q, v)18color[u]<--black
4. The results of the breadth-first search are related to the access sequence of the adjacent points of the given vertex in the 12 rows. The resulting breadth-first tree may be different, but the calculated d is the same.
5. The parent node of the gray vertex may not be a black vertex. When a gray node is generated in the middle of Line 11-18, its parent node has not become black.
6. Run time: a 13th-row test ensures that each node is only in the queue once at most, and therefore only goes out of the queue once at most. The queue operation time is O (V ), each vertex accesses its neighbor table when it is out of the queue. Therefore, each vertex's neighbor table can only be accessed once at most. The sum of the length of all the neighboring tables is O (e ), therefore, the total time complexity is O (V + E ).
7. For a graph, the distance between each node that can be reached by s can be obtained through the breadth-first search.
8. breadth-first tree: a breadth-first tree is created at the same time as the BFS search graph. This tree is represented by the PI domain of each node.
Precursor subgraph: For graph G = (V, E), given the origin s, its precursor subgraph GPI = {VPI, EPI}
Among them, VPI = {vε V, Pi (v )! = Nil} bytes {s}
EPI = {(PI (V), V): vε VPI-{s }}
If the VPI consists of all the points accessible from S, GPI is a breadth-first tree and | EPI |=| VPI |-1
Theorem:If DFS is applied to a directed graph or undirected graph, the PI domain created during the process meets the following conditions: its precursor subgraph GPI = {VPI, EPI} is a breadth-first tree.
Print-Path(G, s, v)1 if s = v2 then print s3 else if pi[v] = nil4 then print error "no path exist"5 else Print-Path(G, s, v)6 print v
3. Graphic search algorithm with deep priority
Deep-first search follows the search graph as deep as possible. In the process of deep priority search, if a newly discovered node still has an edge to be explored from this node as the starting point, it will continue to be explored along this edge. When all the edges of node v have been explored or can be explored, it goes back to the edges starting from the node v, this process continues until all nodes that can be reached from the origin are found. If no node is found, select one from the node and start exploring again. If this is repeated until all nodes are explored.
1. The advanced subgraph of deep priority search forms a depth priority forest composed of several depth priority trees.
2. The depth-first search is also colored by nodes. It is white at the beginning, gray at the time of exploration, and black at the end. This ensures that each node only exists in a deep priority tree.
3. In addition to creating a deep priority tree, DFS also stamps each node with a timestamp. When the node is first discovered (dimmed) write down the first timestamp d [v]. when the end of the inspection V's adjacent table (set to black), write down the second timestamp f [v]. V is white before d [v], V is black after f [v], and V is gray between D [v] and f [v.
4. Deep Priority Search Algorithm: composed of DFS and DFS-visit Processes
DFS(G)1 for each vertex v in V2 do color[v]<--white3 d[v]<--04 f[v]<--05 pi[v]<--nil6 time<--07 for each vertex u in V8 do if color[u] = white9 then DFS-Visit(G, u)DFS-Visit(G, u)1 color[u]<--gray2 time<--time+13 d[u]<--time4 for each vertex v in Adj[u]5 do if color[v] = white6 pi[v]<--u7 DFS-Visit(G, v)8 color[u]<--black9 time<--time+110f[u]<--time
The time complexity is always (V + E ).
5. Edge classification: Based on the depth priority forest generated by DFS, edge can be divided into four types: Tree edge, forward edge, reverse edge, and cross edge.
6. The discovery and completion time of deep Priority Search are in parentheses.
7. White path theorem: In the depth-first Forest of a graph G = (V, E) (directed or undirected graph, node V is the descendant of node u. If you find u at d [u] Time in the search, you can start from vertex u, A path completely composed of white vertices reaches v.
8. You can modify the algorithm DFS to classify the algorithm when edges are encountered. The core idea of the algorithm is that for each edge (u, v), when the edge is first explored, it is determined by the color of the node v, to classify the edge (but the forward side and cross side cannot be distinguished by color ):
A. White indicates that it is a tree edge;
B. Gray indicates that it is a reverse edge;
C. Black indicates the positive side or cross side. If d [u] <D [v], the side (u, v) is the forward side, and the opposite is the cross side.
9. The classification above may be ambiguous for undirected graphs. In the deep-first search of undirected graph G, each edge of G is either a tree edge or a reverse edge.
Iv. topological sorting
The topological sorting of a graph can be seen as a sequence of all vertices of the graph following the horizontal line, so that all the directed edges are directed from left to right.
1. The following short answer algorithm can sort the directed graph topology:
Topological-sort (g)
A. Call DFS (g) to calculate the f [v] of each node V;
B. Insert each vertex into the front end of the linked list;
C. Return a linked list composed of vertices.
Because the Run Time of the Deep-priority search is round (V + E), the time used to insert each of the | v | vertices into the linked list is O (1 ), therefore, the running time of Topology Sorting is always (V + E ).
Can the above algorithm be changed to insert a fixed point to the end of the linked list when we find every fixed point D [v?No. As shown in, shoes is in front of socks. In fact, there is a directed edge between socks and shoes, and shoes should be behind socks.
2,Theorem:Directed Graph G has no loop. If the directed graph G does not have a reverse edge.
3. Theorem: the topological-sort (g) algorithm can generate topological sorting for directed acyclic graph G.
5. Strongly Connected branches
1. In a directed graph, if any two different points are reachable, the directed graph is strongly connected. A strongly connected subgraph of a directed graph is calledStrongly Connected branches.
2. Many algorithms for Directed Graphs start from the decomposition step. This decomposition divides the original problem into several subproblems, where each subproblem corresponds to a strongly connected branch. Constructing the connections between strongly connected branches links the sub-problem solutions.Branch chartTo represent this structure.
3. The algorithm for finding strongly connected branches in a graph G = (V, E) uses the transpose of G, that is, e is composed of the edges in g after changing the direction. If the graph G's adjacent table is known, the time required to create the GT is O (V + E ). G and G' have exactly the same strongly connected branch, that is, in G, U and V are mutually reachable only when they are mutually reachable in GT.
4. The following algorithm runs at least (V + E) to obtain the strongly connected branch of Directed Graph G = (V, E). This algorithm uses two deep-first searches, one operation is performed on Graph G and the other on Graph G:
Strongly_connected_components (g)
A. Call DFS (g) to calculate the completion time of each node U. F [u];
B. Calculate G ';
C. Call DFS (GT), but consider each node in the descending order of F [u] in the main loop of DFS (as calculated in the first line );
D. output the vertices of each tree in the depth-first forest generated in step 1 as their independent strongly connected branches.
The idea of the strongly_connected_component algorithm comes from an important property of the branch graph gscc = (VSCC, ESCC:
Suppose that the strongly connected branch of G is C1, C2,..., CK. The vertex set VSCC is {V1, V2,..., VK}. Each strongly connected branch Ci of G contains a vertex VI. If a certain X ε Ci and a certain y ε CJ and G contain a directed edge (x, y), then an edge (Vi, vj) in ESCC is generated; on the other hand, the figure gscc can be obtained by shrinking the edges in the same strongly connected branch where the associated vertex is in G.
5,Theorem:Let C and C' be two different strongly connected branches in directed graph G = (V, E). Let's set U, V, C, U', V, C ′, assuming that there is a path U → U' in G, it is impossible for g to have a path v → V at the same time.
6,Theorem:Set C and C' to two different strongly connected branches in directed graph G = (V, E. Assume that an edge (u, v) in E, where u in C, V in c ', F (c)> F (C ').
Inference: Let C and C' be two strongly connected branches in directed graph G = (V, E). Assume that there is an edge (u, v, u, C, and V, c ′. then f (c) <F (C ′).
7. Why strongly_connected_components (g) can work properly:
A. The second in-depth priority search on GT: Starting from strongly connected branch C, F (c) is the largest, and searching starts from a vertex x of C, access all vertices of C.
B. According to the inference, GT has no edge from C to other connected branches. The tree whose root is X contains only vertices in C.
C. The next access to c 'and F (c') is the largest out of F (c). Similar to the access to C, when the algorithm performs In-depth priority search on GT in line 3, any side of this branch points to the branch that has been accessed.
Therefore, each deep priority tree is a strongly connected branch.