示例#1
0
def sssp(G, start):
    infinity = weighted.infinity(G)
    dist, parent, fringe = {}, {}, []
    for v in G:
        dist[v], parent[v] = infinity, -1
        fringe.append(v)
    dist[start], parent[start] = 0, start

    doneEarly = False
    while len(fringe) > 0 and not doneEarly:
        weighted.showFringe(G, fringe, dist, infinity)
        u = fringe.pop(weighted.indexOfMin(fringe, dist))
        print '\npick', u, ':',
        if (dist[u] == infinity): doneEarly = True
        for (v, duv) in G[u]:
            if (v in fringe) and (dist[v] > dist[u] + duv):
                dist[v] = dist[u] + duv
                parent[v] = u
                print v, dist[v], ' ',
        print ''
    weighted.showAll(G, dist, parent)
示例#2
0
def primMst(G,start):
  infinity = weighted.infinity(G)
  cost, parent, fringe = {}, {}, []
  for v in G:
    cost[v], parent[v] = infinity, -1
    fringe.append(v)
  cost[start],parent[start] = 0, start

  doneEarly = False
  while len(fringe)>0 and not doneEarly:
    weighted.showFringe(G,fringe,cost,infinity)
    u = fringe.pop(weighted.indexOfMin(fringe,cost))
    print '\npick',u,':',
    if (cost[u] == infinity): doneEarly = True
    for (v,duv) in G[u]:
      if (v in fringe) and (cost[v] > duv):
        cost[v] = duv
        parent[v] = u
        print v,cost[v],' ',
    print ''
  weighted.showAll(G,cost,parent)
  print 'mst weight', sum(cost.values())
示例#3
0
文件: ssspDemo.py 项目: Kulbear/algs
def sssp(G, start):
    infinity = weighted.infinity(G)
    dist, parent, fringe = {}, {}, []
    for v in G:
        dist[v], parent[v] = infinity, -1
        fringe.append(v)
    dist[start], parent[start] = 0, start

    doneEarly = False
    while len(fringe) > 0 and not doneEarly:
        weighted.showFringe(G, fringe, dist, infinity)
        u = fringe.pop(weighted.indexOfMin(fringe, dist))
        print "\npick", u, ":",
        if dist[u] == infinity:
            doneEarly = True
        for (v, duv) in G[u]:
            if (v in fringe) and (dist[v] > dist[u] + duv):
                dist[v] = dist[u] + duv
                parent[v] = u
                print v, dist[v], " ",
        print ""
    weighted.showAll(G, dist, parent)
示例#4
0
def primMst(G, start):
    infinity = weighted.infinity(G)
    cost, parent, fringe = {}, {}, []
    for v in G:
        cost[v], parent[v] = infinity, -1
        fringe.append(v)
    cost[start], parent[start] = 0, start

    doneEarly = False
    while len(fringe) > 0 and not doneEarly:
        weighted.showFringe(G, fringe, cost, infinity)
        u = fringe.pop(weighted.indexOfMin(fringe, cost))
        print "\npick", u, ":",
        if cost[u] == infinity:
            doneEarly = True
        for (v, duv) in G[u]:
            if (v in fringe) and (cost[v] > duv):
                cost[v] = duv
                parent[v] = u
                print v, cost[v], " ",
        print ""
    weighted.showAll(G, cost, parent)
    print "mst weight", sum(cost.values())