Each of the outermost loops in the graph's depth-first traversal and breadth-first traversal algorithm produces an undirected graph of connected components, each of which can produce a spanning tree, which is a forest in which the spanning trees are combined. Using the tree's child brother list notation to represent the forest is the content of this section of the algorithm.
Depth-first Forest code:
Depth first generates forest void Dfstree (amlgraph g,int i,tree * T,bool isvisited[]) {Isvisited[i] = True;bool IsFirst = true; Tree p,q = null;for (int next = Firstadj (G,i), Next! = 1; next = Nextadj (G,i,next)) {if (isvisited[next] = = False) {p = Mak Etreenode (G.adjmulist[next].vexname); if (IsFirst) {(*t)->firstchild = P,isfirst = false;} else{q->nextsibling = P;} Q = P;dfstree (g,next,&q,isvisited);}}} void Dfsforest (amlgraph g,tree * t) {bool Isvisited[max_vex_num] = {false};*t = NULL; Tree p,q;for (int i = 0; i < G.vexnum; i++) {if (isvisited[i] = = False) {p = Maketreenode (g.adjmulist[i].vexname);//each cycle is a spanning tree. if (*t = = NULL) {//The first is the root node *t = p;} else{//The remaining spanning tree is the first spanning tree brother q->nextsibling = p;} Q = p;//Save the previous tree Dfstree (g,i,&q,isvisited);}}
Breadth-first generation of forest code:
Breadth first generates forest void Bfsforest (amlgraph g,tree * t) {bool Isvisited[max_vex_num] = {false}; Tree Treearray[max_vex_num] = {null};//Records the node coordinates of all vertices.//Because the father who is going to find the tree node to traverse. ( This sentence P = treearray[top];) Tree p,q,r; Linkqueue Queue;queueinit (&queue); for (int i = 0; i < G.vexnum; i++) {if (isvisited[i] = = False) {p = Maketreenode (g. Adjmulist[i].vexname); if (i = = 0) {//First spanning tree: *t = P;} else{//The other spanning tree is the first spanning Tree Brothers//(*t)->nextsibling = p; it's just a sibling node. q->nextsibling = P;} Q = p;treearray[i] = p;isvisited[i] = True;enqueue (&queue,i); while (!queueempty (queue)) {int Top;dequeue (&queue , &top); bool IsFirst = true;for (int next = Firstadj (g,top); next! = 1; next = Nextadj (G,top,next)) {if (Isvisited[next] = = False) {Isvisited[next] = True;r = Maketreenode (g.adjmulist[next].vexname); Treearray[next] = R;if (isFirst) {p = treeAr Ray[top];p->firstchild = R,isfirst = false;} else{p->nextsibling = r;} p = r;enqueue (&queue,next);}}}} Queuedestory (&queue);}
Two errors were made in the write breadth-first generation of forest code:
1.
(*t)->nextsibling = p; There is only one sibling node in this writing.
q->nextsibling = p;
2.
Did not write this sentence:
p = treearray[top];
The coordinates of all vertices are then stored in the array to find the parent node of the node.
The detailed source code is as follows:
Project file Network address: Click to open the link
AMLGraph.cpp: Defines the entry point of the console application. Adjacency multiple tables of the graph include "stdafx.h" #include <cstdlib> #include "queue.h" #include "BinaryTree.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 that is related to 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-> ; Inext;while (Next! = NULL) {Arcnode * freenode = Next;next = Next->iindex = = I? Next->inext:next->jnext;if (Freenode->iindex = = i) {////only releases the node IIndex equals I, and does not release free more than once (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);//delete vertices also need 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->inext;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->adjmulist[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);} void Dfstree (amlgraph g,int v,tree * T,bool * isvisited) {Isvisited[v] = True;bool first = true; Tree p,q= null;for (int next = Firstadj (G,V), Next! = 1; next = Nextadj (G,v,next)) {if (isvisited[next] = = False) {p = maket Reenode (G.adjmulist[next].vexname), if (first) {(*t)->firstchild = P;first = false;} else{q->nextsibling = P;} Q = P;dfstree (g,next,&q,isvisited);}}} Depth-first traversal spanning forest void Dfsforest (amlgraph g,tree * Tree) {bool Isvisited[max_vex_num] = {False};*tree = NULL; Tree p,q;for (int i = 0; I <g.vexnum; i++) {if (isvisited[i] = = False) {p = Maketreenode (g.adjmulist[i].vexname); if (!*t REE) {*tree = p;//root node.} else{q->nextsibling = P;}} Q = p;//q points to the root of the current spanning tree: Dfstree (g,i,&p,isvisited);}} */void dfs (amlgraph g,int i,bool isvisited[]) {printf ("%c", g.adjmulist[i].vexname); Isvisited[i] = true;for (int next = Firstadj(G,i); Next! =-1; Next = Nextadj (G,i,next)) {if (isvisited[next] = = False) {DFS (g,next,isvisited);}}} Deep search void Dfstraverse (Amlgraph g) {bool Isvisited[max_vex_num] = {false};p rintf ("---------depth-first search traversal---------\ n"); for ( int i = 0; i < G.vexnum; i++) {if (isvisited[i] = = False) {//has not visited Dfs (g,i,isvisited);}} printf ("\ n");} Wide search void Bfstraverse (Amlgraph g) {bool Isvisited[max_vex_num] = {false};p rintf ("---------breadth-first search traversal---------\ n"); Linkqueue Queue;queueinit (&queue), for (int i = 0; i < G.vexnum; i++) {if (isvisited[i] = = False) {Enqueue (&queue, i);p rintf ("%c", g.adjmulist[i].vexname); Isvisited[i] = True;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);p rintf ("\ n");} Depth-first generation forest void Dfstree (amlgraph g,int i,tree * T,bool isvisited[]) {Isvisited[i] = True;bool IsFIrst = true; Tree p,q = null;for (int next = Firstadj (G,i), Next! = 1; next = Nextadj (G,i,next)) {if (isvisited[next] = = False) {p = Mak Etreenode (G.adjmulist[next].vexname); if (IsFirst) {(*t)->firstchild = P,isfirst = false;} else{q->nextsibling = P;} Q = P;dfstree (g,next,&q,isvisited);}}} void Dfsforest (amlgraph g,tree * t) {bool Isvisited[max_vex_num] = {false};*t = NULL; Tree p,q;for (int i = 0; i < G.vexnum; i++) {if (isvisited[i] = = False) {p = Maketreenode (g.adjmulist[i].vexname);//each cycle is a spanning tree. if (*t = = NULL) {//The first is the root node *t = p;} else{//The remaining spanning tree is the first spanning tree brother q->nextsibling = p;} Q = p;//Save the previous tree Dfstree (g,i,&q,isvisited);}} Breadth first generates forest void Bfsforest (amlgraph g,tree * t) {bool Isvisited[max_vex_num] = {false}; Tree Treearray[max_vex_num] = {null};//Records the node coordinates of all vertices.//Because the father who is going to find the tree node to traverse. ( This sentence P = treearray[top];) Tree p,q,r; Linkqueue Queue;queueinit (&queue); for (int i = 0; i < G.vexnum; i++) {if (isvisited[i] = = False) {p = Maketreenode (g. Adjmulist[i].vexname); if (i = = 0) {//First spanning tree:*t = P;} else{//The other spanning tree is the first spanning Tree Brothers//(*t)->nextsibling = p; it's just a sibling node. q->nextsibling = P;} Q = p;treearray[i] = p;isvisited[i] = True;enqueue (&queue,i); while (!queueempty (queue)) {int Top;dequeue (&queue , &top); bool IsFirst = true;for (int next = Firstadj (g,top); next! = 1; next = Nextadj (G,top,next)) {if (Isvisited[next] = = False) {Isvisited[next] = True;r = Maketreenode (g.adjmulist[next].vexname); Treearray[next] = R;if (isFirst) {p = treeAr Ray[top];p->firstchild = R,isfirst = false;} else{p->nextsibling = r;} p = r;enqueue (&queue,next);}}}} Queuedestory (&queue);} adjacency multiple table int _tmain (int argc, _tchar* argv[]) {amlgraph g;creategrahp (&g);p rintgrahp (g);d fstraverse (g); bfstraverse (g); Tree tree;dfsforest (G,&tree);p rintf ("\ n----------Depth-first traversal of the spanning forest (sequential traversal)------------------\ n");p Reordertraverse ( Tree), Treedestory (&tree), Bfsforest (G,&tree);p rintf ("\ n----------Breadth-first traversal of spanning forest (sequential traversal)------------------\ n") ;p Reordertraverse (tree), Treedestory (&tree);d estorygrAPh (&g); return 0;}
Run:
Look at the data structure write code (40) The depth-first spanning tree and the breadth-first spanning tree of the non-graph