Пример #1
0
def sssp(G, source):
    infinity = weighted.infinity(G)
    dist, parent, fringe = {}, {}, []
    for v in G:
        dist[v], parent[v] = infinity, -1
        fringe.append(v)
    dist[source], parent[source] = 0, source

    while len(fringe) > 0:
        u = fringe.pop(weighted.indexOfMin(fringe, dist))
        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 'to', v, 'now', dist[v], 'via', u

    print 'from', source, ': ',
    for v in sorted(G):
        print v,
    print '\ndistance ',
    for v in sorted(G):
        print dist[v],
    print '\nparent   ',
    for v in sorted(G):
        print parent[v],
Пример #2
0
def prim(G,source):
  cost, parent, fringe = {}, {}, []
  infin = weighted.infinity(G)
  for v in G:
    cost[v], parent[v] = infin, -1
    fringe.append(v)
  cost[source],parent[source] = 0, source

  #doneEarly = False
  sum = 0
  print source, 'start'
  while len(fringe)>0:# and not doneEarly:
    u = fringe.pop(weighted.indexOfMin(fringe,cost))
    sum += cost[u]
    if u != source: print u,parent[u],cost[u]
    if cost[u] == infin:  doneEarly = True
    for (v,costuv) in G[u]:
      if (v in fringe) and (cost[v] > costuv): 
        cost[v] = costuv
        parent[v] = u
  print 'mst weight', sum
Пример #3
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)
Пример #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())
Пример #5
0
def sssp(G,source):
  infinity = weighted.infinity(G)
  dist, parent, fringe = {}, {}, []
  for v in G:
    dist[v], parent[v] = infinity, -1
    fringe.append(v)
  dist[source],parent[source] = 0, source

  while len(fringe)>0:
    u = fringe.pop(weighted.indexOfMin(fringe,dist))
    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 'to',v,'now',dist[v],'via',u

  print 'from',source,': ',
  for v in sorted(G): print v,
  print '\ndistance ',
  for v in sorted(G): print dist[v],
  print '\nparent   ',
  for v in sorted(G): print parent[v],
Пример #6
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)
Пример #7
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())