Write this code as it is in the pseudo code create an empty stack, call it pathS
ID: 3573916 • Letter: W
Question
Write this code as it is in the pseudo code
create an empty stack, call it pathStack
create an empty array of N elements, call it visitedCities // N== # of cities
//assume start city is city 0
set city 0 as visited in visitedCities array
push city 0 to pathStack
set closestCity to 0
set minFlag to false
Output start city
while pathStack is not empty do
set currentCity with top value of pathStack
set min to Integer.MAX_VALUE //minimum distance
for all the remaining cities starting city 1 to N do
if (distance from currentCity to city i is not 0 AND city i is not visited)
if (distance from currentCity to city i is less than min)
min = distance from currentCity to city i
closestCity = i
set minFlag to true
endif
endif
endfor
if(minflag)
set closestCity in visitedCities as visited
push closestCity to pathStack
Output closestCity
set minFlag to false
continue
endif
pop the top element from pathStack
endwhile
Explanation / Answer
Here is the source code for java program to implement the above program
code
import java.util.InputMismatchException;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.