Exemplo n.º 1
0
    def run(G: Graph) -> Result:
        memory: int = 0
        k: int = 0
        W: float = 0.0
        path: List[Edge] = []
        vertices: List[int] = G.vertices()
        memory += sizeof(vertices)

        i: int = randrange(len(vertices))
        start: int = vertices[i]
        del vertices[i]

        u: int = start
        while len(vertices) > 0:
            i = randrange(len(vertices))
            v: int = vertices[i]
            del vertices[i]
            e = G.get_edge(u, v)
            path.append(e)
            W += e.w
            k += 1
            u = v

        e = G.get_edge(u, start)
        path.append(e)
        W += e.w
        k += 1

        return Result(k, W, memory, path=path)
Exemplo n.º 2
0
    def run(G: Graph) -> Result:
        memory: int = 0
        k: int = 0
        W: float = 0.0
        path: List[Edge] = []
        vertices: List[int] = G.vertices()
        memory += sizeof(vertices)

        i: int = randrange(len(vertices))
        start: int = vertices[i]
        del vertices[i]

        G.order_edges()

        u: int = start
        while len(vertices) > 0:
            next_e: Optional[Edge] = next(
                (e for e in G.get_incident_edges(u) if e.snd in vertices),
                None)

            if next_e is None:
                raise Exception('None')
            v: int = next_e.snd

            vertices.remove(v)

            e = G.get_edge(u, v)
            path.append(e)
            W += e.w
            k += 1

            u = v

        e = G.get_edge(u, start)
        path.append(e)
        W += e.w
        k += 1

        return Result(k, W, memory, path=path)
Exemplo n.º 3
0
    def run(G: Graph) -> Result:
        c: Counter = Counter()
        mst, _ = prim(G, 1)
        tree = Graph(G.N, directed=False)
        tree.add_edges(mst)

        visited: List[bool] = [False for _ in range(G.N + 1)]
        walk: List[int] = []

        def DFS(u: int):
            visited[u] = True
            walk.append(u)
            c.inc()
            for e in tree.get_incident_edges(u):
                v = e.snd
                if not visited[v]:
                    DFS(v)

        DFS(1)

        path: List[Edge] = []
        w: float = 0.0
        first: int = walk[0]
        u: int = first

        walk.append(first)

        for v in walk[1:]:
            c.inc()
            e = G.get_edge(u, v)
            path.append(e)
            w += e.w
            u = v

        memory: int = 0
        memory += sizeof(mst)
        memory += sizeof(tree)
        memory += sizeof(visited)
        memory += sizeof(walk)

        return Result(c.get(), w, memory, path=path)