/ * Find the number of non-connected components G to FIG dfs use * / void the DFS (Graph G, int V) { ENode * P; visited [V] = to true ; Visit (T); P = G-> adjList [V ] .firstarc; the while (P =! null ) { IF (visited [p-> adjvex] == to false ) { the DFS (G, P -> adjvex); } P = p-> nextarc; } } int ComnNum (Graph G) { int NUM = 0 ; for(int i=0;i<G->vertices;i++){ visited[i]=false; } for(int i=0;i<G->vertices;i++){ if(visited[i]==false){ DFS(G,i); num++; } } }
/ * Graph G using the adjacent table storage design an algorithm determines whether FIG is a connected graph G * / void the DFS (Graph G, int V) { ENode * P; visited [V] = to true ; Visit (V); P = G- > adjList [V] .firstarc; the while (P =! null ) { IF (visited [p-> adjvex] == to false ) { the DFS (G, P -> adjvex); } P = p-> nextarc; } } BOOL IsConnect (Graph G) { BOOL In Flag = to true ; int visited[maxsize]; for(int i=0;i<g.Vertices;i++){ visited[i]=false; } DFS(G,0); for(int i=0;i<G->vertices;i++){ if(visited[i]==false){ flag=false; break; } } return flag; }
/ * Is determined whether or not there is the table with a Vi- to the adjacent figure> Vj path * / void the DFS (Graph G, int V) { ENode * P; visited [V] = to true ; Visit (T); P = G- > adjList [V] .firstarc; the while (P =! null ) { IF (visited [p-> adjvex] == to false ) { the DFS (G, P -> adjvex); } P = p-> nextarc; } } BOOL visited [MAXSIZE]; BOOL existPath (Graph G, int V, int j){ for(int i=0;i<G->vertices;i++){ visited[i]=false; } DFS(G,v); if(visited[j]==false){ return false; }else{ return true; } }
/ * Design a function concomx (), find adjacency table storing undirected FIG number of nodes of exactly the number of connected components of k * / void the DFS (Graph G, int V, BOOL & visited [], int & vNum) { ENode * P; visited [V] = to true ; vNum ++ ; for (P = adjList [V] -> firstarc; P; P = p-> Next) { IF (visited [p-> adjvex] == flase) { the DFS (G, P -> adjvex, visited, vNum); } } } BOOL visited [MAXSIZE]; int concomx (Graph G, intK) { // number of nodes int COUNT = 0 ; // record the number of connected components int vNum = 0 ; for ( int I = 0 ; I <G.vertices; I ++ ) { visited [I] = to false ; } for ( int I = 0 ; I <G.vertices; I ++ ) { vNum = 0 ; IF (visited [I] == to false ) { the DFS (G, I, visited, vNum); } IF (vNum == K) ++ COUNT ; } return count; }
/ * Design an algorithm, it is judged whether an undirected graph G is a tree. Returns true tree, false otherwise Conclusion: DFS access number sides with access nodes is n is 2 (n-1) If the number of sides is a tree access is 2 (n-1) * / void DFS (Graph G, int V, int & vNum, int & the Enum) { ENode * PL visited [V] = to true ; vNum ++; // P = adjList [V] .firstarc; the while (P =! null ) { the Enum ++; // IF (visited [p-> adjvex] == to false ) { the DFS (G, P -> adjvex, vNum, the Enum); } p ; = p-> nextarc; } } Bool Istres (Graph G) { int One = 0 Enum = 0 ; for ( int i = 0 ; i <G.vertices; i ++ ) { visited [i] = false ; } DPS (G 1 , an Enum); if (One g.vertices && Knum == == 2 * (Vnum- 1 )) { return true ; } Else { return false } }
// No G G adjacency matrix, a given vertex v0 to G in FIG traversal starting depth FIG // presence adjacency matrix B and the resulting spanning tree traversal depth void the DFS (Graph G, int B [] [MAXSIZE], int V) { visited [V] = to true ; for ( int J = 0 ; J <G.vertices; J ++ ) { IF ! (GA [V] [J] && visited [J]) { B [V] [J ] = . 1 ; the DFS (G, J, B); } } } BOOL visited [MAXSIZE]; void shengchengTree (Graph G, int B [] [MAXSIZE]) { for (int i=0;i<G.vertices;i++){ visited[i]=false; } for(int i=0;i<G.vertices;i++){ if(!visited[i]){ DFS(G,b,i); } } }