Question: Module 1 1 Homework - Graphs Overview Implement data structure dependent methods for a directed, non - weighted graph in AdjacencySetGraph and EdgeSetGraph classes. Then
Module Homework Graphs
Overview
Implement data structure dependent methods for a directed, nonweighted graph in AdjacencySetGraph
and EdgeSetGraph classes. Then implement a parent class Graph with methods that are independent of the
underlying data structures:
The Graph class is a convenient way to factor out common functionality, but should not be used on its own
users should specify an AdjacencySetGraph or EdgeSetGraph. We explicitly raise a NotImplementedError
in Graph.init to ensure this.
AdjacencySetGraph
Store a dictionary of vertex:setvertices pairs, to allow fast iteration over neighbors.
Inherits from Graph
initV E initialize a graph with a set of vertices and a set of edges well use tuples as edges,
so E should be a set of tuples Both parameters should be optional a user should be able to use asg
AdjacencySetGraph to create an empty graph.
iter returns an iterator over all vertices in the graph
addvertexv adds vertex to graph
addedgeu v adds edge u v to graph
neighborsv returns an iterator over all outneighbors of v
EdgeSetGraph
Adhere to and implement all of the above bullets, but use an edge set instead of an adjacency set store a set
of vertices and a set of edges instead of a dictionary of vertex:setneighbors pairs
Graph Methods
Methods whose implementations that do not depend on the underlying data structure though the running
times could differ are factored out here.
connectedv v returns True False if there is is not a path from v to v
bfsv returns a valid breadthfirst search tree see below for details
Be careful about efficiency here: use an efficient queue.
Note there could be multiple valid BFS trees based on the same graph. As long as your tree
represents a valid BFS traversal, you will receive full credit.
dfsv returns a valid depthfirst search tree see below for details
Be careful about efficiency here: use an efficient stack.
Note there could be multiple valid DFS trees based on the same graph. As long as your tree
represents a valid DFS traversal, you will receive full credit.
shortestpathv v returns a tuple of the minimum distance between v and v and a list
containing the edges of a minimal distance path from v to vthe edges should be in the proper order
to traverse from v to v If there is no path from v to v return floatinf None
hascycle returns a tuple of True and a list of edges that comprise a cycle if the graph has a cycle
the edges should be in the proper order to traverse the cycle completely Returns False None
otherwise.
BFSDFS Trees
For both bfs and dfs you must return a corresponding tree in a dictionary. Refer to lecture slides or Chapter
in the textbook for more info.
Some Example Behavior
V ABCDEF
E ABAC
BCBD
CE
DF
ED
FE
g AdjacencySetGraphV E
gconnectedAE
True
gbfsA
A: None, B: AC: AD: BE: CF: D
gdfsB
B: None, C: BE: CD: EF: D
ghascycle
TrueDFFEED
gshortestpathAF
ABBDDF
Imports
No imports allowed on this assignment, with the following exceptions:
Any modules you have written yourself
Queue from the queuepy file distributed with the starter code
Stack from the stackpy file distributed with the starter code
typing this is not required, but some students have requested it
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
