Simple cognition of graphs and adjacency matrix and adjacency table storage

Source: Internet
Author: User

The graph consists of a poor non-empty vertex set V and a side e. Each edge is a point pair (v,w). The data elements in the diagram are called vertices . If the point pair is ordered (the next point of each point is fixed), then the graph is called a forward graph , otherwise there is no graph . The value associated with the edge of the graph is called the weighted value , and the weighted graph is called the Net (network ).

Simple concepts related to graphs:
(1) If the diagram contains a line from a vertex to his own side (V,V), then the path V is called the ring .
(2) A simple path means that all points on the path are different, but the first and last points may be the same.
(3) The circle in the direction diagram starts at a vertex, returns to itself, and the path is at least 1 long. If this path is a simple path, then this circle is a simple circle .
Without the circle of the graph, it is required that this side is different from each other.
(4) If a non-directed graph from each vertex, have to each other fixed-point path, then this diagram is Unicom. An undirected graph of this nature is called a strongly connected graph .
(5) If a directed graph of all the direction of the orientation of the non-directional image is called the graph of the underlying map, also known as the base map .
(6) The graph is not strongly connected, but his jhoira is connected, it is called weak connectivity .
A full graph is a graph with one edge between each pair of vertices.

There are generally four ways to store graphs: (1) adjacency matrix method
(2) Adjacency table method
(3) Cross-linked list method
(4) adjacency multiple table method
This paper introduces two kinds of storage structure method adjacency matrix method and Adjacency table method.
The implementation of the adjacency matrix method (for non-directed non-weighted graphs)
The implementation of the code can be compared to the graph below to observe

#pragma once #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <malloc.h> #

Define Default_size 8//default maximum number of vertices #define T Char//enum bool {false,true}; typedef struct GRAPHMATRIX//For non-directed non-weighted graphs, the adjacency matrix method is used to store {int maxvertices;//maximum capacity int numvertices;//fixed point number int Numedgs;

The edge number T *verticeslist;//stores the fixed point structure int **vedges;//the storage boundary}graphmatrix;
    void Init (Graphmatrix *g)//initialization of the matrix {int i,j;
    G->maxvertices = default_size;
    g->numvertices = G->numedgs = 0;
    G->verticeslist = (t*) malloc (sizeof (T) * (g->maxvertices));
    ASSERT (NULL! = g->verticeslist); G->vedges = (int**) malloc (sizeof (int *) * (g->maxvertices));//initial boundary number of memory Max Open assert (NULL! = g->vedges);// Determine if the boundary memory is successful for (i = 0;i<g->maxvertices;++i)//Initialize two-dimensional boundary value {g->vedges[i] = (int*) malloc (sizeof (in
    T) * (g->maxvertices));
} for (i = 0;i<g->maxvertices;++i) {for (j = 0;j<g->maxvertices;++j) {            G-&GT;VEDGES[I][J] = 0; }}} bool Insert (Graphmatrix *g, T x)//Vertex insertion {if (g->numvertices >= g->maxvertices) return F
    Alse;
g->verticeslist[g->numvertices++] = x;
    } void Show (Graphmatrix *g)//adjacency matrix method Display {printf ("");
    for (int i = 0;i<g->numvertices;++i) {printf ("%c", G->verticeslist[i]);
    } printf ("\ n"); for (int i = 0;i<g->numvertices;++i) {printf ("%c", G->verticeslist[i]);//Column input vertex for (int j = 0
        ; j<g->numvertices;++j) {printf ("%d", g->vedges[i][j]);
    } printf ("\ n");
} printf ("\ n"); } int GetPos (Graphmatrix *g,t v)//Get v-fixed position {for (int i = 0;i<g->numvertices;++i) {if (G->vert
    Iceslist[i] = = v) return i;

} return-1;  } void Insertedge (Graphmatrix *gg,t x,t y)//insert Edge {int p1 = GetPos (gg,x);
    Get the x vertex position int p2 = GetPos (gg,y); if (P1==-1 | | p2==-1)//The vertex position is not legal return;
GG-&GT;VEDGES[P1][P2] = Gg->vedges[p2][p1] = 1;//The edge between two points is filled with 1 gg->numedgs++;  } void Removeedge (Graphmatrix *gg,t x,t y)//delete edge {int P1 = GetPos (gg,x);
    Get the x vertex position int p2 = GetPos (gg,y);
    if (P1==-1 | | p2==-1)//vertex position illegal return;
    if (gg->vedges[p1][p2] = = 0)//x,y between this has no side, directly return, otherwise re-assign value 0 return;
GG-&GT;VEDGES[P1][P2] = Gg->vedges[p2][p1] = 0;//The edge between two points is filled with 1 gg->numedgs--;  } void Removevertex (Graphmatrix *gg,t x)//delete vertex {int p = GetPos (gg,x);
    The position of x is not valid if (p = =-1) return; int i,j,numedges = 0;//used to determine the number of edges that the delete point contains for (i=p;i<gg->numvertices-1;++i) {Gg->verticeslist[i] = gg-
    >Verticeslist[i+1];
    } for (I=0;i<gg->numvertices;++i) {if (gg->vedges[p][i]! = 0) numedges++; } for (I=p;i<gg->numvertices-1;++i) {for (j=0;j<gg->numvertices;++j) {gg-& Gt VEDGES[I][J] = gg->vedges[i+1][j]; Delete the row where x is located, and the subsequent row where x is located is assigned to the forward value} for (I=p;i<gg->numvertices;++i) {for (J=0;J&LT;GG-&GT;NUMV  ERTICES;++J) {Gg->vedges[j][i] = gg->vedges[j][i+1];
    Delete the column where x is located and assign the next column in X to the top of the list in turn. gg->numvertices--;
Gg->numedgs-= numedges; } void Destroy (Graphmatrix *gg)//Release the chain table space and release the dynamic request for the two-dimensional space {free (gg->verticeslist);//release Vertex gg->verticeslist = NULL
    ;
    for (int i=0;i<gg->numvertices;++i)//release Edge {free (gg->vedges[i]);
    } free (gg->vedges);
    Gg->numedgs = gg->numvertices = gg->maxvertices = 0;
printf ("Matrix freed\n");
    The test file #include "graph_head.h" void Main () {Graphmatrix GG;      Init (&AMP;GG);
    The initialization of the matrix insert (&gg, ' A ');//The vertex of the matrix is inserted into insert (&AMP;GG, ' B ');
    Insert (&gg, ' C ');
    Insert (&gg, ' D ');
    Insert (&gg, ' E ');

    Show (&AMP;GG);
    Insertedge (&gg, ' a ', ' B ');//The Edge of the matrix is inserted, with 1 representing the edge between the two Insertedge (&gg, ' a ', ' D '); Insertedge (&GG, ' B ', ' C ');
    Insertedge (&gg, ' B ', ' E ');
    Insertedge (&gg, ' E ', ' C ');
    Insertedge (&gg, ' C ', ' D ');

    Show (&AMP;GG); Removeedge (&gg, ' C ', ' E ');

    Destroy side Show (&AMP;GG);

    Removevertex (&gg, ' C ');//Destroy Vertex Show (&AMP;GG);
Destroy (&AMP;GG);//Destroy Matrix Show (&AMP;GG); }

Output of the test file

Implementation of the Adjacency table (for a non-forward permission graph)

#pragma once//non-entitlement graph #include <stdio.h> #include <malloc.h> #include <assert.h> #define Max_size #de

Fine T char typedef struct EDGES {int dex;//vertex subscript struct Edges *link;//pointer to next edge}edges;

typedef struct VERTEX {T data;//vertex data Edges *adj;//vertex pointers to edges}vertex;    typedef struct GRAPH_TABLE {int maxvertices;//max capacity int nmuvertices;//vertex number int numedges;

The number of edges Vertex *nnodetable;//The structure of the vertices of the table,}graph_table;
    void Initgraph (graph_table *gt) {gt->maxvertices = max_size;
    gt->nmuvertices = gt->numedges = 0;
    Gt->nnodetable = (vertex*) malloc (sizeof (VERTEX) * (gt->maxvertices));
    ASSERT (NULL! = gt->nnodetable);
    for (int i= 0;i<gt->maxvertices;++i) {Gt->nnodetable[i].adj = NULL;
    }} void Insertvertex (graph_table *gt,t x)//{if (gt->nmuvertices >= gt->maxvertices) return;
Gt->nnodetable[gt->nmuvertices++].data = x; } void Show (Graph_table *gt) {Edges *p;
        for (int i=0;i<gt->nmuvertices;++i) {printf ("%d,%c:", i,gt->nnodetable[i].data);
        p = gt->nnodetable[i].adj;
            while (NULL! = p) {printf ("%d-----", p->dex);
        p = p->link;
    } printf ("null.\n");
} printf ("\ n"); } int Getpos (graph_table *gt,t x)//Gets the position of the vertex {for (int i= 0;i<gt->nmuvertices;++i) {if (gt->nn
    Odetable[i].data = = x) return i;  } return-1;
    Vertex does not exist in table} void Insertedge (graph_table *gt,t x,t y)//insert Edge {int p1 = Getpos (gt,x);
    int P2 = Getpos (gt,y);
    if (P1==-1 | | p2==-1) return;
    Edges *s;
    s = (Edges *) malloc (sizeof (Edges));//Open Edge node assert (s! = NULL);      S->dex = p2;
    X-->y x to y edge S->link = gt->nnodetable[p1].adj;

    Gt->nnodetable[p1].adj = s;
    s = (Edges *) malloc (sizeof (Edges));//Open Edge node assert (s! = NULL);      S->dex = p1; Y-->x y to x Edge S->liNK = gt->nnodetable[p2].adj;

    Gt->nnodetable[p2].adj = s;
gt->numedges++;    The test file #include "graph_table.h" void Main () {graph_table gt;    The object of the adjacency table Initgraph (&AMP;GT);
    Initialization of adjacency table Insertvertex (&gt, ' A ');//insertion of adjacent vertices Insertvertex (&gt, ' B ');
    Insertvertex (&gt, ' C ');
    Insertvertex (&gt, ' D ');
    Insertvertex (&gt, ' E ');

    Show (&AMP;GT);
    Insertedge (&gt, ' a ', ' B ');//insertion of adjacent edges Insertedge (&gt, ' a ', ' D ');
    Insertedge (&gt, ' B ', ' C ');
    Insertedge (&gt, ' B ', ' E ');
    Insertedge (&gt, ' C ', ' D ');
    Insertedge (&gt, ' C ', ' E ');
Show (&AMP;GT); }

Test results for adjacency table

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.