Look at the data structure write code (38) adjacency Multiple table representation and implementation of graphs

Source: Internet
Author: User
Tags in degrees

The adjacency multiple table of graphs is another representation of the graph without direction. It differs from the adjacency table only in that the adjacency table represents an edge with two vertices, whereas the adjacency multiple table uses a vertex to represent an edge. This makes adjacency multiple tables convenient for certain operations. For example, to mark a searched edge or delete an edge.

Here is the structure of the adjacency multiple tables:

The following 6 edges are represented by 6 arc nodes, pointing with 12 pointers, and each arc node is pointed to 2 times. This allows us to take extra care when releasing memory.


The following code:

Source 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> #define Max_vex_num 20enum e_state{e_state_error = 0,E_STATE_OK = 1,};enum e_visitif{unvisited = 0,visited = 1,};struct arcnode{e_visitif mark;int iindex,jindex;//vertex i,j position in diagram ArcNode * I next;//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 ("The number of vertices and edges of the input graph(arc) number \ 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 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;} Char Firstadj (amlgraph G,char vex) {int location = vexlocation (G,vex); Arcnode * Next = G.adjmulist[location].heaD->inext;if (Next! = NULL) {int index = Next->iindex = = Location? Next->jindex:next->iindex;return G.adjmuli St[index].vexname;} Return ';} Char Nextadj (amlgraph g,char Vex1,char vex2) {int location = vexlocation (G,VEX1); Arcnode * next = G.adjmulist[location].head->inext;while (next! = NULL) {//find to Vex2char Iname = G.adjmulist[next->ii  Ndex].vexname;char jname = g.adjmulist[next->jindex].vexname;if (iname = = Vex2 | | jname = VEX2) {next = Next->iindex = = Location? Next->inext:next->jnext;break;}} if (next = NULL) {int index = Next->iindex = = Location? Next->jindex:next->iindex;return g.adjmulist[index].ve XName;} Return ';} 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;cha R delData1 = G->adjmulist[delnode->iindex].vexname;char DelData2 = g->adjmulist[delnode->jindex].vexname ;d Eletearc (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->jindex:next->iindex;printf (" %c ", g.adjmulist[index].vexname); next = Next->iindex = = I? Next->inext:next->jnext;} printf ("\ n");}} adjacency multiple table int _tmain (int argc, _tchar* argv[]) {amlgraph g;creategrahp (&g);p rintgrahp (g);p rintf ("Vertices of graph:%d, edge (ARC) Tree:%d \ n ", g.vexnum,g.arcnum); char * Isadj = Graphisadj (g, ' B ', ' d ')? "Adjacent": "nonadjacent"; int degree = Graphdegree (g, ' d '); char first = Firstadj (g, ' C '); char next = Nextadj (g, ' d ', ' C ');p rintf The next adjacency point for the C adjacency point of the%c,d is:%c\n ", First,next);p rintf (" B and D%s,d in degrees:%d\n ", Isadj,degree), Insertvex (&g, ' f ');p rintf (" After inserting the F-vertex, the graph structure is as follows: \ n ");p RINTGRAHP (g); Insertarc (&g, ' e ', ' f ');p rintf (" after inserting (e,f) The graph structure is as follows: \ n ");p RINTGRAHP (g);d Eletearc ( &g, ' d ', ' C ');p rintf ("Delete (D,c) after the graph structure is as follows: \ n");p RINTGRAHP (g);d Eletevex (&g, ' C ');p rintf ("delete vertex c after the graph structure is as follows: \ n"); PRINTGRAHP (g);p rintf ("Number of vertices of the graph:%d, number of edges (arcs):%d\n", G.vexnum,g.arcnum);d estorygraph (&g); return 0;}
Run:




Look at the data structure write code (38) adjacency Multiple table representation and implementation of graphs

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.