SlideShare a Scribd company logo
1 of 10
import java.util.*;
import java.io.*;
class Vertex {
// Constructor: set name, chargingStation and index according to given values,
// initilaize incidentRoads as empty array
public Vertex(String placeName, boolean chargingStationAvailable, int idx) {
name = placeName;
incidentRoads = new ArrayList<Edge>();
index = idx;
chargingStation = chargingStationAvailable;
}
public Vertex(String placeName, boolean hasChargingStataion) {
}
public String getName() {
return name;
}
public boolean hasChargingStation() {
return chargingStation;
}
public ArrayList<Edge> getIncidentRoads() {
return incidentRoads;
}
// Add a road to the array incidentRoads
public void addIncidentRoad(Edge road) {
incidentRoads.add(road);
}
public int getIndex() {
return index;
}
private String name; // Name of the place
ArrayList<Edge> incidentRoads; // Incident edges
private boolean chargingStation; // Availability of charging station
private int index; // Index of this vertex in the vertex array of the map
public void setVisited(boolean b) {
}
public Edge[] getAdjacentEdges() {
return null;
}
public boolean isVisited() {
return false;
}
public boolean isChargingStationAvailable() {
return false;
}
}
class Edge {
public Edge(int roadLength, Vertex firstPlace, Vertex secondPlace) {
length = roadLength;
incidentPlaces = new Vertex[] { firstPlace, secondPlace };
}
public Edge(Vertex vtx1, Vertex vtx2, int length2) {
}
public Vertex getFirstVertex() {
return incidentPlaces[0];
}
public Vertex getSecondVertex() {
return incidentPlaces[1];
}
public int getLength() {
return length;
}
private int length;
private Vertex[] incidentPlaces;
public Vertex getEnd() {
return null;
}
}
//A class that represents a sparse matrix
public class RoadMap {
// Default constructor
public RoadMap() {
places = new ArrayList<Vertex>();
roads = new ArrayList<Edge>();
}
// Auxiliary function that prints out the command syntax
public static void printCommandError() {
System.err.println("ERROR: use one of the following commands");
System.err.println(" - Load a map and print information:");
System.err.println(" java RoadMap -i <MapFile>");
System.err.println(" - Load a map and determine if two places are connnected by a path with
charging stations:");
System.err.println(" java RoadMap -c <MapFile> <StartVertexIndex> <EndVertexIndex>");
System.err.println(" - Load a map and determine the mininmum number of assistance cars
required:");
System.err.println(" java RoadMap -a <MapFile>");
}
public static void main(String[] args) throws Exception {
if (args.length == 2 && args[0].equals("-i")) {
RoadMap map = new RoadMap();
try {
map.loadMap(args[1]);
} catch (Exception e) {
System.err.println("Error in reading map file");
System.exit(-1);
}
System.out.println();
System.out.println("Read road map from " + args[1] + ":");
map.printMap();
System.out.println();
}
else if (args.length == 2 && args[0].equals("-a")) {
RoadMap map = new RoadMap();
try {
map.loadMap(args[1]);
} catch (Exception e) {
System.err.println("Error in reading map file");
System.exit(-1);
}
System.out.println();
System.out.println("Read road map from " + args[1] + ":");
map.printMap();
int n = map.minNumAssistanceCars();
System.out.println();
System.out.println("The map requires at least " + n + " assistance car(s)");
System.out.println();
}
else if (args.length == 4 && args[0].equals("-c")) {
RoadMap map = new RoadMap();
try {
map.loadMap(args[1]);
} catch (Exception e) {
System.err.println("Error in reading map file");
System.exit(-1);
}
System.out.println();
System.out.println("Read road map from " + args[1] + ":");
map.printMap();
int startVertexIdx = -1, endVertexIdx = -1;
try {
startVertexIdx = Integer.parseInt(args[2]);
endVertexIdx = Integer.parseInt(args[3]);
} catch (NumberFormatException e) {
System.err.println("Error: start vertex and end vertex must be specified using their indices");
System.exit(-1);
}
if (startVertexIdx < 0 || startVertexIdx >= map.numPlaces()) {
System.err.println("Error: invalid index for start vertex");
System.exit(-1);
}
if (endVertexIdx < 0 || endVertexIdx >= map.numPlaces()) {
System.err.println("Error: invalid index for end vertex");
System.exit(-1);
}
Vertex startVertex = map.getPlace(startVertexIdx);
Vertex endVertex = map.getPlace(endVertexIdx);
if (!map.isConnectedWithChargingStations(startVertex, endVertex)) {
System.out.println();
System.out.println("There is no path connecting " + map.getPlace(startVertexIdx).getName() + "
and "
+ map.getPlace(endVertexIdx).getName() + " with charging stations");
} else {
System.out.println();
System.out.println("There is at least one path connecting " +
map.getPlace(startVertexIdx).getName() + " and "
+ map.getPlace(endVertexIdx).getName() + " with charging stations");
}
System.out.println();
} else {
printCommandError();
System.exit(-1);
}
}
public void loadMap(String filename) {
File file = new File(filename);
places.clear();
roads.clear();
try {
Scanner sc = new Scanner(file);
//Read the first line: number of vertices and number of edges
int numVertices = sc.nextInt();
int numEdges = sc.nextInt();
for (int i = 0; i < numVertices; ++i) {
// Read the vertex name and its charing station flag
String placeName = sc.next();
int charginStationFlag = sc.nextInt();
boolean hasChargingStataion = (charginStationFlag == 1);
// Create a new vertex using the information above and add it to places
Vertex newVertex = new Vertex(placeName, hasChargingStataion, i);
places.add(newVertex);
}
for (int j = 0; j < numEdges; ++j) {
// Read the edge length and the indices for its two vertices
int vtxIndex1 = sc.nextInt();
int vtxIndex2 = sc.nextInt();
int length = sc.nextInt();
Vertex vtx1 = places.get(vtxIndex1);
Vertex vtx2 = places.get(vtxIndex2);
Edge newEdge = new Edge(length, vtx1, vtx2);
Edge reverseEdge = new Edge(length, vtx2, vtx1);
vtx1.addIncidentRoad(newEdge);
vtx2.addIncidentRoad(reverseEdge);
roads.add(newEdge);
}
sc.close();
} catch (Exception e) {
e.printStackTrace();
places.clear();
roads.clear();
}
}
the question is :
//Check if two vertices are connected by a path with charging stations on each itermediate vertex.
//Return true if such a path exists; return false otherwise.
//The worst-case time complexity of your algorithm should be no worse than O(v + e),
//where v and e are the number of vertices and the number of edges in the graph.
starting with
public boolean isConnectedWithChargingStations(Vertex startVertex, Vertex endVertex) {
if (startVertex.getIndex() == endVertex.getIndex()) {
return true;
}

More Related Content

Similar to import java-util--- import java-io--- class Vertex { -- Constructo.docx

Create a JAVA program that performs file IO and database interaction.pdf
Create a JAVA program that performs file IO and database interaction.pdfCreate a JAVA program that performs file IO and database interaction.pdf
Create a JAVA program that performs file IO and database interaction.pdfmalavshah9013
 
Write CC++ a program that inputs a weighted undirected graph and fi.pdf
Write CC++ a program that inputs a weighted undirected graph and fi.pdfWrite CC++ a program that inputs a weighted undirected graph and fi.pdf
Write CC++ a program that inputs a weighted undirected graph and fi.pdf4babies2010
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfsiennatimbok52331
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programmingAnung Ariwibowo
 
Railway reservation system
Railway reservation systemRailway reservation system
Railway reservation systemPrashant Sharma
 
Quest 1 define a class batsman with the following specifications
Quest  1 define a class batsman with the following specificationsQuest  1 define a class batsman with the following specifications
Quest 1 define a class batsman with the following specificationsrajkumari873
 
FedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdf
FedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdfFedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdf
FedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdfalukkasprince
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfaromanets
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdfarshiartpalace
 
Chaincode Development 區塊鏈鏈碼開發
Chaincode Development 區塊鏈鏈碼開發Chaincode Development 區塊鏈鏈碼開發
Chaincode Development 區塊鏈鏈碼開發HO-HSUN LIN
 
4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладкаDEVTYPE
 
AST Rewriting Using recast and esprima
AST Rewriting Using recast and esprimaAST Rewriting Using recast and esprima
AST Rewriting Using recast and esprimaStephen Vance
 
student start_code_U08223_cwk1 (1).DS_Store__MACOSXstudent.docx
student start_code_U08223_cwk1 (1).DS_Store__MACOSXstudent.docxstudent start_code_U08223_cwk1 (1).DS_Store__MACOSXstudent.docx
student start_code_U08223_cwk1 (1).DS_Store__MACOSXstudent.docxhanneloremccaffery
 
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfCountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfAggarwalelectronic18
 
Please fix the java code (using eclipse)package hw4p1;import jav.pdf
Please fix the java code (using eclipse)package hw4p1;import jav.pdfPlease fix the java code (using eclipse)package hw4p1;import jav.pdf
Please fix the java code (using eclipse)package hw4p1;import jav.pdfinfo961251
 
Write a program that reads a graph from a file and determines whether.docx
 Write a program that reads a graph from a file and determines whether.docx Write a program that reads a graph from a file and determines whether.docx
Write a program that reads a graph from a file and determines whether.docxajoy21
 

Similar to import java-util--- import java-io--- class Vertex { -- Constructo.docx (20)

app.js.docx
app.js.docxapp.js.docx
app.js.docx
 
Create a JAVA program that performs file IO and database interaction.pdf
Create a JAVA program that performs file IO and database interaction.pdfCreate a JAVA program that performs file IO and database interaction.pdf
Create a JAVA program that performs file IO and database interaction.pdf
 
Write CC++ a program that inputs a weighted undirected graph and fi.pdf
Write CC++ a program that inputs a weighted undirected graph and fi.pdfWrite CC++ a program that inputs a weighted undirected graph and fi.pdf
Write CC++ a program that inputs a weighted undirected graph and fi.pdf
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programming
 
Railway reservation system
Railway reservation systemRailway reservation system
Railway reservation system
 
Quest 1 define a class batsman with the following specifications
Quest  1 define a class batsman with the following specificationsQuest  1 define a class batsman with the following specifications
Quest 1 define a class batsman with the following specifications
 
FedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdf
FedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdfFedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdf
FedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdf
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
 
3 database-jdbc(1)
3 database-jdbc(1)3 database-jdbc(1)
3 database-jdbc(1)
 
Chaincode Development 區塊鏈鏈碼開發
Chaincode Development 區塊鏈鏈碼開發Chaincode Development 區塊鏈鏈碼開發
Chaincode Development 區塊鏈鏈碼開發
 
4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка
 
AST Rewriting Using recast and esprima
AST Rewriting Using recast and esprimaAST Rewriting Using recast and esprima
AST Rewriting Using recast and esprima
 
student start_code_U08223_cwk1 (1).DS_Store__MACOSXstudent.docx
student start_code_U08223_cwk1 (1).DS_Store__MACOSXstudent.docxstudent start_code_U08223_cwk1 (1).DS_Store__MACOSXstudent.docx
student start_code_U08223_cwk1 (1).DS_Store__MACOSXstudent.docx
 
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfCountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
 
My java file
My java fileMy java file
My java file
 
Please fix the java code (using eclipse)package hw4p1;import jav.pdf
Please fix the java code (using eclipse)package hw4p1;import jav.pdfPlease fix the java code (using eclipse)package hw4p1;import jav.pdf
Please fix the java code (using eclipse)package hw4p1;import jav.pdf
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
Write a program that reads a graph from a file and determines whether.docx
 Write a program that reads a graph from a file and determines whether.docx Write a program that reads a graph from a file and determines whether.docx
Write a program that reads a graph from a file and determines whether.docx
 

More from Blake0FxCampbelld

In C pls -- Write your name here -- Write the compiler used- Visual st.docx
In C pls -- Write your name here -- Write the compiler used- Visual st.docxIn C pls -- Write your name here -- Write the compiler used- Visual st.docx
In C pls -- Write your name here -- Write the compiler used- Visual st.docxBlake0FxCampbelld
 
In Arevalo- Aravind- Ayuso and Roca's (2013) article- we learn that th.docx
In Arevalo- Aravind- Ayuso and Roca's (2013) article- we learn that th.docxIn Arevalo- Aravind- Ayuso and Roca's (2013) article- we learn that th.docx
In Arevalo- Aravind- Ayuso and Roca's (2013) article- we learn that th.docxBlake0FxCampbelld
 
In a Word document- you will need to list and define all key terms wit.docx
In a Word document- you will need to list and define all key terms wit.docxIn a Word document- you will need to list and define all key terms wit.docx
In a Word document- you will need to list and define all key terms wit.docxBlake0FxCampbelld
 
In a survey of 2918 adults- 1477 say they have started paying bills on.docx
In a survey of 2918 adults- 1477 say they have started paying bills on.docxIn a survey of 2918 adults- 1477 say they have started paying bills on.docx
In a survey of 2918 adults- 1477 say they have started paying bills on.docxBlake0FxCampbelld
 
In a population of yellow mushrooms- a mutation resulted in a new purp (1).docx
In a population of yellow mushrooms- a mutation resulted in a new purp (1).docxIn a population of yellow mushrooms- a mutation resulted in a new purp (1).docx
In a population of yellow mushrooms- a mutation resulted in a new purp (1).docxBlake0FxCampbelld
 
In a particular hospital- 5 newborn babies were delivered yesterday- H.docx
In a particular hospital- 5 newborn babies were delivered yesterday- H.docxIn a particular hospital- 5 newborn babies were delivered yesterday- H.docx
In a particular hospital- 5 newborn babies were delivered yesterday- H.docxBlake0FxCampbelld
 
In a paper written by Bendey Coliege econonists Patricia M- Flynn and.docx
In a paper written by Bendey Coliege econonists Patricia M- Flynn and.docxIn a paper written by Bendey Coliege econonists Patricia M- Flynn and.docx
In a paper written by Bendey Coliege econonists Patricia M- Flynn and.docxBlake0FxCampbelld
 
In a given population of Drosophila- curly wings (c) is recessive to t.docx
In a given population of Drosophila- curly wings (c) is recessive to t.docxIn a given population of Drosophila- curly wings (c) is recessive to t.docx
In a given population of Drosophila- curly wings (c) is recessive to t.docxBlake0FxCampbelld
 
In a large population- 51- of the people have been vaccinated- If 4 pe.docx
In a large population- 51- of the people have been vaccinated- If 4 pe.docxIn a large population- 51- of the people have been vaccinated- If 4 pe.docx
In a large population- 51- of the people have been vaccinated- If 4 pe.docxBlake0FxCampbelld
 
In a geographic isolate- a small population has been studied for a gen.docx
In a geographic isolate- a small population has been studied for a gen.docxIn a geographic isolate- a small population has been studied for a gen.docx
In a geographic isolate- a small population has been studied for a gen.docxBlake0FxCampbelld
 
In a democracy- politicians may be shortsighted because they want to w.docx
In a democracy- politicians may be shortsighted because they want to w.docxIn a democracy- politicians may be shortsighted because they want to w.docx
In a democracy- politicians may be shortsighted because they want to w.docxBlake0FxCampbelld
 
In a certain city- the daily consumption of water (in millions of lite.docx
In a certain city- the daily consumption of water (in millions of lite.docxIn a certain city- the daily consumption of water (in millions of lite.docx
In a certain city- the daily consumption of water (in millions of lite.docxBlake0FxCampbelld
 
In a certain geographic location 15- of individuals have disease A- 15.docx
In a certain geographic location 15- of individuals have disease A- 15.docxIn a certain geographic location 15- of individuals have disease A- 15.docx
In a certain geographic location 15- of individuals have disease A- 15.docxBlake0FxCampbelld
 
In 2014- the General Social Survey incuded a question about the role o.docx
In 2014- the General Social Survey incuded a question about the role o.docxIn 2014- the General Social Survey incuded a question about the role o.docx
In 2014- the General Social Survey incuded a question about the role o.docxBlake0FxCampbelld
 
In 1993- when Fischer began his tenure at Kodak- the film industry was.docx
In 1993- when Fischer began his tenure at Kodak- the film industry was.docxIn 1993- when Fischer began his tenure at Kodak- the film industry was.docx
In 1993- when Fischer began his tenure at Kodak- the film industry was.docxBlake0FxCampbelld
 
In search engines analyzes the sequences of search queries to identif.docx
In  search engines analyzes the sequences of search queries to identif.docxIn  search engines analyzes the sequences of search queries to identif.docx
In search engines analyzes the sequences of search queries to identif.docxBlake0FxCampbelld
 
import os import matplotlib-pyplot as plt import pandas as pd import r.docx
import os import matplotlib-pyplot as plt import pandas as pd import r.docximport os import matplotlib-pyplot as plt import pandas as pd import r.docx
import os import matplotlib-pyplot as plt import pandas as pd import r.docxBlake0FxCampbelld
 
Implement the Plates class buildMap function so that it populates the.docx
Implement the Plates class buildMap function so that it populates the.docxImplement the Plates class buildMap function so that it populates the.docx
Implement the Plates class buildMap function so that it populates the.docxBlake0FxCampbelld
 
Implementing AES- Native Instructions (AES-NI) is faster than other im.docx
Implementing AES- Native Instructions (AES-NI) is faster than other im.docxImplementing AES- Native Instructions (AES-NI) is faster than other im.docx
Implementing AES- Native Instructions (AES-NI) is faster than other im.docxBlake0FxCampbelld
 
Implement these classes and a interface in 3 Java programs- Class 1- T.docx
Implement these classes and a interface in 3 Java programs- Class 1- T.docxImplement these classes and a interface in 3 Java programs- Class 1- T.docx
Implement these classes and a interface in 3 Java programs- Class 1- T.docxBlake0FxCampbelld
 

More from Blake0FxCampbelld (20)

In C pls -- Write your name here -- Write the compiler used- Visual st.docx
In C pls -- Write your name here -- Write the compiler used- Visual st.docxIn C pls -- Write your name here -- Write the compiler used- Visual st.docx
In C pls -- Write your name here -- Write the compiler used- Visual st.docx
 
In Arevalo- Aravind- Ayuso and Roca's (2013) article- we learn that th.docx
In Arevalo- Aravind- Ayuso and Roca's (2013) article- we learn that th.docxIn Arevalo- Aravind- Ayuso and Roca's (2013) article- we learn that th.docx
In Arevalo- Aravind- Ayuso and Roca's (2013) article- we learn that th.docx
 
In a Word document- you will need to list and define all key terms wit.docx
In a Word document- you will need to list and define all key terms wit.docxIn a Word document- you will need to list and define all key terms wit.docx
In a Word document- you will need to list and define all key terms wit.docx
 
In a survey of 2918 adults- 1477 say they have started paying bills on.docx
In a survey of 2918 adults- 1477 say they have started paying bills on.docxIn a survey of 2918 adults- 1477 say they have started paying bills on.docx
In a survey of 2918 adults- 1477 say they have started paying bills on.docx
 
In a population of yellow mushrooms- a mutation resulted in a new purp (1).docx
In a population of yellow mushrooms- a mutation resulted in a new purp (1).docxIn a population of yellow mushrooms- a mutation resulted in a new purp (1).docx
In a population of yellow mushrooms- a mutation resulted in a new purp (1).docx
 
In a particular hospital- 5 newborn babies were delivered yesterday- H.docx
In a particular hospital- 5 newborn babies were delivered yesterday- H.docxIn a particular hospital- 5 newborn babies were delivered yesterday- H.docx
In a particular hospital- 5 newborn babies were delivered yesterday- H.docx
 
In a paper written by Bendey Coliege econonists Patricia M- Flynn and.docx
In a paper written by Bendey Coliege econonists Patricia M- Flynn and.docxIn a paper written by Bendey Coliege econonists Patricia M- Flynn and.docx
In a paper written by Bendey Coliege econonists Patricia M- Flynn and.docx
 
In a given population of Drosophila- curly wings (c) is recessive to t.docx
In a given population of Drosophila- curly wings (c) is recessive to t.docxIn a given population of Drosophila- curly wings (c) is recessive to t.docx
In a given population of Drosophila- curly wings (c) is recessive to t.docx
 
In a large population- 51- of the people have been vaccinated- If 4 pe.docx
In a large population- 51- of the people have been vaccinated- If 4 pe.docxIn a large population- 51- of the people have been vaccinated- If 4 pe.docx
In a large population- 51- of the people have been vaccinated- If 4 pe.docx
 
In a geographic isolate- a small population has been studied for a gen.docx
In a geographic isolate- a small population has been studied for a gen.docxIn a geographic isolate- a small population has been studied for a gen.docx
In a geographic isolate- a small population has been studied for a gen.docx
 
In a democracy- politicians may be shortsighted because they want to w.docx
In a democracy- politicians may be shortsighted because they want to w.docxIn a democracy- politicians may be shortsighted because they want to w.docx
In a democracy- politicians may be shortsighted because they want to w.docx
 
In a certain city- the daily consumption of water (in millions of lite.docx
In a certain city- the daily consumption of water (in millions of lite.docxIn a certain city- the daily consumption of water (in millions of lite.docx
In a certain city- the daily consumption of water (in millions of lite.docx
 
In a certain geographic location 15- of individuals have disease A- 15.docx
In a certain geographic location 15- of individuals have disease A- 15.docxIn a certain geographic location 15- of individuals have disease A- 15.docx
In a certain geographic location 15- of individuals have disease A- 15.docx
 
In 2014- the General Social Survey incuded a question about the role o.docx
In 2014- the General Social Survey incuded a question about the role o.docxIn 2014- the General Social Survey incuded a question about the role o.docx
In 2014- the General Social Survey incuded a question about the role o.docx
 
In 1993- when Fischer began his tenure at Kodak- the film industry was.docx
In 1993- when Fischer began his tenure at Kodak- the film industry was.docxIn 1993- when Fischer began his tenure at Kodak- the film industry was.docx
In 1993- when Fischer began his tenure at Kodak- the film industry was.docx
 
In search engines analyzes the sequences of search queries to identif.docx
In  search engines analyzes the sequences of search queries to identif.docxIn  search engines analyzes the sequences of search queries to identif.docx
In search engines analyzes the sequences of search queries to identif.docx
 
import os import matplotlib-pyplot as plt import pandas as pd import r.docx
import os import matplotlib-pyplot as plt import pandas as pd import r.docximport os import matplotlib-pyplot as plt import pandas as pd import r.docx
import os import matplotlib-pyplot as plt import pandas as pd import r.docx
 
Implement the Plates class buildMap function so that it populates the.docx
Implement the Plates class buildMap function so that it populates the.docxImplement the Plates class buildMap function so that it populates the.docx
Implement the Plates class buildMap function so that it populates the.docx
 
Implementing AES- Native Instructions (AES-NI) is faster than other im.docx
Implementing AES- Native Instructions (AES-NI) is faster than other im.docxImplementing AES- Native Instructions (AES-NI) is faster than other im.docx
Implementing AES- Native Instructions (AES-NI) is faster than other im.docx
 
Implement these classes and a interface in 3 Java programs- Class 1- T.docx
Implement these classes and a interface in 3 Java programs- Class 1- T.docxImplement these classes and a interface in 3 Java programs- Class 1- T.docx
Implement these classes and a interface in 3 Java programs- Class 1- T.docx
 

Recently uploaded

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 

Recently uploaded (20)

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 

import java-util--- import java-io--- class Vertex { -- Constructo.docx

  • 1. import java.util.*; import java.io.*; class Vertex { // Constructor: set name, chargingStation and index according to given values, // initilaize incidentRoads as empty array public Vertex(String placeName, boolean chargingStationAvailable, int idx) { name = placeName; incidentRoads = new ArrayList<Edge>(); index = idx; chargingStation = chargingStationAvailable; } public Vertex(String placeName, boolean hasChargingStataion) { } public String getName() { return name; } public boolean hasChargingStation() { return chargingStation; } public ArrayList<Edge> getIncidentRoads() { return incidentRoads; } // Add a road to the array incidentRoads
  • 2. public void addIncidentRoad(Edge road) { incidentRoads.add(road); } public int getIndex() { return index; } private String name; // Name of the place ArrayList<Edge> incidentRoads; // Incident edges private boolean chargingStation; // Availability of charging station private int index; // Index of this vertex in the vertex array of the map public void setVisited(boolean b) { } public Edge[] getAdjacentEdges() { return null; } public boolean isVisited() { return false; } public boolean isChargingStationAvailable() { return false; } } class Edge {
  • 3. public Edge(int roadLength, Vertex firstPlace, Vertex secondPlace) { length = roadLength; incidentPlaces = new Vertex[] { firstPlace, secondPlace }; } public Edge(Vertex vtx1, Vertex vtx2, int length2) { } public Vertex getFirstVertex() { return incidentPlaces[0]; } public Vertex getSecondVertex() { return incidentPlaces[1]; } public int getLength() { return length; } private int length; private Vertex[] incidentPlaces; public Vertex getEnd() { return null; } } //A class that represents a sparse matrix public class RoadMap {
  • 4. // Default constructor public RoadMap() { places = new ArrayList<Vertex>(); roads = new ArrayList<Edge>(); } // Auxiliary function that prints out the command syntax public static void printCommandError() { System.err.println("ERROR: use one of the following commands"); System.err.println(" - Load a map and print information:"); System.err.println(" java RoadMap -i <MapFile>"); System.err.println(" - Load a map and determine if two places are connnected by a path with charging stations:"); System.err.println(" java RoadMap -c <MapFile> <StartVertexIndex> <EndVertexIndex>"); System.err.println(" - Load a map and determine the mininmum number of assistance cars required:"); System.err.println(" java RoadMap -a <MapFile>"); } public static void main(String[] args) throws Exception { if (args.length == 2 && args[0].equals("-i")) { RoadMap map = new RoadMap(); try { map.loadMap(args[1]); } catch (Exception e) { System.err.println("Error in reading map file");
  • 5. System.exit(-1); } System.out.println(); System.out.println("Read road map from " + args[1] + ":"); map.printMap(); System.out.println(); } else if (args.length == 2 && args[0].equals("-a")) { RoadMap map = new RoadMap(); try { map.loadMap(args[1]); } catch (Exception e) { System.err.println("Error in reading map file"); System.exit(-1); } System.out.println(); System.out.println("Read road map from " + args[1] + ":"); map.printMap(); int n = map.minNumAssistanceCars(); System.out.println(); System.out.println("The map requires at least " + n + " assistance car(s)"); System.out.println(); }
  • 6. else if (args.length == 4 && args[0].equals("-c")) { RoadMap map = new RoadMap(); try { map.loadMap(args[1]); } catch (Exception e) { System.err.println("Error in reading map file"); System.exit(-1); } System.out.println(); System.out.println("Read road map from " + args[1] + ":"); map.printMap(); int startVertexIdx = -1, endVertexIdx = -1; try { startVertexIdx = Integer.parseInt(args[2]); endVertexIdx = Integer.parseInt(args[3]); } catch (NumberFormatException e) { System.err.println("Error: start vertex and end vertex must be specified using their indices"); System.exit(-1); } if (startVertexIdx < 0 || startVertexIdx >= map.numPlaces()) { System.err.println("Error: invalid index for start vertex"); System.exit(-1); }
  • 7. if (endVertexIdx < 0 || endVertexIdx >= map.numPlaces()) { System.err.println("Error: invalid index for end vertex"); System.exit(-1); } Vertex startVertex = map.getPlace(startVertexIdx); Vertex endVertex = map.getPlace(endVertexIdx); if (!map.isConnectedWithChargingStations(startVertex, endVertex)) { System.out.println(); System.out.println("There is no path connecting " + map.getPlace(startVertexIdx).getName() + " and " + map.getPlace(endVertexIdx).getName() + " with charging stations"); } else { System.out.println(); System.out.println("There is at least one path connecting " + map.getPlace(startVertexIdx).getName() + " and " + map.getPlace(endVertexIdx).getName() + " with charging stations"); } System.out.println(); } else { printCommandError(); System.exit(-1); } } public void loadMap(String filename) {
  • 8. File file = new File(filename); places.clear(); roads.clear(); try { Scanner sc = new Scanner(file); //Read the first line: number of vertices and number of edges int numVertices = sc.nextInt(); int numEdges = sc.nextInt(); for (int i = 0; i < numVertices; ++i) { // Read the vertex name and its charing station flag String placeName = sc.next(); int charginStationFlag = sc.nextInt(); boolean hasChargingStataion = (charginStationFlag == 1); // Create a new vertex using the information above and add it to places Vertex newVertex = new Vertex(placeName, hasChargingStataion, i); places.add(newVertex); } for (int j = 0; j < numEdges; ++j) { // Read the edge length and the indices for its two vertices int vtxIndex1 = sc.nextInt(); int vtxIndex2 = sc.nextInt(); int length = sc.nextInt(); Vertex vtx1 = places.get(vtxIndex1);
  • 9. Vertex vtx2 = places.get(vtxIndex2); Edge newEdge = new Edge(length, vtx1, vtx2); Edge reverseEdge = new Edge(length, vtx2, vtx1); vtx1.addIncidentRoad(newEdge); vtx2.addIncidentRoad(reverseEdge); roads.add(newEdge); } sc.close(); } catch (Exception e) { e.printStackTrace(); places.clear(); roads.clear(); } } the question is : //Check if two vertices are connected by a path with charging stations on each itermediate vertex. //Return true if such a path exists; return false otherwise. //The worst-case time complexity of your algorithm should be no worse than O(v + e), //where v and e are the number of vertices and the number of edges in the graph. starting with public boolean isConnectedWithChargingStations(Vertex startVertex, Vertex endVertex) { if (startVertex.getIndex() == endVertex.getIndex()) { return true;
  • 10. }