Question: Hello, please how can i make the Dijkstra's code part work to output the shortest path. thank you Here is my code below import java.io.*;

Hello, please how can i make the Dijkstra's code part work to output the shortest path. thank you

Here is my code below

import java.io.*;

import java.util.*;

public class Hospital

{

static ArrayList Vertex = new ArrayList();

static ArrayList Path = new ArrayList();

public static void main(String[] args) throws IOException

{

Scanner in = new Scanner(System.in);

// read in vertices

File file = new File("Vertex.txt");

Scanner infile = new Scanner(file);

String Input = "";

while (infile.hasNextLine())

{

Input = infile.nextLine();

Vertex.add(new Room(Input));

}

//System.out.println(Vertex.size() + " vertices read from file");

// read in edges

file = new File("edge.txt");

infile = new Scanner(file);

String From, Direction, To;

int Count = 0;

while (infile.hasNext())

{

Count++;

From = infile.next();

Direction = infile.next();

To = infile.next();

// locate From Vertex in ArrayList

int IndexFrom = 0;

while (IndexFrom

{

String roomNm = Vertex.get(IndexFrom).RoomName;

if (roomNm.equals(From)) {

break;

}

IndexFrom++;

}

// locate To Vertex in ArrayList

int IndexTo = 0;

while (IndexTo

{

String roomNm = Vertex.get(IndexTo).RoomName;

if (roomNm.equals(To)) {

break;

}

IndexTo++;

}

// create edge

if (IndexFrom

if (Direction.equals("North"))

{

Vertex.get(IndexFrom).North = Vertex.get(IndexTo);

Vertex.get(IndexTo).South = Vertex.get(IndexFrom);

}

if (Direction.equals("South"))

{

Vertex.get(IndexFrom).South = Vertex.get(IndexTo);

Vertex.get(IndexTo).North = Vertex.get(IndexFrom);

}

if (Direction.equals("East"))

{

Vertex.get(IndexFrom).East = Vertex.get(IndexTo);

Vertex.get(IndexTo).West = Vertex.get(IndexFrom);

}

if (Direction.equals("West"))

{

Vertex.get(IndexFrom).West = Vertex.get(IndexTo);

Vertex.get(IndexTo).East = Vertex.get(IndexFrom);

}

}

}

//System.out.println(Count + " edges read from file");

//System.out.println("Press to Continue");

//Choice = in.nextLine();

//System.out.println(Vertex.get(0));

Room Temp = Vertex.get(0);

String Choice = " ";

while (!Choice.equals("q"))

{

System.out.println("You are in room " + Temp.RoomName);

System.out.println("Which direction do you want to go? (North, South, East or West) ");

Choice = in.next();

if (Choice.equals("S") && Temp.South != null)

Temp = Temp.South;

else if (Choice.equals("S") && Temp.South == null)

System.out.println("You can't go that way! Kindly go in another direction");

else if (Choice.equals("N") && Temp.North != null)

Temp = Temp.North;

else if (Choice.equals("N") && Temp.North == null)

System.out.println("You can't go that way! Kindly go in another direction");

else if (Choice.equals("E") && Temp.East != null)

Temp = Temp.East;

else if (Choice.equals("E") && Temp.East == null)

System.out.println("You can't go that way! Kindly go in another direction");

else if (Choice.equals("W") && Temp.West != null)

Temp = Temp.West;

else if (Choice.equals("W") && Temp.West == null)

System.out.println("You can't go that way! Kindly go in another direction");

}

// System.out.println(Temp.South.North.East.West.RoomName);

}

static void Dijkstra(Room Start, Room Finish)

{

// set distance to all rooms (except for Start) to 1000 and visited = false

for (int i=0; i

{

if (Vertex.get(i) == Start)

Vertex.get(i).Distance = 0;

else

Vertex.get(i).Distance = 1000; // set distance to "infinity"

Vertex.get(i).Visited = false;

}

// Do Dijkstra - find Distance to each room

Room Temp = Start;

while (!Finish.Visited)

{

Temp.Visited = true;

if (Temp.North!=null && !Temp.North.Visited && Temp.North.Distance > Temp.Distance+1)

Temp.North.Distance = 1 + Temp.Distance;

if (Temp.South!=null && !Temp.South.Visited && Temp.South.Distance > Temp.Distance+1)

Temp.South.Distance = 1 + Temp.Distance;

if (Temp.East!=null && !Temp.East.Visited && Temp.East.Distance > Temp.Distance+1)

Temp.East.Distance = 1 + Temp.Distance;

if (Temp.West!=null && !Temp.West.Visited && Temp.West.Distance > Temp.Distance+1)

Temp.West.Distance = 1 + Temp.Distance;

int Smallest = 1000;

int SmallestIndex = 0;

for (int i=0; i

{

if (!Vertex.get(i).Visited && Vertex.get(i).Distance

{

Smallest = Vertex.get(i).Distance;

SmallestIndex = i;

}

}

Temp = Vertex.get(SmallestIndex);

}

// populate Path ArrayList with Rooms of shortest path

Temp = Finish;

Path.clear();

Path.add(0,Finish);

while (Temp != Start)

{

int N = 1000, S = 1000, E = 1000, W = 1000;

if (Temp.North != null) N = Temp.North.Distance;

if (Temp.South != null) S = Temp.South.Distance;

if (Temp.East != null) E = Temp.East.Distance;

if (Temp.West != null) W = Temp.West.Distance;

if (N

Temp = Temp.North;

else if (S

Temp = Temp.South;

else if (E

Temp = Temp.East;

else

Temp = Temp.West;

Path.add(0,Temp);

}

}

}

class Room

{

String RoomName;

Room North, South, East, West;

boolean Visited; // used for Dijkstra

int Distance; // used for Dijkstra

Room(String theRoomName)

{

RoomName = theRoomName;

}

}

Here is the Vertex.txt

Entrance SecurityPost Parking Emergency Mortuary WardOne AdminBlock WardTwo Surgery Cafeteria Laboratory BloodBank WardThree

Here is the edge.txt

Entrance East Parking Entrance West SecurityPost Entrance South AdminBlock Cafeteria North AdminBlock Cafeteria West Surgery Cafeteria East Laboratory Cafeteria South WardThree SecurityPost East Entrance WardOne West Mortuary WardOne East AdminBlock WardOne South Surgery Parking West Entrance Parking East Emergency Parking South WardTwo Mortuary East WardOne AdminBlock North Entrance AdminBlock South Cafeteria AdminBlock West Surgery Emergency West Parking Surgery North WardOne Surgery East Cafeteria Surgery South BloodBank BloodBank North Surgery BloodBank East WaedThree WardThree North Cafeteria WardThree West BloodBank WardTwo North Parking WardTwo West AdminBlock WardTwo South Laboratory Laboratory North WardTwo Laboratory West Cafeteria

here is the drawing

Hello, please how can i make the Dijkstra's code part work to

5 Entrance Parking 4 Mortuary WardOne AdminBlock WardTwo 4 4 Surgery Cafetaria Laboratory 5 WardThree BloodBank 5 Entrance Parking 4 Mortuary WardOne AdminBlock WardTwo 4 4 Surgery Cafetaria Laboratory 5 WardThree BloodBank

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!