Beispiel #1
0
weights = {
    (0, 1): 3,
    (1, 7): 4,
    (7, 2): 2,
    (2, 5): 1,
    (5, 6): 8,
    (0, 3): 2,
    (3, 2): 6,
    (3, 4): 1,
    (4, 8): 8,
    (8, 0): 4
}
vertex_values = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

weighted_graph = WeightedGraph(weights, vertex_values)
weighted_graph.build_from_edges()
'''
assert weighted_graph.calc_distance(8,4) == 7
assert [weighted_graph.calc_distance(8,n) for n in range(9)] == [
    4, 7, 12, 6, 7, 13, 21, 11, 0]

print("PASSED")
'''

print("Asserting calc_shortest_path for WeightedGraph class")
assert weighted_graph.calc_shortest_path(8, 4) == [8, 0, 3, 4]
assert weighted_graph.calc_shortest_path(8, 7) == [8, 0, 1, 7]
assert weighted_graph.calc_shortest_path(8, 6) == [8, 0, 3, 2, 5, 6]
print("PASSED")
    (0,1): 3,
    (1,7): 4,
    (7,2): 2,
    (2,5): 1,
    (5,6): 8,
    (0,3): 2,
    (3,2): 6,
    (3,4): 1,
    (4,8): 8,
    (8,0): 4
}
vertex_values = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
weighted_graph = WeightedGraph(weights, vertex_values)

print('Does the weighted_graphs calc_distance work?')

assert weighted_graph.calc_distance(8,4) == 7, 'No'

assert [weighted_graph.calc_distance(8,n) for n in range(9)] == [4, 7, 12, 6, 7, 13, 21, 11, 0], 'Not with multiple'

print('Yes it does', "\n")

print('Does calc_shortest_path work?')

assert weighted_graph.calc_shortest_path(8,4) == [8, 0, 3, 4], '(8, 4) doesnt work'

assert weighted_graph.calc_shortest_path(8,7) == [8, 0, 1, 7], '(8, 7) doesnt work'

assert weighted_graph.calc_shortest_path(8,6) == [8, 0, 3, 2, 5, 6], '(8, 6) doesnt work'

print('Yes it does', "\n")
Beispiel #3
0
    (7,2): 2,
    (2,5): 1,
    (5,6): 8,
    (0,3): 2,
    (3,2): 6,
    (3,4): 1,
    (4,8): 8,
    (8,0): 4
}
vertex_values = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']


print('\nTesting...\n')

weighted_graph_1 = WeightedGraph(weights, vertex_values)
shortest_path_1 = weighted_graph_1.calc_shortest_path(8,4)
print("    Testing Weighted Graph 2's calc_shortest_path")
assert [node.index for node in shortest_path_1] == [8, 0, 3, 4], " Weighted Graph 1's calc_shortest_path was not right, it should be [8, 0, 3, 4], but was {}".format([node.index for node in shortest_path_1])
print("    Weighted Graph 1's row_indices Passed!!!\n")

weighted_graph_2 = WeightedGraph(weights, vertex_values)
shortest_path_2 = weighted_graph_2.calc_shortest_path(6,8)
print("    Testing Weighted Graph 2's calc_shortest_path")
assert [node.index for node in shortest_path_2] == [6, 5, 2, 3, 0, 8], " Weighted Graph 2's calc_shortest_path was not right, it should be [6, 5, 2, 3, 0, 8], but was {}".format([node.index for node in shortest_path_2])
print("    Weighted Graph 2's row_indices Passed!!!\n")

weighted_graph_3 = WeightedGraph(weights, vertex_values)
shortest_path_3 = weighted_graph_3.calc_shortest_path(7,4)
print("    Testing Weighted Graph 3's calc_shortest_path")
assert [node.index for node in shortest_path_3] == [7, 2, 3, 4], " Weighted Graph 3's calc_shortest_path was not right, it should be [7, 2, 3, 4], but was {}".format([node.index for node in shortest_path_3])
print("    Weighted Graph 3's row_indices Passed!!!\n")
Beispiel #4
0
    (1, 7): 4,
    (7, 2): 2,
    (2, 5): 1,
    (5, 6): 8,
    (0, 3): 2,
    (3, 2): 6,
    (3, 4): 1,
    (4, 8): 8,
    (8, 0): 4
}

vertex_values = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
weighted_graph = WeightedGraph(weights, vertex_values)

dist = weighted_graph.calc_distance(8, 4)

assert dist == 7, ('WRONG DIST:', dist, 'SHOULD BE 7')

assert [weighted_graph.calc_distance(8, n) for n in range(9)
        ] == [4, 7, 12, 6, 7, 13, 21, 11,
              0], ('WRONG VALUES:',
                   [weighted_graph.calc_distance(8, n) for n in range(9)
                    ], 'SHOULD BE [4, 7, 12, 6, 7, 13, 21, 11, 0]')

assert weighted_graph.calc_shortest_path(8, 4) == [8, 0, 3, 4], 'BRUH'

assert weighted_graph.calc_shortest_path(8, 7) == [8, 0, 1, 7], 'SECOND BRUH'

assert weighted_graph.calc_shortest_path(8, 6) == [8, 0, 3, 2, 5,
                                                   6], 'THIRD BRUH'