Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def test_averageDegree(graph):
    from part2_week1.graph import Graph
    assert Graph.averageDegree(graph) == 2
Ejemplo n.º 6
0
def test_maxDegree(graph):
    from part2_week1.graph import Graph
    assert Graph.maxDegree(graph) == 3
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
def graph(edges):
    from part2_week1.graph import Graph
    graph = Graph(5)
    for v, w in edges:
        graph.addEdge(v, w)
    return graph