Need to be done in java Add a code in the given code// Subodh Shrestha // CSCI308 Fall 2021 21F // Date: 11/22/2021 Time:3:3
Need to be done in java
Add a code in the given code// Subodh Shrestha
// CSCI308 Fall 2021 21F
// Date: 11/22/2021 Time:3:30PM
package djkstrashortestpath_shrestha;
class DsAlgorithm
{
//1.Find the shortest path for all of the given vertices:
// We have not visited yet.
// Note: If the vertex is marked as true, we have visited it.
//2. Update the distance value of all neighboring vertices
// of the selected vertex
//3. Update distance[loc] if and only if it is not in the
// PathArray, there is an shorter path from source to the vertex at loc.
public static int[] Dijkstra(int graph[][],
int s, int distanceArr[], int VNum)
{
//PathArray[j] will be true if the given vertex j
//is included in the shortest path tree from origin to j
Boolean PathArray[] = new Boolean[VNum];
// Initializing distance = INFINITE PathArray[] = false
for(int j = 0; j < VNum; j++)
{
distanceArr[j] = Integer.MAX_VALUE;
PathArray[j] = false;
}
distanceArr[s] = 0; // node to self distance is 0
for(int i = 0; i < VNum – 1; i++)
{
int minIndex = minimumDistance(distanceArr, PathArray, VNum);
PathArray[minIndex] = true;
for(int j =0; j < VNum; j++)
{
if(!PathArray[j] && graph[minIndex][j] != -1
&& distanceArr[minIndex] != Integer.MAX_VALUE
&& distanceArr[minIndex]
+ graph[minIndex][j] < distanceArr[j])
{
distanceArr[j] = distanceArr[minIndex] + graph[minIndex][j];
}
}
}
return distanceArr;
}
//Compute the min distance value between a vertex and other verticies.
public static int minimumDistance(int distance[],
Boolean PathArray[], int VNum)
{
//Let the min be an integer max value and the index begin with -1
int minimum = Integer.MAX_VALUE;
int MinimumIndex = -1;
for(int i =0; i< VNum; i++)
{
// compare values and keep the minimum
if (PathArray[i] == false && distance[i] <= minimum)
{
minimum = distance[i];
MinimumIndex = i;
}
}
return MinimumIndex;
}
}
Collepals.com Plagiarism Free Papers
Are you looking for custom essay writing service or even dissertation writing services? Just request for our write my paper service, and we'll match you with the best essay writer in your subject! With an exceptional team of professional academic experts in a wide range of subjects, we can guarantee you an unrivaled quality of custom-written papers.
Why Hire Collepals.com writers to do your paper?
Quality- We are experienced and have access to ample research materials.
We write plagiarism Free Content
Confidential- We never share or sell your personal information to third parties.
Support-Chat with us today! We are always waiting to answer all your questions.