Data structure (C implementation) ------- graph breadth first traversal, ------- breadth
[This article is my own learning notes. You are welcome to repost it, but please note the Source: http://blog.csdn.net/jesson20121020]
Algorithm Description:
If the initial state of graph G is that all vertices have not been accessed, and any vertex vi in G is the initial starting point, the breadth-first traversal can be defined as follows: first, access the initial starting point vi, access all neighboring contacts w1, w2 ,..., wk; then, access w1, w2 ,..., all unaccessed vertices adjacent to wk, and so on until all vertices having the same paths with the First vi in the graph are accessed.
Algorithm Implementation:
(1) access the initial vertex vi
(2) top point v accessed mark
(3) vertex v enters the queue
(4) while (the team is not empty ){
Retrieve the first vertex I of the team;
Search all adjacent contacts of vertex I in sequence;
If not, access the adjacent contact and queue it.
}
Source code for traversing the graph's breadth first using the adjacent matrix is as follows:
/*** Span traversal graph **/void BFS_MG (MGraph MG, int s) {// clear the access flag init_Visit (); // define the queue, used to save the adjacent vertex int Q [MAX_VEX_NUM] of the current node; int front = 0; int rear = 0; int I, j, k; printf ("% c \ t ", MG. vexs [s]); visit [s] = 1; Q [rear ++] = s; // traverses the queue while (front <rear) {I = Q [front ++]; for (j = 1; j <= MG. vexnum; j ++) {if (visit [j] = 0 & MG. arcs [I] [j] = 1) {printf ("% c \ t", MG. vexs [j]); visit [j] = 1; Q [rear ++] = j ;}}}}
Source code for graph breadth-first traversal using an adjacent table is as follows:
/*** Span traversal graph **/void BFS_AG (ALGraph AG, int s) {ArcPtr p; // clear the access flag init_Visit (); // define the queue, used to save the adjacent vertex int Q [MAX_VERTEX_NUM] of the current node; int front = 0; int rear = 0; int I, j, k; printf ("% c \ t ", AG. vertices [s]); visit [s] = 1; Q [rear ++] = s; // traverse the queue while (front <rear) {I = Q [front ++]; for (p = AG. vertices [I]. firstarc; p = p-> nextarc) {j = p-> adjvex; if (visit [j] = 0) {printf ("% c \ t ", AG. vertices [j]. vexdata); visit [j] = 1; Q [rear ++] = j ;}}}}
Algorithm Description:
For a connected graph with n vertices and e edges, because each base point needs to be queued once, the while statement needs to be executed n times. For the adjacent matrix, the adjacent points in the inner loop search also need to be executed n times, so the time complexity of BFS_MG is O (n ^ 2). For the adjacent table, the number of inner cycles depends on the total number of edge table nodes of each vertex. Therefore, the time complexity of BFS_AG is O (n + e ).
It can be seen that the extended traversal requires an auxiliary queue and a flag array, so the space complexity is O (n ).
Complete code:
Complete code for traversing the breadth first with an adjacent matrix:
/* ===================================================== ============================================================ Name: graph. c Author: jesson20121020 Version: 1.0 Description: create Graph using Adjacency Matrix, ansi-style ========================================== ======================================================= * /# include <stdio. h> # include <stdlib. h> # define MAX_VEX_NUM 50 typedef char VertexType; typedef enum {DG, UDG} GraphType; typedef str Uct {VertexType vexs [MAX_VEX_NUM]; int arcs [MAX_VEX_NUM] [MAX_VEX_NUM]; int vexnum, arcnum; GraphType type;} MGraph; // set the vertex access flag int visit [MAX_VEX_NUM] in the graph;/*** obtain the subscript of the specified vertex in the vertex set based on the name * vex vertex * return if it is found, the subscript is returned, otherwise, 0 */int getIndexOfVexs (char vex, MGraph * MG) {int I; for (I = 1; I <= MG-> vexnum; I ++) is returned) {if (MG-> vexs [I] = vex) {return I;} return 0;}/*** create an adjacent matrix */void create_MG (MGraph * MG) {int I, j, k; int v 1, v2, type; char c1, c2; printf ("Please input graph type DG (0) or UDG (1):"); scanf ("% d ", & type); if (type = 0) MG-> type = DG; else if (type = 1) MG-> type = UDG; else {printf ("Please input correct graph type DG (0) or UDG (1 )! "); Return;} printf (" Please input vexmun: "); scanf (" % d ", & MG-> vexnum); printf (" Please input arcnum :"); scanf ("% d", & MG-> arcnum); getchar (); for (I = 1; I <= MG-> vexnum; I ++) {printf ("Please input % dth vex (char):", I); scanf ("% c", & MG-> vexs [I]); getchar ();} // initialize the adjacent matrix for (I = 1; I <= MG-> vexnum; I ++) {for (j = 1; j <= MG-> vexnum; j ++) {MG-> arcs [I] [j] = 0 ;}/// enter the edge information and establish the adjacent matrix for (k = 1; k <= MG-> arcnum; k ++) {printf ("Please input % dth arc v1 (char) v2 (char):", k ); scanf ("% c", & c1, & c2); v1 = getIndexOfVexs (c1, MG); v2 = getIndexOfVexs (c2, MG ); if (MG-> type = 1) MG-> arcs [v1] [v2] = MG-> arcs [v2] [v1] = 1; elseMG-> arcs [v1] [v2] = 1; getchar () ;}/ *** print the adjacent matrix and vertex information */void print_MG (MGraph MG) {int I, j; if (MG. type = DG) {printf ("Graph type: Direct graph \ n");} else {printf ("Graph type: Undirect graph \ n ");} printf ("Graph vertex number: % d \ n", MG. vexnum); printf ("Graph arc number: % d \ n", MG. arcnum); printf ("Vertex set: \ n"); for (I = 1; I <= MG. vexnum; I ++) printf ("% c \ t", MG. vexs [I]); printf ("\ nAdjacency Matrix: \ n"); for (I = 1; I <= MG. vexnum; I ++) {j = 1; for (; j <MG. vexnum; j ++) {printf ("% d \ t", MG. arcs [I] [j]);} printf ("% d \ n", MG. arcs [I] [j]) ;}/ *** initialize the vertex access flag **/void init_Visit () {int I; for (I = 0; I <MAX_VEX_NUM; I ++) visit [I] = 0;}/*** span traversal graph **/void BFS_MG (MGraph MG, int s) {// clear the access flag init_Visit (); // define a queue to save the adjacent vertex int Q [MAX_VEX_NUM] of the current node; int front = 0; int rear = 0; int I, j, k; printf ("% c \ t", MG. vexs [s]); visit [s] = 1; Q [rear ++] = s; // traverses the queue while (front <rear) {I = Q [front ++]; for (j = 1; j <= MG. vexnum; j ++) {if (visit [j] = 0 & MG. arcs [I] [j] = 1) {printf ("% c \ t", MG. vexs [j]); visit [j] = 1; Q [rear ++] = j ;}}/ *** main function */int main (void) {MGraph MG; create_MG (& MG); print_MG (MG); printf ("\ nThe result of BFS: \ n"); BFS_MG (MG, 1); return EXIT_SUCCESS ;}
Complete code to implement breadth-first traversal using an adjacent table:
/* ===================================================== ============================================================ Name: ALGraph. c Author: jesson20121020 Version: Copyright: Your copyright notice Description: Graph using linkList, ansi-style ========================================== ======================================================= * /# include <stdio. h> # include <stdlib. h> # include <stdio. h> # define MAX_VERTEX_NUM 50 typedef enum {DG, UDG} GraphTy Pe; typedef char VertexType; // table node typedef struct ArcNode {int adjvex; // adjacent node int weight; // side weight struct ArcNode * nextarc; // next node pointer} ArcNode, * ArcPtr; // header node typedef struct {VertexType vexdata; int id; ArcPtr firstarc;} VNode; // header node array typedef struct {VNode vertices [MAX_VERTEX_NUM]; int vexnum, arcnum; GraphType type;} ALGraph; int visit [MAX_VERTEX_NUM]; /*** obtain the subscript In the vertex Array Based on the vertex character */int getIndexOfVexs (char vex, ALGrap H * AG) {int I; for (I = 1; I <= AG-> vexnum; I ++) {if (AG-> vertices [I]. vexdata = vex) {return I;} return 0;}/*** create an adjacent table */void create_AG (ALGraph * AG) {ArcPtr p, q; int I, j, k, type; VertexType v1, v2; printf ("Please input graph type UG (0) or UDG (1):"); scanf ("% d ", & type); if (type = 0) AG-> type = DG; else if (type = 1) AG-> type = UDG; else {printf ("Please input correct graph type UG (0) or UDG (1 )! "); Return;} printf (" please input vexnum: "); scanf (" % d ", & AG-> vexnum); printf (" please input arcnum :"); scanf ("% d", & AG-> arcnum); getchar (); for (I = 1; I <= AG-> vexnum; I ++) {printf ("please input the % dth vex (char):", I); scanf ("% c", & AG-> vertices [I]. vexdata); getchar (); AG-> vertices [I]. firstarc = NULL;} for (k = 1; k <= AG-> arcnum; k ++) {printf ("please input the % dth arc v1 (char) v2 (char): ", k); scanf (" % c % C ", & v1, & v2); I = getIndexOfVexs (v1, AG); j = getIndexOfVexs (v2, AG ); // create an adjacent table based on the graph type // method 1, insert it to the linked list Header/* if (AG-> type = DG) {// Directed Graph p = (ArcPtr) malloc (sizeof (ArcNode); p-> adjvex = j; p-> nextarc = AG-> vertices [I]. firstarc; AG-> vertices [I]. firstarc = p;} else {// undirected graph p = (ArcPtr) malloc (sizeof (ArcNode); p-> adjvex = j; p-> nextarc = AG-> vertices [I]. firstarc; AG-> vertices [I]. firstarc = p; p = (ArcPtr) malloc (sizeof (ArcNode); p-> adjvex = I; p-> nextarc = AG-> vertices [j]. firstarc; AG-> vertices [j]. firstarc = p;} * // method 2, inserted to the end of the linked list if (AG-> type = DG) {// Directed Graph p = (ArcPtr) malloc (sizeof (ArcNode); p-> adjvex = j; // The table is empty if (AG-> vertices [I]. firstarc = NULL) {AG-> vertices [I]. firstarc = p;} else {// find the last table node q = AG-> vertices [I]. firstarc; while (q-> nextarc! = NULL) {q = q-> nextarc;} q-> nextarc = p;} p-> nextarc = NULL;} else {// undirected graph p = (ArcPtr) malloc (sizeof (ArcNode); p-> adjvex = j; // The table is empty if (AG-> vertices [I]. firstarc = NULL) {AG-> vertices [I]. firstarc = p;} else {// find the last table node q = AG-> vertices [I]. firstarc; while (q-> nextarc! = NULL) {q = q-> nextarc;} q-> nextarc = p;} p-> nextarc = NULL; p = (ArcPtr) malloc (sizeof (ArcNode )); p-> adjvex = I; // The table is empty. if (AG-> vertices [j]. firstarc = NULL) {AG-> vertices [j]. firstarc = p;} else {// find the last table node q = AG-> vertices [j]. firstarc; while (q-> nextarc! = NULL) {q = q-> nextarc;} q-> nextarc = p;} p-> nextarc = NULL;} getchar ();}} /*** output Graph Information */void print_AG (ALGraph AG) {ArcPtr p; int I; if (AG. type = DG) {printf ("Graph type: Direct graph \ n");} else {printf ("Graph type: Undirect graph \ n ");} printf ("Graph vertex number: % d \ n", AG. vexnum); printf ("Graph arc number: % d \ n", AG. arcnum); printf ("Vertex set: \ n"); for (I = 1; I <= AG. vexnum; I ++) printf ("% c \ t", AG. Vertices [I]. vexdata); printf ("\ nAdjacency List: \ n"); for (I = 1; I <= AG. vexnum; I ++) {printf ("% d", I); p = AG. vertices [I]. firstarc; while (p! = NULL) {printf ("--> % d", p-> adjvex); p = p-> nextarc;} printf ("\ n ");}} /*** initialize the vertex access flag **/void init_Visit () {int I; for (I = 0; I <MAX_VERTEX_NUM; I ++) visit [I] = 0;}/*** span traversal graph **/void BFS_AG (ALGraph AG, int s) {ArcPtr p; // clear the access flag init_Visit (); // define a queue to save the adjacent vertex int Q [MAX_VERTEX_NUM] of the current node; int front = 0; int rear = 0; int I, j, k; printf ("% c \ t", AG. vertices [s]); visit [s] = 1; Q [rear ++] = s; // traverse the queue while (front <rear) {I = Q [front ++]; for (p = AG. vertices [I]. firstarc; p = p-> nextarc) {j = p-> adjvex; if (visit [j] = 0) {printf ("% c \ t ", AG. vertices [j]. vexdata); visit [j] = 1; Q [rear ++] = j ;}}} int main (void) {ALGraph AG; create_AG (& AG ); print_AG (AG); printf ("\ nThe result of BFS: \ n"); BFS_AG (AG, 1); return EXIT_SUCCESS ;}