JAVA Implementation of the warshall Algorithm
Package graph; </P> <p> class vertex {<br/> Public char label; <br/> // ------------------------------------------------------------- </P> <p> Public vertex (char Lab) // constructor <br/> {<br/> label = lab; <br/>}< br/>}// end class vertex <br/> ///////////////////// //////////////////////////////////////// /// </P> <p>/** <br/> * directed graph <br/> */<br/> public class warshallgraph {<br/> private final int max_vert S = 5; <br/> private vertex vertexlist []; // list of vertices <br/> private int adjmat [] []; // adjacency matrix <br/> private int nverts; // current number of vertices <br/> private char sortedarray []; </P> <p> // users <br/> Public warshallgraph () // constructor <br/>{< br/> vertexlist = new vertex [max_verts]; <br/> // adjacency matrix <br/> adjmat = N EW int [max_verts] [max_verts]; <br/> nverts = 0; <br/> for (Int J = 0; j <max_verts; j ++) <br/> // set adjacency <br/> for (int K = 0; k <max_verts; k ++) <br/> // matrix to 0 <br/> adjmat [J] [k] = 0; <br/>}// end constructor <br/> // ------------------------------------------------------------- </P> <p> Public void addvertex (char Lab) {<br/> vertexlist [nverts ++] = new vertex (LAB); <br/>}</P> <p> //--------- -------------------------------------------------- <Br/> Public void addedge (INT start, int end) {<br/> adjmat [start] [end] = 1; <br/>}</P> <p> // --------------------------------------------------------------- <br/> Public void displayvertex (INT v) {<br/> system. out. print (vertexlist [v]. label); <br/>}</P> <p> // ------------------------------------------------------------- <br/> Public void warshall () <br />{< Br/> for (INT y = 0; y <max_verts; y ++) {// row <br/> for (INT x = 0; x <max_verts; X ++) {// column <br/> If (adjmat [y] [x] = 1) {// y-> x <br/> for (INT z = 0; Z <max_verts; Z ++) {<br/> If (adjmat [Z] [Y] = 1 & Z! = X) {// Z-> Y locate the node connected to Y <br/> adjmat [Z] [x] = 1; // Z-> x <br/>}< br/>}// end Topo <br/> // --------------------------------------------------------------- </P> <p> Public void printgraph () {<br/> for (INT I = 0; I <max_verts; I ++) {<br/> for (Int J = 0; j <max_verts; j ++) {<br/> system. out. print (adjmat [I] [J] + ""); <br/>}< br/> system. out. println (); <br/>}< br/> // -------------------------------------------------------- <br/> Public static void main (string [] ARGs) {<br/> warshallgraph thegraph = new warshallgraph (); <br/> thegraph. addvertex ('A'); // 0 <br/> thegraph. addvertex ('B'); // 1 <br/> thegraph. addvertex ('C'); // 2 <br/> thegraph. addvertex ('D'); // 3 <br/> thegraph. addvertex ('E'); // 4 <br/> // thegraph. addvertex ('F'); // 5 <br/> // thegraph. addvertex ('G'); // 6 <br/> // thegraph. addvertex ('H'); // 7 </P> <p> thegraph. addedge (0, 2); // ad <br/> thegraph. addedge (1, 0); // AE <br/> thegraph. addedge (1, 4); // AE <br/> thegraph. addedge (2, 3); // AE <br/> thegraph. addedge (3, 4); // be <br/> thegraph. addedge (4, 2); // cf <br/> // thegraph. addedge (3, 6); // DG <br/> // thegraph. addedge (4, 6); // eg <br/> // thegraph. addedge (5, 7); // FH <br/> // thegraph. addedge (6, 7); // GH <br/> thegraph. warshall (); <br/> thegraph. printgraph (); <br/>}< br/>