Question
Hello. I am trying to implement Dijkstra's algorithm. I am trying to follow the following pseudo code : I got stuck on few steps and
Hello.
I am trying to implement Dijkstra's algorithm.
I am trying to follow the following pseudo code :
I got stuck on few steps and I need your help.
I did the following:
/* ShortestPaths.java This template includes some testing code to help verify the implementation. To interactively provide test inputs, run the program with java ShortestPaths To conveniently test the algorithm with a large input, create a text file containing one or more test graphs (in the format described below) and run the program with java ShortestPaths file.txt where file.txt is replaced by the name of the text file. The input consists of a series of graphs in the following format:
import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.util.PriorityQueue; import java.util.Stack; import java.util.LinkedList; import java.util.Iterator; import edu.princeton.cs.algs4.*; import java.util.*;
//Do not change the name of the ShortestPaths class public class ShortestPaths{
//TODO: Your code here public static int n;
static void ShortestPaths(int[][][] adj, int source){ n = adj.length; int [] d = new int[n];//current node int [] pi = new int[n];//previous node int S;//u+e.weight EdgeWeightedGraph G = new EdgeWeightedGraph(n); for (int i = 0; i
// I'M STUCK HERE! I DON'T KNOW HOW TO CONTINUE OR IF WHAT I DID IS CORRECT. PLEASE HELP! for(Edge e : G.adj(u)) { int v = e.either(); int w = e.other(v); if (d[u] + e.weight() 0){ //If a file argument was provided on the command line, read from the file try{ s = new Scanner(new File(args[0])); } catch(java.io.FileNotFoundException e){ System.out.printf("Unable to open %s ",args[0]); return; } System.out.printf("Reading input values from %s. ",args[0]); } else{ //Otherwise, read from standard input s = new Scanner(System.in); System.out.printf("Reading input values from stdin. "); } int graphNum = 0; double totalTimeSeconds = 0; //Read graphs until EOF is encountered (or an error occurs) while(true){ graphNum++; if(graphNum != 1 && !s.hasNextInt()) break; System.out.printf("Reading graph %d ",graphNum); int n = s.nextInt(); int[][][] adj = new int[n][][]; int valuesRead = 0; for (int i = 0; i edgeList = new LinkedList
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started