I. Summary
Deep Search policy: during deep search, if the last detected vertex has an edge not detected starting from this point, it will continue to be detected along this edge. After all the edges of vertex v have been detected, the search will go back to those edges that find that vertex v has a starting point.
Timestamp: The first timestamp d [v] is recorded when vertex v is first detected, record the second timestamp f [v]. V is white before d [v], gray between D [v] and f [v], and black after f [v.
Parentheses theorem, nesting of descendant intervals, and white path Theorem
Edge classification: (1) tree edge (2) reverse edge (3) forward edge (4) Cross edge
For deep search of undirected graph G, each edge of G is either a tree edge or a reverse edge.
Ii. Code 1. link_graph.h
# Include <iostream> # include <queue> using namespace STD; # define n 100 # define white 0 # define gray 1 # define black 2 # define none 0 # define tree 1 # define back 2 # define forward 3 # Define cross 4 struct edge {int start; int end; int value; int type; edge * Next; edge (int s, int e, int V): start (s), end (E), value (V ), type (none), next (null) {}}; struct vertex {int D, F; // int color of the first detected time and the end time of the check; // vertex color int P; // point to the parent node of the traversal result ed GE * head; // point to the next vertex (): color (white), P (0), head (null ){};}; class link_graph {public: int time; int N; vertex * V; link_graph (INT num): n (Num) {v = new vertex [n + 1];} ~ Link_graph () {Delete [] V;} void addsingleedge (INT start, int end, int value = 1) {edge * newedge = new edge (START, end, value ); if (V [start]. head = NULL | V [start]. head-> end) {newedge-> next = V [start]. head; V [start]. head = newedge;} else {edge * E = V [start]. head, * pre = E; while (E! = NULL & E-> end <End) {pre = E; E = e-> next;} If (E & E-> end = end) {Delete newedge; return;} newedge-> next = E; pre-> next = newedge;} void adddoubleedge (int A, int B, int value = 1) {addsingleedge (a, B, value); addsingleedge (B, A, value);} void deletesingleedge (INT start, int end) {edge * E = V [start]. head, * pre = E; while (E & E-> end <End) {pre = E; E = e-> next ;} if (E = NULL | E-> end) return; If (E = V [start]. head) V [start]. head = e-> next; elsepre-> next = e-> next; Delete E;} void deletedoubleedge (int A, int B) {deletesingleedge (A, B ); deletesingleedge (B, A);} // 22.3 void DFS (); void dfs_visit (INT U); void print_vertex_time (); void print_edge_type () ;}; void link_graph :: DFS () {int U; // initialize for (u = 1; U <= N; U ++) {v [u] for each vertex. color = white; V [u]. P = NULL;} // time stamp initialization time = 0; // search for vertices in V in sequence. When a white vertex is found, call dfs_visit to access the vertex for (u = 1; U <= N; U ++) if (V [u]. color = white) dfs_visit (U);} void link_graph: dfs_visit (int u) {int V; edge * E; // set u to gray V [u]. color = gray; // add value to the global variable time ++; // record the new value of time as the time V [u]. D = time; E = V [u]. head; while (e) {v = e-> end; // If the vertex is white if (V [v]. color = white) {// recursively access the vertex v [v]. P = u; dfs_visit (V); // tree edge e-> type = tree;} else if (V [v]. color = gray) {// reverse side e-> type = back;} else if (V [v]. color = black) {// forward side if (V [u]. d <V [v]. d) E-> type = forward; // cross edge elsee-> type = cross;} e = e-> next ;} // after all edges starting from u are explored, the U is set to black V [u]. color = black; // record the completion time in F [u] Time ++; V [u]. F = time;} void link_graph: print_vertex_time () {int I; for (I = 1; I <= N; I ++) {// because the example in the book uses the letter numbers starting from Q, there is such a conversion cout <char ('P' + I) <': '; cout <V [I]. d <''<V [I]. F <Endl ;}} void link_graph: print_edge_type () {int I; for (I = 1; I <= N; I ++) {edge * E = V [I]. head; while (e) {cout <char (e-> Start + 'P') <"->" <char (e-> end + 'P ') <":"; Switch (e-> type) {Case tree: cout <"tree edge" <Endl; break; case back: cout <"reverse edge" <Endl; break; case forward: cout <"forward edge" <Endl; break; Case Cross: cout <"Cross edge" <Endl; break;} e = e-> next ;}}}
2. Main. cpp
# Include <iostream> using namespace STD; # include "link_graph.h" int main () {int n, m, I; char start, end; // number of input points and number of edges CIN> N> m; // construct an Empty Graph link_graph * g = new link_graph (N ); // input edge for (I = 1; I <= m; I ++) {CIN> Start> end; G-> addsingleedge (START-'P ', end-'P');} // depth-first search for G-> DFS (); // output the first access time and end time of each vertex G-> print_vertex_time (); // output the type of each edge G-> print_edge_type (); Delete g; return 0 ;}
3. Test Data Book p335, fig 22-610 14q s
S v
V W
W S
Q W
Q T
T Y
Y Q
T x
X Z
Z x
U y
R Y
R u4. for output results, see 22.3-2. Exercise 22.3-1. The key is "any time"
Directed Graph
| |
White |
Gray |
Black |
| White |
Tbfc |
BC |
C |
| Gray |
TF |
Tfb |
TFC |
| Black |
|
B |
Tfbc |
Undirected graph
| |
White |
Gray |
Black |
| White |
TB |
TB |
|
| Gray |
TB |
TB |
TB |
| Black |
|
TB |
TB |
22.3-2q: 1 16r: 17 20 s: 2 7 T: 8 15u: 18 19 V: 3 6 W: 4 5x: 9 12y: 13 14z: 10 11q-> S: tree edge Q-> T: Tree edge Q-> W: forward edge R-> U: Tree edge R-> Y: Cross edge S-> V: tree edge t-> X: Tree edge t-> Y: Tree edge U-> Y: Cross Edge V-> W: Tree edge w-> S: reverse side X-> Z: Tree side y-> q: reverse side Z-> X: reverse side 22.3-3 (U (V (Y (XX) y) V) u) (w (zz) W) 22.3-6 Introduction to algorithms-22.3-6-implement DFS 22.3-7 with stacks, (1) there is an edge from u to V, (2) d [u] = 1, D [v] = 2, D [u] <D [v]. In the depth-first Forest, V is not the descendant of U. 22.3-8, (1) there is a path from u to V 22.3-9 does not seem to need to change the number of 22.3-1022.3-11 Unicom branches, expressed in Ceil, the code is modified as follows:
Void link_graph: DFS () {int U, Ceil = 0; // initialize for (u = 1; U <= N; U ++) for each vertex) {v [u]. color = white; V [u]. P = NULL;} // time stamp initialization time = 0; // search for vertices in V in sequence. When a white vertex is found, call dfs_visit to access the vertex for (u = 1; U <= N; U ++) {If (V [u]. color = white) {Ceil ++; dfs_visit (u, Ceil) ;}} void link_graph: dfs_visit (int u, int Ceil) {int V; edge * E; // set u to gray V [u]. color = gray; // add value to the global variable time ++; // record the new value of time as the time V [u]. D = time; E = V [u]. head; while (e) {v = e-> end; // If the vertex is white if (V [v]. color = white) {// recursively access the vertex v [v]. P = u; dfs_visit (v, Ceil); // tree edge e-> type = tree;} else if (V [v]. color = gray) {// reverse side e-> type = back;} else if (V [v]. color = black) {// forward side if (V [u]. d <V [v]. d) E-> type = forward; // cross edge elsee-> type = cross;} e = e-> next ;} // after all edges starting from u are explored, the U is set to black V [u]. color = black; V [u]. ceil = Ceil; // record the completion time in F [u] Time ++; V [u]. F = time ;}
22.3-12 a single connected graph has no positive edge