Exemplo n.º 1
0
def test_dublicate_values_to_a_weight_graph_adds_some_nodes(num):
    """Test that adding duplicate values to the graph add only unique items."""
    from weight_graph import Graph
    g = Graph()
    for x in range(num):
        g.add_node(x % 5)
    assert len(g.nodes()) == 5 if num > 5 else num
Exemplo n.º 2
0
def test_has_node(node, n, result):
    """Test to check if a node exists in graph."""
    from weight_graph import Graph
    g = Graph()
    for idx in node:
        g.add_node(idx)
    assert g.has_node(n) == result
Exemplo n.º 3
0
def test_adding_unique_values_to_a_weight_graph_adds_all_nodes(num):
    """Test that adding unique values to the graph adds all of them."""
    from weight_graph import Graph
    g = Graph()
    for x in range(num):
        g.add_node(x)
    assert len(g.nodes()) == num
Exemplo n.º 4
0
def test_del_nodes(node, result):
    """Test to check the deleted nodes aren't there."""
    from weight_graph import Graph
    g = Graph()
    for idx in node:
        g.add_node(idx)
    assert g.del_node(1) == result
Exemplo n.º 5
0
def test_nodes(node, result):
    """Test to check if all nodes are there."""
    from weight_graph import Graph
    g = Graph()
    for idx in node:
        g.add_node(idx)
    assert g.nodes() == result
Exemplo n.º 6
0
def test_nodes_of_filled_weight_graph_has_all_nodes(num):
    """Test that nodes lists all the nodes in a graph."""
    from weight_graph import Graph
    g = Graph()
    for x in range(num):
        g.add_node(x)
    assert len(g.nodes()) == num
    assert sorted(g.nodes()) == list(range(num))
Exemplo n.º 7
0
def test_has_node_returns_true_if_node_serched_is_in_weight_graph(num):
    """Test that has node returns true if looking for node present in weight graph."""
    from weight_graph import Graph
    g = Graph()
    for x in range(num):
        g.add_node(x)
    for x in range(num):
        assert g.has_node(x)
Exemplo n.º 8
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
Exemplo n.º 9
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
Exemplo n.º 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')