-golang version of adjacency matrix for non-graph storage

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The implementation of an Golang graph storage using adjacency matrix is implemented, and the language is realized.

What is the adjacency matrix storage method?

The adjacency matrix is stored through a one-dimensional array and a two-dimensional array to complete the construction of the graph. One-dimensional arrays are used to store each vertex in a diagram, and a two-dimensional array is used to store information about edges or arcs in a diagram.

is a diagram that will be implemented using adjacency matrix storage later in the article

The vertex array is

{'A', 'B', 'C', 'D'}

Edge Array (two-dimensional array) is a matrix form

    //     A     B     C     D    //A [65536   5     3     6  ]    //B [  5   65536   7   65536]    //C [  3     7   65536   9  ]    //D [  6   65536   9   65536]

A one-dimensional array stores each vertex, and a two-dimensional array is shown in the form of a matrix that is the matrix above. For example, 1th row 2nd column, for array[0][1] = 5
Represents the edge of A to B, and the weight of the edge is 5. The matrix is actually a symmetric matrix because it is a non-graph. In addition, 65536 of the matrix is actually an impossible to take a weight, because the weight may appear as 0 of the case, so it is absolutely impossible to be a weight value as an impossible value. For example array[0][0] = 65536 means that the side of a to a does not exist.

Next is the code implementation

 PackageMainImport "FMT"typeVertextypeRune //Vertex numeric typeConst(INFINITYInt32=65536 //Impossible value)//No direction diagramtypeAdjacencymatrixstruct{Vers []vertextype Edges []Int32NumedgesInt32NumversInt32}//To record two vertices and weights for each edgetypeEdgeweightinfostruct{V0Int32V1Int32WeightInt32}//Initialize adjacency matrix//VERSC Vertex Count//vers represents vertex collection//ewi represents each edge of the adjacency matrixfunc(This *adjacencymatrix) Init (VERSCInt32, Vers []vertextype, Ewis []edgeweightinfo] {this. Edges = Make([][]Int32, VERSC) forI: =0; I <Len(this.) Edges); i++ {this. Edges[i] = Make([]Int32, VERSC) forIndex, _: =RangeThis. Edges[i] {this. Edges[i][index] = INFINITY//Start all initialized to an impossible value}} this. Vers = Vers for_, Ewi: =RangeEwis {this. EDGES[EWI.V0][EWI.V1] = Ewi.weight//Because it is a non-direction graph, so it is a symmetric matrixThis. Edges[ewi.v1][ewi.v0] = ewi.weight} fmt. Println (this. Edges)//A B C D    //a [65536 5 3 6]    //b [5 65536 7 65536]    //c [3 7 65536 9]    //d [6 65536 9 65536]}funcMain () {varAm *adjacencymatrix =New(Adjacencymatrix)varvers []vertextype = []vertextype{' A ',' B ',' C ',' D '}varEwis []edgeweightinfo = []edgeweightinfo{edgeweightinfo{0,1,5}, Edgeweightinfo{0,2,3}, Edgeweightinfo{0,3,6}, Edgeweightinfo{1,2,7}, Edgeweightinfo{2,3,9},} am. Init(4, Vers, Ewis)}
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.