// ================================================ ======================================
// Use adjacency list implement DFS and BFS
// By: chlaws
// Time: 08-8-4
// PS: transshipment don't delete this headmark
// ================================================ ======================================
# Include <stdio. h>
# Include <stdlib. h>
# Define max_vertex_num 20
Int visited [max_vertex_num];
Typedef char vertextype;
Typedef struct arcnode {
Int adjvex; // The vertex position pointed to by the arc.
Struct arcnode * nextarc; // point to the next table (edge) node
Int Info; // weight
} Arcnode; // edge node type
Typedef struct vnode {
Vertextype data;
Arcnode * firstarc;
} Vnode, adjlist [max_vertex_num];
Typedef struct {
Adjlist vertices; // list of adjacent tables
Int vexnum, arcnum; // Number of vertices and number of Edges
} Algraph;
# Define maxqsize max_vertex_num
Typedef struct sqnode {
Vertextype base [maxqsize];
Int front;
Int rear;
} Sqqueue;
Void initqueue (sqqueue & Q)
{
For (int ix = 0; ix <maxqsize; ++ IX)
{
Q. Base [ix] = 0;
}
Q. Front = Q. Rear = 0;
}
Int locatevex (algraph g, char U );
Void createalgraph_adjlist (algraph & G );
Void dfstraverse (algraph G );
Void DFS (algraph g, int V );
Void bfstraverse (algraph G );
Void BFS (algraph g, int V );
Void main ()
{
Algraph g;
Printf ("/n/N // ========================== create an adjacent table ========== ==============================/// n/n ");
Createalgraph_adjlist (g );
Printf ("/n/N // ================================ deep prioritized search ========== ==============================/// n/n ");
Dfstraverse (g );
Printf ("/n/N // ===================================== ==============================/// n/n ");
Bfstraverse (g );
}
// Find the subscript of the matched data in the array
Int locatevex (algraph 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 createalgraph_adjlist (algraph & G)
{
Int I, J, K, W; char V1, V2;
Arcnode * P;
Printf ("input vexnum & arcnum:/N ");
Scanf ("% d, % d", & G. vexnum, & G. arcnum );
Printf ("input vertices:/N ");
For (I = 0; I <G. vexnum; I ++)
{
Fflush (stdin );
Scanf ("% C", & G. vertices [I]. data );
G. vertices [I]. firstarc = NULL;
}
Printf ("input arcs (V1, V2, W):/N ");
For (k = 0; k <G. arcnum; k ++)
{
Fflush (stdin );
Scanf ("% C, % C, % d", & V1, & V2, & W );
I = locatevex (G, V1 );
J = locatevex (G, V2 );
P = (arcnode *) malloc (sizeof (arcnode ));
P-> adjvex = J;
P-> info = W;
P-> nextarc = G. vertices [I]. firstarc;
G. vertices [I]. firstarc = P;
} Return;
}
// Train of thought:
/*************************************** *************************************
** Search by breadth first after accessing the starting vertex v,
** Starting from V, access the neighboring vertices W1, W2 ,..., WT,
** Then access W1, W2,… in sequence ,..., All the neighboring vertices of wt that have not been accessed.
** Start from these accessed vertices, and then access all of their unaccessed adjacent vertices,
**... Do this until all vertices in the graph are accessed.
**************************************** *************************************/
Void bfstraverse (algraph g)
{
Int V;
For (int v = 0; v <G. vexnum; ++ V)
Visited [v] = 0;
For (V = 0; v <G. vexnum; ++ V)
If (! Visited [v]) BFS (G, V );
}
Void BFS (algraph g, int V)
{
Arcnode * P; sqqueue Q;
Initqueue (Q );
Printf ("% C", G. vertices [v]. data );
Visited [v] = 1;
Q. Base [q. Rear] = V;
Q. Rear = (Q. Rear + 1) % maxqsize;
While (Q. Front! = Q. rear)
{
V = Q. Base [q. Front];
Q. Front = (Q. Front + 1) % maxqsize;
P = G. vertices [v]. firstarc;
While (P)
{
If (! Visited [p-> adjvex])
{
Printf ("% C", G. vertices [p-> adjvex]. data );
Visited [p-> adjvex] = 1;
Q. Base [q. Rear] = p-> adjvex;
Q. Rear = (Q. Rear + 1) % maxqsize;
}
P = p-> nextarc;
}
}
}
/*
DFS is just the opposite.
*/
Void dfstraverse (algraph g)
{
For (int ix = 0; ix <G. vexnum; ++ IX)
{
Visited [ix] = 0;
}
For (INT ver = 0; ver <G. vexnum; ++ VER)
{
If (! Visited [VER]) DFS (G, Ver );
}
}
Void DFS (algraph g, int V)
{
Printf ("POS: [% d] data: % C/N", V, G. vertices [v]. data );
Visited [v] = 1;
Arcnode * Parc;
Parc = G. vertices [v]. firstarc;
While (PARC)
{
If (! Visited [Parc-> adjvex]) DFS (G, PARC-> adjvex );
Parc = Parc-> nextarc;
}
}
// DFS search result
/*
Input vexnum & arcnum:
9,10
Input vertices:
A
B
C
D
E
F
G
H
I
Input arcs (V1, V2 & W ):
A, B, 1
A, C, 1
A, D, 1
B, E, 1
B, c, 1
E, G, 1
F, d, 1
F, C, 1
F, H, 1
H, I, 1
POs: [0] data:
POs: [3] data: d
POs: [2] data: c
POs: [1] data: B
POs: [4] data: E
POs: [6] data: G
POs: [5] data: F
POs: [7] data: H
POs: [8] data: I
Press any key to continue...
*/