One: Define the adjacency table structure storage diagram
The traversal of the package graph;//The Establishment of adjacency table implementation diagram//storage edge class Edgenode {int index;//used to use index, in fact, the standard notation is (adjvertex) int value;//weight Edgenode Nextarc; The type of the Adjacency table node pointing to the next arc}//class Vertexnode {String name; Edgenode Firstarc = new Edgenode (); Point to the first arc}public class Graph {vertexnode[] adjlist;//Save the header node of the adjacency table int e;////The number of edges of the graph int v;////graph vertex number boolean[] visit;public graph ( int V, int e) {this.v = V;THIS.E = E;adjlist = new Vertexnode[e + 1];//Learn the good habits of Java, dynamically allocate space, create an array of vertex tables visit = new Boolean[e + 1]; Mark for (int i = 0; i < e; i++) {visit[i] = false;}}
Two: DFS process
The traverse of the package diagram; public class Dfsgraph {public static void DFS (Graph G, int k) {System.out.println (g.adjlist[k].name); G.visit[k] = true; Edgenode p = new Edgenode ();p = G.adjlist[k].firstarc;while (p!=null) {if (g.visit[p.index]!=true) {DFS (g,p.index);} P=p.nextarc;}}}
Three: Building a diagram
The traversal of the package diagram; Import Java.util.scanner;public class Creategraph {private static graph g;public static graph Getgraph () {RET Urn G;} public static void Creategraph () {Scanner sc = new Scanner (system.in); System.out.println ("Please enter the number of vertices V and the number of sides E:"); int v = sc.nextint (); int e = Sc.nextint (); G = new Graph (V, e); System.out.println ("Please enter each vertex information:"); for (int i = 0; i < G.V; i++) {G.adjlist[i] = new Vertexnode (); G.adjlist[i].name = Sc.next (); G.adjlist[i].firstarc = null; Indispensable}system.out.println ("Please enter each edge information (separated by a space):"); for (int i = 0; i < G.E; i++) {Edgenode en1 = new Edgenode ();//guaranteed E1,e2 are legally input string e1 = Sc.next (); String e2 = Sc.next (), int v1 = index (e1); int v2 = index (e2); en1.index = v1; The subscript of en1 is V1en1.nextarc = G.adjlist[v2].firstarc; G.adjlist[v2].firstarc = EN1; Edgenode en2 = new Edgenode (); en2.index = v2; The subscript of en2 is V2en2.nextarc = G.adjlist[v1].firstarc; G.adjlist[v1].firstarc = En2;}} public static void Outputgraph () {//Do not know why null pointer exception try {System.out.println ("Output adjacency table storage condition:"); Edgenode en = new edgenode ();(int i = 0; i < G.E; i++) {System.out.print (g.adjlist[i].name); en = g.adjlist[i].firstarc;while (en = null) {System.out.print ("+" + G.adjlist[en.index].name); en = En.nextarc;} System.out.println ();}} catch (NullPointerException e) {}}private static int Index (String E1) {for (int i = 0; i < G.V; i++) {if (G.adjlist[i]. Name.equals (E1)) {return i;}} return-1;}}
Four: Testing
The traverse of the package diagram; public class Graphdemo {public static void main (string[] args) {creategraph.creategraph (); Creategraph.outputgraph (); SYSTEM.OUT.PRINTLN ("The process of the DFS diagram is as follows:");D Fsgraph.dfs (creategraph.getgraph (), 0);}} /* * Please enter the number of vertices V and the number of E:4 5 * Please enter each vertex information: a b c d * Please enter each edge information (separated by a space): * a b * A c * A d * b c * b d * *
Five, test results
The adjacency table of the Java implementation diagram and the DFS