示例#1
0
文件: prim.py 项目: lalitzz/DS
def prim(G):
  cost = {}
  parent = {}
  u = None
  P = PriorityQueue()
  for v in G.vertexes:
    if u is None:
      u = v
    cost[v] = float('inf')
    P.add(float('inf'), v)
    parent[v] = None

  cost[u] = 0
  P.change_priority(u, 0)
  for i in P.Q:
    print(i)
  while not P.isEmpty():
    print('wtf')
    v_ele = P.get_min()
    vertex = v_ele.data
    print('minimum', v_ele)
    for u, v, w in G.get_all_vertex(vertex):
      print(u, v, w)
      if P.check_ele(v) and cost[v] > cost[u] + w:
        cost[v] = cost[u] + w
        parent[v] = u
        P.change_priority(v, cost[v])
  print(cost)
  print(parent)
示例#2
0
def prims(G):
    cost = {}
    parent = {}
    u = None
    P = PriorityQueue()
    for v in G.vertexes:
        if u is None:
            u = v
        cost[v] = float('inf')
        P.add(float('inf'), v)
        parent[v] = None

    cost[u] = 0
    P.change_priority(u, 0)
    while not P.isEmpty():
        v_ele = P.get_min()
        vertex = v_ele.data
        for u, v, w in G.get_all_vertex(vertex):
            if P.check_ele(v) and cost[v] > cost[u] + w:
                cost[v] = cost[u] + w
                parent[v] = u
                P.change_priority(v, cost[v])