Пример #1
0
def test_adding_duplicate_edges_to_a_weight_graph_adds_unique_edges(num):
    """Test that adding duplicate edges to the graph unique edges."""
    from weight_graph import Graph
    g = Graph()
    for x in range(num):
        g.add_edge(x % 5, x % 5 + 1, x + 1)
    assert len(g.edges()) == 5 if num > 5 else num
Пример #2
0
def test_edges_of_filled_weight_graph_has_all_edges(num):
    """Test that edges lists all the edges in a graph."""
    from weight_graph import Graph
    g = Graph()
    for x in range(num):
        g.add_edge(x, x + 1, x + 2)
    assert len(g.edges()) == num
Пример #3
0
def test_adding_unique_edges_to_a_weight_graph_adds_all_edges(num):
    """Test that adding unique edges to the weight graph adds all edges."""
    from weight_graph import Graph
    g = Graph()
    for x in range(num):
        g.add_edge(x, x + 1, x + 2)
    assert len(g.edges()) == num
Пример #4
0
def test_adjacent_returns_false_if_specific_pair_of_values_has_no_edge(num):
    """Test adjacent is false if pair of values given doesn't exist in graph as edge."""
    from weight_graph import Graph
    g = Graph()
    for x in range(num):
        g.add_edge(x, x + 1, x % 3 + 1)
    for x in range(num - 1):
        assert not g.adjacent(x, x + 2)
Пример #5
0
def test_adjacent_returns_true_if_specific_pair_of_values_given_exist(num):
    """Test adjacent is true if pair of values given exist in graph as edge."""
    from weight_graph import Graph
    g = Graph()
    for x in range(num):
        g.add_edge(x, x + 1, x % 3 + 1)
    for x in range(num):
        assert g.adjacent(x, x + 1)
Пример #6
0
def test_del_edge(node, result):
    """Test to check the deleted edges aren't there."""
    from weight_graph import Graph
    g = Graph()
    for idx in node:
        g.add_node(idx)
    g.add_edge(2, 3)
    g.add_edge(1, 4)
    assert g.del_edge(1, 4) == result
Пример #7
0
def test_edge(node, result):
    """Test to check if all edges are there."""
    from weight_graph import Graph
    g = Graph()
    for idx in node:
        g.add_node(idx)
    g.add_edge(2, 3)
    g.add_edge(1, 4)
    assert g.edges() == result
Пример #8
0
def test_neighbor(node, n, result):
    """Test to check that the correct node have the right edge."""
    from weight_graph import Graph
    g = Graph()
    for idx in node:
        g.add_node(idx)
    g.add_edge(1, 1)
    g.add_edge(1, 4)
    g.add_edge(4, 2)
    g.add_edge(3, 5)
    assert g.neighbors(n) == result
Пример #9
0
def complex_weight_graph():
    """Create a graph with interconnecting nodes and edges."""
    from weight_graph import Graph
    g = Graph()
    g.add_edge(0, 1, 4)
    g.add_edge(1, 0, 4)
    g.add_edge(0, 7, 8)
    g.add_edge(7, 0, 8)
    g.add_edge(1, 7, 11)
    g.add_edge(7, 1, 11)
    g.add_edge(7, 8, 7)
    g.add_edge(8, 7, 7)
    g.add_edge(7, 6, 1)
    g.add_edge(6, 7, 1)
    g.add_edge(1, 2, 8)
    g.add_edge(2, 1, 8)
    g.add_edge(2, 5, 4)
    g.add_edge(5, 2, 4)
    g.add_edge(2, 8, 2)
    g.add_edge(8, 2, 2)
    g.add_edge(2, 3, 7)
    g.add_edge(3, 2, 7)
    g.add_edge(8, 6, 6)
    g.add_edge(6, 8, 6)
    g.add_edge(6, 5, 2)
    g.add_edge(5, 6, 2)
    g.add_edge(5, 3, 14)
    g.add_edge(3, 5, 14)
    g.add_edge(5, 4, 10)
    g.add_edge(4, 5, 10)
    g.add_edge(3, 4, 9)
    g.add_edge(4, 3, 9)
    return g
Пример #10
0
from heapq import heappop, heappush


def dijkstra(graph, start, target):
    unique = count()
    visited = set()
    heap = [(0, unique, start, ())]
    while heap:
        weight, junk, node, path = heappop(heap)
        if node == target:
            return weight, path
        if node not in visited:
            visited.add(node)
            for neighbor, edge in graph[node].items():
                heappush(heap, (weight + edge, next(unique),
                         neighbor, (neighbor, path)))


if __name__ == '__main__':
    g = Graph()
    g.add_node('A')
    g.add_node('B')
    g.add_node('C')
    g.add_node('D')
    g.add_edge('A', 'B', 4)
    g.add_edge('A', 'C', 20)
    g.add_edge('B', 'D', 5)
    g.add_edge('C', 'D', 200)

    dijkstra(g, 'A', 'D')