Search a graph is ordered along the edge of the graph to visit all fixed points, the graph of the search algorithm can enable us to find a lot of graph structure information, Graph search technology is the core of the graph algorithm neighborhood.
One, two kinds of computer representations of graphs
1. adjacency table: This method means that the sparse graph is relatively compact and simple.
typedef struct{int ADJVEX;//The position of the adjacency vertex struct arcnode *next;int weight;//edge weight}arcnode;typedef struct{vertextype data; Arcnode *firstarc;} Vnode, Adjlist[max_vertex_num];
A, the vertices in the adjacency table are stored in random order, and the adjacency vertices of each vertex are stored in random order.
b, the number of vertices in the adjacency table is the number of nodes in the graph V, if G is a non-direction graph, then the length of all adjacency table and is 2E, if G is a graph, all adjacency table length and E.
C, no matter the graph or the graph, the required storage capacity is O (v+e).
D, insufficient: Determine whether an edge exists the need to search all vertices in the adjacency table of the vertex.
2. Adjacency Matrix Method: This method is suitable for dense graphs. It is very fast to infer whether two vertices are adjacent.
The adjacency matrix first numbers the vertices in the graph 1...| v|, after numbering, with one | v| X | v| Matrix to represent the diagram. Whether the element aij in the matrix is 0 indicates whether there is an edge between VI and VJ, and the space for the storage matrix is O (| v|^2). Independent of the number of sides.
A, in some applications of the non-direction diagram. In order to save storage space can only store the upper triangular or lower triangular matrix.
b, the weight of the edge can be stored with matrix elements;
c, for a non-weighted graph with a bits to indicate whether there is an edge can save storage space.
Second, breadth-first graph search algorithm
Given a graph g= (V, E) and source point S, the breadth-first search algorithm systematically explores all the sides of G. Thus discovering all the vertices from S can reach. and calculates the distance of s to all of these vertices (the minimum number of sides).
At the same time, the algorithm generates a breadth-first tree with a root of S and includes all the vertices that can be reached.
For random nodes departing from S. The shortest path from S to v in the path corresponding to the S to V in the breadth precedence tree. The algorithm has the same effect on both the graph and the graph.
The algorithm extends from start to finish through the boundary between the found and the vertices that are not found.
1, the operation process: Breadth First search for each vertex coloring (lime black), the beginning of time are white. The first time I hit a vertex. The vertex is found, at which point the vertex is changed from white to non-white. The gray and black vertices are the vertices that are found. All vertices that are adjacent to the black vertex are discovered. Gray vertices can be contiguous with some white vertices, representing discovered and uncovered boundaries.
2, Breadth first search established a breadth first tree, whenever a white vertex is found, the vertex and the associated edge is added to the tree.
3. Additional data structure: Store the color of the array color, store the parent node's pi, and store the shortest distance from the source point s to the vertex D, storing the gray node's FIFO queue Q.
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]<-- 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 breadth-first search are related to the order of access of the neighboring points of a given vertex in 12 rows, and the resulting breadth-first tree may be different, but the calculation of D is the same.
5. The parent node of the gray vertex is not necessarily a black vertex, when a gray node is generated in the middle of 11-18 rows. Its parent node has not been blackened.
6, Execution time: The 13th line of the test to ensure that each node at most only into the queue once, so also at most only out of the queue once. The time taken by the queue operation is O (V). Each vertex visits its adjacency table when it is out of the queue. So the adjacency table for each vertex is only visited once, and the sum of all adjacency table tables is O (E), so the total time complexity is O (v+e).
7, for a graph, breadth-first search can get from the s can reach each node distance.
8. Breadth-first tree: A breadth-first tree is established at the same time as the BFS search graph, which is represented by the Pi field of each node.
Precursor diagram: For figure g= (v,e), given the Origin s, its precursor to GPI = {Vpi. EPI}
The VPI = {v∈v, pi (v)! = Nil}∪{s}
Epi = {(Pi (v), V): V∈VPI-{s}}
The VPI is assumed to consist of all fixed points from S. Then the GPI is a breadth-first tree, and | Epi | = | Vpi | -1
theorem: Assume that Dfs is applied to a single, or no-map, the PI domain established at the same time satisfies the condition: its precursor, 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, PI[V]) 6 Print V
Three, depth-first graph search algorithm
The strategy followed by depth-first search is as deep as possible in the search graph. In the process of depth-first search, for newly discovered nodes. Suppose there are edges that are explored with this node as the starting point. Continue to explore along this side. When all the sides of Node v have been explored or are boundless. It goes back to the edge where the node V is found to be the starting point, and the process continues until it finds all the nodes that are available from the origin. Assuming that there are still no discovered nodes, choose one from the beginning and start exploring again. Assume this repetition. Until all the nodes have been explored.
1. The ancestors of the depth-first search the sub-chart forms a depth-priority forest composed of several depth-priority trees.
2, Depth first search also for the node coloring, the most beginning is white. Gray when you are searching. The end is black. This ensures that each node exists only in a tree of depth precedence.
3, in addition to creating a depth priority tree. DFS also stamps each node with a timestamp, and when the node is first discovered (gray), note the first timestamp d[v], and when you finish checking the adjacency table for V (black), note the second timestamp f[v].
Before D[v] v is white. After F[v] v is black. Between D[v] and F[v] V is gray.
4. Depth-First search algorithm: There are two processes consisting of Dfs and dfs-visit
DFS (G) 1 for each vertex v in V2 does color[v]<--white3 d[v]<--04 f[v]<--05 pi[v]<--nil6 time<--07 for each ve Rtex u in V8 does 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<--t Ime+110f[u]<--time
The time complexity is θ (v+e).
5. Edge Classification: Based on the depth-priority forest generated by DFS, the edge can be divided into four categories-tree edge, forward edge, reverse edge. Cross edges.
6. The discovery and completion time of depth-first search has the parentheses nature.
7, White path theorem: In a diagram g= (v,e) (with or without the graph) in the depth of the first forest. Node V is the descendant of the node U and can be set from the vertex u only when you are found in the search at D[u] time. A path that consists entirely of white vertices reaches v.
8, can make some changes to the algorithm DFS. It can be categorized when the edge is encountered. The core idea of the algorithm is for each edge (U,V), when the edge is first explored. That is, according to the color of the node V reached. To classify the edge (but the forward and cross edges 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 means that he is a positive or a cross-side. Suppose D[u]<d[v]. Then the Edge (U,V) is the forward edge, and vice versa is the cross edge.
9, the above classification for the non-direction of the diagram said. There may be ambiguity.
In the process of depth-first searching for graph G, each edge of G is either a tree edge or a reverse edge.
Iv. topological sequencing
The topological ordering of a graph can be thought of as a sequence of all the vertices of the graph along the horizontal line, so that all the forward edges are pointed from left to right.
1. The following simple algorithm can be used to sort the graphs in topological order:
Topological-sort (G)
A, call DFS (G) to compute the f[v of each node V].
b, when each vertex is finished. Insert it into the front of the list;
C. Returns a linked list of vertices.
since the execution time of the depth-first search is θ (v+e), the | Each inserted linked list in the v| vertex occupies a time of O (1), so the execution time for topological sequencing is θ (v+e).
Can the above algorithm be changed to the point where each fixed point d[v] will be inserted at the end of the list? no way. For example, as seen, assuming this, shoes is in front of the socks, in fact. Socks to shoes there is a forward edge. Shoes should be in the back of socks. Contradiction.
2. lemma: There is a direction graph G no loop when and only if the depth of the first search for G does not get the reverse edge.
3, Theorem: Topological-sort (g) algorithm can generate a forward- free graph G topological ordering.
Five, strong connected branches
1, in the directed graph, it is assumed that no matter what two different points can be reached each other. It is said that the graph is strongly connected. A strongly connected sub-graph of a forward graph is called its strong connective branch.
2, a lot of algorithms about the graph are from the decomposition step, such decomposition can divide the original problem into a number of sub-problems. Each of these sub-sub-problems corresponds to a strongly connected branch. The linkage between the strongly connected branches of the structure is also linked to the solution of the problem, and we are able to represent this construct with a graph called a branching diagram .
3, looking for the strong connected branch of Figure g= (v,e) algorithm used the transpose of G, that is, E ' from the side of G to change the direction of the composition. If the adjacency table of Figure G is known, the time required to build the GT is O (v+e). G and G ' have exactly the same strong connected branches, that is, in G you and V are mutually accessible and only when they are mutually accessible in the GT.
4, the following execution time is θ (v+e) algorithm can be derived from the g= (v,e) strong connected branch, the algorithm uses two depth first search, once on the figure G, and once on the figure G ':
Strongly_connected_components (G)
A, call DFS (G) to calculate the completion time of each node u f[u];
b, calculate the G '.
C. Call DFS (GT), but consider each node in the DFS's main loop in descending order of F[u] (as in the first row);
D. The vertex of each tree in the depth-first forest in the output 3rd step, as a separate strong connected branch.
The idea of the strongly_connected_component algorithm comes from an important property of the branch graph GSCC = (VSCC, ESCC):
If the strong connected branch of G is C1, C2,..., Ck. The vertex set VSCC is {v1, v2,..., VK}, and for each strongly connected branch ci of G, a vertex VI is included. If there is a forward edge (x, y) for a x∈ci and a y∈cj,g, then there is an edge (vi, VJ) ∈ESCC. From another point of view, contraction of those whose associated vertices are in the same strong connected branch of G, you can get the figure GSCC.
5, lemma: set C and c′ is a graph G = (V, E) in the two different strong connected branches. Set U, V∈c, u′, v′∈c′, and if there is a pathway u→u′ in G, then it is impossible to have access v′→v at the same time in G.
6, lemma: set C and C ' for the direction of the graph g= (v,e) two different strong connected branches.
If an edge (u,v) ∈e, U∈c,v∈c '. Then f (c) >f (C ').
Inference: Set C and C ' for the g= (v,e) two different strong connected branches, if there is an edge (U, V) ∈et, U∈c and v∈c′. So f (C) < F (c′).
7, Strongly_connected_components (G) can be the cause of normal work:
A, the second time to run in the GT depth-first search: from the strong connected branch C start. F (c) is the largest, and the search starts at some vertex x of C. Access c all vertices.
b, according to inference, the GT does not have from C to other connected branches of the edge. The tree with the root x includes only the vertices in C.
C. Next interview C '. F (c ') is the largest outside F (c), as is the case with the access C process when the algorithm is in depth-first search on the 3rd line of the GT. Whatever side of the branch points to the branch that has been interviewed.
Therefore, each depth-priority tree is a strongly connected branch.
The basic algorithm and application of--lec 10 graph based on algorithm introduction