Adjacency matrix non-direction diagram (b) C + + detailed

Source: Internet
Author: User
Tags array length

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

Class Matrixudg {
    private:
        char Mvexs[max];    Vertex set
        int mvexnum;             Vertex number
        int medgnum;             Number of edges
        int Mmatrix[max][max];   Adjacency Matrix public
    
    :
        //create diagram (own input data)
        MATRIXUDG ();
        Create diagram (with provided matrix)
        MATRIXUDG (char vexs[], int vlen, char edges[][2], int elen);
        ~MATRIXUDG ();
    
        Print matrix queue diagram
        void print ();
    
    Private:
        //Read an input character
        char Readchar ();
        Returns the position of the CH in the Mmatrix matrix
        int getPosition (char ch);

The MATRIXUDG is the corresponding structure of the adjacency matrix.
Mvexs is used to save vertices, Mvexnum is the number of vertices, medgnum is the number of edges, and Mmatrix is a two-dimensional array for storing matrix information. For example, mmatrix[i][j]=1, which means "vertex I (i.e. mvexs[i])" and "Vertex J (i.e. Mvexs[j])" are adjacency points, and mmatrix[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) *
 parameter description:
 *     vexs  -vertex array
 *     Vlen-  vertex array length
 *     edges --Edge Array
 *     Elen  --Length of Edge
 array
/MATRIXUDG::MATRIXUDG (char vexs[], int vlen, char edges[][2], int Elen)
{
    int i, p1, p2;
    
    Initialize "vertex number" and "number of edges"
    mvexnum = Vlen;
    Medgnum = Elen;
    Initialize "vertex" for
    (i = 0; i < mvexnum i++)
        mvexs[i] = vexs[i];
    
    Initializes the "side"
    for (i = 0; i < medgnum; i++)
    {
        //Read the start vertex and end vertex of the edge
        p1 = getPosition (edges[i][0));
        P2 = getPosition (edges[i][1]);
    
        MMATRIX[P1][P2] = 1;
        MMATRIX[P2][P1] = 1;
    }
}

The function is to use known data to create an adjacency matrix without direction graph. In fact, in this article's test program source code, this method creates the G1 graph is above figure. The specific calling code is as follows:

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 = sizeof (VEXS)/sizeof (vexs[0));
int elen = sizeof (edges)/sizeof (edges[0));
matrixudg* PG;
    
PG = new Matrixudg (Vexs, Vlen, edges, Elen);

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.