adjacency table storage diagram, implementation of Java code for DFS traversal diagram

Source: Internet
Author: User


ImportJava.util.*; Public classmain{Static intMax_vertexnum = 100; Static int[] visited =New int[Max_vertexnum]; Public Static voidmain (String [] args) {Graph G=NewGraph (); Creatgraph (G); Output (G); for(inti=0;i<g.vertex_num;i++) Visited[i]=0; System.out.println ("The result of the DFS traversal is:"); DFS (G,0);//starting DFS traversal from g.node_list[0] } Static voidcreatgraph (Graph G) {Scanner in=NewScanner (system.in); System.out.println ("Please enter the number of vertices V and the number of sides E, (v e):"); G.vertex_num=In.nextint (); G.edge_num=In.nextint (); System.out.println ("Please enter each vertex information:"); for(inti=0;i<g.vertex_num;i++) {G.node_list[i]=NewVertexnode (); G.node_list[i].date=In.next (); G.node_list[i].first_edge=NULL;//is important} System.out.println ("Please enter each edge information (separated by a space):"); for(inti=0;i<g.edge_num;i++) {Edgenode P=NewEdgenode (); String str1=In.next (); String str2=In.next (); intV1 =Locatevex (G,STR1); intV2 =Locatevex (G,STR2); P.vertex=v1; P.next=G.node_list[v2].first_edge; G.node_list[v2].first_edge=p; Edgenode Q=NewEdgenode ();//If there is a map, the following code does not exist (the storage order of the above code may vary)Q.vertex =v2; Q.next=G.node_list[v1].first_edge; G.node_list[v1].first_edge=Q; } } Static intLocatevex (Graph g,string s) { for(inti=0;i<g.vertex_num;i++){ if(G.node_list[i].date.equals (s))returni; } return-1; } Static voidoutput (Graph G) {System.out.println ("Output adjacency Table storage:"); Edgenode P=NewEdgenode (); for(inti=0;i<g.vertex_num;i++) {System.out.print (g.node_list[i].date); P=G.node_list[i].first_edge; while(p!=NULL) {System.out.print ("+" +g.node_list[p.vertex].date); P=P.next; } System.out.println (); } } Static voidDFS (Graph G,intk) {System.out.println (g.node_list[k].date); VISITED[K]=1; Edgenode P=NewEdgenode (); P=G.node_list[k].first_edge; while(p!=NULL){ if(visited[p.vertex]!=1) DFS (G,p.vertex); P=P.next; } }}//Vertex Storageclassvertexnode{String Date; Edgenode First_edge=NewEdgenode ();}//Edge Storageclassedgenode{intVertex; Edgenode next;}//Diagram Storageclassgraph{Vertexnode [] node_list=Newvertexnode[100]; intVertex_num,edge_num;}

Output: Note: (the order in which the adjacency table is saved is related to the order of the input edges)

adjacency table storage diagram, implementation of Java code for DFS traversal diagram

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.