Depth-first traversal of adjacency matrices (Java edition)

Source: Internet
Author: User

This is a graph with a right to the Edge

Vertex arrays: [V0, V1, v2, v3, V4]

Edge array: v0 v1 v2 v3 v4
V0 6
V1 9 3
V2 2 5
V3 1
Package Com.datastruct;import Java.util.Scanner; Public classMgraph {
//Definition diagram structure, using adjacency matrix storage Private Static classgraph{FinalintMaxvex =Ten;//maximum number of verticesFinalintINFINITY =65535;//use 65535来 to represent InfinityString vexs[]=NewString[maxvex];//Vertex Table intArc[][] =New int[Maxvex] [Maxvex];//adjacency Matrix intnumvertexes;//number of vertices intNumedges;//Number of sides }
//Print the basic information of the drawing Public Static voidprintgraph (Graph g) {System. out. println ("all vertices:"); for(intI=0; i<g.numvertexes;i++) {System. out. Print (g.vexs[i]+" "); }
System. out. println (); System. out. println ("Matrix table:"); for(intI=0; i<g.numvertexes;i++){ for(intj=0; j<g.numvertexes;j++) {System. out. Print (g.arc[i][j]+"\ t"); } System. out. println (); } }
  //Create diagram Structure Public Static voidcreatemgraph (Graph g) {inti,j,k,w; Scanner Scanner=NewScanner (System.inch); System. out. println ("Enter the number of vertices and the number of edges:"); G.numvertexes=Scanner.nextint (); G.numedges=Scanner.nextint (); System. out. println ("Enter vertex information:"); for(i=0; i<g.numvertexes;i++) {G.vexs[i]=Scanner.next (); } //Initialize the adjacency matrix so that all the numbers are infinite for(i=0; i<g.numvertexes;i++){ for(j=0; j<g.numvertexes;j++) {G.arc[i][j]=g.infinity; } }
    //construct a relationship between each vertex for(k=0; k<g.numedges;k++) {System. out. println ("Subscript vi,vj and weights on the input edge (VI,VJ)"); I=Scanner.nextint (); J=Scanner.nextint (); W=Scanner.nextint (); G.ARC[I][J]=W; } } Public StaticBoolean visited[] =Newboolean[ -];//access identity, number greater than or equal to maximum number of vertices//corresponding to String vexs[] = new String[maxvex]; Vertex table Public Static voidDFS (Graph G,inti) { intJ; Visited[i]=true; System. out. println ("Vertex:"+G.vexs[i]); //③
for(j=0; j<g.numvertexes;j++){ if(G.arc[i][j] <65535&&!Visited[j]) {//④DFS (G,J); } } }
/*
Access ideas:
An array visited[] is used to mark whether the vertex has been accessed, visited[] 's subscript and g.vexs[] one by one correspond, visited[0] is true the identity g.vexs[0] was visited
The program starts at Vertex V0 and ends at V4 end ①i=0
discover that V0 is not being accessed ②
Output V0③
then find the line where V0 is located, and find V4 has a connection with it ④
Output V4③
then look for the line where V4 is located, and no one has anything to do with it ④
fallback to the location where V0 found V4, at which point V0 has reached the end of the line
And then back to ①, for the second cycle, I=1
discover that V1 is not being accessed ②
Output V1③
then look for the row where V1 is located, V0 and V2 have links to it, but V0 is visited, all found V2④
Output V2③
then find the line where V2 is located and find that V0 and V3,v0 are visited, all found V3④
Output V3③
then find the row where V3 is located, find V4,v4 is visited, all fallback
go back to ① .
for the next cycle, i=2
subsequent loops find vertices are accessed until the loop ends and the traversal ends

Find v0 v4 v1 v2 v3
*/ Public Static voidDfstraverse (Graph g) {inti; //Initialize the Access ID, all vertices are not accessed for(i=0; i<g.numvertexes;i++) {Visited[i]=false; } for(i=0; i<g.numvertexes;i++) { //① if(!Visited[i]) { //② DFS (g,i); } } } Public Static voidMain (string[] args) {Graph g=NewGraph (); Createmgraph (g);//Create DiagramPrintgraph (g);//Vertex and adjacency matrices for input graphsDfstraverse (g);//Depth-first traversal diagram } }

Depth-first traversal of adjacency matrices (Java edition)

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.