简体   繁体   中英

Prims algorithm

I'm trying to implement Prims Algorithm to my graph program, but I am having some difficulties with it. I'm following a guide that I found at this site .

It seems to be working fine to some degree but the guides output is:

Edge   Weight
0 - 1    2
1 - 2    3
0 - 3    6
1 - 4    5

And my solution is returning:

Edge   Weight
1 - 0    2 
1 - 2    3 
1 - 3    8 <---- this one seems off.
1 - 4    5 

And I really can't figure out what's wrong with my code.

My code is:

void b_Prim(){ 
    reset_adjmat(G); // resets current adjmat and creates a new one.
    int V = b_card(G); // b_card = cardinality
    int count, i, v, u, min_index, min = -1,pIndex = 1;
    int key[V];   // Key values used to pick minimum eWeight edge in cut
    int mstSet[V];  // To represent set of vertices not yet included in MST

 // Initialize all keys as INFINITE
 for (i = 0; i < V; i++){
    key[i] = -1; 
    mstSet[i] = 0;
 } 
 // Always include first 1st vertex in MST.

 key[0] = 0;     // Make key 0 so that this vertex is picked as first vertex
 source[0] = -1; // First node is always root of MST

 // The MST will have V vertices
 for (count = 0; count < V-1; count++)
 {
    // Pick thd minimum key vertex from the set of vertices
    // not yet included in MST
       for (v = 0; v < V; v++)
            if (mstSet[v] == 0 && ((min == -1 && key[v] != -1) || key[v] < min)){
                min = key[v];
                min_index = v;
            }

    u = min_index;

    // Add the picked vertex to the MST Set

   mstSet[u] = 1;

    // Update key value and source index of the adjacent vertices of
    // the picked vertex. Consider only those vertices which are not yet
    // included in MST
    for (v= 0; v < V; v++)

       // graph[u][v] is non zero only for adjacent vertices of m
       // mstSet[v] is false for vertices not yet included in MST
       // Update the key only if graph[u][v] is smaller than key[v]

      if (adjmat[u][v] != 0 && mstSet[v] == 0 && (key[v] == -1 || adjmat[u][v] <  key[v])){
         source[pIndex]  = u;
         dest[pIndex] = v;
         key[v] = adjmat[u][v];
         eWeight[pIndex] = key[v];
         pIndex++;
     }else if(adjmat[u][v] != 0 && mstSet[v] == 0 && key[v] == 0){
         source[pIndex]  = u;
         dest[pIndex] = v;
         eWeight[pIndex] = adjmat[u][v];
         pIndex++;
     }
 }

 }

This comment :

// Initialize all keys as INFINITE

does not correspond to what the code does :

key[i] = -1;

since you use this comparison further :

key[v] < min

which will yield different results if key[v] is -1 than what would be expected if key[v] was 'infinity'. In other words, this causes the above comparison to be true a lot more often than it should.

There might be more issues - I haven't checked in detail, but the use of pIndex looks suspicious eg.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM