Figure shows the JAVA code implementation of the shortest path Floyd and Dijkstra Algorithms
package graph;/** * Created by Thinkpad on 2014/3/31. */public class VertexType{ private String name; private int id; public VertexType(int i, String a) { id = i; name = a; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; }}
package graph;/** * Created by Thinkpad on 2014/3/31. */public class EdgeType{ protected int weight; public EdgeType(int weight) { this.weight = weight; }}
Package graph; import java. util. arrayList; import java. util. hashMap; public class GraphMatrixOperator {private static final int INFINITY = 65535; private ArrayList> arcList = new ArrayList> (); private ArrayList
Vertexes = new ArrayList
(); Private HashMap
VertexNodeHashMap = new HashMap
(); // Protected int [] p;/* is used to store the Array under the shortest path * // protected int [] d; /* used to store the weights of the shortest paths to each point and */private int numVertexes; private int numEdges; public ArrayList> getArcList () {return arcList;} public ArrayList
GetVertexes () {return vertexes;} public HashMap
GetVertexNodeHashMap () {return vertexNodeHashMap;}/* Create an undirected network graph's Adjacent matrix to represent */public static void initMGraph (GraphMatrixOperator g) {} protected static void initVertexes (GraphMatrixOperator g) {for (int I = 0; I <g. numVertexes; I ++)/* reads vertex information and creates a vertex table */{g. getVertexes (). set (I, new VertexType (I, "") ;}} protected static void initArcWithIJ (GraphMatrixOperator g) {for (int I = 0; I <g. getNumVertexes (); I ++) {for (int j = I; j <g. getNumVertexes (); j ++) {g. getArcList (). get (j ). get (I ). weight = g. getArcList (). get (I ). get (j ). weight ;}} protected static void initNet (GraphMatrixOperator g) {for (int I = 0; I <g. numVertexes; I ++)/* initialization figure */{for (int j = 0; j <g. numVertexes; j ++) {if (I = j) {g. getArcList (). get (I ). set (j, new EdgeType (0);} else {g. getArcList (). get (I ). set (j, new EdgeType (INFINITY); g. getArcList (). get (j ). set (I, new EdgeType (INFINITY) ;}}} private static void initArcList (GraphMatrixOperator g, int size) {for (int I = 0; I <size; I ++) {ArrayList
Al = new ArrayList
(); G. getArcList (). add (al); for (int j = 0; j <size; j ++) {if (I = j) {al. add (new EdgeType (0);} else {al. add (new EdgeType (INFINITY) ;}}} public static void addArcFromI2J (GraphMatrixOperator g, String a, String B, int w) {addVex (g, ); addVex (g, B); g. getArcList (). get (g. getVertexNodeHashMap (). get (). getId ()). get (g. getVertexNodeHashMap (). get (B ). getId ()). weight = w; g. getArcList (). get (g. g EtVertexNodeHashMap (). get (B ). getId ()). get (g. getVertexNodeHashMap (). get (). getId ()). weight = w;} private static void addVex (GraphMatrixOperator g, String a) {if (! G. getVertexNodeHashMap (). containsKey (a) {int size = g. getVertexes (). size (); VertexType e = new VertexType (size, a); g. getVertexes (). add (e); g. getVertexNodeHashMap (). put (a, e) ;}/ * Dijkstra algorithm, the shortest path p [v] From v0 vertex of Directed Network G to other vertex v and the value of d [v] with weight length * // * p [v] is the subscript of the forward vertex, d [v] indicates the shortest path length from v0 to v and */public static void shortestPath_Dijkstra (GraphMatrixOperator g, int v0, int [] p, int [] d) {int v, w, k = 0, min; int maxvex = g. getNumVertexes (); int [] finalInt = new int [maxvex];/* finalInt [w] = 1 indicates obtaining the shortest path from vertex v0 to vw */int n = g. numVertexes; for (v = 0; v <n; v ++)/* initialization data */{finalInt [v] = 0; /* Initialize all vertices to the unknown Shortest Path state */d [v] = g. getArcList (). get (v0 ). get (v ). weight;/* Add the weight value to the vertex connected to v0 */p [v] =-1; /* the initialization path array P is-1 */} d [v0] = 0;/* the path from v0 to v0 is 0 */finalInt [v0] = 1; /* the path from v0 to v0 does not need to be required * // * Start the main loop. The shortest path from v0 to a v vertex is obtained each time */for (v = 1; v <n; v ++) {min = INFINITY;/* closest to the v0 vertex currently known */for (w = 0; w <n; w ++) /* Find the vertex closest to v0 */{if (finalInt [w] = 0) & (d [w] <min) {k = w; min = d [w];/* w vertex closer to v0 vertex */} finalInt [k] = 1; /* set the closest vertex found to 1 */for (w = 0; w <n; w ++) /* modify the current Shortest Path and distance */{/* if the path passing through the v vertex is shorter than the current path */if (0 = finalInt [w]) & (min + g. getArcList (). get (k ). get (w ). weight) <d [w]) {/* indicates that a shorter path is found. Modify d [w] and p [w] */d [w] = min + g. getArcList (). get (k ). get (w ). weight;/* modify the current path length */p [w] = k ;}} System. out. println ("final []:"); for (int I: finalInt) {System. out. print (I);} System. out. println ();} public static void main (String [] args) {/* calculate the shortest path from a point to other points */String [] src = new String [] {"a, B, 1", "B, c, 2 ", "a, c, 4", "B, d, 7"}; GraphMatrixOperator g = createMGraph (src); logMGraph (g); int v0 = 0; main_Dijkstra (g, v0); main_floyd (g);} private static GraphMatrixOperator createMGraph (String [] src) {GraphMatrixOperator g = new GraphMatrixOperator (); initArcList (g, src. length); for (String s: src) {System. out. println (s); String [] line = s. split (","); addArcFromI2J (g, line [0], line [1], Integer. valueOf (line [2]);} int size = g. getVertexes (). size (); g. setNumVertexes (size); g. setNumEdges (src. length); return g;} private static void logMGraph (GraphMatrixOperator g) {for (VertexType vertexType: g. getVertexes () {System. out. print (vertexType. getName () + "");} System. out. println (); for (ArrayList
EdgeTypes: g. getArcList () {for (EdgeType edgeType: edgeTypes) {System. out. print (edgeType. weight + "");} System. out. println () ;}} private static void main_Dijkstra (GraphMatrixOperator g, int v0) {int size = g. getVertexes (). size (); int [] p = new int [size]; int [] d = new int [size]; shortestPath_Dijkstra (g, v0, p, d); System. out. println ("weight used to store the shortest path to each point and:"); for (int I: d) {System. out. print (I + "");} System. out. println (); System. out. println ("used to store the array below the Shortest Path:"); for (int I: p) {System. out. print (I + "");} System. out. println (); System. out. println ("Shortest Path descending order:"); int j; for (int I = 1; I <g. numVertexes; ++ I) {System. out. print (v0 + "-" + I + ":"); j = I; while (p [j]! =-1) {System. out. print (g. getVertexes (). get (p [j]). getName (); j = p [j];} System. out. println ();} System. out. println ("\ n the shortest path length from the source point to each vertex is:"); for (int I = 1; I <g. numVertexes; ++ I) {System. out. print (g. getVertexes (). get (v0 ). getName () + "-"); System. out. print (g. getVertexes (). get (I ). getName () + ":"); System. out. println (d [I]) ;}} public void setNumVertexes (int numVertexes) {this. numVertexes = numVert Exes;} public int getNumVertexes () {return numVertexes;} public void setNumEdges (int numEdges) {this. numEdges = numEdges;} public int getNumEdges () {return numEdges;}/* Floyd algorithm, find the Shortest Path p [v] [w] And d [v] [w] with weight length from vertex v to other vertex w in graph G. */Public static void shortestPath_Floyd (GraphMatrixOperator g, int [] [] pList, int [] [] dList) {int size = g. numVertexes; for (int v = 0; v <size; ++ v)/* initialize D and P */{for (int w = 0; w <size; ++ w) {dList [v] [w] = g. arcList. get (v ). get (w ). weight;/* d [v] [w] values are the weights between corresponding points */pList [v] [w] = w; /* initialize P */} for (int k = 0; k <size; ++ k) {for (int v = 0; v <size; ++ v) {for (int w = 0; w <size; ++ w ){ If (dList [v] [w]> dList [v] [k] + dList [k] [w]) {/* If the path to the k vertex is smaller than the path between the original two points after the subscript */dList [v] [w] = dList [v] [k] + dList [k] [w ]; /* set the current weight between two points to a smaller value */pList [v] [w] = pList [v] [k]; /* set the path to the vertex with the subscript k */}}}} public static void main_floyd (GraphMatrixOperator g) {int size = g. getVertexes (). size (); int [] [] pList = new int [size] [size]; int [] [] dList = new int [size] [size]; shortestPath_Floyd (g, pList, dList); printP Ath (size, pList, dList); printDList (size, dList); printPList (size, pList);} private static void printPList (int size, int [] [] pList) {System. out. println ("subscript of the shortest path P:"); for (int v = 0; v <size; ++ v) {for (int w = 0; w <size; ++ w) {System. out. print (pList [v] [w] + "-");} System. out. println () ;}} public static void printDList (int size, int [] [] dList) {System. out. println ("Shortest Path weight and D:"); for (int v = 0; v <size; ++ v) {for (int w = 0; w <size; ++ w) {System. out. print (dList [v] [w] + "-");} System. out. println () ;}} public static void printPath (int size, int [] [] pList, int [] [] dList) {System. out. println ("the shortest path between each vertex is as follows:"); for (int v = 0; v <size; ++ v) {for (int w = v + 1; w <size; w ++) {System. out. println (v + "-" + w + ":" + dList [v] [w]); printPathFromV2W (pList, v, w) ;}} public static Void printPathFromV2W (int [] [] pList, int v, int w) {int k; k = pList [v] [w]; /* obtain the subscript of the first path vertex */System. out. print (v + "-");/* print source point */while (k! = W)/* If the path vertex subscript is not the end */{System. out. print (k + "-");/* print path vertex */k = pList [k] [w];/* obtain the subscript of the next path vertex */} System. out. println (w);/* print the end */}}