This program is implemented with the graph's adjacent hands. If you want to implement the DFS algorithm using the graph's adjacent linked list, you only need to change the firstadj (), nextadj (), and graph creation operations in this program to the adjacent linked list, you do not need to modify other DFS main program frameworks.
Temp. cpp
// Depth-first search (DFS) of the graph // the graph is represented by an adjacent matrix # include <iostream> # include <fstream> # include "temp. H "using namespace STD; int locatevex (mgraph g, vertextype v) {// find the subscript of the element whose value is V in the vexs (vertex) of graph G, -1; for (INT I = 0; I <G. vexnum; I ++) {If (G. vexs [I] = V) {return I;} return-1;} status createmgraph (mgraph & G) {// create graph G, input (Read File) the value is // 1. number of vertices, number of edges and arcs, graph type // 2. input vertex values in sequence // 3. enter the start and end points of an edge in sequence; ifstream in; In. open ("data3.txt", IOS: In); Cout <"the lower column data is read from the file data3.txt" <Endl; cout <"Enter the number of vertices, edges/arcs, and graph types" <Endl; int kind; In> G. vexnum> G. arcnum> kind; // read data from a file. If any ending mark (such as a space or line feed) is encountered, the data is stopped. Switch (kind) {Case 0: G. kind = Ng; break; Case 1: G. kind = DG; break; Case 2: G. kind = NW; break; Case 3: G. kind = dw; break;} cout <"Enter the vertex value in sequence" <Endl; For (INT I = 0; I <G. vexnum; I ++) {In> G. vexs [I];} For (INT I = 0; I <G. vexnum; I ++) {for (Int J = 0; j <G. vexnum; j ++) {G. ARCs [I] [J] = 0 ;}} cout <"Enter the start and end points of an edge in sequence:" <Endl; For (int K = 0; k <G. arcnum; k ++) {int V1, V2, I, j; In> V1> V2; I = Lo Catevex (G, V1); j = locatevex (G, V2); G. ARCs [I] [J] = 1; if (G. kind = Ng) // undirected graph G. ARCs [J] [I] = 1 ;}cout <Endl; Return OK;} int firstadj (mgraph g, int v) {for (INT I = 0; I <G. vexnum; I ++) {If (G. ARCs [v] [I]) {return I;} return-1;} int nextadj (mgraph g, int V, int W) {for (INT I = W + 1; I <G. vexnum; I ++) {If (G. ARCs [v] [I]) {return I;} return-1;} void visitfuc (mgraph g, int v) {cout <G. vexs [v] <Endl;} void DF S (mgraph g, int V, int visited []) {// starting from the vertex with the serial number V, perform a deep-first search on the graph G to traverse visitfuc (G, V ); // access v visited [v] = true; // mark the access as for (int w = firstadj (G, V); W! =-1; W = nextadj (G, V, W) {If (! Visited [w]) DFS (G, W, visited) ;}} void dfstraverse (mgraph g) {// The depth first searches for the traversal graph Gint visited [visited_number]; for (int v = 0; v <G. vexnum; V ++) {visited [v] = false; // initialization} For (int v = 0; v <G. vexnum; V ++) {If (! Visited [v]) {DFS (G, V, visited); // If vertex v is not accessed, traverse cout from v <"this is a DFS" <Endl ;}}int main () {mgraph g; createmgraph (g); dfstraverse (g );}
Temp. h
# Define vertextype int # define adjmatrix int typedef Enum {ng, DG, NW, DW} graphkind; # define status int # define max_n 10 # define OK 1 # define error 0 # define false 0 # define true 1 # define visited_number 10 typedef struct {vertextype vexs [max_n]; // indicates the vertex array; adjmatrix arcs [max_n] [max_n]; // indicates the two-dimensional array of edges; int vexnum, arcnum; // The number of vertices and edges; graphkind kind;} mgraph; // defines the image mgraph, which is represented by an adjacent matrix.
Data3.txt
5 4 01 2 3 4 51 2 1 32 4 2 3