The
DFS (depth-first-search) Depth-first search algorithm is a very common kind of algorithm in graph traversal algorithms. Share to everyone for your reference. The specific method is as follows:
#include <iostream> #include <algorithm> #include <iterator> using namespace std;
#define MAX_VERTEX_NUM struct Node {int adjvex;
struct Node *next;
int info;
};
typedef struct VNODE {char data;
Node *first;
}vnode, Adjlist[max_vertex_num];
struct Graph {adjlist vertices;
int Vexnum, arcnum;
};
int Visited[max_vertex_num];
int Locatevex (Graph G, char u) {int i;
for (i = 0; i < G.vexnum. i++) {if (U = = g.vertices[i].data) return i;
if (i = = G.vexnum) {printf ("Error u!\n");
Exit (1);
return 0;
} void Creategraph (Graph &g) {int I, J, K, W;
Char v1, v2, enter;
Node *p;
printf ("Input Vexnum & arcnum:\n");
scanf ("%d", &g.vexnum);
scanf ("%d", &g.arcnum);
printf ("Input vertices:\n");
for (i = 0; i < G.vexnum i++) {scanf ("%c%c", &enter, &g.vertices[i].data);
G.vertices[i].first = NULL;
printf ("Input Arcs (v1, v2, W): \ n"); for (k = 0; k < g.arcnum; k++) {scanf ("%c%c", &enteR, &V1);
scanf ("%c%c", &enter, &v2);
scanf ("%d", &w);
i = Locatevex (G, v1);
j = Locatevex (G, v2);
p = (node *) malloc (sizeof (node));
P->adjvex = j;
P->info = W;
P->next = G.vertices[i].first;
G.vertices[i].first = p;
} void DFS (Graph &g, int v) {Node *p;
printf ("%c", g.vertices[v].data);
VISITED[V] = 1;
p = G.vertices[v].first;
while (p) {if (!visited[p->adjvex]) DFS (G, P->adjvex);
p = p->next;
} void Dfstranverse (Graph &g) {for (int v = 0; v < g.vexnum; v++) visited[v] = 0;
for (int v = 0; v < g.vexnum; v++) {if (!visited[v)) DFS (G, v);
int main () {Graph G;
Creategraph (G);
Dfstranverse (G);
}
To write Dfs in a different way. The specific code is as follows:
#include <iostream> #include <string> using namespace std;
#define MaxLen struct Node {int data;
Node *next;
};
struct Link {int count;
String name;
Node *head;
};
struct Graph {Link Link[maxlen];
int vexnum;
int arcnum;
};
int FindIndex (Graph &g, string name) {int index =-1;
for (int i = 0; i < G.vexnum i++) {if (g.link[i].name = = name) {index = i;
Break
} if (index = = 1) cout << "error" << Endl;
return index;
} void Constructgraph (Graph &g) {cout << "construct graph yooo" << Endl;
cout << "Enter Vexnum" << Endl;
Cin >> G.vexnum;
String array[] = {"V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8"};
const int size = sizeof array/sizeof *array;
for (int i = 0; i < G.vexnum i++) {g.link[i].name = Array[i];
G.link[i].head = NULL;
} string Leftname;
String Rightname;
cout << "Enter a pair" << Endl;
CIN >> Leftname >> rightname; WhIle (Leftname!= "End" && rightname!= "End") {int leftindex = FindIndex (G, leftname);
int rightindex = FindIndex (G, rightname);
Node *node = new node;
Node->data = Rightindex;
Node->next = NULL;
Node->next = G.link[leftindex].head;
g.link[leftindex].head = node;
cout << "Enter a pair" << Endl;
CIN >> Leftname >> rightname;
} bool Flag[maxlen];
void Dfstranverse (Graph &g, int num) {cout << g.link[num].name << "";
Flag[num] = true;
Node *head = G.link[num].head;
while (head!= NULL) {int index = head->data;
if (!flag[index]) dfstranverse (G, index);
Head = head->next;
} void Main () {Graph G;
Constructgraph (G);
for (int i = 0; i < maxlen i++) flag[i] = false;
Dfstranverse (G, 0);
}
The iterative traversal algorithm for DFS is as follows:
void DFS (Graph &g)
{
stack<int> istack;
Istack.push (0);
cout << g.link[0].name << "";
Flag[0] = true;
while (!istack.empty ())
{
int index = Istack.top ();
Node *head = g.link[index].head;
while (head!= NULL && Flag[head->data] = = true) Head
= head->next;
if (head!= NULL)
{
index = head->data;
if (!flag[index])
{
cout << g.link[index].name << "";
Flag[index] = true;
Istack.push (index);
}
else
Istack.pop ();
}
}
Perceptual friends can test run the example code in this article to deepen the impression, I believe this article in the C program algorithm design has a certain reference value.