Adjacency matrix Direction Diagram (iii) Java detailed

Source: Internet
Author: User

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

public class Matrixdg {
    
    private char[] Mvexs;       Vertex set
    private int[][] Mmatrix;    Adjacency matrix
    
    ...
}

MATRIXDG is a structural body corresponding to the graph of adjacency matrix.

Mvexs is used to save vertices, 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
 *     edges--Edge array
/public MATRIXDG (char[] vexs, char[][] edges) {
    
    //Initialize "vertex number" and "number of edges"
    int vlen = vexs.length;
    int elen = edges.length;
    
    Initialize "vertex"
    Mvexs = new Char[vlen];
    for (int i = 0; i < mvexs.length i++)
        mvexs[i] = vexs[i];
    
    Initialize "Edge"
    Mmatrix = new Int[vlen][vlen];
    for (int i = 0; i < Elen; i++) {
        //read edge start vertex and end vertex
        int p1 = getPosition (edges[i][0]);
        int 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 = new char[][]{
    {' A ', ' B '}, {' B ', ' C '}, {' B ', ' e '}, {' 
    b ', ' F '}, {' 
    C ', ' E ' 
    }, {' D '}, ' , ' C '}, 
    {' E ', ' B '}, 
    {' E ', ' D '}, 
    {' F ', ' G '}}; 
MATRIXDG PG;
    
PG = new Matrixdg (vexs, edges);

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.