It is written in a cross-link table structure and implemented according to the description in the data structure book and your own understanding. But I do not understand it thoroughly, so I do not know whether there are any errors. But I have tried several of them.
# Include <iostream> # include <vector> using namespace STD; // # define max_vertex_num 20 typedef struct arcbox {int tailvex, headvex; // struct arcbox * HLINK and * tlink at the end of the arc and the header vertex; // point to the chain domain of the arc header and the same arc tail} arcbox; typedef struct vexnode {int data; arcbox * firstin, * firstout; // point to the first incoming and outgoing arcs of the vertex respectively} vexnode; typedef struct {vexnode xlist [max_vertex_num]; // header vector int vexnum, arcnum; // Number of vertices and arcs in the directed graph} olgraph; // locate the vertex in the XLI Int locatevex (olgraph g, int data) {for (INT I = 0; I <G. vexnum; I ++) {If (G. xlist [I]. data = data) {return I ;}} cout <"error the vertex" <data <"is not in the list" <Endl; Return-1 ;} // create void createdg (olgraph & G) {cout <"Please input the number of vertex, the number of arc:"; CIN> G. vexnum> G. arcnum; For (INT I = 0; I <G. vexnum; I ++) {cout <"Please input vertex dat A: "; CIN> G. xlist [I]. data; G. xlist [I]. firstin = NULL; // initialize the pointer G. xlist [I]. firstout = NULL;} For (int K = 0; k <G. arcnum; k ++) {int V1, V2; // tail and header cout of the arc <"Please input the tail and head vertex of each tail :"; cin> V1> V2; int I = locatevex (G, V1); Int J = locatevex (G, V2); arcbox * P = new arcbox; p-> headvex = J; P-> tailvex = I; P-> HLINK = G. xlist [J]. firstin; P-> tlink = G. xlist [I]. firstout; G. xlist [J]. firstin = P; G. xlist [I]. firstout = P ;}/// unidirectional depth preference search // input: graph G, start traversing point V, traversing Mark visited, in the traversal direction, Dir 0 indicates traversing from the end to the head. 1 indicates traversing from the end to the end. vecor stores the out-of-traversal sequence void DFS (olgraph g, int V, int * visited, int Dir, vector <int> * VEC) {visited [v] = 1; (* VEC ). push_back (V); If (DIR = 0) // traverses the {arcbox * w = G. xlist [v]. firstout; while (W! = NULL) // note the while {If (visited [w-> headvex] = 1) {W = W-> tlink ;} else // This point has not been accessed recursively traverse this point {DFS (G, W-> headvex, visited, Dir, VEC); W = W-> tlink ;}}} else // traverse from the beginning to the end {arcbox * w = G. xlist [v]. firstin; while (W! = NULL) // find the next traversal point {If (visited [w-> tailvex]) = 1) {W = W-> HLINK ;} else // This point has not been accessed recursively traverse this point {DFS (G, W-> tailvex, visited, Dir, VEC); W = W-> HLINK ;}}}} // search for the vector <vector <int> findconnectedpart (olgraph g) {vector <int> connectedpart; vector <int> finished; int * visited = new int [G. vexnum]; memset (visited, 0, G. vexnum * sizeof (INT); // The Initialization is not accessed. // traverse the for (int v = 0; v <G. vexnum; V ++) {If (visited [v] = 0) // No {vector <int> VEC; DFS (G, V, visited, 0, & VEC); finished. push_back (VEC) ;}}// traverse memset (visited, 0, G. vexnum * sizeof (INT); vector <int >:: iterator it; vector <int >:: iterator it2; int * Find = new int [G. vexnum]; // find indicates whether the vertex is actually searched for (INT I = 0; I <G. vexnum; I ++) {find [I] = 0; visited [I] = 1;} For (it2 = finished. begin (); it2 <finished. end (); it2 ++) {// The visited parts that have been traversed remain unchanged, that is, all of them are 1; find [I] = 0 indicates that this time does not traverse node I, to skip I, set their visited [I] = 1; but they are not actually accessed yet. // For example, get two parts (1, 2, 3, 4) from the end to the first time) (5) // in order to find the reconnection component, the components that traverse from start to end are (1 2 3 5) (4) // but actually the reconnection component is (1, 2, 3) (4) (5) Three for (IT = it2-> begin (); it <it2-> end (); It ++) {visited [* It] = 0; // set the visited of the vertex to 0 only for this traversal, if the other value is 1, no one will traverse the find [* It] = 1;} For (IT = it2-> begin (); It <it2-> end (); it ++) {If (visited [* It] = 0) // No {vector <int> VEC; DFS (G, * It, visited, 1, & VEC); connectedpart. push_back (VEC) ;}}// output reconnection component int n = 0; cout <"reconnection components:" <Endl; For (it2 = connectedpart. begin (); it2 <connectedpart. end (); it2 ++) {cout <++ n <":"; for (IT = it2-> begin (); it <it2-> end (); It ++) {cout <G. xlist [* it]. data <"" ;}cout <Endl ;}delete [] visited; Delete [] Find; return connectedpart;} int main () {olgraph g; createdg (g ); findconnectedpart (g); Return 0 ;}
Bytes. I haven't seen it yet.
[Data structure] DFS calculates the strongly connected component of a directed graph.