Exemplo n.º 1
0
def test_cyclic_graph_no_path():
    graph = Graph()
    nodes = [["a","b"],["b","a"],["c"]]
    graph.make_unweighted_from_list(nodes)
    expected_path = []
    actual_path = graph.depth_first_search(start=Node("a"), target=Node("c"))
    assert actual_path == expected_path
Exemplo n.º 2
0
def test_unweighted_remove_node_node():
    graph = Graph()
    nodes = [["a","b"],["b","c"],["a","c"],["c","a"]]
    graph.make_unweighted_from_list(nodes)
    graph.remove_node(Node("a"))
    test_graph = {Node("b"):[Edge("b","c")], Node("c"):[]}
    assert compare_graphs(test_graph, graph.graph) == True
Exemplo n.º 3
0
def test_cyclic_graph():
    graph = Graph()
    nodes = [["a", "b"], ["b", "c"], ["c", "d"], ["d", "e"], ["d", "a"],
             ["a", "d"], ["e", "z"], ["z", "a"]]
    graph.make_unweighted_from_list(nodes)
    with raises(CyclicGraphException):
        actual_path = graph.topological_sort()
Exemplo n.º 4
0
def test_with_disconnected_nodes():
    graph = Graph()
    nodes = [["a"], ["b"]]
    graph.make_unweighted_from_list(nodes)
    actual_path = graph.dijkstras_with_target(start=Node("a"), target=Node("b"))
    expected_path = []
    assert actual_path == expected_path
Exemplo n.º 5
0
def test_cyclic_graph():
    graph = Graph()
    nodes = [["a","b"], ["b","c"], ["b", "a"], ["b", "z"], ["c", "d"], ["c", "e"], ["e", "a"]]
    graph.make_unweighted_from_list(nodes)
    expected_path = ["a", "b", "c", "e"]
    actual_path = graph.dijkstras_with_target(start=Node("a"), target=Node("e"))
    assert actual_path == expected_path
Exemplo n.º 6
0
def test_cyclic_graph_large():
    graph = Graph()
    nodes = [["a", "b"], ["b", "c"], ["c", "d"], ["d", "e"], ["d", "a"],
             ["a", "d"], ["e", "z"], ["z", "a"]]
    graph.make_unweighted_from_list(nodes)
    expected_path = ["a", "d", "e", "z"]
    actual_path = graph.breadth_first_search(start=Node("a"), target=Node("z"))
    assert actual_path == expected_path
Exemplo n.º 7
0
def test_weighted_list_of_int():
    graph = Graph()
    nodes = [[1, 2, 99], [2, 3, 26], [3, 5, 130], [5, 1, 2], [2, 5, 0]]
    graph.make_unweighted_from_list(nodes)
    test_graph = {
                 Node(1):[Edge(1,2,99)],
                 Node(2):[Edge(2,3,26), Edge(2,5,0)],
                 Node(3):[Edge(3,5,130)],
                 Node(5):[Edge(5,1,2)]}
    assert compare_graphs(test_graph, graph.graph) == True
Exemplo n.º 8
0
def test_unweighted_directed():
    graph = Graph()
    nodes = [["a","b"],["b","c"],["a","c"]]
    graph.make_unweighted_from_list(nodes)
    test_graph = {
                 Node("a"):[Edge("a","b"),Edge("a","c")],
                 Node("b"):[Edge("b","c")],
                 Node("c"):[]
                 }
    assert compare_graphs(test_graph, graph.graph) == True
Exemplo n.º 9
0
def test_unweighted_list_of_int():
    graph = Graph()
    nodes = [[1, 2], [2, 3], [3, 5], [5, 1], [2,5]]
    graph.make_unweighted_from_list(nodes)
    test_graph = {
                 Node(1):[Edge(1,2)],
                 Node(2):[Edge(2,3), Edge(2,5)],
                 Node(3):[Edge(3,5)],
                 Node(5):[Edge(5,1)]}
    assert compare_graphs(test_graph, graph.graph) == True
Exemplo n.º 10
0
def test_unweighted_edges_set():
    test_graph = {
                 Node("a"):[Edge("a","b"),Edge("a","c")],
                 Node("b"):[Edge("b","c")],
                 Node("c"):[]
                 }
    graph = Graph()
    nodes = [["a","b"],["b","c"],["a","c"]]
    graph.make_unweighted_from_list(nodes)
    for node in graph.graph:
        assert graph.graph[node] == test_graph[node.name]
Exemplo n.º 11
0
def test_reset_visited():
    nodes = [["a","b"]]
    graph = Graph()
    graph.make_unweighted_from_list(nodes)
    graph.reset_visited()
    new_graph = Graph()
    new_graph.make_unweighted_from_list(nodes)
    assert compare_graphs(new_graph.graph, graph.graph) == True
    for node in graph.graph:
        node.visited = True
    assert compare_graphs(new_graph.graph, graph.graph) == False
Exemplo n.º 12
0
def test_unweighted_add_node():
    # make the same graph as one of the ones above
    # add a node and assert its equal
    graph = Graph()
    nodes = [["a","b"],["b","c"],["a","c"]]
    graph.make_unweighted_from_list(nodes)
    graph.add_node(["b","b"])
    test_graph = {
                  Node("a"):[Edge("a","b"),Edge("a","c")],
                  Node("b"):[Edge("b","c"),Edge("b","b")],
                  Node("c"):[]
                 }
    assert compare_graphs(test_graph, graph.graph) == True
Exemplo n.º 13
0
def test_strongly_connected_graph():
    graph = Graph()
    nodes = []
    nodes_to_add = set(["a","b","c","d","e","f","g","h","i"])
    for node in nodes_to_add:
        for other_node in nodes_to_add:
            if other_node != node:
                nodes.append([node,other_node])
    graph = Graph()
    graph.make_unweighted_from_list(nodes, directed=False)
    actual_path = graph.dijkstras_with_target(start = Node("f"), target = Node("i"))
    expected_path = ["f","i"]
    assert actual_path == expected_path
Exemplo n.º 14
0
def test_add_single_node():
    nodes = [["a"]]
    graph = Graph()
    graph.make_unweighted_from_list(nodes)
    test_graph = {Node("a"):[]}
    assert compare_graphs(test_graph, graph.graph)
Exemplo n.º 15
0
def get_default_graph(directed=True):
    graph = Graph()
    nodes = [["a","b"],["a","c"],["a","d"],["b","e"],["b","d"],["c","f"]]
    graph.make_unweighted_from_list(nodes, directed)
    return graph