Data structure (C implementation) ------- graph's Adjacent matrix representation

Source: Internet
Author: User

Data structure (C implementation) ------- graph's Adjacent matrix representation

 

The adjacent matrix represents the adjacent relationship between adjacent vertices. Set G = (V, E) to a graph with n vertices. If (vi, vj) belongs to E, then, the element A [I] [j] = wij or 1 in the adjacent matrix of G. Otherwise, A [I] [j] = 0 or infinity, where, wij can indicate the edge weight.

The undirected graph or undirected network's Adjacent matrix must be symmetric, because when (vi, vj) belongs to e, there are also (vj, vi) belong to E. However, Directed Graphs or directed network-connected adjacent matrices are not necessarily symmetric. Therefore, when an adjacent matrix is used to represent a directed graph with n vertices or directed networks, the storage space required is n ^ 2. Because the undirected graph or undirected network adjacent matrix is symmetric, the compression storage method can be used to store only the elements in the lower Triangle Matrix (including the elements on the primary diagonal, the storage space only needs n * (n + 1)/2. Obviously, the spatial complexity of the adjacent matrix representation is s (n) = O (n ^ 2 ).

In addition to the adjacent matrix used to represent the relationship between vertices, a one-dimensional array is usually required to store vertex information. The design is as follows:

 

#define MAX_VEX_NUM 50typedef char VertexType;typedef enum {DG, UDG} GraphType;typedef struct {VertexType vexs[MAX_VEX_NUM];int arcs[MAX_VEX_NUM][MAX_VEX_NUM];int vexnum, arcnum;GraphType type;} MGraph;

The algorithm for creating the graph's Adjacent matrix is described as follows:

 

(1) type of input graph (undirected graph or directed graph)

(2) Number of vertices and edges in the input Graph

(3) enter the character information of the vertex to create the vertex array.

(4) initialize the adjacent matrix

(5) enter the edge information and establish the adjacent matrix of the graph. Note that the difference is the graph type. In addition, if the right graph is used, the adjacent matrix stores the edge weight, the graph is not authorized here.

 

The source code of the algorithm is as follows:

 

Void create_MG (MGraph * MG) {int I, j, k; int v1, v2, type; char c1, c2; printf (Please input graph type DG (0) or UDG (1) :); scanf (% d, & type); if (type = 0) MG-> type = DG; else if (type = 1) MG-> type = UDG; else {printf (Please input correct graph type DG (0) or UDG (1 )!); Return;} printf (Please input vexmun:); scanf (% d, & MG-> vexnum); printf (Please input arcnum:); scanf (% d, & MG-> arcnum); getchar (); for (I = 1; I <= MG-> vexnum; I ++) {printf (Please input % dth vex (char) :, I); scanf (% c, & MG-> vexs [I]); getchar () ;}// initialize the adjacent matrix for (I = 1; I <= MG-> vexnum; I ++) {for (j = 1; j <= MG-> vexnum; j ++) {MG-> arcs [I] [j] = 0 ;}// enter the edge information and create an adjacent matrix for (k = 1; k <= MG-> arcnum; k ++) {printf (Please input % dth arc v1 (char) v2 (char):, k); scanf (% c, & c1, & c2 ); v1 = getIndexOfVexs (c1, MG); v2 = getIndexOfVexs (c2, MG); if (MG-> type = 1) MG-> arcs [v1] [v2] = MG-> arcs [v2] [v1] = 1; elseMG-> arcs [v1] [v2] = 1; getchar ();}}
Algorithm Description:

 

The time complexity of this algorithm is O (n ^ 2 + n * e). The O (n ^ 2) time is spent on the initialization of the adjacent matrix, O (n * e) takes time to establish the adjacent matrix, because in general, e <

 

The complete code is as follows:

 

/* ===================================================== ============================================================ Name: graph. c Author: jesson20121020 Version: 1.0 Description: create Graph using Adjacency Matrix, ansi-style ========================================== ======================================================= * /# include
 
  
# Include
  
   
# Define limit 50 typedef char VertexType; typedef enum {DG, UDG} GraphType; typedef struct {VertexType vexs [MAX_VEX_NUM]; int arcs [MAX_VEX_NUM] [limit]; int vexnum, arcnum; graphType type;} MGraph;/*** obtain the subscript of the specified vertex in the vertex set based on the name * vex vertex * return. If the subscript is found, the subscript is returned. Otherwise, returns 0 */int getIndexOfVexs (char vex, MGraph * MG) {int I; for (I = 1; I <= MG-> vexnum; I ++) {if (MG-> vexs [I] = vex) {return I ;}} return 0 ;}/*** Create an adjacent matrix */void create_MG (MGraph * MG) {int I, j, k; int v1, v2, type; char c1, c2; printf (Please input graph type DG (0) or UDG (1) :); scanf (% d, & type); if (type = 0) MG-> type = DG; else if (type = 1) MG-> type = UDG; else {printf (Please input correct graph type DG (0) or UDG (1 )!); Return;} printf (Please input vexmun:); scanf (% d, & MG-> vexnum); printf (Please input arcnum:); scanf (% d, & MG-> arcnum); getchar (); for (I = 1; I <= MG-> vexnum; I ++) {printf (Please input % dth vex (char) :, I); scanf (% c, & MG-> vexs [I]); getchar () ;}// initialize the adjacent matrix for (I = 1; I <= MG-> vexnum; I ++) {for (j = 1; j <= MG-> vexnum; j ++) {MG-> arcs [I] [j] = 0 ;}// enter the edge information and create an adjacent matrix for (k = 1; k <= MG-> arcnum; k ++) {printf (Please input % dth arc v1 (char) v2 (char):, k); scanf (% c, & c1, & c2 ); v1 = getIndexOfVexs (c1, MG); v2 = getIndexOfVexs (c2, MG); if (MG-> type = 1) MG-> arcs [v1] [v2] = MG-> arcs [v2] [v1] = 1; elseMG-> arcs [v1] [v2] = 1; getchar () ;}/ *** print the adjacent matrix and vertex information */void print_MG (MGraph MG) {int I, j; if (MG. type = DG) {printf (Graph type: Direct graph);} else {printf (Graph type: Undirect graph);} printf (Graph vertex number: % d, MG. vexnum); printf (Graph arc number: % d, MG. arcnum); printf (Vertex set:); for (I = 1; I <= MG. vexnum; I ++) printf (% c, MG. vexs [I]); printf (Adjacency Matrix :); for (I = 1; I <= MG. vexnum; I ++) {j = 1; for (; j <MG. vexnum; j ++) {printf (% d, MG. arcs [I] [j]);} printf (% d, MG. arcs [I] [j]) ;}/ *** main function */int main (void) {MGraph MG; create_MG (& MG); print_MG (MG ); return EXIT_SUCCESS ;}
  
 

Execution result:

 

 

Please input graph type UG(0) or UDG(1) :0Please input vexmun : 4Please input arcnum : 4Please input 1th vex(char):aPlease input 2th vex(char):bPlease input 3th vex(char):cPlease input 4th vex(char):dPlease input 1th arc v1(char) v2(char) : a bPlease input 2th arc v1(char) v2(char) : a cPlease input 3th arc v1(char) v2(char) : a dPlease input 4th arc v1(char) v2(char) : b cGraph type: Direct graphGraph vertex number: 4Graph arc number: 4vertex set: abcdAdjacency Matrix:0111001000000000
In fact, there are other storage methods for the graph, such as the adjacent table and the cross linked list.

 

Related Article

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.