Algorithms Lab Ex 7
Algorithms Lab Ex 7
Algorithms Lab
CSE 206
2 Problem analysis
Given a graph and a source vertex in the graph, we have to find shortest paths from source to all vertices in
the given graph. Dijkstra’s algorithm is very similar to Prim’s algorithm for minimum spanning tree. Like
Prim’s MST, we generate a SPT (shortest path tree) with given source as root. We maintain two sets, one set
contains vertices included in shortest path tree, other set includes vertices not yet included in shortest path
tree. At every step of the algorithm, we find a vertex which is in the other set (set of not yet included) and
has a minimum distance from the source. Below are the detailed steps used in Dijkstra’s algorithm to find the
shortest path from a single source vertex to all other vertices in the given graph.
1. Create a set sptSet (shortest path tree set) that keeps track of vertices included in shortest path tree,
i.e., whose minimum distance from source is calculated and finalized. Initially, this set is empty.
2. Assign a distance value to all vertices in the input graph. Initialize all distance values as INFINITE.
Assign distance value as 0 for the source vertex so that it is picked first.
4 Implementation in Java
1 // A Java program for Dijkstra’s single source shortest path algorithm.
2 // The program is for adjacency matrix representation of the graph
3
4 import java.util.*;
5 import java.lang.*;
6 import java.io.*;
7
8 class ShortestPath {
9 // A utility function to find the vertex with minimum distance value,
10 // from the set of vertices not yet included in shortest path tree
11 static final int V = 5;
12 int minDistance(int dist[], Boolean sptSet[])
13 {
14 // Initialize min value
15 int min = Integer.MAX_VALUE, min_index = −1;
16
17 for (int v = 0; v < V; v++)
18 if (sptSet[v] == false && dist[v] <= min) {
19 min = dist[v];
20 min_index = v;
21 }
22
23 return min_index;
24 }
25
26 // A utility function to print the constructed distance array
27 void printSolution(int dist[])
28 {
29 System.out.println("Vertex \t\t Distance from Source");
30 for (int i = 0; i < V; i++)
31 System.out.println(i + " \t\t " + dist[i]);
32 }
33
34 // Function that implements Dijkstra’s single source shortest path
35 // algorithm for a graph represented using adjacency matrix
36 // representation
37 void dijkstra(int graph[][], int src)
38 {
39 int dist[] = new int[V]; // The output array. dist[i] will hold
40 // the shortest distance from src to i
41
42 // sptSet[i] will true if vertex i is included in shortest
43 // path tree or shortest distance from src to i is finalized
44 Boolean sptSet[] = new Boolean[V];
45
46 // Initialize all distances as INFINITE and stpSet[] as false
47 for (int i = 0; i < V; i++) {
48 dist[i] = Integer.MAX_VALUE;
49 sptSet[i] = false;
50 }
51
52 // Distance of source vertex from itself is always 0
53 dist[src] = 0;
54
55 // Find shortest path for all vertices
56 for (int count = 0; count < V − 1; count++) {
57 // Pick the minimum distance vertex from the set of vertices
58 // not yet processed. u is always equal to src in first
59 // iteration.
60 int u = minDistance(dist, sptSet);
61
62 // Mark the picked vertex as processed
63 sptSet[u] = true;
64
65 // Update dist value of the adjacent vertices of the
66 // picked vertex.
67 for (int v = 0; v < V; v++)
68
69 // Update dist[v] only if is not in sptSet, there is an
70 // edge from u to v, and total weight of path from src to
71 // v through u is smaller than current value of dist[v]
72 if (!sptSet[v] && graph[u][v] != 0 && dist[u] != Integer.MAX_VALUE &&
dist[u] + graph[u][v] < dist[v])
73 dist[v] = dist[u] + graph[u][v];
74 }
75
76 // print the constructed distance array
77 printSolution(dist);
78 }
79
80 // Driver method
81 public static void main(String[] args)
82 {
83 /* Let us create the example graph discussed above */
84 int graph[][] = new int[][] { { 0, 10, 5, 0, 0 },
85 { 0, 0, 2, 1, 0 },
86 { 0, 3, 0, 9, 2 },
87 { 0, 0, 0, 0, 4 },
88 { 7, 0, 0, 6, 0 }};
89 ShortestPath t = new ShortestPath();
90 t.dijkstra(graph, 0);
91 }
92 }
7 Lab Task (Please implement yourself and show the output to the
instructor)
1. Prove that, if there exists negative edge, Dijkstra’s shortest path algorithm may fail to find the shortest
path. Please add Bellman Ford Algorithm
9 Policy
Copying from internet, classmate, seniors, or from any other source is strongly prohibited. 100% marks will be
deducted if any such copying is detected.