Storage form of graph: adjacency table

Source: Internet
Author: User
Tags define bool header insert int size

adjacency table: Adjacency table is a chain-type storage structure of graphs. In the adjacency table, a single linked list is established for each vertex in the graph, and the nodes in the I single linked list represent the edges attached to the Vertex VI (arcs with the Vertex VI as the end of the graph). Each node is composed of three domains, in which the adjacency point field indicates the location of the point adjacent to Vertex VI, the Link field indicates the node of the next edge or arc, and the data field stores the information related to the edge or arc, such as the weight value. A table header node is attached to each linked list. In the header node, the name of the storage Vertex VI is set, in addition to setting the Chain field to the first node of the list. As shown below:

Realize:

/************************************** Graph Storage adjacency table by ROWANDJJ 2014/6/23 **************************************/  
      
#include <iostream> using namespace std; #define MAX_VERTEX_NUM 20//maximum vertex number typedef enum{dg,dn,ag,an}graphkind;//-direction graph, forward network, non-direction graph, non-network typedef struct _ARCNODE_/  
      
/table Node (ARC) {int adjvex;//adjacency point ordinal struct _arcnode_ *nextarc;//point to the next arc int info;//information (weights)}arcnode; typedef struct _VNODE_//head Node {char data;//top roll name Arcnode *firstarc;//point to First arc}vnode,adjlist[max_vertex_num  
      
]; typedef struct _ALGRAPH_//adjacency Table {adjlist vertices;//adjacency table int vexnum;//vertex number int arcnum;//arc number Graphkin  
      
D kind;//The type}algraph of the graph;  void (*visitfunc) (char); Global function pointer bool Visited[max_vertex_num];  
/* Access flags Array (global amount) */void Visit (char p) {cout<<p<< ""; }//-----------------operation-------------------------------------//Back to the column page: http://www.bianceng.cnhttp:// Www.bianceng.cn/Programming/sjjg/int Locatevex (algraph g,char u); if there is a vertex u in G, return the vertex to the position in the diagram, otherwise return 1 bool Creategraph (algraph* G);//Use adjacency table storage structure, Construct a graph G with no relevant information (construct 4 graphs with one function) void Destroygraph (algraph* g);//Destroy Figure G char Getvex (algraph g,int v);//through serial number V get top name bool Putvex (a lgraph* G,char V,char value);/to v Assignment value int Firstadjvex (algraph G,char v);//returns the ordinal int nextadjvex of the first contiguous vertex of Vertex v (algraph G,char V,char W); Returns the ordinal number of the next contiguous vertex of V (relative to W), if W is the last contiguous point of V, returns-1 void Insertvex (algraph* G,char v);//Add a new vertex v in Figure G ( Do not add arc associated with vertex, leave Insertarc () to do) bool Deletevex (algraph* G,char v);//delete vertex v in G and its associated arc bool Insertarc (algraph* g,c Har V,char w);//Add arc <v,w&gt to G; If G is not, add symmetrical arc <w,v> bool Deletearc (algraph* G,char w); remove arc in G V,char  
gt; If G is not, then also remove the symmetric arc <w,v> void Dfstravel (algraph* g,void (*visit) (char));//Depth priority void DFS (Algraph g,int v); void Bfstravel (Algraph g,void (*visit) (char)),//Breadth First void Display (Algraph G);//print Graph//----------------Secondary queue------ ------------------------------------#define Max_queue_size typedef struct _QUEUENODE_ {int data;  
struct _queuenode_ *next;  
}queuenode;  
    typedef struct _QUEUE_ {Queuenode *phead;  
    Queuenode *ptail;  
int size;  
      
}queue;  
BOOL Initqueue (Queue *q);  
BOOL Destroyqueue (Queue *q);  
BOOL Dequeue (Queue *q,int* e);  
BOOL EnQueue (Queue *q, int e);  
BOOL Queueempty (Queue Q); ------------------------------------------------------------------bool Initqueue (Queue *q) {q->phead = Q  
    >ptail = (Queuenode *) malloc (sizeof (Queuenode)); if (!  
    Q->phead) {return false;  
    } q->phead->next = NULL;  
    q->size = 0;  
return true;  
    BOOL EnQueue (Queue *q, int e) {Queuenode *node = (queuenode*) malloc (sizeof (Queuenode));  
    Node->data = e;  
    Node->next = NULL;  
    Q->ptail->next = node;  
    q->ptail = node;  
    q->size++;  
return true; BOOL Dequeue (Queue *q,int* e) {QUeuenode *node = q->phead->next;  
        if (node) {*e = node->data;  
        Q->phead->next = node->next;  
        if (Q->ptail = node) {Q->ptail = q->phead;  
      
        Free (node);  
    q->size--;  
return true;  
BOOL Queueempty (Queue Q) {return q.size = 0;  
    BOOL Destroyqueue (Queue *q) {Queuenode *ptemp = q->phead->next;  
        while (ptemp!= NULL) {q->phead->next = ptemp->next;  
        Free (ptemp);  
    Ptemp = q->phead->next;  
    Free (q->phead);  
    q->size = 0;  
return true;  
    }//------------------------------------------------------------------int Locatevex (algraph g,char u) {  
    int i;  
        for (i = 0; i < G.vexnum. i++) {if (U = = g.vertices[i].data) {return i;  
}} return-1; } bool CreatEgraph (algraph* G) {int i,j,k;  
    int w;//weight char va,vb;//arc tail, ARC head arcnode *p;//arc cout<< "Please enter the type of the graph (forward graph: 0, have to net: 1, no to graph: 2, no to Net: 3):";  
    scanf ("%d",& (*g). kind);  
    cout<< "Please enter the vertex number of the graph, the number of edges:";  
    cin>>g->vexnum;  
      
    cin>>g->arcnum;  
    cout<< "Please enter the vertex value:" <<endl;  
        Construct vertex for (i = 0; i < g->vexnum; i++) {cin>>g->vertices[i].data;  
    G->vertices[i].firstarc = NULL; } if (G->kind = 1 | |  
    G->kind = = 3)//Net {cout<< "Please enter each arc (edge) of the weight, arc tail and arc head: \ n";  
    }else//Figure {cout<< "Please enter the arc tail and the arc Head of each arc (edge) in order \ n"; }//Constructor table node list for (k = 0; k < g->arcnum; k++) {if (G->kind = 1 | |  
            G->kind = = 3)//NET {cin>>w;  
            cin>>va;  
        cin>>vb;  
            }else//Figure {cin>>va;  
cin>>vb;        The position of the arc head is i = Locatevex (*g,va);  
          
        j = Locatevex (*G,VB);  
        p = (Arcnode *) malloc (sizeof (Arcnode));  
              
        P->adjvex = j; if (g->kind = 1 | | G->kind = = 3)//Net {P->info = w;//weight}else {p->info = NUL  
        L  
      
        }//Insert Table P->nextarc = g->vertices[i].firstarc;//inserted in table header G->vertices[i].firstarc = p; In the case of a g->kind graph or a non-direction net, you also need to increase the symmetric node if (= = 2 | |  
            G->kind = = 3) {p = (Arcnode *) malloc (sizeof (Arcnode));  
                  
            P->adjvex = i;  
            if (G->kind = = 3)//If not to the net, also need the weight value {p->info = W;  
            }else {p->info = NULL;  
            }//Insert Table P->nextarc = g->vertices[j].firstarc; G->vertiCes[j].firstarc = p;  
} return true;  
    } void Display (Algraph G) {Arcnode *p;  
    int i;  
        Switch (g.kind) {case dg:cout<< "forward graph";  
    Break  
        Case ag:cout<< "no direction diagram";  
    Break  
        Case dn:cout<< "have to net";  
    Break  
        Case an:cout<< "No to Net";  
    Break  
    Default:break;  
    } cout<<endl;  
    cout<< "vertices:" <<endl;  
    for (i = 0; i < G.vexnum i++) {cout<<g.vertices[i].data<< "";  
    } cout<<endl;  
    Side cout<< "side:" <<endl;  
        for (i = 0; i < g.vexnum i++) {p = G.vertices[i].firstarc; while (p) {if (G.kind = = 0 | | G.kind = = 1)//has to {cout<<g.vertices[i].data<< "" <<g.vertices[p->adjve  
                X].data; if (G.kind = 1)//has toNET {cout<< "" <<p->info;  
                    }else//{if (i < P->adjvex)/no repeat printing {  
                    cout<<g.vertices[i].data<< "" <<G.vertices[p->adjvex].data;  
                    if (G.kind = 3)//No to net {cout<< "" <<p->info;  
            }}} cout<<endl;  
        p = p->nextarc;  
    }} void Destroygraph (algraph* G) {Arcnode *p,*q;  
          
    int i;  
        for (i = 0; i < g->vexnum; i++) {p = g->vertices[i].firstarc;  
            while (p) {q = p->nextarc;  
            Free (p);  
        p = q;  
    } g->arcnum = 0;  
G->vexnum = 0;  
    } char Getvex (algraph G,int v) {if (V>=g.vexnum | | v<0) {exit (0);  
return g.vertices[v].data;  
    BOOL Putvex (algraph* G,char V,char value) {int i = Locatevex (*g,v);  
    if (i = = 1) {return false;  
      
    } g->vertices[i].data = value;  
return true;  
    int Firstadjvex (algraph G,char v) {int i = Locatevex (g,v);  
    if (I < 0) {return-1;  
    } arcnode *arcnode = G.vertices[i].firstarc;  
    if (Arcnode = = NULL) {return-1;  
Return arcnode->adjvex;  
    int Nextadjvex (algraph g,char V,char w) {int i,j;  
    i = Locatevex (g,v);  
    j = Locatevex (g,w);  
    Arcnode *p = G.vertices[i].firstarc;  
    while (P && P->adjvex!= j) {p = p->nextarc;  
    } if (!p | |!p->nextarc)//Not found W or W is the last contiguous point {return-1;  
    else {return p->nextarc->adjvex; } void Insertvex (Algraph* G,char v) {g->vertices[g->vexnum].data = v;  
      
    G->vertices[g->vexnum].firstarc = NULL;  
g->vexnum++;  
    BOOL Deletevex (algraph* G,char v) {int i,j;  
    Arcnode *p,*q;  
    1. Delete all the data in the row with the vertex in the adjacency table V, change the total number of arcs, vertex total i = Locatevex (*g,v);  
    if (I < 0 | | | I >= g->vexnum)//Illegal position {return false;  
    } p = g->vertices[i].firstarc;  
        while (p)//delete arc in turn {q = p->nextarc;  
        Free (p);  
        p = q;  
    g->arcnum--;  
    } g->vexnum--; 2. Change the position of the vertex after Vertex v in the array (move forward one bit) for (j = i; J < g->vexnum; J + +) {G->vertices[j] = G->vertice  
    S[J+1]; //3. Iterate over the remaining adjacency table and find the arc or edge that contains the vertex v and delete it. Also note that for each arc/edge traversed, update the ordinal number for (j = 0; J < g->vexnum; J + +) {p = g->vertices[j].firstarc;//p point to  
      The first arc or edge while (p) {if (P->adjvex = i) of the vertex being traversed if it finds an arc or edge that points to the deleted vertex {          if (p = = g->vertices[j].firstarc)//If the node to be deleted is the first node {g->vertices[j].  
                    Firstarc = p->nextarc;  
                    Free (p);  
                    p = g->vertices[j].firstarc;  
                    if (g->kind <= 1)//If there is a direction, you also need to change the number of arcs {g->arcnum--;  
                    }}else//is not the first node {Q->nextarc = p->nextarc;  
                    Free (p);  
                    p = q->nextarc;  
                    if (g->kind <= 1)//If there is a direction, you also need to change the number of arcs {g->arcnum--; }}else//If the current arc is not the arc to be found, continue traversing the {if (P->adjvex & Gt  
                i)//(very critical) update serial number {p->adjvex--;  
                } q = P;  
     p = p->nextarc;//point to Next arc       }} return true;  
    BOOL Insertarc (algraph* G,char V,char w) {int i,j,weight;  
    Arcnode *arcnode;  
    1. The ordinal number i = Locatevex (*g,v) of V and W in the adjacency table;  
    j = Locatevex (*g,w);  
    if (i<0 | | j<0) {return false;  
    } g->arcnum++; if (g->kind = 1 | |  
        G->kind = = 3) {cout<< "Input weight:"; cin>>weight;//input Weights}//2. Generates an arc node that is inserted into the position of the first adjacent point of Vertex v (if it is a net, user input weights are required) Arcnode = (arcnode*) Mall  
    OC (sizeof (Arcnode));  
    Arcnode->adjvex = j; if (g->kind = 1 | |  
    G->kind = = 3) {arcnode->info = weight;  
    else {arcnode->info = NULL;  
    } Arcnode->nextarc = g->vertices[i].firstarc;  
    G->vertices[i].firstarc = Arcnode;   
        3. If it is not, then a symmetric node must be generated and inserted into the appropriate position if (G->kind >= 2) {Arcnode = (Arcnode *) malloc (sizeof (Arcnode)); Arcnode->Adjvex = i;  
        if (G->kind = 3)//No to the net {arcnode->info = weight;  
        else {arcnode->info = NULL;  
        } Arcnode->nextarc = g->vertices[j].firstarc;  
    G->vertices[j].firstarc = Arcnode;  
return true;  
    BOOL Deletearc (algraph* G,char V,char w) {int i,j;  
    Arcnode *p,*q;  
    1. The ordinal number i = Locatevex (*g,v) of V and W in the adjacency table;  
    j = Locatevex (*g,w);  
    if (I < 0 | | | J < 0) {return false;  
    //2. Delete v-w p = g->vertices[i].firstarc;  
        while (P && p->adjvex!=j) {q = p;  
    p = p->nextarc;  
        if (P && p->adjvex==j)//Find arc <v-w> {if (p = = G->vertices[i].firstarc)//p refers to the first arc  
        {G->vertices[i].firstarc = p->nextarc; else {Q->nextarc = p->nextarc; 
        Free (p);  
    g->arcnum--;  
        //3. If there is no direction, delete w-v if (G->kind >= 2) {p = g->vertices[j].firstarc;  
            while (P && p->adjvex!=i) {q = p;  
        p = p->nextarc; } if (P && p->adjvex==i)//Find arc <w-v> {if (p = = G->vertices[j].firstarc)  
            P refers to the first arc {G->vertices[j].firstarc = p->nextarc;  
            else {Q->nextarc = p->nextarc;  
        Free (p);  
} return true;  
    } void Dfstravel (algraph* g,void (*visit) (char)) {int i;  
    Visitfunc = Visit;  
    for (i = 0; i < g->vexnum; i++) {Visited[i] = false;  
        for (i = 0; i < g->vexnum; i++) {if (!visited[i)) {DFS (*g,i); }  
    }  
    cout<<endl;  
    } void DFS (Algraph g,int v) {int i;  
    Char v1,w1;  
    V1 = Getvex (g,v);  
    VISITED[V] = true;  
      
    Visitfunc (G.vertices[v].data);  
            for (i = Firstadjvex (g,v1);i>=0; i = Nextadjvex (G,v1,w1 = Getvex (g,i))) {if (!visited[i)) {  
        DFS (G,i);  
    }} void Bfstravel (Algraph g,void (*visit) (char)) {Queue q;  
    Initqueue (&AMP;Q);  
    Char w1,u1;  
    int i,u,w;  
    for (i = 0; i < G.vexnum; i++) {visited[i] = false;  for (i = 0; i < G.vexnum i++) {if (!visited[i)) {Visited[i]  
            = true;  
            Visit (G.vertices[i].data);  
                  
            EnQueue (&q,i); while (!  
                Queueempty (q)) {dequeue (&q,&u);  
                u1 = Getvex (g,u); for (w = Firstadjvex (g,u1); w>=0;w = Nextadjvex (g,u1,w1=geTvex (g,w))) {if (!visited[w]) {VI  
                        SITED[W] = true;  
                        Visit (G.vertices[w].data);  
                    EnQueue (&AMP;Q,W);  
    }}}} destroyqueue (&AMP;Q);  
cout<<endl;  
    int main () {algraph graph;  
    Creategraph (&graph);  
          
    Display (graph);  
    cout<< "Depth First:" <<endl;  
    Dfstravel (&graph,visit);  
    cout<< "Breadth First:" <<endl;  
    Bfstravel (Graph,visit);  
          
    Destroygraph (&graph);  
return 0; }

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.