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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Prim's algorithm

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Prim's algorithm

    Think about what it means:

    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. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

    So your final span tree:

    A to B
    A to C
    A to D

    with a total weight of 9.

    You could repeat the same exercise by starting with any of the other nodes and it would be a similar tree, but the starting node would be the node you started with connected to the other 3 nodes. Note that this is only true if the above listed assumptions about the implementation not changing the tree if the distances are equal is true.

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Prim's algorithm

    Thank you for your explanation

Similar Threads

  1. SDES algorithm
    By low1988 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 2nd, 2010, 10:57 AM
  2. [SOLVED] Algorithm Help
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2010, 05:12 PM
  3. Prim's MST
    By BarbTheBarb in forum Algorithms & Recursion
    Replies: 0
    Last Post: May 11th, 2010, 09:38 AM
  4. Q:BackTracking Algorithm
    By Cross`17 in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 18th, 2010, 12:33 AM
  5. algorithm
    By AmmrO in forum Algorithms & Recursion
    Replies: 13
    Last Post: September 24th, 2009, 10:18 PM