# Look at the data structure write code (39) Graph traversal (deep search and wide search)

Source: Internet
Author: User

There are two kinds of traversal algorithms for graphs: depth-first search traversal and breadth-first search traversal. The depth-first search traverses similar-to-tree sequential traversal. Breadth-first search traverses a similar sequence traversal to the tree. Only the graph can have disconnected nodes, so it is necessary to traverse the entire array of vertices.

Deep search traversal always first accesses the adjacency point of the current node, and the wide search algorithm is to access the adjacency point of the vertex first before the neighboring point of the access vertex is accessed.

The specific traversal sequence is as follows:

The following code traverses the underlying structure with the adjacency multiple table of the graph.

First change the return value of the previous section's lookup adjacency point and the next adjacency point, as well as the code error of the adjacency point, and add one less sentence:

if (Next->iindex = = Location2 | | next->jindex = = location2) {
Next = Next->iindex = = Location1? next->inext:next->jnext;
Break
}
Next = Next->iindex = = Location1? next->inext:next->jnext;

`int Firstadj (amlgraph g,int location) {Arcnode * next = g.adjmulist[location].head->inext;if (next! = NULL) {int index = Next->iindex = = Location? Next->jindex:next->iindex;return index;} return-1;} int Nextadj (amlgraph g,int location1, int location2) {Arcnode * next = G.adjmulist[location1].head->inext;while (next!) = NULL) {if (Next->iindex = = Location2 | | next->jindex = = location2) {next = Next->iindex = Location1? next->i Next:next->jnext;break;} Next = Next->iindex = = Location1? Next->inext:next->jnext;} if (next = NULL) {int index = Next->iindex = = location1? Next->jindex:next->iindex;return index;} return-1;}`

Then the specific code for deep search and wide search:

`void Dfs (amlgraph g,int i,bool * isvisitedarray) {printf ("%c", g.adjmulist[i].vexname); Isvisitedarray[i] = true;for (int Next = Firstadj (G,i); Next! =-1; Next = Nextadj (G,i,next)) {if (isvisitedarray[next] = = False) {DFS (G,next,isvisitedarray);}}} Depth-First search traversal void Dfstraver (Amlgraph g) {bool Isvisited[max_vex_num] = {false};p rintf ("---------- Depth-first traversal------------------\ n "); for (int i = 0; i < G.vexnum; i++) {if (isvisited[i] = = False) {DFS (g,i,isvisited);}} printf ("\ n");} Breadth-First search traversal void Bfstraverse (Amlgraph g) {bool Isvisited[max_vex_num] = {false};p rintf ("---------- Breadth-first traversal------------------\ n "); Linkqueue Queue;queueinit (&queue); for (int i = 0; i < G.vexnum; i++) {if (isvisited[i] = = False) {printf ("%c", G.ADJMU List[i].vexname); Isvisited[i] = True;enqueue (&queue,i), while (!queueempty (queue)) {int Top;dequeue (&queue, &top), for (int next = Firstadj (G,top), Next! =-1; next = Nextadj (G,top,next)) {if (isvisited[next] = = False) {printf ("% C ", g.adjmulist[next].vexname); Isvisited[next] = True; Enqueue (&queue,next);}}}} Queuedestory (&queue);}`

The complete source code is as follows:

Wide search to use the chain team code is not put in, want to see can go into the network address download project files.

`AMLGraph.cpp: Defines the entry point of the console application. Adjacency multiple tables of the graph include "stdafx.h" #include <cstdlib> #include "queue.h" #define Max_vex_num 20enum e_visitif{ unvisited = 0,visited = 1,};struct arcnode{e_visitif mark;int iindex,jindex;//vertex i,j position in diagram Arcnode * iNext;// The next arc Arcnode associated with the i vertex point * jnext;//the next arc};struct Vnode{char vexname associated with the J Vertex Point; Arcnode * head;//head pointer};struct amlgraph{vnode adjmulist[max_vex_num];//vertex array int vexnum,arcnum;};/ /Gets the head node of the arc Arcnode * Getheadnode () {Arcnode * Pnode = (Arcnode *) malloc (sizeof (Arcnode)); if (pnode) {Pnode->iindex = Pnode ->jindex = -1;pnode->inext = Pnode->jnext = Null;pnode->mark = unvisited;} return pnode;} Arcnode * Getarcnode (int iindex,int jindex) {Arcnode * Pnode = Getheadnode (); if (pnode) {Pnode->iindex = IINDEX;PNODE-&G T;jindex = Jindex;} return pnode;} int vexlocation (amlgraph G,char vex) {for (int i = 0; i < G.vexnum; i++) {if (G.adjmulist[i].vexname = vex) {return i;}} return-1;} void Creategrahp (Amlgraph * g) {printf ("number of vertices and edges (arcs) of the input graph \ n"); scanf ("%d%d%*c",&G->vexnum,&g->arcnum);      Constructs the vertex set printf ("Enter the vertex set \ n"); for (int i = 0; i < g->vexnum; i++) {char name;scanf ("%c", &name); g->adjmulist[i].vexname = NAME;G-&G      T;adjmulist[i].head = Getheadnode ();//Build head node and have head pointer pointing to head node}//construct vertex relationship fflush (stdin);      printf ("Please enter the relationship of vertices \ n");  for (int i = 0; i < g->arcnum; i++) {char vex1,vex2;scanf ("%c%c%*c", &vex1,&vex2); int location1 = vexlocation (*g,vex1); int location2 = Vexlocation (*G,VEX2); Arcnode * Pnode = Getarcnode (location1,location2);p Node->inext = g->adjmulist[location1].head->inext;g-> Adjmulist[location1].head->inext = Pnode;pnode->jnext = g->adjmulist[location2].head->inext;g->    Adjmulist[location2].head->inext = Pnode; }}void destorygraph (Amlgraph * g) {for (int i = 0; i < g->vexnum; i++) {Arcnode * next = G->adjmulist[i].head-&gt ; Inext;while (Next! = NULL) {Arcnode * freenode = Next;next = Next->iindex = I? next->inext: next->jnext;if (Freenode->iindex = = i) {////only releases iIndex equals I node, not multiple free (freenode);}} Free (g->adjmulist[i].head); g->adjmulist[i].head = Null;g->adjmulist[i].vexname = "; g->vexNum = g-> Arcnum = 0;}} Vertex vex1 and vertex vex2 are adjacent bool Graphisadj (amlgraph G,char Vex1,char vex2) {int location = vexlocation (G,VEX1); Arcnode * next = G.adjmulist[location].head->inext;while (next! = NULL) {if (G.adjmulist[next->iindex].vexname = = Vex2 | | G.adjmulist[next->jindex].vexname = = Vex2) {return true;} Next = Next->iindex = = Location? Next->inext:next->jnext;} return false;} int Graphdegree (amlgraph G,char vex) {int degree = 0;int location = vexlocation (G,vex); Arcnode * Next = g.adjmulist[location].head->inext;//calculates all out-of-degrees while (next! = NULL) {degree++;next = Next->iindex = Loc Ation? Next->inext:next->jnext;} return degree;} Insert Edge (arc) void Insertarc (Amlgraph * G,char Vex1,char vex2) {int location1 = vexlocation (*g,vex1); int location2 = Vexlocation (*G,VEX2); Arcnode *node = Getarcnode (location1,location2); node->inext = g->adjmulist[location1].head->inext;g->adjmulist[ Location1].head->inext = Node;node->jnext = g->adjmulist[location2].head->inext;g->adjmulist[ Location2].head->inext = Node;g->arcnum + +;} Delete Edge (arc) void Deletearc (Amlgraph * G,char Vex1,char vex2) {G->arcnum--;int location1 = vexlocation (*g,vex1); int Location2 = Vexlocation (*G,VEX2); Arcnode * Next = g->adjmulist[location1].head->inext; Arcnode * pre = G->adjmulist[location1].head;while (next! = NULL) {if (Next->iindex = = Location2) {if (pre = = G->ad Jmulist[location1].head | | Pre->iindex = = Location1) {//delete the first node. Or the precursor of index = Location1pre->inext = Next->jnext;} Else{pre->jnext = Next->jnext;} break;} else if (Next->jindex = = Location2) {if (pre = = G->adjmulist[location1].head | | pre->iindex = = location1) {// The first node is deleted. Or a precursor of index = Location1pre->inext = Next->inext;} Else{pre->jnext = Next->inext;} break;} Pre = Next; next = Next->iindex = = Location1? Next->inext:next->jnext;} Next = G->adjmulist[location2].head->inext;pre = G->adjmulist[location2].head;while (next! = NULL) {if (next- >iindex = = location1) {if (pre = = G->adjmulist[location2].head | | pre->iindex = = location2) {//delete the first node. or Index of the precursor = Location1pre->inext = Next->jnext;} Else{pre->jnext = Next->jnext;} Free (next); else if (Next->jindex = = Location1) {if (pre = = G->adjmulist[location2].head | | pre->iindex = = location2) {// The first node is deleted. Or a precursor of index = Location1pre->inext = Next->inext;} Else{pre->jnext = Next->inext;} Free (next); Pre = Next;next = Next->iindex = = Location2? Next->inext:next->jnext;}} Insert vertex void Insertvex (Amlgraph * g, char vex) {if (G->vexnum < max_vex_num) {G->adjmulist[g->vexnum].vexname = Vex;g->adjmulist[g->vexnum].head = Getheadnode (); g->vexnum++;}} Delete vertex void Deletevex (Amlgraph * G,char vex) {int location = vexlocation (*g,vex);Deleting vertices also needs to traverse the entire graph to find the ARC node associated with Vex for (int i = 0; i < g->vexnum; i++) {Arcnode * next = G->adjmulist[i].head->i Next;while (Next! = NULL) {if (Next->iindex = = Location | | next->jindex = = LOCATION) {Arcnode * Delnode = Next;next = Next->iindex = = Location? Next->inext:next->jnext;char delData1 = G->adjmulist[delnode->iindex].vexname;char DelData2 = g-> Adjmulist[delnode->jindex].vexname;deletearc (G,DELDATA1,DELDATA2);} Else{next = Next->iindex = = location? Next->inext:next->jnext;}}} Change the position of elements that result from deleting vertices ... for (int i = 0; i < g->vexnum; i++) {Arcnode * next = g->adjmulist[i].head->inext;while (next = NULL) {if (Next ->iindex = = i) {if (Next->iindex > location) {next->iindex--;} if (Next->jindex > location) {next->jindex--;}} Next = Next->iindex = = Location? Next->inext:next->jnext;}} Free (g->adjmulist[location].head);//Release head node//vex the following vertex moves up for (int i = location + 1; i < g->vexnum; i++) {G-&GT;ADJMU LiST[I-1] = G->adjmulist[i];} G->vexnum--;} void Printgrahp (Amlgraph g) {for (int i = 0; i < G.vexnum; i++) {printf (the adjacency of "%c" is: ", g.adjmulist[i].vexname); Arcnode * Next = g.adjmulist[i].head->inext;//Delete all arc tails while (next! = NULL) {int index = Next->iindex = = I? Next->ji ndex:next->iindex;printf ("%c", g.adjmulist[index].vexname); next = Next->iindex = = I? Next->inext:next->jnext;} printf ("\ n");}} int Firstadj (amlgraph g,int location) {Arcnode * next = g.adjmulist[location].head->inext;if (next! = NULL) {int index = Next->iindex = = Location? Next->jindex:next->iindex;return index;} return-1;} int Nextadj (amlgraph g,int location1, int location2) {Arcnode * next = G.adjmulist[location1].head->inext;while (next!) = NULL) {if (Next->iindex = = Location2 | | next->jindex = = location2) {next = Next->iindex = Location1? next->i Next:next->jnext;break;} Next = Next->iindex = = Location1? Next->inext:next->jnext;} if (next! = NULL) {int index = Next->iindex = = Location1? Next->jindex:next->iindex;return index;} return-1;} void Dfs (amlgraph g,int i,bool * isvisitedarray) {printf ("%c", g.adjmulist[i].vexname); Isvisitedarray[i] = true;for (int Next = Firstadj (G,i); Next! =-1; Next = Nextadj (G,i,next)) {if (isvisitedarray[next] = = False) {DFS (G,next,isvisitedarray);}}} Depth-First search traversal void Dfstraver (Amlgraph g) {bool Isvisited[max_vex_num] = {false};p rintf ("---------- Depth-first traversal------------------\ n "); for (int i = 0; i < G.vexnum; i++) {if (isvisited[i] = = False) {DFS (g,i,isvisited);}} printf ("\ n");} Breadth-First search traversal void Bfstraverse (Amlgraph g) {bool Isvisited[max_vex_num] = {false};p rintf ("---------- Breadth-first traversal------------------\ n "); Linkqueue Queue;queueinit (&queue); for (int i = 0; i < G.vexnum; i++) {if (isvisited[i] = = False) {printf ("%c", G.ADJMU List[i].vexname); Isvisited[i] = True;enqueue (&queue,i), while (!queueempty (queue)) {int Top;dequeue (&queue, &top); for (int next = Firstadj (g,top); next! = 1; next = Nextadj (G,top,next){if (Isvisited[next] = = False) {printf ("%c", g.adjmulist[next].vexname); Isvisited[next] = True;enqueue (&queue, Next);}}}} Queuedestory (&queue);} adjacency multiple table int _tmain (int argc, _tchar* argv[]) {amlgraph g;creategrahp (&g);p rintgrahp (g);d fstraver (g); Bfstraverse (g ); return 0;}`
Run:

The results of a deep search can be seen from the adjacency table of a: first, access the a node, then access the first adjacency point D of a, then access the first adjacency point C of D, then access the first adjacency point of C and then access the adjacency point of E

C and B, C was visited, Access B, then Access B's adjacency point, etc... Finally, a separate vertex FG is accessed.

So deep search results are: ADCEBFG

Wide search results: First access A, then access all the adjacency points of a, DCB, access all the neighboring points of D CA (have been accessed skipped), all the adjacency points of C Edba, only E is not accessed, access to him, and then access all the adjacency points of B: ECA and so on ... Last access to individual vertex FG

So the results of the wide search: ADCBEFG

Look at the data structure write code (39) Graph traversal (deep search and wide search)

Related Keywords:

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.