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

1. Given a rectangular field with few sensors present on it, write a program to

ID: 3915571 • Letter: 1

Question

1. Given a rectangular field with few sensors present on it, write a program to cross it by taking the shortest safe route without activating the sensors. Sensors are marked with 0. For example: consider the below matrix The shortest safe path has length of 11 and the route is marked in Hint: Find shortest path from any cell in first column to any cell in the last column of the matrix. 2. Given an edge weighted graph, write a java program named Path.java that will: a. Given a source vertex, find shortest paths from source to all vertices in the given graph. b. If the graph is directed, then find shortest distances between every pair of vertices.

Explanation / Answer

// shortest path using Dijkstra's algorithm

import java.util.*;

import java.lang.*;

import java.io.*;

class ShortestPath

{

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

static final int V=9;

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;

}

// To print the distance array

void printSolution(int dist[], int n)

{

System.out.println("Vertex Distance from Source");

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

System.out.println(i+" from source "+dist[i]);

}

// implementing Dijkstra's single source shortest path

void dijkstra(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

Boolean sptSet[] = new Boolean[V];

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

printSolution(dist, V);

}

// Driver method

public static void main (String[] args)

{

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

int graph[][] = new int[][]{{0, 1, 0, 0, 0, 0, 0, 1, 0, 0},

{1, 0, 1, 0, 0, 0, 0, 1, 0, 1},

{0, 1, 0, 1, 0, 1, 0, 0, 1,0},

{0, 0, 1, 0, 1, 1, 0, 0, 0,0},

{0, 0, 0, 1, 0, 1, 0, 0, 0,1},

{0, 0, 1, 1, 1, 0, 1, 0, 0,1},

{0, 0, 0, 0, 0, 1, 0, 1, 1,0},

{1, 1, 0, 0, 0, 0, 1, 0, 1,0},

{0, 0, 0, 1, 0, 1, 0, 0, 0,1},

{0, 0, 1, 0, 0, 0, 1, 1, 0,0}

};

ShortestPath t = new ShortestPath();

t.dijkstra(graph, 0);

}

}