Adjacency matrix non-direction diagram (a) C language detailed

Source: Internet
Author: User

An introduction to the non-direction graph of adjacency matrices

The non-direction graph of adjacency matrix refers to the graph represented by adjacency matrix.

The above figure G1 contains "a,b,c,d,e,f,g" a total of 7 vertices, and contains "(A,c), (A,d), (a,f), (B,c), (C,d), (E,g), (f,g)" A total of 7 sides. Since this is a a,c graph, the edges (c,a) and sides (the edges) are the same; the edges are enumerated in alphabetical order.

The matrix on the right of the above figure is a schematic diagram of the adjacency matrix in memory of the G1. The a[i][j]=1 indicates that the vertex I and the J Vertex are adjacent points, a[i][j]=0 means that they are not adjacency points, whereas A[i][j] represents the value of the J column of line I, for example, a[1,2]=1, which means that the 1th vertex (i.e. vertex b) and 2nd vertex (C) are contiguous points.

Code description of the adjacency matrix non-direction graph

1. Basic definition

The adjacency matrix
typedef struct _GRAPH
{
    char vexs[max];       Vertex set
    int vexnum;           Vertex number
    int edgnum;           Number of edges
    int Matrix[max][max];//adjacency Matrix
}graph, *pgraph;

Graph is the corresponding structure of adjacency matrix.
Vexs is used to save vertices, Vexnum is the number of vertices, edgnum is the number of edges, and matrix is a two-dimensional array to hold the information of matrices. For example, matrix[i][j]=1, which means "vertex I (i.e. vexs[i])" and "Vertex J (i.e. Vexs[j])" are adjacency points, and matrix[i][j]=0 means that they are not adjacency points.

2. Create a matrix

This provides two ways to create matrices. One is with known data , and the other requires the user to enter data manually .

2.1 Creating the diagram (with the provided matrix)

 * * * Create diagram (with provided matrix)/graph* create_example_graph () {char vexs[] = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G '}; Char edges[][2] = {' A ', ' C '}, {' A ', ' d '}, {' A ', ' F '}, {' B ', ' C '}, {' C ', ' d '} 
    , {' E ', ' g '}, {' F ', ' G '}};
    int vlen = LENGTH (Vexs);
    int Elen = LENGTH (edges);
    int I, p1, p2;
    
    graph* PG;
    Enter "vertex number" and "number of edges" if ((pg= (graph*) malloc (sizeof (Graph)) = = null) return null;
    
    memset (PG, 0, sizeof (Graph));
    Initialize "vertex number" and "number of edges" pg->vexnum = Vlen;
    Pg->edgnum = Elen;
    Initializes "vertex" for (i = 0; i < pg->vexnum; i++) {pg->vexs[i] = vexs[i]; }//Initialize "Edge" for (i = 0; i < pg->edgnum; i++) {//read edge start vertex and end vertex P1 = get_position (*
        PG, edges[i][0]);
    
        P2 = get_position (*PG, edges[i][1]);
        PG->MATRIX[P1][P2] = 1;
    PG->MATRIX[P2][P1] = 1;
} return PG; }

CreateExamplegraph is the function of creating an adjacency matrix without direction graph.

Note: This method creates a G1 graph, which is the diagram above.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.