Beispiel #1
0
def test_graph_neighbors_one_node_one_edge():
    graph = Graph()
    a = graph.add_node("a")
    edge_a = graph.add_edge(a, a)
    actual = graph.get_neighbors(a)
    expected = [edge_a]
    assert actual == expected
Beispiel #2
0
def test_get_neigh():
    g = Graph()
    a = g.add_node('a')
    b = g.add_node('b')
    g.add_edge(a, b)
    actual = g.get_neighbors(a)
    expected = [('b', 1)]
    assert actual == expected
Beispiel #3
0
def test_one_node_one_edge():
    g = Graph()
    a = g.add_node('a')
    g.add_edge(a, a)
    actual = g.get_neighbors(a)
    expected = [('a', 1)]

    assert actual == expected
Beispiel #4
0
def test_get_neighbors_none():

    graph = Graph()

    banana = graph.add_node("banana")

    neighbors = graph.get_neighbors(banana)

    assert len(neighbors) == 0
Beispiel #5
0
def test_graph_neighbors():
    graph = Graph()
    a = graph.add_node("a")
    b = graph.add_node("b")
    c = graph.add_node("c")
    edge_ab = graph.add_edge(a, b)
    edge_ac = graph.add_edge(a, c)
    actual = graph.get_neighbors(a)
    expected = [edge_ab, edge_ac]
    assert actual == expected
Beispiel #6
0
def test_get_neighbors_returns_edges_with_custom_weight():

    graph = Graph()

    banana = graph.add_node("banana")

    apple = graph.add_node("apple")

    graph.add_edge(apple, banana, 44)

    neighbors = graph.get_neighbors(apple)

    neighbor_edge = neighbors[0]

    assert neighbor_edge.weight == 44
def get_edges(route_map: Graph, itinerary: list) -> tuple:
    """Determine if the given itinerary is possible with direct flights and how much it will cost

    Args:
        route_map (Graph): Map of the existing cities
        itinerary (list): List of cities

    Returns:
        tuple: Wheter or not the trip is possible with direct fligts only, total trip price
    """
    def get_node_reference(city: any) -> Vertex:
        """Get a node reference that matches provided value

        Args:
            city (any): Value to look for

        Returns:
            Vertex: Reference to the vertex
        """
        for node in route_map.get_nodes():
            if node.val == city:
                return node
        return None

    if len(itinerary) < 2:
        raise ValueError('You must provide at least 2 cities')

    possible, price = True, 0
    start_city = get_node_reference(itinerary[0])

    for i in range(1, len(itinerary)):
        end_city = itinerary[i]
        connections = route_map.get_neighbors(start_city)

        for connection in connections:
            if connection['vertex'].val == end_city:
                price += connection['weight']
                start_city = get_node_reference(end_city)
                possible = True
                break
            else:
                possible = False

        if not possible:
            return (False, f'${0}')

    return (True, f'${price}')
Beispiel #8
0
def test_get_neighbors_returns_edges_with_default_weight():

    graph = Graph()

    banana = graph.add_node("banana")

    apple = graph.add_node("apple")

    graph.add_edge(apple, banana)

    neighbors = graph.get_neighbors(apple)

    actual = neighbors[0].weight

    expected = 0

    assert actual == expected
Beispiel #9
0
def test_get_neighbors_returns_edges():

    graph = Graph()

    banana = graph.add_node("banana")

    apple = graph.add_node("apple")

    graph.add_edge(apple, banana)

    neighbors = graph.get_neighbors(apple)

    assert len(neighbors) == 1

    neighbor = neighbors[0]

    assert isinstance(neighbor, Edge)

    assert neighbor.vertex.value == 'banana'