def test_numberOfSelfLoops(graph): from part2_week1.graph import Graph assert Graph.numberOfSelfLoops(graph) == 0 graph.addEdge(0, 0) assert Graph.numberOfSelfLoops(graph) == 1 graph.addEdge(4, 4) assert Graph.numberOfSelfLoops(graph) == 2
def dfs_bfs_graph(): from part2_week1.graph import Graph graph = Graph(11) for v, w in [(0, 1), (0, 2), (0, 6), (0, 5), (5, 3), (3, 4), (4, 6), (4, 5), (7, 8), (9, 10)]: graph.addEdge(v, w) return graph
def test_initiation(): from part2_week1.graph import Graph graph = Graph(3) with pytest.raises(TypeError): Graph(3.5) with pytest.raises(ValueError): Graph(-3) with pytest.raises(ValueError): Graph(0)
def test_add_edges(): from part2_week1.graph import Graph graph = Graph(3) assert graph.V == 3 graph.addEdge(0, 1) assert graph.E == 1 graph.addEdge(0, 2) assert graph.E == 2 graph.addEdge(1, 2) assert graph.E == 3 # add existing edge should not increase any count graph.addEdge(1, 2) assert graph.E == 3 assert graph.V == 3
def test_averageDegree(graph): from part2_week1.graph import Graph assert Graph.averageDegree(graph) == 2
def test_maxDegree(graph): from part2_week1.graph import Graph assert Graph.maxDegree(graph) == 3
def test_degree(graph): from part2_week1.graph import Graph assert Graph.degree(graph, 0) == 2 assert Graph.degree(graph, 2) == 3 with pytest.raises(ValueError): Graph.degree(graph, 5)
def graph(edges): from part2_week1.graph import Graph graph = Graph(5) for v, w in edges: graph.addEdge(v, w) return graph