You are here:

C++/Prim's Algorithm


QUESTION: I want to read in a 5x5 matrix, as well as the number of vertices and starting point:

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

and the program should output this:
0   1   3  -1 -1
1   0  -1  -1 -1
3  -1   0   4  2
-1  -1   4   0 -1
-1  -1   2  -1  0
Ive tried using a 2d array but i cant get it to work any help would be appreciated thanks

ANSWER: Hello,

I don't quite understand what you are trying to do. Do you want to make your 5x5  matrix a 2x2 one? Also, what are you trying to accomplish using a prim's algorithm? Are you just interested in seeing a code that shows how prim's algorithm works?
Please, explain further for me to figure out what you want to accomplish.

[an error occurred while processing this directive]---------- FOLLOW-UP ----------

QUESTION: the 5 is from the input files stating the number of vertices. The 1 is the starting point. I have to input the top matrix and use prims algorithm to find the minimum spanning tree so the output looks like the bottom matrix. -1 means there isnt an edge between the two vertices


sorry for the delay. I had to understand what you really want to do. I think this link might be the best bet for what you are trying to accomplish. If after checking it you still can't figure it out, then let me know. stay blessed


All Answers

Answers by Expert:

Ask Experts




I can confidently answer any question both basic and advanced in C++. Areas like templates, algorithms, Gui and design patterns are of interest to me. i will not answer questions about web programming.


I have a 3- year- work experience in software Engineering.

I am a bachelor of Engineering graduate

©2017 All rights reserved.

[an error occurred while processing this directive]