3) Implement the if __name__ == "__main__" : block at the bottom of this file to do the following: 3a) The if main block at the bottom should get the name of the highway graph file from the command line arguments.  See the video in Python that explains how to get command line arguments.  3b) Call your parse_highway_graph_file function to parse that file, and to construct a WeightedGraph object from it.  3c) Write some code that outputs (with print statements) the degree of each vertex. You can have one vertex per line, indicating its id (just its 0-based index) followed by its degree.  IMPORTANT: Your parse_highway_graph_file function should NOT produce any output. You will have code in your if main block that will have the print statements. The WeightedGraph class has a degree method, inherited from Graph that will return to you the degree of a vertex.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 16PE
icon
Related questions
Question

3) Implement the if __name__ == "__main__" : block at the bottom of this file to do the following:

3a) The if main block at the bottom should get the name of the highway graph file from the command line arguments.  See the video in Python that explains how to get command line arguments.

 3b) Call your parse_highway_graph_file function to parse that file, and to construct a WeightedGraph object from it.

 3c) Write some code that outputs (with print statements) the degree of each vertex. You can have one vertex per line, indicating its id (just its 0-based index) followed by its degree.  IMPORTANT: Your parse_highway_graph_file function should NOT produce any output. You will have code in your if main block that will have the print statements. The WeightedGraph class has a degree method, inherited from Graph that will return to you the degree of a vertex.

# 2) Implement the parse_highway_graph_file function.
#
# Specifically, it should be able to parse a file of the format
# from this set of graphs: http://tm.teresco.org/graphs/ or
# https://travelmapping.net/graphs/
#
# Details of the format itself can be found here:
#
http://courses.teresco.org/metal/graph-formats.shtml
#
# Only worry about the "simple" format (and NOT the "collapsed" format).
# You might start by looking at one of the smaller graphs to see what
# the format looks like, such as Andora.
#First line of all files is: TMG 1.0 simple
# Second line tells you number of vertices and number of edges: VE
# The next V lines provide one
vertex per line of
# the form: StringID latitude longitude
# You only really need the latitude and longitude values,
# and simply use 0 to V-1 as the vertex ids instead of the given strings.
# The next E lines provide the edges, one per line,
# of the form: from to aStringValue YouDontNeedForThisAssignment.
# You only need the from and to values, and not the 3rd value on the line.
# The from and to tell you the endpoints of an edge (0 based
#indices into the list of vertices).
# For each edge in this list add an edge to a WeightedGraph object.
# The WeightedGraph class is an undirected graph, so we're assuming that
#the roads in the highway graph data can be traversed in both directions.
# The weight for the edge should be the haversine distance
# (which you implemented a function to compute in Step 1).
# HINTS (for parsing input file) :
#Among the Python videos within Blackboard, I have some videos that
# are especially useful for this assignment, including getting
# input from a file. You can find additional Python file IO examples
# here: https://docs.python.org/3/tutorial/inputoutput.html
# Scroll to 7.2 and look at examples of open. Specifically, see example
# that uses "with" which has the advantage that Python will
# automatically close the file for you at the end of the with block, as
# I demonstrated in the Python videos within Blackboard.
#
# The read() method reads the entire file at once as a String.
# You will find it useful, however, to instead iterate over the lines
# of the file. You can either use the readline ()
#method directly for this. Or, see the example on that same page, and
# in the examples I did in the videos, that uses a loop of the form:
#for line in f (each iteration of this loop will automatically call
#readline () to get the next line of the file and loop will iterate
#over entire file). However, you will probably find it more useful to
# explicitly call readline () directly since you will probably have a
#loop to get the vertex latitudes and longitudes, and then a second
#loop to get the edge data, rather than a single loop for the entire file.
#
# Useful methods for parsing the input graph file:
# The split method for Strings (which I also demonstrated in a video):
# https://docs.python.org/3/library/stdtypes.html#string-methods
Transcribed Image Text:# 2) Implement the parse_highway_graph_file function. # # Specifically, it should be able to parse a file of the format # from this set of graphs: http://tm.teresco.org/graphs/ or # https://travelmapping.net/graphs/ # # Details of the format itself can be found here: # http://courses.teresco.org/metal/graph-formats.shtml # # Only worry about the "simple" format (and NOT the "collapsed" format). # You might start by looking at one of the smaller graphs to see what # the format looks like, such as Andora. #First line of all files is: TMG 1.0 simple # Second line tells you number of vertices and number of edges: VE # The next V lines provide one vertex per line of # the form: StringID latitude longitude # You only really need the latitude and longitude values, # and simply use 0 to V-1 as the vertex ids instead of the given strings. # The next E lines provide the edges, one per line, # of the form: from to aStringValue YouDontNeedForThisAssignment. # You only need the from and to values, and not the 3rd value on the line. # The from and to tell you the endpoints of an edge (0 based #indices into the list of vertices). # For each edge in this list add an edge to a WeightedGraph object. # The WeightedGraph class is an undirected graph, so we're assuming that #the roads in the highway graph data can be traversed in both directions. # The weight for the edge should be the haversine distance # (which you implemented a function to compute in Step 1). # HINTS (for parsing input file) : #Among the Python videos within Blackboard, I have some videos that # are especially useful for this assignment, including getting # input from a file. You can find additional Python file IO examples # here: https://docs.python.org/3/tutorial/inputoutput.html # Scroll to 7.2 and look at examples of open. Specifically, see example # that uses "with" which has the advantage that Python will # automatically close the file for you at the end of the with block, as # I demonstrated in the Python videos within Blackboard. # # The read() method reads the entire file at once as a String. # You will find it useful, however, to instead iterate over the lines # of the file. You can either use the readline () #method directly for this. Or, see the example on that same page, and # in the examples I did in the videos, that uses a loop of the form: #for line in f (each iteration of this loop will automatically call #readline () to get the next line of the file and loop will iterate #over entire file). However, you will probably find it more useful to # explicitly call readline () directly since you will probably have a #loop to get the vertex latitudes and longitudes, and then a second #loop to get the edge data, rather than a single loop for the entire file. # # Useful methods for parsing the input graph file: # The split method for Strings (which I also demonstrated in a video): # https://docs.python.org/3/library/stdtypes.html#string-methods
def parse_highway_graph_file
(filename) :
"""Parses a highway graph file and return a WeightedGraph
representing a highway graph.
if
Keyword arguments:
filename
graph data relative to the current working directory.
"" "
#
#
#
#
# Hint 1: There are a couple different ways of structuring your code.
#Here is one way:
You could parse the file first, generating
-
--
The name of the file containing the highway
pass
a list of the edges as tuples, and a list of the weights.
Once you have that you can then do something like:
g = WeightedGraph (v, edges, weights)
name
assuming that v is the number of vertices, edges is a Python
list of ordered-pairs (tuples with 2 components), and weights
is a list of the edge weights in the same order as the edges list.
#
#
#
# Hint 2: Here is a different way of structuring this:
#
You could parse the first couple lines to get the number of
vertices v.
#
#
The construct an initial graph with: g = WeightedGraph (v)
#
And then continue parsing the file calling add_edge once for each
edge.
#
#
# Obviously replace this return statement with what is needed
# to return the WeightedGraph object after you've constructed one.
# So just replace None below with whatever you named your graph variable.
return None
main
:
Transcribed Image Text:def parse_highway_graph_file (filename) : """Parses a highway graph file and return a WeightedGraph representing a highway graph. if Keyword arguments: filename graph data relative to the current working directory. "" " # # # # # Hint 1: There are a couple different ways of structuring your code. #Here is one way: You could parse the file first, generating - -- The name of the file containing the highway pass a list of the edges as tuples, and a list of the weights. Once you have that you can then do something like: g = WeightedGraph (v, edges, weights) name assuming that v is the number of vertices, edges is a Python list of ordered-pairs (tuples with 2 components), and weights is a list of the edge weights in the same order as the edges list. # # # # Hint 2: Here is a different way of structuring this: # You could parse the first couple lines to get the number of vertices v. # # The construct an initial graph with: g = WeightedGraph (v) # And then continue parsing the file calling add_edge once for each edge. # # # Obviously replace this return statement with what is needed # to return the WeightedGraph object after you've constructed one. # So just replace None below with whatever you named your graph variable. return None main :
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning