Data structure Java version of the adjacency matrix implementation diagram (11)

Source: Internet
Author: User

The adjacency matrix implementation diagram uses a matrix to subscript the matrix as a vertex if there is an edge between the vertex and the vertex. Then set the value to 1 on the point at which the matrix corresponds. (Default is 0)

  

 Packagemygraph;Importjava.util.List;/*** adjacency Matrix representation graph *@authorRanter **/ Public classVertex {Private Charlable;//Matrix Element    Private int[] List =New int[20] [20]; Private Char[] Vertexlist =New Char[20]; Private intNverts;//Current vertex subscriptVertex () { This. Nverts = 0; }    //Add a vertex     Public voidAddvertex (Charlable) {Vertexlist[nverts++] =lable; }    //Add an edge     Public voidAddedge (intStartintend) {List[start][end]= 1; List[end][start]= 1; }    //Print Matrix     Public voidPrintmatrix () { for(inti = 0; i < nverts; i + +) {             for(intj = 0; J < Nverts; J + +) {System.out.print (list[i][j]);        } System.out.println (); }    }}

Test procedure:

     Public Static voidMain (string[] args) {Vertex v=NewVertex (); V.addvertex (' A ');//0V.addvertex (' B ');//1V.addvertex (' C ');//2V.addvertex (' d ');//3V.addvertex (' e ');//4V.addedge (0, 2);//a-cV.addedge (1, 4);//b-eV.addedge (2, 4);//b-eV.addedge (0, 4);//a-eV.printmatrix (); }

Test results:

   a b c d e    a  0 0 1 0 1b  0 0 0 0 1C  1 0 0 0 1d  0 0 0 0 0
   
    e  1 1 1 0 0
   

Data structure Java version of the adjacency matrix implementation diagram (11)

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.