Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8886275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:31:07+00:00 2026-06-14T21:31:07+00:00

I have looked at the following prim’s algorithm (in order to create a minimum

  • 0

I have looked at the following prim’s algorithm (in order to create a minimum spanning tree) and I am unsure as to what the input value s in the following code is, I think the G of course would be the graph sent (adjacency matrix or list graphs) and I think the value s is where the start should be? Also if it is the start then in what way would you send a starting value to the following algorithm?:

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 

Any help will be much appreciated, thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:31:08+00:00Added an answer on June 14, 2026 at 9:31 pm

    Here you are:

    #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]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code does not work all the time. I have looked at countless
I have looked here and from what I understand the following regex simply means
I've looked around but have yet to find a great solution the the following
I have looked all over and cannot figure out why this code isn't working.
To start out, I have looked at the following pages and don't quite have
I have defined the following button in jqgrid. I have looked up alot of
I have looked at the following question: Could someone provide a C# example using
I have the following code to give a Excel cell Validation with custom text.
I have looked at the following, but they aren't clear, particularly the reference to
I have looked at the following software tools: Ragel ANTLR BNF Converter Boost::Spirit Coco/R

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.