【问题标题】:Prim's Algorithm input parameter (values) in PythonPython中Prim的算法输入参数(值)
【发布时间】:2012-11-26 03:44:39
【问题描述】:

我查看了以下 prim 的算法(为了创建最小生成树),我不确定以下代码中的输入值 s 是什么,我认为 G 当然是发送的图(邻接矩阵或列表图),我认为值 s 应该从哪里开始?另外,如果它是开始,那么您将以什么方式将起始值发送到以下算法?:

from heapq import heappop, heappush

def prim(self, G, s): 
    P, Q = {}, [(0, None, s)] 
    while Q: 
        _, p, u = heappop(Q) 
        if u in P: continue 
        P[u] = p 
        for v, w in G[u].items(): 
            heappush(Q, (w, u, v)) 
    return P 

任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 这是从哪里来的?您可以发布 heappop 和 heapsuh 的代码吗? G 可能是一个字典。
  • 如下:from heapq import heappop, heappush docs.python.org/2/library/heapq.html
  • 单个节点的初始树。必须从某个地方开始。

标签: python algorithm minimum-spanning-tree prims-algorithm


【解决方案1】:

你在这里:

#A = adjacency matrix, u = vertex u, v = vertex v
def weight(A, u, v):
    return A[u][v]

#A = adjacency matrix, u = vertex u
def adjacent(A, u):
    L = []
    for x in range(len(A)):
        if A[u][x] > 0 and x <> u:
            L.insert(0,x)
    return L

#Q = min queue
def extractMin(Q):
    q = Q[0]
    Q.remove(Q[0])
    return q

#Q = min queue, V = vertex list
def decreaseKey(Q, K):
    for i in range(len(Q)):
        for j in range(len(Q)):
            if K[Q[i]] < K[Q[j]]:
                s = Q[i]
                Q[i] = Q[j]
                Q[j] = s

#V = vertex list, A = adjacency list, r = root
def prim(V, A, r):
    u = 0
    v = 0

    # initialize and set each value of the array P (pi) to none
    # pi holds the parent of u, so P(v)=u means u is the parent of v
    P=[None]*len(V)

    # initialize and set each value of the array K (key) to some large number (simulate infinity)
    K = [999999]*len(V)

    # initialize the min queue and fill it with all vertices in V
    Q=[0]*len(V)
    for u in range(len(Q)):
        Q[u] = V[u]

    # set the key of the root to 0
    K[r] = 0
    decreaseKey(Q, K)    # maintain the min queue

    # loop while the min queue is not empty
    while len(Q) > 0:
        u = extractMin(Q)    # pop the first vertex off the min queue

        # loop through the vertices adjacent to u
        Adj = adjacent(A, u)
        for v in Adj:
            w = weight(A, u, v)    # get the weight of the edge uv

            # proceed if v is in Q and the weight of uv is less than v's key
            if Q.count(v)>0 and w < K[v]:
                # set v's parent to u
                P[v] = u
                # v's key to the weight of uv
                K[v] = w
                decreaseKey(Q, K)    # maintain the min queue
    return P

A = [ [0,  4,  0,  0,  0,  0,   0,  8,  0],
      [4,  0,  8,  0,  0,  0,   0, 11,  0],
      [0,  8,  0,  7,  0,  4,   0,  0,  2],
      [0,  0,  7,  0,  9, 14,   0,  0,  0],
      [0,  0,  0,  9,  0, 10,   0,  0,  0],
      [0,  0,  4, 14, 10,  0,   2,  0,  0],
      [0,  0,  0,  0,  0,  2,   0,  1,  6],
      [8, 11,  0,  0,  0,  0,   1,  0,  7],
      [0,  0,  2,  0,  0,  0,   6,  7,  0]]
V = [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]

P = prim(V, A, 0)
print P

[None, 0, 5, 2, 3, 6, 7, 0, 2]

【讨论】:

  • 谢谢,上面的 cmets 和这个 prim 算法都回答了我的问题。上面的算法我假设它只适用于邻接列表并从发送给它的某个节点开始,cMinor 发布的算法适用于我最有可能使用的邻接矩阵。谢谢!
【解决方案2】:

G 是图或邻接矩阵,s 是你可以给出的任何随机起始节点,你选择哪个节点都没有关系

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多