1. Create the vertex of the graph
package com.jzm.Graph;public class GraphNode {public GraphNode link;public int info;}
2. Create the foreground and successor of the graph
package com.jzm.Graph;public class GraphList {public GraphNode first;public GraphNode last;public boolean visitable;public int getAjd(int[] ajd) {GraphNode current = first;int length = 0;while(current != null) {ajd[length++] = current.info;current = current.link;}return length;}public void addNode(int v) {GraphNode node = new GraphNode();node.info = v;if(first == null) {first = node;last = node;} else {last.link = node;last = node;}}}
3. Write a queue
package com.jzm.Graph;public class Queue {public GraphNode first;public GraphNode last;public int count;public void addQueue(int info) {GraphNode node = new GraphNode();node.info = info;if(first == null) {first = node;last = node;} else {last.link = node;last = last.link;}count++;} public void deleteQueue() {if(first == null) {System.out.println("null queue");} else {first = first.link;count--;}}public boolean isEmpty() {return count == 0;}public int front() {if(first == null) {return -1;}return first.info;}public int back() {if(last == null) {return -1;}return last.info;}}
4. Create a chart
Package COM. jzm. graph; public class graph {private int length; private graphlist [] list; private int [] [] weight; Public graph (INT length) {This. length = length; List = new graphlist [length]; Weight = new int [length] [length];} public void DFS (int v) {int [] AJD = new int [length]; int ajdlength = list [v]. getajd (AJD); list [v]. visitable = true; system. out. print (V + ""); For (INT I = 0; I <ajdlength; I ++) {int W = AJD [I]; If (! List [w]. visitable) {DFS (w) ;}}// the depth first traverses public void dfstravel () {for (INT I = 0; I <length; I ++) {list [I]. visitable = false;} For (INT I = 0; I <length; I ++) {If (! List [I]. visitable) {DFS (I) ;}}// the breadth first traverses public void bfstravel () {for (INT I = 0; I <length; I ++) {list [I]. visitable = false;} BFS ();} private void BFS () {queue = new Queue (); For (INT Index = 0; index <length; index ++) {If (! List [Index]. visitable) {queue. addqueue (INDEX); list [Index]. visitable = true; system. Out. Print (index + ""); While (! Queue. isempty () {int temp = queue. front (); queue. deletequeue (); int [] AJD = new int [length]; int ajdlength = list [temp]. getajd (AJD); For (INT I = 0; I <ajdlength; I ++) {int W = AJD [I]; If (! List [w]. visitable) {system. out. print (W + ""); queue. addqueue (w); list [w]. visitable = true ;}}}}// Shortest Path private int [] using path (INT v) {int [] Using Path = new int [length]; boolean [] weightfound = new Boolean [length]; for (INT I = 0; I <length; I ++) {// approaching infinity path [I] = 9999; weightfound [I] = false;} queue path [v] = 0; weightfound [v] = true; queue = new Queue (); queue. addqueue (V); While (! Queue. isempty () {int temp = queue. front (); queue. deletequeue (); int [] AJD = new int [length]; int ajdlength = list [temp]. getajd (AJD); For (INT I = 0; I <ajdlength; I ++) {int W = AJD [I]; If (! Weightfound [w]) {If (export path [w]> export path [temp] + weight [temp] [W]) {export path [w] = export path [temp] + weight [temp] [W] ;}} int minweightnode = 0; For (INT I = 0; I <length; I ++) {If (! Weightfound [I]) {minweightnode = I; for (Int J = 0; j <length; j ++) {If (! Weightfound [J]) {If (export path [J] <export path [minweightnode]) {minweightnode = J ;}} break ;}} if (! Weightfound [minweightnode]) {weightfound [minweightnode] = true; queue. addqueue (minweightnode);} return queue path;} // length public int length () {system. out. println ("length =" + length); Return length;} public Boolean isempty () {return length = 0 ;}// Add the public void addgraph (INT info) node) {for (INT I = 0; I <length; I ++) {If (list [I] = NULL) {graphlist G = new graphlist (); G. addnode (Info); list [I] = g; break ;}}}/ /Add an edge public void addedge (INT vfrom, int VTO, int value) {list [vfrom]. addnode (VTO); weight [vfrom] [VTO] = value;} // print the public void print () {for (INT I = 0; I <length; I ++) {graphnode current = list [I]. first; while (current! = NULL) {system. out. print (current.info + ""); current = current. link;} system. out. println ("") ;}} public static void main (string [] ARGs) {graph = New Graph (5); system. out. println ("create graph start"); For (INT I = 0; I <5; I ++) {graph. addgraph (I);} graph. addedge (0, 1, 16); graph. addedge (0, 3, 2); graph. addedge (0, 4, 3); graph. addedge (3, 4, 7); graph. addedge (3, 1, 12); graph. addedge (4, 1, 10); graph. addedge (4, 3, 5); graph. addedge (4, 2, 4); graph. addedge (2, 1, 3); graph. addedge (1, 2, 5); graph. print (); system. out. println ("create graph end"); int [] vertex path = graph. export path (0); For (INT I = 0; I <export path. length; I ++) {system. out. print (export path [I] + "");}}}