Question

I am trying to use the Prim algorithm with prim_minimum_spanning_tree function of the boost library, using a file containing all my graph points. I successfuly created the graph I wanted with the kruskal_minimum_spanning_tree function of boost library.

But with the prim_minimum_spanning_tree I only get the direct parent of each points so my graph is not complete.

I used the examples given in the boost documentation :

for prim : http://www.boost.org/doc/libs/1_47_0/libs/graph/example/prim-example.cpp

and the kruskal one http://www.boost.org/doc/libs/1_47_0/libs/graph/example/kruskal-example.cpp

A concrete example : I have this file of points :

10
0 0.655171
0.304819 0.674978
0.106754 0.516587
0.489669 0.602466
0.369945 0.256661
0.374187 0.825587
0.172704 0.2978
0.643544 0.789666
0.987823 0.800592
0.464248 0.538987

with the krukal functions I get :

3 <--> 9
1 <--> 5
0 <--> 2
1 <--> 3
6 <--> 4
6 <--> 2
3 <--> 7
2 <--> 1
8 <--> 7

with the prim function I get :

1 <--> 5
2 <--> 0
3 <--> 9
4 <--> 6
5 <--> 1
6 <--> 4
7 <--> 3
8 <--> 7
9 <--> 3

I have the drawing of those graph but I can't post images nor several linksl. But as you can see I miss 3 links.

How can I get the same result as I have with kruskal with the prim function?

Thank you,

PS : I used the code of the example, as a base and tried a bunch of modifications without any success, couldn't find any usefull information on the web or on the documentation.

Was it helpful?

Solution

I have found the answer to my own question, might be usefull to someone else.

I was linking the points twice, which work for the kruskal's algorithm, but not for the prim's one, for example in my edges I had :

0 <--> 1
1 <--> 0

where I should have only linked the points once for the prim's algorithm.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top