Data structure--definition and storage structure of graphs

Source: Internet
Author: User

Definition of diagram

A graph is a collection of edges between a vertex with a poor non-empty set and a vertex. Note: In the diagram structure, no vertices are allowed, and in the definition, if V is a collection of vertices, the poor non-null of the vertex set V is emphasized.

In the figure, if there is no vertex to its own edge, and the same edge does not recur, it is called a simple diagram.

Storage structure of graphs

Adjacency Matrix

Consider that the graph is made up of vertices and edges or arcs. It is difficult to fit together, it is natural to consider the two structures to be stored separately. Vertices are large and small, so storing them in an array is a good choice. The edge or arc is the relationship between vertex and vertex, one dimension can not be confused, then consider using a two-dimensional array to store.

For graphs with weighted values, the weights of the unreachable edges can be infinitely large. Using this infinity represents two vertices unreachable.

Adjacency Matrix Structure Code

For simplicity, the default vertex is the int type, that is, the subscript//weight of the v0,v1,v2 and the int type # define Maxvex 100#define INF 65535typedef struct{int vexs[maxvex];int Arc[maxvex][maxvex];int numvertexes, Numedges;} Mgraph;

  

Create code for the no-map:

void Createmgraph (Mgraph *g) {//k is a loop variable int k = 0;//i,j represents the subscript of the vertex int i = 0;int j = 0;printf ("Number of read vertices and edges ... \ n"); scanf ("%d,%d", &am P G->numvertexes, &g->numedges);//input vertex information printf ("read vertex information ... \ n"); for (k = 0; k < g->numvertexes; k++) {scanf ("%d", &g->vexs[k]);} Initialize all of the edges for infinity for (i = 0; i < g->numvertexes; i++) {for (j = 0; J < g->numvertexes; J + +) {G->arc[i][j] = INF;}} Forint weight = 0;//Gets the input value of the value printf ("Read Edge (VI,VJ) The next table I, J, and corresponding weights of the two vertices ... \ n"); for (k = 0; k < g->numedges; k++) {scanf (" %d,%d,%d ", &i, &j, &weight); G->ARC[I][J] = weight; G->arc[j][i] = weight;} For}//createmgraph ()

The two-dimensional array of edges of a non-graph is a symmetric matrix, and the creation of a graph is simpler.

Test code:

void Main (void) {freopen ("In.txt", "R", stdin); Mgraph mgraph; Mgraph *pmgraph = &mGraph; Createmgraph (pmgraph); fclose (stdin); return;}

Test data:

// In.txt 4,501230,1,0, 2,20,3,71,2,82 ,3,5

adjacency table

Memory in the tree's storage structure, down to a child notation, the combination of this array and linked table storage method is called Adjacency table.

The array of attention is called the vertex table, and the linked list is the edge table node.

For a graph, it is actually simpler, but each vertex of the graph is divided into degrees and degrees.

adjacency Table Structure Code:

#define The weights of the Maxvex 100typedef struct Edgenode {int adjvex;int weight;//edge, with int representing the struct edgenode *next;} edgenode;typedef struct vertexnode{int data; Edgenode *firstedge;} Adjlist;typedef struct{adjlist adjlist[maxvex];int numvertexs;int numedges;} Adjgraph;

  

adjacency table to create a graph code:

void Createadjgraph (Adjgraph *g) {//unified, K used as the loop variable int k = 0;printf ("read vertices and number of edges ... \ n"); scanf ("%d%d", &g->numvertexs , &g->numedges);p rintf ("read vertices ... \ n"); for (k = 0; k < g->numvertexs; k++) {scanf ("%d", &g->adjlist[k]. data); G->adjlist[k].firstedge = NULL;} printf ("Read edge information ... \ n");//i,j is used as the following table representing vertices, weight represents the weight of the edge int i = 0;int J = 0;int weight = 0;for (k = 0; k < g->numedges; k++) { scanf ("%d%d%d", &i, &j, &weight); Edgenode *pedgenode = (edgenode*) malloc (sizeof (Edgenode));p Edgenode->adjvex = J;pedgenode->weight = weight; Pedgenode->next = null;//uses the head interpolation method to insert this side node Pedgenode->next = g->adjlist[i].firstedge; G->adjlist[i].firstedge = pedgenode;/* non-graphic insert edge information//for the non-graphic, here to insert edgenode *pedgenode = (edgenode*) malloc (sizeof ( Edgenode));p Edgenode->adjvex = I;pedgenode->weight = Weight;pedgenode->next = null;//using the head interpolation method, Insert this edge node into pedgenode->next = g->adjlist[j].firstedge; G->adjlist[j].firstedge = Pedgenode;*/}}//createadjgraph

When inserting edge table nodes, it is simpler to use the head interpolation method.

Test code:

void Main (void) {freopen ("In.txt", "R", stdin); Adjgraph adjgraph; Adjgraph *padjgraph = &adjGraph; Createadjgraph (padjgraph); fclose (stdin);}

  

Test data:

//In.txtTen  +0 1 2 3 4 5 6 7 8 90 1 40 2 10 3 32 3 11 4 91 5 82 4 62 5 72 6 83 5 43 6 74 7 54 8 65 7 85 8 66 7 66 8 57 9 78 9 3

Data structure--definition and storage structure 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.