Ejemplo n.º 1
0
    def generate_prims_maze(self) -> List[Edge]:
        edge_queue = PriorityQueue(self.ascending_edge_order)
        visited = {}
        T = list()
        for x in range(0, int(self.columns / 2) + 1):
            for y in range(0, int(self.rows / 2) + 1):
                visited[Node(2 * x, 2 * y)] = False

        for node in visited:
            for neighbour in self.range_2_neighbours_of_(node):
                if not visited[neighbour]:
                    edge_queue.enqueue(
                        Edge(node, neighbour, random.randrange(1000)))

            while not edge_queue.is_empty() and visited[
                    edge_queue.first().node_b]:
                edge_queue.dequeue()

            if not edge_queue.is_empty():
                edge = edge_queue.first()
                node_sr = Node(int((edge.node_b.x + edge.node_a.x) / 2),
                               int((edge.node_a.y + edge.node_b.y) / 2))
                T.append(Edge(edge.node_a, node_sr, 0))
                T.append(Edge(node_sr, edge.node_b, 0))
                visited[edge.node_b] = True
        return T
Ejemplo n.º 2
0
 def add_edge(self, e: Edge):
     v = e.either()
     w = e.other(v)
     self.__validate_vertex(v)
     self.__validate_vertex(w)
     self.adj[v].add(e)
     self.adj[w].add(e)
     self.E += 1
Ejemplo n.º 3
0
def test_weighted_list_of_int():
    graph = Graph()
    nodes = [[1, 2, 99], [2, 3, 26], [3, 5, 130], [5, 1, 2], [2, 5, 0]]
    graph.make_unweighted_from_list(nodes)
    test_graph = {
                 Node(1):[Edge(1,2,99)],
                 Node(2):[Edge(2,3,26), Edge(2,5,0)],
                 Node(3):[Edge(3,5,130)],
                 Node(5):[Edge(5,1,2)]}
    assert compare_graphs(test_graph, graph.graph) == True
Ejemplo n.º 4
0
def test_unweighted_list_of_int():
    graph = Graph()
    nodes = [[1, 2], [2, 3], [3, 5], [5, 1], [2,5]]
    graph.make_unweighted_from_list(nodes)
    test_graph = {
                 Node(1):[Edge(1,2)],
                 Node(2):[Edge(2,3), Edge(2,5)],
                 Node(3):[Edge(3,5)],
                 Node(5):[Edge(5,1)]}
    assert compare_graphs(test_graph, graph.graph) == True
Ejemplo n.º 5
0
def test_weighted_directed():
    graph = Graph()
    nodes = [["a","b",1],["b","c",2],["a","c",3]]
    graph.make_weighted_from_list(nodes)
    test_graph = {
                 Node("a"):[Edge("a", "b",1), Edge("a","c",3)],
                 Node("b"):[Edge("b","c",2)],
                 Node("c"):[]
                 }
    assert compare_graphs(test_graph, graph.graph) == True
Ejemplo n.º 6
0
def test_unweighted_edges_set():
    test_graph = {
                 Node("a"):[Edge("a","b"),Edge("a","c")],
                 Node("b"):[Edge("b","c")],
                 Node("c"):[]
                 }
    graph = Graph()
    nodes = [["a","b"],["b","c"],["a","c"]]
    graph.make_unweighted_from_list(nodes)
    for node in graph.graph:
        assert graph.graph[node] == test_graph[node.name]
Ejemplo n.º 7
0
def test_unweighted_add_node():
    # make the same graph as one of the ones above
    # add a node and assert its equal
    graph = Graph()
    nodes = [["a","b"],["b","c"],["a","c"]]
    graph.make_unweighted_from_list(nodes)
    graph.add_node(["b","b"])
    test_graph = {
                  Node("a"):[Edge("a","b"),Edge("a","c")],
                  Node("b"):[Edge("b","c"),Edge("b","b")],
                  Node("c"):[]
                 }
    assert compare_graphs(test_graph, graph.graph) == True
Ejemplo n.º 8
0
def test_weighted_add_node():
    graph = Graph()
    nodes = [["a","b",5],["b","c",4],["a","c",3]]
    graph.make_weighted_from_list(nodes)
    graph.add_node(["b","b",2])
    graph.add_node(["b","d",1])
    test_graph = {
                  Node("a"):[Edge("a","b",5),Edge("a","c",3)],
                  Node("b"):[Edge("b","c",4),Edge("b","b",2),Edge("b","d",1)],
                  Node("c"):[],
                  Node("d"):[]
                 }
    assert compare_graphs(test_graph, graph.graph) == True
Ejemplo n.º 9
0
def test_unweighted_remove_node_node():
    graph = Graph()
    nodes = [["a","b"],["b","c"],["a","c"],["c","a"]]
    graph.make_unweighted_from_list(nodes)
    graph.remove_node(Node("a"))
    test_graph = {Node("b"):[Edge("b","c")], Node("c"):[]}
    assert compare_graphs(test_graph, graph.graph) == True
Ejemplo n.º 10
0
def main():
    with open("../resources/tinyEWG.txt", ) as f:
        values = "".join(f.readlines()).splitlines()
        V, E = int(values[0]), int(values[1])
        g = EdgeWeightedGraph(V)
        for line in values[2:]:
            vertices = line.split(' ')
            v, w, weight = int(vertices[0]), int(vertices[1]), float(
                vertices[2])
            e = Edge(v, w, weight)
            g.add_edge(e)
    print(g)
Ejemplo n.º 11
0
def test_weighted_remove_node():
    graph = Graph()
    nodes = [["a","b",1],["b","b",99],["c","b",2],["d","b",3],["e","b",4],["f","z","a"]]
    graph.make_weighted_from_list(nodes)
    graph.remove_node(Node("b"))
    test_graph = {
                 Node("a"):[],
                 Node("c"):[],
                 Node("d"):[],
                 Node("e"):[],
                 Node("f"):[Edge("f","z","a")],
                 Node("z"): []
                 }
    assert compare_graphs(test_graph, graph.graph) == True
Ejemplo n.º 12
0
    def add_edge(self, v_1: int, v_2: int, weight: float):
        """
        add weighted edge between vertices v_1 and v_2
        complexity O(1)
        :param v_1: a vertex
        :param v_2: a vertex
        :param weight: weight of the edge
        """
        assert (0 <= v_1 < self.v)
        assert (0 <= v_2 < self.v)

        e = Edge(v_1, v_2, weight)
        self.data[v_1].append(e)
        self.data[v_2].append(e)
        self.edges.append(e)
Ejemplo n.º 13
0
def test_unweighted_undirected():
    graph = Graph()
    nodes = [["a","b"],["b","c"],["a","c"]]
    graph.make_unweighted_from_list(nodes, False)
    test_graph = {
                 Node("a"):[Edge("a","b"),Edge("a","c")],
                 Node("b"):[Edge("b","a"),Edge("b","c")],
                 Node("c"):[Edge("c","b"),Edge("c","a")]
                 }
    assert compare_graphs(test_graph, graph.graph) == True
Ejemplo n.º 14
0
def main():
    with open("../resources/tinyEWG.txt", ) as f:
        values = "".join(f.readlines()).splitlines()
        V, E = int(values[0]), int(values[1])
        g = EdgeWeightedGraph(V)
        for line in values[2:]:
            vertices = line.split(' ')
            v, w, weight = int(vertices[0]), int(vertices[1]), float(
                vertices[2])
            e = Edge(v, w, weight)
            g.add_edge(e)
    s = 6
    sp = DijkstraUndirectedSP(g, s)
    print(sp)
    for t in range(g.get_V()):
        if sp.has_path_to(t):
            print(f'{s} to {t} ({sp.dist_to(t)})')
            q = sp.path_to(t)
            while not q.empty():
                print(q.get())
            print()
        else:
            print(f'{s} to {t} no path\n')
Ejemplo n.º 15
0
def test_end_set():
    edge = Edge(None, "b")
    assert "b" == edge.end
Ejemplo n.º 16
0
def test_start_set():
    edge = Edge("a", None)
    assert "a" == edge.start
Ejemplo n.º 17
0
def test_default_weight():
    edge = Edge(None, None)
    assert edge.weight == 0
Ejemplo n.º 18
0
def test_weight_set():
    edge = Edge(None, None, 99)
    assert edge.weight == 99
Ejemplo n.º 19
0
from graphs.edge import Edge
from graphs.edge_weighted_graph import EdgeWeightedGraph
from data_structures.weighted_quick_union import WeightedQuickUnion

if __name__ == "__main__":
    g = EdgeWeightedGraph(8)

    g.add_edge(Edge(0, 7, 0.16))
    g.add_edge(Edge(2, 3, 0.17))
    g.add_edge(Edge(1, 7, 0.19))
    g.add_edge(Edge(0, 2, 0.26))
    g.add_edge(Edge(5, 7, 0.28))
    g.add_edge(Edge(1, 3, 0.29))
    g.add_edge(Edge(1, 5, 0.32))
    g.add_edge(Edge(2, 7, 0.34))
    g.add_edge(Edge(4, 5, 0.35))
    g.add_edge(Edge(1, 2, 0.36))
    g.add_edge(Edge(4, 7, 0.37))
    g.add_edge(Edge(0, 4, 0.38))
    g.add_edge(Edge(6, 2, 0.40))
    g.add_edge(Edge(3, 6, 0.52))
    g.add_edge(Edge(6, 0, 0.58))
    g.add_edge(Edge(6, 4, 0.93))

    edges = sorted(g.edges(), key=lambda e: e.weight())
    wqu = WeightedQuickUnion(g.V())

    mst = []
    weight = 0.0

    for e in edges: