Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Prim's algorithm

For prim's algorithm, if a vertice have the same weight to other vertices, so which one will it be choosen to put in the tree?
For example

A to B is 3
A to C is 3
A to D is 3
B to C is 3
B to D is 3
C to D is 3

How will be a minimal spanning tree be form?

2. ## Re: Prim's algorithm

It means that all the path to that point takes the same cost. So, it doesn't matter which path you choose. It's like saying do I want to go north 1 mile, then west 1 mile, or do I want to go west 1 mile and then north 1 mile? They're different paths, but the exact same distances.

3. ## Re: Prim's algorithm

yea, they have the same distance, so Prim's algorithm will choose which path first? If we start the tree from A, so the next one is B, C or D?

4. ## Re: Prim's algorithm

Without looking at a specific implementation, it's undetermined unless you knew which node the algorithm started looking from. Also, I would probably suspect that in general an implementation wouldn't change the span tree unless the distance was less than the current known shortest distance.

So, let's pretend the implementation looks top-down in the list.

A to B is 3
A to C is 3
A to D is 3
B to C is 3
B to D is 3
C to D is 3

Start from A, and look at all connected nodes.

The current span tree is A to B, A to C, and A to D with a total span weight of 9. Then it would look at B. B to C is an additional 3 to make 6, don't change the tree. B to D is also an additional 3, so don't change the tree. Look at node C and you'll find the similar condition between C and D.