Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement Dijkstra\'s algorithm and Floyd\'s algorithm in JAVA . Dijkstra\'s alg

ID: 3722371 • Letter: I

Question

Implement Dijkstra's algorithm and Floyd's algorithm in JAVA. Dijkstra's algorithm will take an adjacency matrix W, which is a 2-D array, as a parameter. Then the updated algorithm should run Dijkstra’s Algorithm on W n times, once on each vertex 1, 2, …, n . The results should be stored in a second 2-D array “D”, where D[ i ][ j ] is the length of a shortest path from the i th vertex to the j th vertex. Floyd’s algorithm will not require modification and should also return a 2-D array “D”, formatted in the same way.

The graphs used can be hard-coded into the program. After the algorithms have run, let the user ask for the distance between two nodes to verify that it has worked correctly.

Explanation / Answer

// A Java program for Dijkstra's single source shortest path algorithm.

// The program is for adjacency matrix representation of the graph

import java.util.*;

import java.lang.*;

import java.io.*;

class ShortestPath

{

// A utility function to find the vertex with minimum distance value,

// from the set of vertices not yet included in shortest path tree

static final int V=9;

final static int INF = 99999;

static int[][] result1 = new int[V][V];

static int[][] result2 = new int[V][V];

int minDistance(int dist[], Boolean sptSet[])

{

// Initialize min value

int min = Integer.MAX_VALUE, min_index=-1;

for (int v = 0; v < V; v++)

if (sptSet[v] == false && dist[v] <= min)

{

min = dist[v];

min_index = v;

}

return min_index;

}

// A utility function to print the constructed distance array

void initializeResultMatrix(int dist[], int n, int src)

{

for (int i = 0; i < V; i++){

result1[src][i] = dist[i];

}

}

// Funtion that implements Dijkstra's single source shortest path

// algorithm for a graph represented using adjacency matrix

// representation

void dijkstra(int graph[][]){

for(int i =0; i< V; i++)

dijkstraAlgorithm(graph, i);

}

void dijkstraAlgorithm(int graph[][], int src)

{

int dist[] = new int[V]; // The output array. dist[i] will hold

// the shortest distance from src to i

// sptSet[i] will true if vertex i is included in shortest

// path tree or shortest distance from src to i is finalized

Boolean sptSet[] = new Boolean[V];

// Initialize all distances as INFINITE and stpSet[] as false

for (int i = 0; i < V; i++)

{

dist[i] = Integer.MAX_VALUE;

sptSet[i] = false;

}

// Distance of source vertex from itself is always 0

dist[src] = 0;

// Find shortest path for all vertices

for (int count = 0; count < V-1; count++)

{

// Pick the minimum distance vertex from the set of vertices

// not yet processed. u is always equal to src in first

// iteration.

int u = minDistance(dist, sptSet);

// Mark the picked vertex as processed

sptSet[u] = true;

// Update dist value of the adjacent vertices of the

// picked vertex.

for (int v = 0; v < V; v++)

// Update dist[v] only if is not in sptSet, there is an

// edge from u to v, and total weight of path from src to

// v through u is smaller than current value of dist[v]

if (!sptSet[v] && graph[u][v]!=0 &&

dist[u] != Integer.MAX_VALUE &&

dist[u]+graph[u][v] < dist[v])

dist[v] = dist[u] + graph[u][v];

}

// print the constructed distance array

initializeResultMatrix(dist, V, src);

}

void floydWarshall(int graph[][])

{

int dist[][] = new int[V][V];

int i, j, k;

/* Initialize the solution matrix same as input graph matrix.

Or we can say the initial values of shortest distances

are based on shortest paths considering no intermediate

vertex. */

for (i = 0; i < V; i++)

for (j = 0; j < V; j++){

if(graph[i][j] == 0 && i!=j)

dist[i][j] = INF;

else

dist[i][j] = graph[i][j];

}

/* Add all vertices one by one to the set of intermediate

vertices.

---> Before start of a iteration, we have shortest

distances between all pairs of vertices such that

the shortest distances consider only the vertices in

set {0, 1, 2, .. k-1} as intermediate vertices.

----> After the end of a iteration, vertex no. k is added

to the set of intermediate vertices and the set

becomes {0, 1, 2, .. k} */

for (k = 0; k < V; k++)

{

// Pick all vertices as source one by one

for (i = 0; i < V; i++)

{

// Pick all vertices as destination for the

// above picked source

for (j = 0; j < V; j++)

{

// If vertex k is on the shortest path from

// i to j, then update the value of dist[i][j]

if (dist[i][k] + dist[k][j] < dist[i][j])

dist[i][j] = dist[i][k] + dist[k][j];

}

}

}

//initializing the result of floyd

result2 = dist;

}

// Driver method

public static void main (String[] args)

{

/* Let us create the example graph discussed above */

int graph[][] = new int[][]{{0, 4, 0, 0, 0, 0, 0, 8, 0},

{4, 0, 8, 0, 0, 0, 0, 11, 0},

{0, 8, 0, 7, 0, 4, 0, 0, 2},

{0, 0, 7, 0, 9, 14, 0, 0, 0},

{0, 0, 0, 9, 0, 10, 0, 0, 0},

{0, 0, 4, 14, 10, 0, 2, 0, 0},

{0, 0, 0, 0, 0, 2, 0, 1, 6},

{8, 11, 0, 0, 0, 0, 1, 0, 7},

{0, 0, 2, 0, 0, 0, 6, 7, 0}

};

ShortestPath t = new ShortestPath();

t.dijkstra(graph);

System.out.println("dijkstra");

for (int i=0; i<V; i++){

for (int j =0; j<V; j++)

System.out.print(result1[i][j] + " ");

System.out.println("");

}

t.floydWarshall(graph);

System.out.println("floydWarshall");

for (int i=0; i<V; i++){

for (int j =0; j<V; j++)

System.out.print(result2[i][j] + " ");

System.out.println("");

}

Scanner reader = new Scanner(System.in); // Reading from System.in

System.out.println("Enter a start node: ");

int n = reader.nextInt();

System.out.println("Enter a end node: ");

int m = reader.nextInt();

System.out.println("shortest distance according to dijkstra :" + result1[n][m]);

System.out.println("shortest distance according to floydWarshall :" + result2[n][m]);

  

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote