Exemplo n.º 1
0
def test_large_vs_known_implementation():
    """
    Creates a large random graph and runs dijkstra's algorithm with this implementation
    as well as a known good implementation I grabbed from github. Compares that the results
    returned are the same
    """
    import random
    size = 1000
    my_graph = Graph()
    my_nodes = []
    other_graph = OtherGraph()
    for i in range(size):
        start_node = i
        end_node = random.randint(0, size)
        length = random.randrange(0, 10000)
        my_nodes.append([start_node, end_node, length])
        other_graph.add_edge(start_node, end_node, length)
    
    my_graph.make_weighted_from_list(my_nodes)
    my_prev, my_unvisited = my_graph.dijkstra(start=Node(0))
    other_visited, other_prev = known_dijkstras_implementation(other_graph, 0)
    unvisited_diff = my_unvisited.keys() - other_visited.keys()
    visited_diff = other_visited.keys() - my_unvisited.keys()
    
    assert my_prev == other_prev and my_unvisited.keys() == unvisited_diff and other_visited.keys() == visited_diff