Non-direction graph of adjacency matrix (iii) Java detailed explanation

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

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

The MATRIXUDG is the corresponding structure of the 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 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
 *     edges--Edge array
/public MATRIXUDG (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;
        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 = new char[][]{
        {' A ', ' C '}, {' A ', ' d '}, {' A ', ' F '}, {' 
        B ', ' C '} 
        , {' C ', ' d '}, {' 
        E '}, ' , ' G '}, 
        {' F ', ' G '}};
    MATRIXUDG PG;
    
    PG = new Matrixudg (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.