The graph's Adjacent matrix stores the representation and depth-first and breadth-first traversal, and the adjacent matrix

Source: Internet
Author: User

The graph's Adjacent matrix stores the representation and depth-first and breadth-first traversal, and the adjacent matrix

1 # include <stdio. h> 2 # include <stdlib. h> 3 4 # define OK 1 5 # define ERROR 0 6 # define MAX_VERTAX_SIZE 20 7 8 typedef char VerElemType; 9 typedef char ElemType; 10 typedef int Status; 11 12 typedef struct Graph {13 VerElemType VertaxMatrix [MAX_VERTAX_SIZE]; // vertex array 14 int AdjacentMatrix [MAX_VERTAX_SIZE] [MAX_VERTAX_SIZE]; // adjacent matrix 15 int VertaxNum; // Number of vertices 16 int EageNum; // Number of edges 17} Graph; 18 19 // queue, in the figure 20 typedef struct QueueNode {21 ElemType data; 22 struct QueueNode * next; 23} QueueNode, * QueueNodePtr; 24 typedef struct Queue {25 QueueNodePtr front; 26 QueueNodePtr rear; 27} Queue; 28 29 Status InitQueue (Queue * q) {30 (* q ). front = (QueueNodePtr) malloc (sizeof (struct QueueNode); 31 (* q ). rear = (* q ). front; 32 (* q ). rear-> next = NULL; 33 return OK; 34} 35 Status EnterQueue (Queue * q, El EmType e) {36 QueueNodePtr n; 37 n = (QueueNode *) malloc (sizeof (struct QueueNode); 38 n-> data = e; 39 n-> next = q-> rear-> next; 40 q-> rear-> next = n; 41 q-> rear = n; 42 return OK; 43} 44 Status DeleteQueue (Queue * q, ElemType * e) {45 QueueNodePtr p; 46 if (q-> front = q-> rear) {47 printf ("Empty \ n"); 48 return ERROR; 49} 50 p = q-> front-> next; 51 * e = p-> data; 52 q-> front-> next = p-> next; 53 free (P); 54 if (p = q-> rear) 55 q-> rear = q-> front; 56 return OK; 57} 58 Status IsQueueEmpty (Queue q) {59 return q. front = q. rear? OK: ERROR; 60} 61 62 // locate the subscript of a vertex 63 int LocateVertax (Graph G, VerElemType ver) {// Test 1 through 64 int I; 65 for (I = 0; I <G. vertaxNum; I ++) {66 if (G. vertaxMatrix [I] = ver) 67 return I; 68} 69 return-1; 70} 71 // create an undirected Graph 72 Status CreateUDG (Graph * G) {73 int I, j, k; 74 VerElemType x, y; 75 printf ("Create Undigraph. \ n "); 76 printf (" Please enter the number of Vertax and Eage: \ n "); 77 scanf (" % d % * c ", & (* G ). vertaxNum, & (G-> EageNum); // % * c eat and press ENTER 78 79 printf ("OK, please input value of % d Vertax. \ n ", G-> VertaxNum); 80 for (I = 0; I <G-> VertaxNum; I ++) {// initialize the vertex array 81 scanf ("% c % * c", & (G-> VertaxMatrix [I]); 82} 83 84 for (I = 0; I <G-> VertaxNum; I ++) // initialize the adjacent table 85 for (j = 0; j <G-> VertaxNum; j ++) 86G-> AdjacentMatrix [I] [j] = 0; 87 // for (I = 0; I <G-> VertaxNum; I ++) {// initialize the adjacent Table 88 // for (j = 0; j <G-> VertaxNum; j ++) 89 // printf ("% d", G-> AdjacentMatrix [I] [j]); 90 // printf ("\ n"); 91 //} 92 93 for (k = 0; k <G-> EageNum; k ++) {94 printf ("OK, please input two Vertax of Eage: % d, separated by Spaces. \ n ", k + 1); 95 scanf (" % c % * c ", & x, & y); 96 I = LocateVertax (* G, x ); 97 j = LocateVertax (* G, y); 98G-> AdjacentMatrix [I] [j] = G-> AdjacentMatrix [j] [I] = 1; 99} 100 return OK; 101} 102 // print Adjacent Matrix 103 Status PrintAdjacentMatrix (Graph G) {104 int I, j; 105 printf ("Adjacent Matrix \ n"); 106 for (I = 0; I <G. vertaxNum; I ++) {107 for (j = 0; j <G. vertaxNum; j ++) {108 printf ("% 3d", G. adjacentMatrix [I] [j]); 109} 110 printf ("\ n"); 111} 112 return OK; 113} 114 115 // Graph depth first traversal 116 // returns the first adjacent vertex of v. If no adjacent vertex exists,-1117 int FirstAdjacentVertax (Graph G, VerElemType v) is returned) {118 int index_v = LocateVertax (G, v); 119 int 120 for (I = 0; I <G. vertaxNum; I ++) {121 if (G. adjacentMatrix [index_v] [I] = 1) 122 return I; 123} 124 return-1; 125} 126 // w is the adjacent contact of v, returns the value-1127 int NextAdjacentVertax (Graph G, VerElemType v, VerElemType w) except the next vertex of w (starting from w) {128 int index_v = LocateVertax (G, v); 129 int index_w = LocateVertax (G, w); 130 int I; 131 for (I = index_w + 1; I <G. vertaxNum; I ++) {132 if (G. adjacentMatrix [index_v] [I] = = 1) 133 return I; 134} 135 return-1; 136} 137 // recursive thought of DFS: Access v, 138 // start depth first traversal from the first adjacent point of v, 139 // then start depth first traversal from the second adjacent point of v. Until there are no adjacent contacts 140 141 int visitedArray [MAX_VERTAX_SIZE]; 142 143 void visit (VerElemType c) {144 printf ("% c", c ); 145} 146 VerElemType GetVertaxValue (Graph G, int position) {147 return G. vertaxMatrix [position]; 148} 149 Status DFS (Graph G, VerElemType v) {// Depth First Search150 VerElemType w; 151 visit (v); 152 visitedArray [LocateVertax (G, v)] = 1; // accessed, 1153 154 for (w = GetVertaxValue (G, FirstAdjacentVertax (G, V); LocateVertax (G, w )! =-1; w = GetVertaxValue (G, NextAdjacentVertax (G, v, w) {155 if (visitedArray [LocateVertax (G, w)]! = 1) 156 DFS (G, w); 157} 158 return OK; 159} 160 Status DFSTraverse (Graph G) {161 int I; 162 for (I = 0; I <G. vertaxNum; I ++) {163 visitedArray [I] = 0; 164} 165 for (I = 0; I <G. vertaxNum; I ++) {166 if (visitedArray [I] = 0) {167 DFS (G, GetVertaxValue (G, I); 168} 169} 170 return OK; 171} 172 // BFS (breadth first traversal): similar to the hierarchy traversal of the queue and tree 173 // thought: Join the first vertex, 174 // team the elements in the pair, if no access exists, call visit to access and queue all adjacent vertices in the 175 Status BFSTravers E (Graph G) {176 ElemType c; 177 Queue q; 178 InitQueue (& q); 179 int I, j; 180 for (I = 0; I <G. vertaxNum; I ++) 181 visitedArray [I] = 0; 182 183 for (I = 0; I <G. vertaxNum; I ++) {184 if (visitedArray [I] = 0) {185 EnterQueue (& q, G. vertaxMatrix [I]); 186 visitedArray [I] = 1; 187 while (IsQueueEmpty (q )! = OK) {188 DeleteQueue (& q, & c); // all entries in the team are edited for access, so that no repeated entries in the team will come in 189 visit (c ); 190 for (j = FirstAdjacentVertax (G, c); j! =-1; j = NextAdjacentVertax (G, c, GetVertaxValue (G, j) {191 if (visitedArray [j] = 0) {192 EnterQueue (& q, getVertaxValue (G, j); 193 visitedArray [j] = 1; // all entries in the team are edited to be accessed, in this way, there will be no repeated entries in the team: 194} 195} 196} 197} 198 199 200 int main () {201 Graph G; 202 CreateUDG (& G ); 205 PrintAdjacentMatrix (G); 206 printf ("the Result of DFS (Depth First Search) is:"); 207 DFSTraverse (G ); 208 printf ("\ nthe REsult of BFS (Breadth First Srarch) is:"); 209 BFSTraverse (G); 210 return 0; 211}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.