Graph-Search: Initial scaffolding
This commit is contained in:
86
Graph-Search/README.md
Normal file
86
Graph-Search/README.md
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
# Lab 11: Graph Search - Bus Stops
|
||||||
|
|
||||||
|
## Graph Search Basics
|
||||||
|
|
||||||
|
The purpose of this lab is to give you practice implementing graph data
|
||||||
|
structures and basic graph traversal and reachability queries.
|
||||||
|
|
||||||
|
You are building an app for a newly created bus system connecting Harrisonburg
|
||||||
|
to Charlottesville. There are many different bus routes each of which travels
|
||||||
|
between named stops along its route. Not all of the bus routes cross each other,
|
||||||
|
so it is possible that there are some pairs of stops where there is no way of
|
||||||
|
getting on a bus at the first stop and making changes to get to the final stop.
|
||||||
|
Your task is to implement a graph search function that can determine a sequence
|
||||||
|
of stops a user can use to get from a starting stop to an ending stop, or to
|
||||||
|
tell the user that there is no bus route possible between those two stops.
|
||||||
|
|
||||||
|
## Input
|
||||||
|
|
||||||
|
The bus route segments are described as single stretches of road between two
|
||||||
|
consecutive stops. All such pairs will be described in one direction, but all
|
||||||
|
bus routes are bidirectional, since buses travel in both directions along each
|
||||||
|
route.
|
||||||
|
|
||||||
|
The first line of input has an integer n that is the number of road segments
|
||||||
|
between consecutive stops. The next n lines of input describe the consecutive
|
||||||
|
stops, each of which is named by a string of alphabetical characters with no
|
||||||
|
spaces. The line will contain both of the stops of the segment, separated by a
|
||||||
|
space.
|
||||||
|
|
||||||
|
The final line of input is the query, which is two stop names separated by a
|
||||||
|
space.
|
||||||
|
|
||||||
|
## Output
|
||||||
|
|
||||||
|
Give a sequence of stop names starting at the starting stop of the query and
|
||||||
|
ending at the ending stop separated by spaces (if one exists), or output "no
|
||||||
|
route possible" if none is found.
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<td>Sample Input</td>
|
||||||
|
<td>Sample Output</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><pre>
|
||||||
|
2
|
||||||
|
Hburg JMU
|
||||||
|
Charlottesville JMU
|
||||||
|
Hburg Charlottesville</pre></td>
|
||||||
|
<td><pre>Hburg JMU Charlottesville</pre></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><pre>
|
||||||
|
4
|
||||||
|
Hburg JMU
|
||||||
|
JMU Upark
|
||||||
|
Upark Hburg
|
||||||
|
CVO UVA
|
||||||
|
Hburg UVA</pre></td>
|
||||||
|
<td><pre>no route possible</pre></td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
Larger example files (the one used on Gradescope) can be downloaded here:
|
||||||
|
bus_stops_t3_in.txt Download bus_stops_t3_in.txt bus_stops_t3_exp.txt Download
|
||||||
|
bus_stops_t3_exp.txt Depending how you implement your graph, the order of stops
|
||||||
|
on the path may not be unique, so, checking your solution requires writing a bit
|
||||||
|
more code to validate your path (and of course, that is optional).
|
||||||
|
|
||||||
|
## Hints:
|
||||||
|
|
||||||
|
- Store the stops in a dictionary and use a set for the edge list.
|
||||||
|
- You will need to implement a method that uses a spanning tree. The lab is
|
||||||
|
asking you for a path between the locations (not necessarily the shortest
|
||||||
|
path).
|
||||||
|
|
||||||
|
## Submission:
|
||||||
|
|
||||||
|
Your code in a file name `cs412_bus_stops_a.py` and submit your code to
|
||||||
|
gradescope.
|
||||||
|
|
||||||
|
## Rubrics:
|
||||||
|
|
||||||
|
- (6 points) program solves the examples
|
||||||
|
- (3 points) solves the hidden examples
|
||||||
|
- (1 point) instructor review
|
||||||
47
Graph-Search/cs412_bus_stops_a.py
Normal file
47
Graph-Search/cs412_bus_stops_a.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
"""
|
||||||
|
name: Nicholas Tamassia
|
||||||
|
|
||||||
|
Honor Code and Acknowledgments:
|
||||||
|
|
||||||
|
This work complies with the JMU Honor Code.
|
||||||
|
|
||||||
|
Comments here on your code and submission.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
|
||||||
|
def whatever_first_search(
|
||||||
|
graph: dict[str, set[str]], start: str, stop: str
|
||||||
|
) -> list[str]:
|
||||||
|
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
# All modules for CS 412 must include a main method that allows it
|
||||||
|
# to imported and invoked from other python scripts
|
||||||
|
def main():
|
||||||
|
n: int = int(input())
|
||||||
|
|
||||||
|
graph: dict[str, set[str]] = {}
|
||||||
|
|
||||||
|
for _ in range(0, n):
|
||||||
|
u, v = input().split()
|
||||||
|
|
||||||
|
if u not in graph:
|
||||||
|
graph[u] = set([v])
|
||||||
|
else:
|
||||||
|
graph[u].add(v)
|
||||||
|
|
||||||
|
target = input().split()
|
||||||
|
|
||||||
|
path = whatever_first_search(graph, target[0], target[1])
|
||||||
|
|
||||||
|
if len(path) == 0:
|
||||||
|
print("no route possible")
|
||||||
|
else:
|
||||||
|
print(" ".join(path))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
4
Graph-Search/inputs/sample1.txt
Normal file
4
Graph-Search/inputs/sample1.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
2
|
||||||
|
Hburg JMU
|
||||||
|
Charlottesville JMU
|
||||||
|
Hburg Charlottesville
|
||||||
6
Graph-Search/inputs/sample2.txt
Normal file
6
Graph-Search/inputs/sample2.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
4
|
||||||
|
Hburg JMU
|
||||||
|
JMU Upark
|
||||||
|
Upark Hburg
|
||||||
|
CVO UVA
|
||||||
|
Hburg UVA
|
||||||
68419
Graph-Search/inputs/sample3.txt
Normal file
68419
Graph-Search/inputs/sample3.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user