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
Beispiel #2
0
def path_map():
    g = Graph()

    print(type(g))

    a = g.add_vertex('a')
    b = g.add_vertex("b")
    c = g.add_vertex("c")
    d = g.add_vertex("d")
    e = g.add_vertex("e")
    f = g.add_vertex("f")

    g.add_edge(a, c, 1)
    g.add_edge(c, a, 1)

    g.add_edge(b, c, 2)
    g.add_edge(c, b, 2)

    g.add_edge(b, e, 3)
    g.add_edge(e, b, 3)

    g.add_edge(c, d, 5)
    g.add_edge(d, c, 5)

    g.add_edge(d, e, 4)
    g.add_edge(e, d, 4)

    return g
Beispiel #3
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
Beispiel #4
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 #5
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 #6
0
def test_add_edge_sunny():

    graph = Graph()

    start = graph.add_node("start")

    end = graph.add_node("end")

    graph.add_edge(start, end)
Beispiel #7
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
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
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
Beispiel #11
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
Beispiel #12
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 #13
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 #14
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 #15
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'
def test_graph():
    graph = Graph()
    for num in range(6):
        graph.add_vertex(num)

    print(graph.vertices)

    graph.add_edge(0, 1, 5)
    graph.add_edge(0, 5, 2)
    graph.add_edge(1, 2, 4)
    graph.add_edge(2, 3, 9)
    graph.add_edge(3, 4, 7)
    graph.add_edge(3, 5, 3)
    graph.add_edge(4, 0, 1)
    graph.add_edge(5, 4, 8)
    graph.add_edge(5, 2, 1)

    for vertex in graph:
        for connection in vertex.get_connections():
            print(f"{vertex.get_id()} connected to {connection.get_id()}")
Beispiel #17
0
def test_graph_size_fail():
    graph = Graph()
    a = graph.add_node("a")
    b = graph.add_node("b")
    graph.add_edge(a, b)
    assert len(graph._adjacency_list) != 3
Beispiel #18
0
def test_add_edge_true():
    graph = Graph()
    a = graph.add_node("a")
    b = graph.add_node("b")
    graph.add_edge(a, b)
    assert True
def flights():
    graph = Graph()
    pandora = graph.add_node("pandora")
    narnia = graph.add_node("narnia")
    arendelle = graph.add_node("arendelle")
    metroville = graph.add_node("metroville")
    monstropolis = graph.add_node("monstropolis")
    naboo = graph.add_node("naboo")
    graph.add_edge(pandora, arendelle, 150)
    graph.add_edge(pandora, metroville, 82)
    graph.add_edge(narnia, metroville, 37)
    graph.add_edge(narnia, naboo, 250)
    graph.add_edge(metroville, pandora, 82)
    graph.add_edge(metroville, arendelle, 99)
    graph.add_edge(metroville, monstropolis, 105)
    graph.add_edge(metroville, naboo, 26)
    graph.add_edge(metroville, narnia, 37)
    graph.add_edge(arendelle, pandora, 150)
    graph.add_edge(arendelle, metroville, 99)
    graph.add_edge(arendelle, monstropolis, 42)
    graph.add_edge(monstropolis, arendelle, 42)
    graph.add_edge(monstropolis, metroville, 105)
    graph.add_edge(monstropolis, naboo, 73)
    graph.add_edge(naboo, monstropolis, 73)
    graph.add_edge(naboo, metroville, 26)
    graph.add_edge(naboo, narnia, 250)
    return {
        "graph": graph,
        "pandora": pandora,
        "narnia": narnia,
        "arendelle": arendelle,
        "metroville": metroville,
        "monstropolis": monstropolis,
        "naboo": naboo,
    }
Beispiel #20
0
    def graph(self):
        graph = Graph()

        # Create nodes
        vertex_1 = graph.add_vertex(1)
        vertex_2 = graph.add_vertex(2)
        vertex_3 = graph.add_vertex(3)
        vertex_4 = graph.add_vertex(4)
        vertex_5 = graph.add_vertex(5)
        vertex_6 = graph.add_vertex(6)

        # Create edges
        graph.add_edge(vertex_1, vertex_2, 150)
        graph.add_edge(vertex_2, vertex_1, 150)
        graph.add_edge(vertex_1, vertex_3, 82)
        graph.add_edge(vertex_3, vertex_1, 82)
        graph.add_edge(vertex_2, vertex_3, 99)
        graph.add_edge(vertex_3, vertex_2, 99)
        graph.add_edge(vertex_2, vertex_4, 42)
        graph.add_edge(vertex_4, vertex_2, 42)
        graph.add_edge(vertex_3, vertex_4, 105)
        graph.add_edge(vertex_4, vertex_3, 105)
        graph.add_edge(vertex_3, vertex_5, 37)
        graph.add_edge(vertex_5, vertex_3, 37)
        graph.add_edge(vertex_3, vertex_6, 26)
        graph.add_edge(vertex_6, vertex_3, 26)
        graph.add_edge(vertex_5, vertex_6, 250)
        graph.add_edge(vertex_6, vertex_5, 250)
        graph.add_edge(vertex_4, vertex_6, 73)
        graph.add_edge(vertex_6, vertex_4, 73)

        return graph
Beispiel #21
0
def test_breadth_first():

    g = Graph()

    pandora = g.add_node("Pandora")

    arendelle = g.add_node("Arendelle")

    metroville = g.add_node("Metroville")

    monstropolis = g.add_node("Monstropolis")

    narnia = g.add_node("Narnia")

    naboo = g.add_node("Naboo")

    g.add_edge(pandora, arendelle)
    g.add_edge(arendelle, pandora)

    g.add_edge(arendelle, metroville)
    g.add_edge(metroville, arendelle)

    g.add_edge(arendelle, monstropolis)
    g.add_edge(monstropolis, arendelle)

    g.add_edge(metroville, monstropolis)
    g.add_edge(monstropolis, metroville)

    g.add_edge(metroville, narnia)
    g.add_edge(narnia, metroville)

    g.add_edge(metroville, naboo)
    g.add_edge(naboo, metroville)

    g.add_edge(narnia, naboo)
    g.add_edge(naboo, narnia)

    values = []

    g.breadth_first(pandora, lambda v: values.append(v.value))

    assert values == [
        "Pandora", "Arendelle", "Metroville", "Monstropolis", "Narnia", "Naboo"
    ]