Data structure (c implementation) adjacency matrix representation of-------graphs

Source: Internet
Author: User

Tag: the adjacency matrix of graphs represents the array storage of graphs

[This is my own study notes, welcome reprint, but please specify the source:http://blog.csdn.net/jesson20121020]

An adjacency matrix is a matrix that represents an adjacent relationship between vertices in a vertex. G= (V,e) is a graph with n vertices, if (VI,VJ) is E, the element corresponding to the adjacency matrix of g a[i][j] = Wij or 1, otherwise, a[i][j] = 0 or infinity, where wij can refer to the weight of the edge.

The adjacency matrix of an VI,VJ or a non-net must be symmetrical, since (VJ,VI) belongs to E when (a) belongs to E. The adjacency matrix of the graph or the forward net is not necessarily symmetrical, so the storage space required is n^2 when the adjacency matrix is used to represent a forward graph or a network with n vertices. Since the adjacency matrix of the graph or the non-direction network is symmetric, a compressed storage method can be used to store only the elements in the lower triangular matrix (including the elements on the main diagonal), and the storage space needs only n (n+1)/2. Obviously, the spatial complexity of adjacency matrix notation is s (n) = O (n^2).

In addition to storing adjacency matrices used to represent adjacent relationships between vertices, a one-dimensional array is usually required to store vertex information in the adjacency matrix representation diagram. The design is as follows:

#define Max_vex_num 50typedef char vertextype;typedef enum {DG, UDG} graphtype;typedef struct {vertextype vexs[max_vex_nu M];int arcs[max_vex_num][max_vex_num];int Vexnum, Arcnum; Graphtype type;} Mgraph;

The algorithm for constructing adjacency matrix of graphs is described as follows:

(1) Type of input graph (no direction or graph)

(2) The number of vertices and the number of edges of the input graph

(3) Entering the character information of the vertex, creating the vertex array

(4) Initializing adjacency matrix

(5) Input the information of the edge, establish the adjacency matrix of the graph, note that the distinction is the type of the graph, in addition, if it is the right graph, adjacent matrix to save its edge weight, here is an unauthorized map.


The algorithm source code 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):"); s CANF ("%d", &type), if (type = = 0) Mg->type = Dg;else if (type = = 1) Mg->type = udg;else {printf ("Please input Corre CT graph type DG (0) or UDG (1)! "); return;} printf ("Please input Vexmun:"); scanf ("%d", &mg->vexnum);p rintf ("Please input arcnum:"); scanf ("%d", &mg-& Gt;arcnum); GetChar (); for (i = 1; I <= mg->vexnum; i++) {printf ("Please input%dth vex (char):", I); scanf ("%c", &m G->vexs[i]); GetChar ();} Initialize the adjacency matrix for (i = 1; I <= mg->vexnum; i++) {for (j = 1; J <= mg->vexnum; J + +) {Mg->arcs[i][j] = 0;}} Enter the information for the edge, establish the adjacency matrix for (k = 1; k <= mg->arcnum; k++) {printf ("Please input%dth Arc v1 (char) v2 (char):", k); scanf ("%c  %c ", &AMP;C1, &c2); v1 = Getindexofvexs (c1, mg); v2 = Getindexofvexs (c2, MG); if (Mg->type = = 1) mg->arcs[v1][v2] = MG-&GT;ARCS[V2][V1] = 1;elsemg->arcs[v1][v2] = 1;getchar ();}}
Algorithm Description:

The time complexity of the algorithm is O (n^2+n*e), where O (n^2) time is spent on the initialization of the adjacency matrix operation, O (n*e) time is spent on the establishment of the adjacency matrix operation, because in general, e<<n^2, so the algorithm's time complexity O (n^2 )。


The complete code is as follows:

/* ============================================================================ name:graph.c Author:jesson 20121020 version:1.0 description:create Graph using adjacency Matrix, Ansi-style ================================= =========================================== * * #include <stdio.h> #include <stdlib.h> #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;/** * Gets the subscript of the specified vertex in the vertex collection by name * Vex Vertex * return if found, returns subscript, otherwise returns 0 */int Getindexofvexs (char vex, mgraph *mg) {int I;FO R (i = 1; I <= mg->vexnum; i++) {if (mg->vexs[i] = = vex) {return i;}} return 0;} /** * Create adjacency 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 (" Ple ASE Input CoRrect Graph type DG (0) or UDG (1)! "); return;} printf ("Please input Vexmun:"); scanf ("%d", &mg->vexnum);p rintf ("Please input arcnum:"); scanf ("%d", &mg-& Gt;arcnum); GetChar (); for (i = 1; I <= mg->vexnum; i++) {printf ("Please input%dth vex (char):", I); scanf ("%c", &m G->vexs[i]); GetChar ();} Initialize the adjacency matrix for (i = 1; I <= mg->vexnum; i++) {for (j = 1; J <= mg->vexnum; J + +) {Mg->arcs[i][j] = 0;}} Enter the information for the edge, establish the adjacency matrix for (k = 1; k <= mg->arcnum; k++) {printf ("Please input%dth Arc v1 (char) v2 (char):", k); scanf ("%c %c ", &AMP;C1, &c2); v1 = Getindexofvexs (c1, mg); v2 = Getindexofvexs (c2, MG); if (Mg->type = = 1) mg->arcs[v1][v2] = MG-&GT;ARCS[V2][V1] = 1;elsemg->arcs[v1][v2] = 1;getchar ();}} /** * Print adjacency matrix and vertex information */void print_mg (mgraph MG) {int I, j;if (mg.type = = DG) {printf ("Graph type:direct graph\n");} else{printf ("Graph type:undirect graph\n");} printf ("Graph vertex number:%d\n", Mg.vexnum);p rintf ("graph arc Number:%d\n", Mg.arcnum);p rintf ("Vertex set:\n "); for (i = 1; I <= mg.vexnum; i++) printf ("%c\t", Mg.vexs[i]);p rintf ("\nadjacency matrix:\n"); for (i = 1; I <= mg.vexnum; i++) {j = 1;for (; J < MG . Vexnum; J + +) {printf ("%d\t", Mg.arcs[i][j]);} printf ("%d\n", Mg.arcs[i][j]);}} /** * Main function */int main (void) {mgraph mg;create_mg (&mg);p rint_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):d please input 1th Arc v1 (char) v2 (char): a B Please input 2th Arc v1 (char) v2 (char): A cplease input 3th arc v1 (char) v2 (char): A dplease Input 4th Arc v1 (char) v2 (c HAR): b cgraph type:direct graphgraph vertex number:4graph arc Number:4vertex set:abcdadjacency matrix:01110010000000 00
The above realizes the graph adjacency matrix representation, actually, the diagram also has other storage methods, such as the adjacency table, the cross-linked list and so on

Data structure (c implementation)-------Graph's adjacency matrix representation

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.