예제 #1
0
def recorre_aristas_anchura(grafo: Digraph, v_inical: Vertex) -> List[Edge]:
    aristas = []
    seen = set()
    queue = Fifo()
    queue.push((v_inical, v_inical))
    seen.add(v_inical)
    while len(queue) > 0:
        u, v = queue.pop()
        aristas.append((u, v))
        for suc in grafo.succs():
            if suc not in seen:
                seen.add(suc)
                queue.push((u, v))
    return aristas
예제 #2
0
def dijkstra(g: Digraph, d: WeightingFunction, v_inicial, v_final):
    D = {}
    added = set()
    recorrido = []
    for v in g.V:
        D[v] = float("infinity")
    D[v_inicial] = 0
    frontera = {v_inicial: v_inicial}
    while len(frontera) > 0:
        v_destino = argmin(frontera.keys(), lambda v: D[v])
        added.add(v_destino)
        v_origen = frontera[
            v_destino]  # Como en el recorrido de backpointers, aquí el origen del v_destino es el valor de los vertices frontera
        recorrido.append((v_origen, v_destino))
        del frontera[v_destino]
        if v_destino == v_final:
            break
        for i in g.succs(v_destino):
            if i not in added and D[v_destino] + d(v_destino, i) < D[i]:
                frontera[i] = v_destino
                D[i] = D[v_destino] + d(v_destino, i)
    return recorrido
예제 #3
0
from algoritmia.datastructures.digraphs import Digraph
g = Digraph(E=[(0, 1), (0, 2), (1, 2)])
print(g.V)
print(g.succs(0))
예제 #4
0
from algoritmia.datastructures.digraphs import Digraph

aristas = [(1, 3), (1, 7), (1, 9), (3, 7)]

g = Digraph(E=aristas)

print(g.succs(1))
print(g.preds(7))
print(g.V)  #Vertices
print(g.E)  #Aristas