The minimum spanning tree--prim algorithm and Kruskal algorithm with weighted graphs
--dijkstra algorithm for shortest path algorithm with weighted graphs
Package graph;//path.java//demonstrates shortest path with weighted, directed graphs shortest path algorithm with weighted Graph//To run this PROGRAM:C >java Pathapp////////////////////////////////////////////////////////////////class DistPar//Distance and Parent {//items stored in spath array public int distance; Distance from the start to the vertex public int parentvert; Current parent of this vertex//-------------------------------------------------------------public distpar (int PV, int d)//constructor {distance = D; Parentvert = PV; }//-------------------------------------------------------------}//End Class distpar//////////////////////////// Class Vertex {public char label; Label (e.g. ' A ') public boolean isintree;//-------------------------------------------------------------public Ve Rtex (char Lab)//constructor {label = Lab; Isintree = false; }-------------------------------------------------------------}//End Class vertex////////////////////////////// Class Graph {private final int max_verts = 20; Private final int INFINITY = 1000000; Private Vertex vertexlist[]; List of vertices private int adjmat[][]; adjacency matrix private int nverts; Current number of vertices private int ntree; Number of Verts in tree Private Distpar spath[]; Array for shortest-path data private int currentvert; Current vertex private int starttocurrent; Distance to currentvert//-------------------------------------------------------------public Graph () constructor {vertexlist = new vertex[max_verts]; adjacency Matrix Adjmat = new Int[max_verts][max_verts]; nverts = 0; Ntree = 0; for (int j=0; j<max_verts; j + +)//Set adjacencyfor (int k=0; k<max_verts; k++)//matrix adjmat[j][k] = INFINITY; To infinity spath = new Distpar[max_verts]; Shortest paths}//end constructor//-------------------------------------------------------------public void Addvertex (char Lab) {vertexlist[nverts++] = new Vertex (Lab); }//-------------------------------------------------------------public void Addedge (int start, int end, int weight) {Adjmat[start][end] = weight; (directed)}//-------------------------------------------------------------public void path ()/ /Find all shortest paths {int starttree = 0; Start at vertex 0 vertexlist[starttree].isintree = true; Ntree = 1; Put it in the tree//transfer row of distances from Adjmat to spath for (int j=0; j<nverts; J + +) { int tempdist = Adjmat[starttree][j]; SPATH[J] = new Distpar (STARttree, tempdist); }//until all vertices is in the tree while (Ntree < nverts) {int indexmin = Getmin (); Get minimum from spath int mindist = spath[indexmin].distance; if (mindist = = INFINITY)//If all infinite {//or in tree, SYSTEM.OUT.PR Intln ("There is unreachable vertices"); Break Spath is complete} else {//reset Currentvert Current Vert = Indexmin; To closest vert starttocurrent = spath[indexmin].distance; Minimum distance from Starttree are//to Currentvert, and is starttocurrent}//Put Curr ent vertex in tree Vertexlist[currentvert].isintree = true; ntree++; Adjust_spath (); Update spath[] Array}//End while (ntree<nverts) displaypaths (); //Display spath[] Contents ntree = 0; Clear tree for (int j=0; j<nverts; j + +) Vertexlist[j].isintree = false; }//end path ()//-------------------------------------------------------------public int getmin ()//GE T entry from spath {//with minimum distance int mindist = INFINITY; assume minimum int indexmin = 0; for (int j=1; j<nverts; j + +)//For each vertex, {//If it's in tree and I F (!vertexlist[j].isintree &&//smaller than old one spath[j].distance < Mindis T) {mindist = spath[j].distance; Indexmin = j; Update Minimum}}//end for return indexmin; return index of Minimum}//End Getmin ()//-------------------------------------------------------------Publi c void AdJust_spath () {//Adjust values in Shortest-path array spath int column = 1; Skip starting vertex while (column < nverts)//Go across columns {//If this column ' s Vertex already in tree, skip it if (vertexlist[column].isintree) {column++; Continue }//Calculate distance for one spath entry//get Edge from Currentvert to column I NT Currenttofringe = Adjmat[currentvert][column]; Add distance from start int starttofringe = starttocurrent + currenttofringe; Get distance of current spath entry int spathdist = Spath[column].distance; Compare distance from start with spath entry if (Starttofringe < spathdist)//if shorter, { Update spath Spath[column].parentvert = Currentvert; Spath[cOlumn].distance = Starttofringe; } column++; }//End while (column < nverts)}//End Adjust_spath ()//--------------------------------------------------------- ----public void displaypaths () {for (int j=0; j<nverts; j + +)//display contents of spath[] { System.out.print (Vertexlist[j].label + "="); b= if (spath[j].distance = = INFINITY) System.out.print ("INF"); INF else System.out.print (spath[j].distance); A. Char parent = vertexlist[Spath[j].parentvert].label; System.out.print ("(" + Parent + ")"); (A)} System.out.println (""); }//-------------------------------------------------------------}//End Class graph////////////////////////////// Class Path {public static void main (string[] args) {Graph thegraph = new Graph (); Thegraph.addvertex (' A '); //0 (start) thegraph.addvertex (' B '); 1 Thegraph.addvertex (' C '); 2 Thegraph.addvertex (' D '); 3 Thegraph.addvertex (' E '); 4 Thegraph.addedge (0, 1, 50); AB Thegraph.addedge (0, 3, 80); AD Thegraph.addedge (1, 2, 60); BC Thegraph.addedge (1, 3, 90); BD Thegraph.addedge (2, 4, 40); CE Thegraph.addedge (3, 2, 20); DC Thegraph.addedge (3, 4, 70); DE Thegraph.addedge (4, 1, 50); EB System.out.println ("Shortest paths"); Thegraph.path (); Shortest paths System.out.println (); }//End main ()}//End Class pathapp////////////////////////////////////////////////////////////////
Java data structure--with weighted graphs