Ejemplo n.º 1
0
def instance():
    """Create an instance of Graph."""
    from weighted_graph import Graph
    inst = Graph()
    for edge in EDGES:
        inst.add_edge(*edge)
    return inst
def non_multi_connected_nodes_shortpath():
    g = Graph()
    g.add_edge("A", "B", 2)
    g.add_edge("A", "C", 2)
    g.add_edge("A", "E", 0)
    g.add_edge("B", "D", 0)
    g.add_edge("B", "F", 0)
    g.add_edge("C", "G", 2)
    g.add_edge("G", "B", 1)
    return g
Ejemplo n.º 3
0
def airports(file=None):
    """Return weighted graph of airports, destination_cities, and distances."""
    airport_dict = format_data(file)
    all_routes = Graph()
    for city in airport_dict:
        for destination in airport_dict[city]['destination_cities']:
            try:
                all_routes.add_edge(
                    city,
                    destination,
                    calculate_distance(airport_dict[city]['lat_lon'],
                                       airport_dict[destination]['lat_lon'])
                )
            except KeyError:
                continue
    return all_routes
def non_multi_connected_nodes():
    g = Graph()
    g.add_edge("A", "B")
    g.add_edge("A", "C")
    g.add_edge("A", "E")
    g.add_edge("B", "D")
    g.add_edge("B", "F")
    g.add_edge("C", "G")
    return g
def test_noeds():
    """
    Test the graph containes correct test_noeds
    """
    g = Graph()
    g.add_node('a')
    g.add_edge('a', 'b', 9)
    g.add_edge('a', 'e', 3)
    g.add_edge('b', 'c', 6)
    g.add_edge('b', 'e', 9000)
    g.add_edge('c', 'd', 6)
    g.add_edge('e', 'c', 5)
    g.add_edge('e', 'd', 1)
    g.add_edge('e', 'f', 3)
    g.add_edge('f', 'a', 8)
    assert 
def test_real_short():
    """
    Test with the shortest path having the lowest weight
    """
    g = Graph()
    g.add_node('a')
    g.add_edge('a', 'b', 9)
    g.add_edge('a', 'e', 3)
    g.add_edge('b', 'c', 6)
    g.add_edge('b', 'e', 9000)
    g.add_edge('c', 'd', 6)
    g.add_edge('e', 'c', 5)
    g.add_edge('e', 'd', 1)
    g.add_edge('e', 'f', 3)
    g.add_edge('f', 'a', 8)

    assert g.dijkstra('a', 'd') == (['a', 'e', 'd'], 4)
def test_real_long():
    """
    Test the longest path having the shortest weight
    """
    g = Graph()
    g.add_node('a')
    g.add_edge('a', 'b', 1)
    g.add_edge('a', 'e', 9000)
    g.add_edge('b', 'c', 1)
    g.add_edge('c', 'e', 1)
    g.add_edge('c', 'd', 6)
    g.add_edge('e', 'd', 3)
    g.add_edge('e', 'f', 1)
    g.add_edge('f', 'd', 1)

    assert g.dijkstra('a', 'd') == (['a', 'b', 'c', 'e', 'f', 'd'], 5)
    assert g.dijkstra('a', 'a') == (['a'], 0)
Ejemplo n.º 8
0
def empty_instance():
    """Create an instance of Graph."""
    from weighted_graph import Graph
    return Graph()
def empty_graph():
    return Graph()
def edges_graph():
    g = Graph()
    g.add_edge("A", "B", 2)
    return g
def non_empty_graph():
    g = Graph()
    g.add_node("A")
    return g
Ejemplo n.º 12
0
                new_distance = nearest_distance + edge.distance
                if distances_not_visited[edge.node] > new_distance:
                    distances_not_visited[edge.node] = new_distance
    return shortest_paths[end_node]


if __name__ == '__main__':
    node_u = GraphNode('U')
    node_d = GraphNode('D')
    node_a = GraphNode('A')
    node_c = GraphNode('C')
    node_i = GraphNode('I')
    node_t = GraphNode('T')
    node_y = GraphNode('Y')

    graph = Graph([node_u, node_d, node_a, node_c, node_i, node_t, node_y])
    graph.add_edge(node_u, node_a, 4)
    graph.add_edge(node_u, node_c, 6)
    graph.add_edge(node_u, node_d, 3)
    graph.add_edge(node_d, node_u, 3)
    graph.add_edge(node_d, node_c, 4)
    graph.add_edge(node_a, node_u, 4)
    graph.add_edge(node_a, node_i, 7)
    graph.add_edge(node_c, node_d, 4)
    graph.add_edge(node_c, node_u, 6)
    graph.add_edge(node_c, node_i, 4)
    graph.add_edge(node_c, node_t, 5)
    graph.add_edge(node_i, node_a, 7)
    graph.add_edge(node_i, node_c, 4)
    graph.add_edge(node_i, node_y, 4)
    graph.add_edge(node_t, node_c, 5)
Ejemplo n.º 13
0
    def getMinimum(self , src , edges):
        minimum = edges[0]
        if (minimum.nodes[0] == src):
            minimum = minimum.nodes[1]
        else:
            minimum = minimum.nodes[0]
        for edge in edges:
            nodeIndex = [i for i , x in enumerate(edge.nodes) if edge.nodes[i] != src][0]
            if edge.nodes[nodeIndex].distance < minimum.distance:
                minimum = edge.nodes[nodeIndex]
        return minimum




myGraph = Graph()
vertices = [Node("A") , Node("B") , Node("C") , Node("D") , Node("E") , Node("F")]

for vertex in vertices:
    myGraph.add_vertex(vertex)

edges = [
    #A -> B                               #A -> C                       #A -> D
    ((vertices[0] , vertices[1]) , 2) , ((vertices[0] , vertices[2]) , 5) , ((vertices[0] , vertices[3]) , 1) ,
    #B -> C                                #B -> D
    ((vertices[1] , vertices[2]) , 3) , ((vertices[1] , vertices[3]) , 2) ,
    #C -> D                                    #C -> E                       #C -> F
    ((vertices[2] , vertices[3]) , 3) , ((vertices[2] , vertices[4]) , 1) , ((vertices[2] , vertices[5]) , 5),
    #D -> E
    ((vertices[3] , vertices[4]) , 1),
    #E -> F
def test_nodes_non_empty(non_empty_graph):
    ''' Test node listing. '''
    g = Graph()
    g.add_node("B")
    # Test populated graph
    assert "A" and "B" in g.graph
Ejemplo n.º 15
0
def graph():
    """Return initialized graph."""
    from weighted_graph import Graph
    return Graph()