Question

This lab is about finding a path from some source city to a destination city. Cities...

This lab is about finding a path from some source city to a destination city. Cities are

represented by the City enum in the provided jar file. CitySelector is a provided interface that

determines a “next hop” city (the next city to go to). CityPathConnector is a provided interface

that uses a CitySelector to determine a path from a given source city to a given destination city.

Starting from the source city, a CityPathConnector will get the next city to visit from its

CitySelector. The CityPathConnector will repeatedly do this until the CitySelector presents the

destination city, at which point the path is complete. The CityPathConnector needs to keep track

of which intermediary cities were visited.

For example, a path from New York to Atlanta might be:

New York, Washington, Boston, Los Angeles, Atlanta

Note that the realistic practicality of the path is not a concern for this lab.

However, here’s a caveat. The CitySelector may return null; this denotes that there are no paths

from the current city. When this happens, the CityPathConnector needs to go back to the

previously visited city.

For example, given a source of New York and destination of Atlanta, the first part of the path

might be:

New York, Washington, Boston

If the next city to visit is null, then the path becomes

New York, Washington, Boston, Washington

And the CityPathConnector checks for a next city from Washington (a CitySelector will not

present a city more than once).

From there, the path can be completed; for example:

New York, Washington, Boston, Washington, Charlotte, Atlanta

Or, it will be possible that the CitySelector will return a string of null values, denoting that there

is no path to the destination; for example:

New York, Washington, Boston, Washington, New York (null value and done).

Values of null presented by the CitySelector denote dead-end cities. When this happens on a

path, we will also want to know what the “direct” (for lack of a better term) path is. For example,

if the full path is:

New York, Washington, Boston, Washington, Charlotte, Atlanta

Then the direct path is:

New York, Washington, Charlotte, Atlanta

And when there is no path to the destination, the direct path will be empty. For example (going

from New York to Atlanta):

Full Path: New York, Washington, Boston, Washington, New York

Direct Path: (empty)

Part 1

1. Create an implementation of the CityPathConnector interface.

a. Use the stack data structure (java.lang.Stack) in your implementation.

Part 2

There are three ways to test and validate your implementation, as follows.

1. Use the RandomCitySelector which will present cities or null values in random order. A

null value has equal chance as any eligible city to be presented.

2. Use the ListCitySelector. This allows you to hard code a city order so you can test

specific test cases.

3. Use CityPathConnectorTester. This will automatically run your CityPathConnector

against a series of predefined test cases and will show you which test cases were failed,

if any.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

the java program is written below to find the shortest path between two cities. Here i have defined 11 cities and weighed each path. Stack data structure is made used of. In below program the shortest path between any two cities names as A,D,F,H,J,K,M,O,P,R,Z is calculated using Dikjistra Algorithm. You can change source and destination city in the program, as of now it is given as A to Z.

import java.util.PriorityQueue;
import java.util.Stack;
import java.util.Collections;
import java.util.Scanner;


class Vertex implements Comparable<Vertex>
{
    public final String name;
    public PathSelector[] adjacencies;
    public double minDistance = Double.POSITIVE_INFINITY;
    public Vertex previous;
    public Vertex(String argName) { name = argName; }
    public String toString() { return name; }
    public int compareTo(Vertex other)
    {
        return Double.compare(minDistance, other.minDistance);
    }

}


class PathSelector
{
    public final Vertex target;
    public final double weight;
    public PathSelector(Vertex argTarget, double argWeight)
    { target = argTarget; weight = argWeight; }
}

public class Dijkstra
{
    public static void computePaths(Vertex source)
    {
        source.minDistance = 0.;
        PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
    vertexQueue.add(source);

    while (!vertexQueue.isEmpty()) {
        Vertex u = vertexQueue.poll();

            // Visit each edge exiting u
            for (PathSelector e : u.adjacencies)
            {
                Vertex v = e.target;
                double weight = e.weight;
                double distanceThroughU = u.minDistance + weight;
        if (distanceThroughU < v.minDistance) {
            vertexQueue.remove(v);

            v.minDistance = distanceThroughU ;
            v.previous = u;
            vertexQueue.add(v);
        }
            }
        }
    }

    public static Stack<Vertex> getShortestPathTo(Vertex target)
    {
        Stack<Vertex> path = new Stack<Vertex>();
        for (Vertex vertex = target; vertex != null; vertex = vertex.previous)
            path.add(vertex);

        Collections.reverse(path);
        return path;
    }

    public static void main(String[] args)
    {
     
      //Scanner sc = new Scanner(System.in);
       //char i = sc.nextChar();
    
      // mark all the vertices
        Vertex A = new Vertex("A");
        Vertex B = new Vertex("B");
        Vertex D = new Vertex("D");
        Vertex F = new Vertex("F");
        Vertex K = new Vertex("K");
        Vertex J = new Vertex("J");
        Vertex M = new Vertex("M");
        Vertex O = new Vertex("O");
        Vertex P = new Vertex("P");
        Vertex R = new Vertex("R");
        Vertex Z = new Vertex("Z");

        // set the edges and weight
        A.adjacencies = new PathSelector[]{ new PathSelector(M, 8) };
        B.adjacencies = new PathSelector[]{ new PathSelector(D, 11) };
        B.adjacencies = new PathSelector[]{ new PathSelector(A, 10) };
        D.adjacencies = new PathSelector[]{ new PathSelector(B, 11) };
        F.adjacencies = new PathSelector[]{ new PathSelector(K, 23) };
        K.adjacencies = new PathSelector[]{ new PathSelector(O, 40) };
         K.adjacencies = new PathSelector[]{ new PathSelector(A, 34) };
        J.adjacencies = new PathSelector[]{ new PathSelector(K, 25) };
        M.adjacencies = new PathSelector[]{ new PathSelector(R, 8) };
        O.adjacencies = new PathSelector[]{ new PathSelector(K, 40) };
        O.adjacencies = new PathSelector[]{ new PathSelector(B, 40) };
        P.adjacencies = new PathSelector[]{ new PathSelector(Z, 18) };
        R.adjacencies = new PathSelector[]{ new PathSelector(P, 15) };
        Z.adjacencies = new PathSelector[]{ new PathSelector(P, 18) };

      
        computePaths(A); // run Dijkstra
        System.out.println("Distance to " + Z + ": " + Z.minDistance);
        Stack<Vertex> path = getShortestPathTo(Z);
        System.out.println("Path: " + path);
    }
}

Sample output:

Distance to Z: 108.0
Path: [J, K, A, M, R, P, Z]

Add a comment
Know the answer?
Add Answer to:
This lab is about finding a path from some source city to a destination city. Cities...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Cost of Living in Selected U.S. Cities Cost-of-living index City number New York (Manhattan) 216.7...

    The Cost of Living in Selected U.S. Cities Cost-of-living index City number New York (Manhattan) 216.7 San Francisco, CA 164.0 Washington, DC 140.1 Los Angeles, CA 136.4 Boston, MA 132.5 Philadelphia, PA 126.5 Seattle, WA 121.4 Chicago, IL 116.9 Richmond, VA 104.5 Phoenix, AZ 100.7 Detroit, MI 99.4 Atlanta, GA 95.6 Charlotte, NC 93.2 Dallas, TX 91.9 Source US Census Annual Average 2010 The table shown here gives a cost-of-living index for 14 different cities. Currently, you are living and...

  • the class is supoose to implement the provided interface Router implemement the methods as per the...

    the class is supoose to implement the provided interface Router implemement the methods as per the interface. Also provide junit test cases and test with a test file. please write in java its for a graph implementation. the interface is provided. Create a class, Drone Router, that will determine the best (i.e. minimum cost) path between the distribution center and a specified waypoint. The DroneRouter class will implement the edu.metrostate.ics340.p3.Router interface (provided). Feature Specification Information Class Drone Router implements edu.metrostate.ics...

  • Use the following information to answer questions 25-31 A traveler wants to know if the prices of hotels are different. She samples 10 cities and finds the prices below. Use a paired-sample t-t...

    Use the following information to answer questions 25-31 A traveler wants to know if the prices of hotels are different. She samples 10 cities and finds the prices below. Use a paired-sample t-test to determine whether the difference between hotel prices is significant at the a 0.01 confidence level Cities Atlanta Boston Chicago Dallas Denver Indianapolis Los Angeles New York City 517 Philadelphia Washington, DC 251 Hyatt Regency prices in dollars Hilton prices in dollars 90 273 204 303 189...

  • 4. A distributor received goods at five US ports, from which the goods are sent on...

    4. A distributor received goods at five US ports, from which the goods are sent on to ten major customers in the following ten cities: Salt Lake City, Las Vegas, Denver, St. Louis, Houston, Atlanta, Detroit, Boston, Baltimore, and Albany. Each customer has monthly demand of 300 cases. The monthly availability of the goods at the ports is shown in the table below: Ports = Seattle Oakland Norfolk Savannah New York Monthly Availability (cases) = 600 1000 500 400 500...

  • For this question, consider names of cities that are made from any number of words where...

    For this question, consider names of cities that are made from any number of words where the first letter is always in uppercase and the words are separated by a space Implement a regular expression as a Java String p like so String p " such that is a regular expression pattern that will match any String that contains the name of a city and only such strings. Your answer must include the whole line of code above, meaning that...

  • write a SQL statements to perform the following: Select all years a World Series game was...

    write a SQL statements to perform the following: Select all years a World Series game was played Select all losing teams that played in a World Series game Select all winning and losing teams that played in a World Series game Select all cities of a winning or losing team that played in a World Series game Select all winning and losing teams that played in a World Series game, and provide aliases of "Winning Team" and "Losing Team" Select...

  • PYTHON Text file Seriesworld attached below and it is a list of teams that won from...

    PYTHON Text file Seriesworld attached below and it is a list of teams that won from 1903-2018. First time mentioned won in 1903 and last team mentioned won in 2018. World series wasn’t played in 1904 and 1994 and the file has entries that shows it. Write a python program which will read this file and create 2 dictionaries. The first key of the dictionary should show name of the teams and each keys value that is associated is the...

  • Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The...

    Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The application will be to add large numbers. Review adding large numbers Remember that we can use stacks to safely add integer values that overflow the int data type g. in Java, the maximum possible int value Integer.MAX_VALUE is: 2147483647 so any int addition larger than this will overflow and fail Using stacks to add large numbers safely Will actually represent the large integers to...

  • ***** running late, mere 3 hours left for due time, please help ** #needed in c++...

    ***** running late, mere 3 hours left for due time, please help ** #needed in c++ #inputfile MinPath2.txt 0   SF 1   LA 2   DALLAS 3   CONCORD 4   PHOENIX 5   CHICAGO 6   ST LOUIS 7   BOSTON 8   NY 9   LONDON 10   PARIS 11   TOKYO 12   BANGKOK 13   MEXICO CITY 14   MONTREAL -1   0   1   40 0   2   100 0   4   130 0   8   200 0   9   800 0   10   900 1   2   50 1   3   80 1   4   70 1   8  ...

  • The Acme Trucking company has hired you to write software to help dispatch its trucks. One important element of this sof...

    The Acme Trucking company has hired you to write software to help dispatch its trucks. One important element of this software is knowing the distance between any two cities that it services. Design and implement a Distance class that stores the distances between cities in a two-dimensional array. This class contains the following required data members and methods: Required Data Members: String[] cities; //it is used to store city names int[][] distance; // this 2-D array repreents distance between two...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT