Пример #1
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
Пример #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
Пример #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
Пример #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
Пример #5
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
Пример #6
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
Пример #7
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
Пример #8
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
Пример #9
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)
Пример #10
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)
Пример #11
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)
Пример #12
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
Пример #13
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
Пример #14
0
def dijkstra(graph, start, end):
    """Calculate the shortest path."""
    g = Graph(graph)
    final = {}
    # all_visited = []
    visited, d = [], deque([start])
    while d:
        vertex = d.pop()
        visited.append(vertex)
        if g.neighbors(vertex):
            d.extend(g.neighbors(vertex))
            continue

        # FIND THE WEIGHT OF THIS PATH
        try:
            weight = 0
            for idx in range(len(visited)):
                weight += int(g.weight(visited[idx], visited[idx + 1]))
        except IndexError:
            pass

        path = list(visited)
        final[weight] = path

        #
        print(visited)
        try:
            print(d)
            for item in range(len(visited)):
                if g.neighbors(visited[-item - 1]) not in d:
                    visited.pop()
                else:
                    break
        except IndexError:
            pass
        print(visited)

        break
    print('################')
    # print('WEIGHT: ' + str(weight))
    # print('FINAL PATH: ' + str(path))
    print(final)
Пример #15
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))
Пример #16
0
def dijkstra(graph, start, end):
    """Calculate the shortest path."""
    g = Graph(graph)
    final = {}
    # all_visited = []
    visited, d = [], deque([start])
    while d:
        vertex = d.pop()
        visited.append(vertex)
        if g.neighbors(vertex):
            d.extend(g.neighbors(vertex))
            continue

        # FIND THE WEIGHT OF THIS PATH
        try:
            weight = 0
            for idx in range(len(visited)):
                weight += int(g.weight(visited[idx], visited[idx + 1]))
        except IndexError:
            pass

        path = list(visited)
        final[weight] = path

        #
        print(visited)
        try:
            print(d)
            for item in range(len(visited)):
                if g.neighbors(visited[-item - 1]) not in d:
                    visited.pop()
                else:
                    break
        except IndexError:
            pass
        print(visited)

        break
    print('################')
    # print('WEIGHT: ' + str(weight))
    # print('FINAL PATH: ' + str(path))
    print(final)
Пример #17
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')
Пример #18
0
def test_empty_constructor_constructs_empty_weight_graph():
    """Test that a new graph is empty."""
    from weight_graph import Graph
    g = Graph()
    assert len(g.graph) == 0
Пример #19
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