Adjacency matrix Direction diagram (ii) C + + detailed

Source: Internet
Author: User
Tags array length

An introduction to the graph of adjacency matrix

The direction graph of adjacency matrix refers to a direction graph expressed by adjacency matrix.

The above figure G2 contains a total of 7 vertices of "a,b,c,d,e,f,g" and contains "<A,B>,<B,C>,<B,E>,<B,F>,<C,E>,<D,C>" <E,B>,<E,D>,<F,G> "A total of 9 sides.

The matrix on the right of the above figure is a schematic diagram of the adjacency matrix in memory of the G2. A[i][j]=1 means that the first vertex to the J Vertex is an edge, a[i][j]=0 represents not an edge, while A[i][j] represents the value of column J of Line I, for example, a[1,2]=1, which means that the 1th vertex (i.e. vertex b) to 2nd vertex (C) is an edge.

Code description of the direction graph of adjacency matrix

1. Basic definition

#define MAX
    
class Matrixdg {
    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)
        MATRIXDG ();
        Create diagram (with provided matrix)
        MATRIXDG (char vexs[], int vlen, char edges[][2], int elen);
        ~MATRIXDG ();
    
        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);

MATRIXDG is a structural body corresponding to the graph of 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 vertex i is the starting point, and Vertex J is the endpoint.

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
/MATRIXDG::MATRIXDG (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;
    }
}

The function is to create an adjacency matrix with a direction graph. In fact, the method creates a direction graph, which is the figure G2 above. Its invocation method is as follows:

Char vexs[] = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G '};
Char edges[][2] = {
    {' A ', ' B '}, 
    {' B ', ' C '}, {' B ', ' 
    e '}, 
    {' B ', ' F '}, 
    {' C ', ' e '}, 
    {' D ', ' C '}, 
    {' E ', ' B '}, 
    {' E ', ' D '}, 
    {' F ', ' G '}}; 
int vlen = sizeof (VEXS)/sizeof (vexs[0));
int elen = sizeof (edges)/sizeof (edges[0));
matrixdg* PG;
    
PG = new MATRIXDG (Vexs, Vlen, edges, Elen);

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.