Exemplo n.º 1
0
def test_add_edge_true():
    g = Graph()
    a = g.add_node('a')
    b = g.add_node('b')
    g.add_edge(a, b)

    assert True
Exemplo n.º 2
0
def test_graph_nodes_list():
    graph = Graph()
    a = graph.add_node("a")
    b = graph.add_node("b")
    actual = graph.get_nodes()
    expected = [a, b]
    assert actual == expected
Exemplo n.º 3
0
def test_size():
    graph = Graph()
    a = graph.add_node('a')
    b = graph.add_node('b')
    expected = 2
    actual = graph.size()
    assert actual == expected
Exemplo n.º 4
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
def build_word_ladder_graph(words_file):
    buckets = {}
    word_graph = Graph()

    with open(words_file) as file:
        for line in file:
            # Remove CRLF chars
            word = line[:-1]

            # For every word, construct a bucket, if bucket exists, then
            # add word to list
            for i in range(len(word)):
                bucket = f'{word[:i]}_{word[i + 1:]}'
                if bucket in buckets:
                    buckets[bucket].append(word)
                else:
                    buckets[bucket] = [word]

    # For every bucket, make edges between all words in each bucket
    for bucket in buckets.keys():
        for word in buckets[bucket]:
            for other_word in buckets[bucket]:
                if word != other_word:
                    word_graph.add_edge(word, other_word)

    return word_graph
Exemplo n.º 6
0
def test_add_node_returns_vertex():

    graph = Graph()

    vertex = graph.add_node("spam")

    assert isinstance(vertex, Vertex)
Exemplo n.º 7
0
def test_get_nodes():
    g = Graph()
    g.add_node('a')
    g.add_node('b')
    expected = ['a', 'b']
    actual = []
    [actual.append(item.value) for item in g.get_nodes()]
    assert actual == expected
Exemplo n.º 8
0
def test_repr():
    g = Graph()
    g.add_node('a')
    g.add_node('b')

    actual = g
    expected = 'a : [],b : [] '
    assert str(actual) == expected
Exemplo n.º 9
0
def test_size_empty():

    graph = Graph()

    expected = 0

    actual = graph.size()

    assert actual == expected
Exemplo n.º 10
0
def test_add_node_return_has_correct_value():

    graph = Graph()

    expected = "spam"  # a vertex's value that comes back

    actual = graph.add_node("spam").value

    assert actual == expected
Exemplo n.º 11
0
def test_get_neighbors_none():

    graph = Graph()

    banana = graph.add_node("banana")

    neighbors = graph.get_neighbors(banana)

    assert len(neighbors) == 0
Exemplo n.º 12
0
 def setUp(self):
     self.graph = Graph(directed=True, cyclic=False)
     self.graph.addEdge('A', 'C', 1)
     self.graph.addEdge('A', 'D', 2)
     self.graph.addEdge('B', 'D', 3)
     self.graph.addEdge('B', 'E', 2)
     self.graph.addEdge('D', 'F', 3)
     self.graph.addEdge('D', 'G', 4)
     self.graph.addEdge('G', 'I', 2)
     self.graph.addEdge('H', 'I', 3)
Exemplo n.º 13
0
def test_add_edge_interloper_end():

    graph = Graph()

    end = Vertex("end")

    start = graph.add_node("start")

    with pytest.raises(KeyError):
        graph.add_edge(start, end)
Exemplo n.º 14
0
def test_size_one():

    graph = Graph()

    graph.add_node("spam")

    expected = 1

    actual = graph.size()

    assert actual == expected
Exemplo n.º 15
0
def build_knights_graph(chess_board_size):
    knights_graph = Graph()

    for row in range(chess_board_size):
        for col in range(chess_board_size):
            node_id = pos_to_node_id(row, col, chess_board_size)
            new_positions = generate_legal_moves(row, col, chess_board_size)

            for pos in new_positions:
                new_node_id = pos_to_node_id(pos[0], pos[1], chess_board_size)
                knights_graph.add_edge(node_id, new_node_id)

    return knights_graph
def test_empty_graph():
    graph = Graph()
    cities = []
    actual_connected, actual_weight = get_edge(graph, cities)
    expected_connected, expected_weight = False, 0
    assert actual_connected == expected_connected
    assert actual_weight == expected_weight
Exemplo n.º 17
0
def test_graph_gets_neighbors():
    graph = Graph()
    node = graph.add_node('node')
    node_two = graph.add_node('node_two')
    graph.add_edge(node, node_two)
    neighbor = graph.get_neighbor(node)
    neighbor_two = neighbor[0]
    assert neighbor_two.vertex.value == 'node_two'
    assert len(neighbor) == 1
Exemplo n.º 18
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
Exemplo n.º 19
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
Exemplo n.º 20
0
def test_add_edge_sunny():

    graph = Graph()

    start = graph.add_node("start")

    end = graph.add_node("end")

    graph.add_edge(start, end)
Exemplo n.º 21
0
def test_get_nodes():

    graph = Graph()

    graph.add_node("banana")

    graph.add_node("apple")

    expected = 2

    actual = len(graph.get_nodes())

    assert actual == expected
Exemplo n.º 22
0
def test_size_two():

    graph = Graph()

    graph.add_node("spam")

    graph.add_node("bacon")

    expected = 2

    actual = graph.size()

    assert actual == expected
Exemplo n.º 23
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
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}')
Exemplo n.º 25
0
def test_add_edge_with_weight():

    graph = Graph()

    start = graph.add_node("start")

    end = graph.add_node("end")

    weight = 10

    graph.add_edge(start, end, weight)
Exemplo n.º 26
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
Exemplo n.º 27
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
Exemplo n.º 28
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'
Exemplo n.º 29
0
 def test_proof_of_life(self):
     graph = Graph()
     assert graph
Exemplo n.º 30
0
 def graph(self):
     return Graph()