See Data structure Write code (36) Adjacency table representation and implementation of graphs

Source: Internet
Author: User
Tags in degrees

The adjacency table notation for graphs is to create a linked list for each vertex, with information about the arcs in the same arc at the end of the list, which are stored in the array. The following is an adjacency table of the G2 graph


Adjacency table saves space compared to adjacency matrix, but also brings some operational inconvenience, for example, to see whether two vertices are adjacent, need to traverse the linked list, in the case of the dimension of the non-graph vertex, simply traverse the list of vertices, and the degree of the graph vertex needs to traverse the whole graph to find the number of the arc head. If you do not want to do this, you can create an inverse adjacency table, which is the information that holds the arc of the same arc head in the chain table. The next SectionTo says the cross-linked list is similar to this structure.

The following code:

Graph.cpp: Defines the entry point of the console application. adjacency table notation for graphs # include "stdafx.h" #include <cstdlib> #include <climits> #define Max_vertex_num 20#define  INFINITY int_maxenum e_graph_kind {DG = 0,//There is a direction to the graph dn,//there is a direction to the net udg,//no direction of the network}; struct arcnode//Edge (ARC) {int adjvex;//vertex position in array Arcnode * nextadj;int weight;//weight};typedef struct vnode//vertex {Arcnode * head;/ /head pointer char vexname;//vertex name}adjlist[max_vertex_num];struct graph//diagram {adjlist list;//adjacency table int arcnum,vexnum; E_graph_kind kind;};/ /vertex position in the array int vexlocation (Graph G,char vex) {for (int i = 0; i < G.vexnum; i++) {if (G.list[i].vexname = vex) {return i;} }return-1;} Arcnode * Getheadnode () {//Get head node: Arcnode * node = (arcnode*) malloc (sizeof (Arcnode)); if (node! = NULL) {Node->adjvex = -1;node->nextadj = NULL;NODE-&G T;weight = INFINITY;} return node;} Arcnode * Getarcnode (Graph G,char vexname) {arcnode * node = Getheadnode (); if (node! = NULL) {int location = Vexlocation (g,v Exname); node->adjvex = location;} return node;} void Createdg (Graph * gRaph);  void Createdn (graph * graph);  void Createudg (graph * graph);    void Createudn (graph * graph);      void Graphcreate (Graph * graph) {E_graph_kind Kind;      printf ("Enter the type of diagram you want to create (with direction: 0, Mesh: 1, no graph: 2, no mesh: 3) \ n");      scanf ("%d", &kind);          Switch (kind) {case dg:createdg (graph);      Break          Case Dn:createdn (graph);      Break          Case UDG:CREATEUDG (graph);      Break          Case UDN:CREATEUDN (graph);      Break      Default:break; }}//Graph void createdg (graph * g) {g->kind = dg;printf ("Input graph's vertex tree and Edge (arc) tree \ 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->list[i].vexname = Name;g->lis    T[i].head = Getheadnode ();//The head pointer points to the 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); Arcnode * node = Getarcnode (*G,VEX2); Node->nextadj = G->list[location1].head->nextadj;g->list[location1]    . Head->nextadj = node; }}//has a direction to the net. void Createdn (graph * g) {g->kind = dn;printf ("Input graph's vertex tree and Edge (arc) tree \ 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->list[i].vexname = Name;g->lis    T[i].head = Getheadnode ();      }//construct vertex relationship fflush (stdin);      printf ("Please enter the relationship of vertices \ n"); for (int i = 0; i < g->arcnum; i++) {char vex1,vex2;int weight;scanf ("%c%c%d%*c", &vex1,&vex2,&w  eight); int location1 = vexlocation (*G,VEX1); Arcnode * node = Getarcnode (*G,VEX2); node->weight = Weight;node->nextadj = g->list[location1].head->    Nextadj;g->list[location1].head->nextadj = node; }}//non-graphic void Createudg (grapH * g) {g->kind = udg;printf ("Input graph's vertex tree and Edge (arc) tree \ 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->list[i].vexname = Name;g->lis    T[i].head = Getheadnode ();      }//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); Arcnode * Node1 = Getarcnode (*g,vex2); Node1->nextadj = g->list[location1].head->nextadj;g->list[ Location1].head->nextadj = Node1;int Location2 = vexlocation (*G,VEX2); Arcnode * Node2 = Getarcnode (*g,vex1); Node2->nextadj = g->list[location2].head->nextadj;g->list[    Location2].head->nextadj = Node2; }}//createudn (graph * g) {g->kind = udn;printf ("Input graph's vertex tree and Edge (arc) tree \ n"); scanf ("%d%d%*c",&g->vexnum,&      G->arcnum); Constructs the vertex set printf ("Please enter the vertex set \ n"); for (int i = 0; i < g->vexnum; i++) {char name;scanf ("%c", &name); g->list[i].vexname = Name;g->lis    T[i].head = Getheadnode ();      }//construct vertex relationship fflush (stdin);      printf ("Please enter the relationship of vertices \ n"); for (int i = 0; i < g->arcnum; i++) {char vex1,vex2;int weight;scanf ("%c%c%d%*c", &vex1,&vex2,&w  eight); int location1 = vexlocation (*G,VEX1); Arcnode * Node1 = Getarcnode (*g,vex2); node1->weight = Weight;node1->nextadj = g->list[location1].head-> Nextadj;g->list[location1].head->nextadj = Node1;int Location2 = vexlocation (*G,VEX2); Arcnode * Node2 = Getarcnode (*g,vex1); node2->weight = Weight;node2->nextadj = g->list[location2].head->    Nextadj;g->list[location2].head->nextadj = Node2;  }}void graphdestory (Graph * g) {for (int i = 0; i < g->vexnum; i++) {Arcnode * next = G->list[i].head;while (next ! = NULL) {Arcnode * freenode = Next;next = Next->nextadj;free (freenode);} G->list[i].hEAD = Null;g->list[i].vexname = ";} G->vexnum = G->arcnum = 0;} Vex1 and Vex2 are adjacent. BOOL Grphisadj (Graph G,char Vex1,char vex2) {int location = vexlocation (G,VEX1); Arcnode * Next = g.list[location].head->nextadj;//The first node is the successor while (next! = NULL) of the head node {if (G.list[next->adjvex]. Vexname = = Vex2) {return true;} Next = Next->nextadj;} return false;} The degree of the vertex vex.//Direction = out degree + penetration//non-direction = out of int graphdegree (Graph G,char vex) {int degree = 0;int location = vexlocation (G,vex); Arcnode * next = G.list[location].head->nextadj;while (next! = NULL) {//out of degree ++;next = Next->nextadj;} if (G.kind = = DG | | g.kind = = DN) {//graph also need to traverse graph, look for in degrees. for (int i = 0; i < G.vexnum; i++) {Arcnode * next = g.list[i].head-& Gt;nextadj;while (Next! = NULL) {if (Next->adjvex = = location) {degree + +;} Next = Next->nextadj;}}} return degree;} Vex the first adjacency point Char Firstadj (Graph G,char vex) {int location = vexlocation (G,vex); Arcnode * next = g.list[location].head->nextadj;if (next! = NULL) {return G.list[next->adjvex].Vexname;} Return ';} Vex1 next adjacency point relative to Vex2 ... Char Nextadj (Graph g,char Vex1,char vex2) {int location = vexlocation (G,VEX1); Arcnode * next = G.list[location].head->nextadj;while (next! = NULL) {//Find to vex2if (G.list[next->adjvex].vexname = = VEX2) {break;} Next = Next->nextadj;} if (next! = NULL) {Arcnode * NextNode = next->nextadj;if (nextnode! = null) {return g.list[nextnode->adjvex].vexname; }}return ';} Insert vertex void Insertvex (Graph * G,char vex) {if (G->vexnum < max_vertex_num) {g->list[g->vexnum].vexname = Vex; G->list[g->vexnum].head = Getheadnode (); g->vexnum++;}} Delete vertex void Deletevex (Graph * G,char vex) {int location = vexlocation (*g,vex);//Free space Arcnode * next = g->list[location].h Ead->nextadj;int delnum = 0;while (next! = NULL) {Arcnode * freenode = Next;next = Next->nextadj;free (freeNode);d Elnu m++;} Vex the vertices below to move up for (int i = location + 1; i < g->vexnum; i++) {g->list[i-1] = g->list[i];} G->vexnum--;//Delete Edge (ARC) associated with vertex vex (and update all nodes Adjvex)To traverse a diagram: for (int i = 0; i < g->vexnum; i++) {Arcnode * next = g->list[i].head->nextadj; Arcnode * pre = G->list[i].head;while (next! = NULL) {if (Next->adjvex = = location) {Arcnode * freenode = Next;next = Next->nextadj;pre->nextadj = Next;free (freenode);d elnum++;} else {if (Next->adjvex > location) {//the node position below the vertex is reduced 1next->adjvex--;} Pre = Next;next = Next->nextadj;}}} G->arcnum-= delnum;//have to if (G->kind = = UDG | | g->kind = UDN) {g->arcnum + = DELNUM/2;}} Insert Edge (arc) void Insertarc (Graph * G,char Vex1,char vex2) {int location1 = vexlocation (*G,VEX1); Arcnode * Node1 = Getarcnode (*g,vex2); Node1->nextadj = g->list[location1].head->nextadj;g->list[ Location1].head->nextadj = node1;//The graph needs to be inserted on the other side. if (G->kind = = UDG | | g->kind = UDN) {int location2 = Vexlocation ( *G,VEX2); Arcnode * Node2 = Getarcnode (*g,vex1); Node2->nextadj = g->list[location2].head->nextadj;g->list[ Location2].head->nextadj = Node2;} G->arcnum + +;} Delete Edge (arc) voidDeletearc (Graph * G,char Vex1,char vex2) {G->arcnum--;int location1 = vexlocation (*g,vex1); int location2 = Vexlocation (*G,VEX2); Arcnode * Next = g->list[location1].head->nextadj; Arcnode * pre = G->list[location1].head;while (next! = NULL) {if (Next->adjvex = = location2) {Pre->nextadj = next-& Gt;nextadj;free (next); break;} Pre = Next;next = Next->nextadj;} if (G->kind = = UDG | | g->kind = = UDN) {//No map also need to delete the other side next = G->list[location2].head->nextadj;pre = G->list [Location2].head;while (Next! = NULL) {if (Next->adjvex = = location1) {Pre->nextadj = Next->nextadj;free (next); break;} Pre = Next;next = Next->nextadj;}}} void Printgrahp (Graph g) {for (int i = 0; i < G.vexnum; i++) {printf ("The adjacency point of%c:", g.list[i].vexname); Arcnode * next = G.list[i].head->nextadj;while (next! = NULL) {printf ("%c", g.list[next->adjvex].vexname); next = Next->nextadj;} printf ("\ n");}} int _tmain (int argc, _tchar* argv[]) {graph g;graphcreate (&g);p rintgrahp (g);p rintf ("The number of vertices of the graph:%d, Edge (ARC) Tree:%d\n ", G.vexnum,g.arcnum), char first = Firstadj (g, ' a '); char next = Nextadj (g, ' A ', ' C '); char * Isadj = Grphisadj (g, ' C ', ' d ')? "Adjacent": "Not adjacent"; int degree = Graphdegree (g, ' d ');p rintf ("A's first adjacency point is the next adjacency point of the C adjacency point of%c,a is:%c\n", First,next);p rintf ("C and D%s, The degree of D is:%d\n ", Isadj,degree), Insertvex (&g, ' e ');p rintf (" After inserting the e vertex the graph structure is as follows: \ n ");p RINTGRAHP (g); Insertarc (&g, ' a ', ' E ');p rintf ("Insert (a,e) Edge (ARC) after the graph structure as follows: \ n");p RINTGRAHP (g);d Eletearc (&g, ' d ', ' C ');p rintf ("Delete (d,c) Edge (ARC) after the graph structure as follows: \ n"); PRINTGRAHP (g);d Eletevex (&g, ' a ');p rintf ("After deleting vertex a the graph structure is as follows: \ n");p RINTGRAHP (g);p rintf ("Vertices of the graph:%d, edge (ARC) Tree:%d\n", G.vexnum,g.arcnum);//The timely destruction of memory is a good habit graphdestory (&g); return 0;}
Run:

The vertex tree of the last figure is 4 and the number of edges (arcs) is 1

See Data structure Write code (36) Adjacency 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.