Reprint indicated source, original address: http://blog.csdn.net/powerwoo25/article/details/47869457
breadth Traversal (BFS) and depth traversal (DFS) of graphs
Train of thought: Read the user input node number, edge of the vertices, using an adjacency matrix to represent the connectivity in the diagram. Then take the first node and put it into a two-terminal queue for BFS.
Reprinted annotated Source, original address: http://blog.csdn.net/powerwoo25/article/details/47869457/* BFS * * * #include <stdio.h> #include <
Deque> using namespace std;
typedef struct NODE {int flag, d, pre;
Node (): Flag (0), D (10000), pre (-1) {};
}vertex;
const int MAXV = 10000;
int MATRIX[MAXV][MAXV];
void Readedge (int num) {int STARTV, ENDV;
printf ("Enter your edges:\n"); while (~SCANF ("%d,%d", &startv, &ENDV) && (StarTV < num) && (STARTV >= 0) && (endv &L T
num) && (endv >= 0)) {MATRIX[STARTV][ENDV] = Matrix[endv][startv] = 1;
} void BFS (deque<int> Q, vertex *a, int num) {int i;
A[0].flag = 1;
A[0].D = 0;
Q.push_back (0); while (!
Q.empty ()) {int u = q.front ();
Q.pop_front ();
for (i = 0; i < num i++) {if (Matrix[u][i]!= 0 && a[i].flag = 0) {
A[i].flag = 1;
A[I].D = a[u].d + 1; A[i].pre = u;
Q.push_back (i);
} A[u].flag = 2;
} void Printvertex (vertex* A, int num) {int I, j, K;
for (j = 0; J < Num. ++j) {for (k = 0; k < num; ++k) {printf ("%d", matrix[j][k]);
printf ("\ n"); for (i = 0; i < num ++i) {printf ("Vertex:%d, flag:%d, pre:%d, D:%d\n", I, A[i].flag, A[i].pre, A
[I].D);
int main () {freopen ("J://test.txt", "R", stdin);
int vertexn;
printf ("Please enter the quantity of your vertex\n");
scanf ("%d", &vertexn);
if (Vertexn < 1 | | vertexn > MAXV) {perror ("Invalid parameter:vertexn\n");
return-1;
} vertex VERTEXS[VERTEXN];
Readedge (VERTEXN);
Deque<int> Vertexq;
Vertexq.clear ();
BFS (VERTEXQ, Vertexs, VERTEXN);
Printvertex (Vertexs, VERTEXN);
return 0; }
Idea: Dfs mainly uses recursive return for depth-first traversal
Reprinted annotated Source, original address: http://blog.csdn.net/powerwoo25/article/details/47869457/* DFS/* #include <stdio.h> using
namespace Std;
typedef struct Node {int flag;
int pre;
int d, F;
Node (): Flag (0), Pre ( -1), D (0), F (0) {};
}vertex;
const int MAXV = 10000;
int time = 0;
int MATRIX[MAXV][MAXV];
void Readedge (int num) {int STARTV, ENDV;
printf ("Enter your edges:\n"); while (~SCANF ("%d,%d", &startv, &ENDV) && (StarTV < num) && (STARTV >= 0) && (endv &L T
num) && (endv >= 0)) {MATRIX[STARTV][ENDV] = Matrix[endv][startv] = 1;
} void Printvertex (vertex* A, int num) {int I, j, K;
for (j = 0; J < Num. ++j) {for (k = 0; k < num; ++k) {printf ("%d", matrix[j][k]);
printf ("\ n"); for (i = 0; i < num ++i) {printf ("Vertex:%d, flag:%d, pre:%d, D:%d, F:%d\n", I, A[i].flag, A[i]
. Pre, A[I].D, A[I].F); } void DFs_visit (vertex *a, int index, int num) {int i;
A[INDEX].D = ++time;
A[index].flag = 1;
for (i = 0; i < num ++i) {if (Matrix[index][i]!= 0) {if (A[i].flag = = 0) {
A[i].pre = index;
Dfs_visit (A, I, num);
}} A[index].flag = 2;
A[INDEX].F = ++time;
} void DFS (vertex *a, int num) {int i;
for (i = 0; i < num; ++i) {if (A[i].flag = = 0) {dfs_visit (A, I, num);
()} int main () {freopen ("J://test.txt", "R", stdin);
int vertexn;
printf ("Please enter the quantity of your vertex\n");
scanf ("%d", &vertexn);
if (Vertexn < 1 | | vertexn > MAXV) {perror ("Invalid parameter:vertexn\n");
return-1;
} vertex VERTEXS[VERTEXN];
Readedge (VERTEXN);
DFS (Vertexs, VERTEXN);
Printvertex (Vertexs, VERTEXN);
return 0; }
# test.txt
0,2
3,4
2,4
4,1
3,1 5,6 2,6 7,9 3,8 8,9