Storage-adjacency matrix of graphs

Source: Internet
Author: User

I can't tell myself this is a preview, or review

BFS and DFS are finally starting.

First review A


The storage structure of the so-called adjacency matrix (adjacency matrix) is to use a one-dimensional array to store the information of vertices in a graph, and to represent the adjacency between vertices in the graph with a matrix. Assuming that figure g= (v,e) has n determined vertices, v={v0,v1,..., vn-1}, the vertices in G are adjacent to a nxn matrix, and the elements of the matrix are:


where Wij represents the weight of the edge (VI,VJ) or <vi,vj>, ∞ represents the number of values allowed by a computer that is greater than all of the edge weights.
the adjacency matrix notation is shown in Figure 8.7.
the adjacency matrix notation is used to represent the mesh shown in Figure 8.8.


It is easy to see from the adjacency matrix storage method of graphs that this representation has the following characteristics:
the adjacency matrix of the ① graph must be a symmetric matrix. Therefore, it is only necessary to store the elements of the triangular matrix when the adjacency matrix is stored.
② for the graph, the number of rows I (or column i) of the adjacency matrix is not 0 elements (or non-∞ elements), which is the degree TD (VI) of the first vertex.
③ for a forward graph, the number of rows I (or columns i) of the adjacency matrix is not 0 elements (or non-∞ elements) that is exactly the out OD (vi) (or admission ID (vi)) of the I-vertex.
④ using the adjacency matrix method to store the graph, it is easy to determine whether there are edges connected between any two vertices in the graph; However, to determine how many edges are in the diagram, it is very costly to detect each element in rows and columns.

This is the limitation of storing graphs with adjacency matrices.

The following describes the adjacency matrix storage representation of the graph.

when using adjacency matrix to store graphs, in addition to using a two-dimensional array to represent the adjacency matrix of adjacent relationships between vertices, a one-dimensional array is needed to store the vertex information, plus the number of vertices and the number of edges of the graph. The form can be described as follows:

#define MAXVERTEXNUM 100/* Maximum vertex number is set to 100*/typedef char Vertextype; /* Vertex type set to character type */typedef int edgetype; /* The weight of the edge is set to integer */typedef struct {vertextype vexs[maxvertexnum];/* Vertex table */edetype Edges[maxvertexnum][maxvertexnum];/* adjacency Matrix, That is, the side table */int n,e; /* vertex number and edge number */}mgragh; /*maragh is the type of graph stored in the adjacency matrix */

The algorithm for establishing adjacency matrix storage for a graph is as follows:

void Createmgraph (Mgraph *g) {/* Create an adjacency matrix for graph G to store */int i,j,k,w;char ch;printf ("Enter the number of vertices and sides (input format: Top count, number of edges): \ n"); scanf ("%d,% D ",& (g->n),& (g->e));/* input vertex number and number of sides */printf (" Enter vertex information (input format: vertex <CR>): \ n "); for (i=0;i<g->n;i + +) scanf ("\n%c",& (G->vexs[i])); /* Enter vertex information, set up vertex table */for (i=0;i<g->n;i++) for (j=0;j<g->n;j++) g->edges[i][j]=0; /* Initialize the adjacency matrix */printf ("Enter the ordinal of the two vertices corresponding to each edge (input format: i,j): \ n"); for (k=0;k<g->e;k++) {scanf ("\n%d,%d", &i,&j); * Enter the E-bar to establish the adjacency matrix */g->edges[i][j]=1; /* If adding g->edges[j][i]=1;,*//*, the adjacency matrix storage of the graph is established */}}/*createmgraph*/


Storage-adjacency matrix 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.