示例#1
0
def mst_prim_jarnik(g):
    d = {}
    tree = []
    pq = AdaptiveHeapPriorityQueue()
    pqlocator = {}

    for v in g.vertices():
        if len(d) == 0:
            d[v] = 0
        else:
            d[v] = float('inf')
        pqlocator[v] = pq.add(d[v], (v, None))

    while not pq.is_empty():
        key, value = pq.remove_min()
        u, e = value
        del pqlocator[u]
        if e is not None:
            tree.append(e)
        for link in g.incident_edges(u):
            v = link.opposite(u)
            if v in pqlocator:
                wgt = link.element()
                if wgt < d[v]:
                    d[v] = wgt
                    pq.upadte(pqlocator[v], d[v], (v, link))

    return tree
示例#2
0
def shortest_path_lengths(g, src):
    d = {}
    cloud = {}
    pq = AdaptiveHeapPriorityQueue()
    pqlocator = {}

    for v in g.vertices():
        if v is src:
            d[v] = 0
        else:
            d[v] = float('inf')
        pqlocator[v] = pq.add(d[v], v)

    while not pq.is_empty():
        key, u = pq.remove_min()
        cloud[u] = key
        del pqlocator[u]
        for e in g.incident_edges(u):
            v = e.opposite(u)
            if v not in cloud:
                wgt = e.element()
                if d[u] + wgt < d[v]:
                    d[v] = d[u] + wgt
                    pq.upadte(pqlocator[v], d[v], v)

    return cloud