Programming Assignment 2 - Testing for cycles
Due: Oct 1st at 5pm.
In this assignment you will program a graph class and a method for testing whether a given undirected graph has a cycle. Your algorithm should be based on BFS; use the adjacency list representation to implement the graph class and should run in O(m + n) time, where m is the number of edges of the graph and n is the number of nodes. We will look at the code to verify that your implementation satisfies this bound. This means that you can't use python dictionaries in the graph traversal part. You may use the python Queue class.
Your code should be structured as a single python file called HW2.py. We will call to test your code as:
g = HW2.load_dot(file_name)
# loading the graph
has_cycle = HW2.has_cycle(g)
# testing whether it has a cycle
# your function should return a Boolean value
The return value of your has_cycle function should be a Boolean value that indicates if the given graph has a cycle or not.
The graph is loaded from a file in dot format.
Here's an example of a graph in this format:
graph G {
n1 -- n2
n3 -- n4
n3 -- n2
n5 -- n2
}
Note that node names can be longer than a single character.
