Thread: Help me understand this while( ) statement

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    30

    Help me understand this while( ) statement

    Hi, I know this is probably a silly question but I am having trouble understanding a piece of code i am looking at -
    Code:
      while (labels[y] != y)
        y = labels[y];
    What exactly does the while loop do here? Is this just equivalent to writing
    Code:
      if (labels[y] != y)
        y = labels[y];
    since there is no counter? Or is this some trick that i dont know?

    If so, what is the advantage of the first way?

    Thanks a lot!


    (If you are wondering where its from, I'm trying to understand the hoshen-kopelman algorithm as implemented here, the larger code fragment is

    Code:
    int find(int x) {
     int y = x;
     while (labels[y] != y)
       y = labels[y];
    
     while (labels[x] != x) {
       int z = labels[x];
       labels[x] = y;
       x = z;
     }
     return y;
    }
    )

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The loop changes the value of "y" till it equals the value of "labels[y]".
    So, it is NOT the same of the if statement in most cases.

    Math note: This is NOT a math equation; it could never equal depending on the data in the array labels.

    Tim S.
    Last edited by stahta01; 02-02-2012 at 03:36 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    OK, thanks for getting back to me. Forgive me for being a bit slow, but I'm struggling to get my head around this. Let me try and explain my reasoning -

    while(labels[y] != y) checks if the value stored at the y index of the array 'labels' is different to the variable 'y'. If it is indeed different, then y = labels[y] gives the variable y the value that is stored at the y index of the array 'labels'.. am i right so far?

    So for example, if y=4 and labels[4]=5, the while() statement would be true, since 5 != 4. The variable y, which had previously stored the value of 4 would be assigned a new value, '5'.

    Obviously I am going wrong here somewhere.

    You say that "The loop changes the value of "y" till it equals the value of "labels[y]" ", which implies it will potentially change it more than once until the condition is met... but how would more than one change ever be needed? Isn't y = labels[y]; sort of definitive? It just sets it in one go. Thats why i thought that the statement might be equivalent to an if().


    thanks for your time

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This is beyond me explaining in the time I have left at school today; you are thinking as a Math student does; not as a C programmer does.
    To me it is obvious but; how to explain it is not obvious. I added the only example I can think of.

    Assume labels is equal to 0,0,2,2,3,3 and you give it the value of 5 to find.
    Remember array first index is zero.

    Tim S.
    Last edited by stahta01; 02-02-2012 at 04:01 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    Quote Originally Posted by stahta01 View Post
    This is beyond me explaining in the time I have left at school today; you are thinking as a Math student does; not as a C programmer does.
    To me it is obvious but; how to explain it is not obvious.

    Tim S.
    haha, yes I am a physics student not a CS student, i guess i have a mathematical mindset.

    Thanks anyway, you pointed me in the right direction im sure ill get it eventually

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    Well I spoke to my professor today and he couldn't understand it either, this is clearly some sort of CS black magic that is impenetrable for physicists!

    I worked through the example stahta01 kindly provided though, and tried coding it (i'll include it below).

    I think I understand a bit better now... setting y=labels[y] does not necessarily mean that labels[y]=y ??! I see what you meant about a mathematical mindset, consider my mind expanded lol.

    Thanks a lot for your help, no doubt ill be back with more questions soon!

    Code:
    #include <stdio.h>
    
    int labels[6] = {0,0,2,2,3,3};
    int find(int x);
    
    int main()
    {
        int x=5, y=0;
        y = find(x);
        printf("find(x) returns %d.\n", y);
        return 0;
    }
    
    int find(int x) {
     int y = x;
     while (labels[y] != y){
        printf("loop called! y = %d\n", y);
        y = labels[y];
     }
    
     while (labels[x] != x) {
       int z = labels[x];
       labels[x] = y;
       x = z;
     }
     return y;
    }
    
    /* ******************** output **********************
    
    loop called! y = 5
    loop called! y = 3
    find(x) returns 2.
    
    Process returned 0 (0x0)   execution time : 0.087 s
    Press any key to continue.
    
    ************************************************** */

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I was going to explain this using snippets of the code, the the forum doesn't seem to want me to post anything with an equal sign, so here's the all English version. Hope it's clear enough.

    Think of it like a series of clues leading to a destination. y represents your current location. Setting it to x before the loop means you start at position x. The labels array is an array of clues at each location. You keep searching while you haven't arrive yet, i.e. while the clue at the current location tells you to go elsewhere. If you're not there, you "go to a new position" by assigning y the value of the clue at your current location. The if statement would only take you one step along this path.

    Exactly why that is done depends on what the find algorithm is supposed to find, how the labels array is set up and what data it contains. That I can't tell you from your code sample

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Okay, I can see that the best way to explain this is going to be by example: So take this:
    Code:
    const int labels[] = {2, 4, 1, 3, 3};
    int y = 0;
    while (labels[y] != y)
       y = labels[y];
    This table shows the values of y and labels[y] at the top of the loop.
    y labels[y]
    0 2 (not equal)
    2 1 (not equal)
    1 4 (not equal)
    4 3 (not equal)
    3 3 (equal, stop)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    Quote Originally Posted by anduril462 View Post
    I was going to explain this using snippets of the code, the the forum doesn't seem to want me to post anything with an equal sign, so here's the all English version. Hope it's clear enough.
    Cheers, thats very helpful.

    (by the way, the code is used as part of a Hoshen-Kopelman Algorithm (i got it from here) to identify and label clusters of 1's in a 2d array of 1's and 0's. I am interested in this as I am doing a project using kinetic Monte Carlo simulations of molecules on a surface.)

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    Quote Originally Posted by iMalc View Post
    This table shows the values of y and labels[y] at the top of the loop.
    y labels[y]
    0 2 (not equal)
    2 1 (not equal)
    1 4 (not equal)
    4 3 (not equal)
    3 3 (equal, stop)
    Thanks a lot, that makes it very clear

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The 'while' and 'if' are equivalent since there is no increment operation inside the loop - therefore no implied traversing the array.
    Unless y = labels[y] is not permanent.... labels[] may be some global variable that gets altered in another thread that's co-executing. I don't think that's expected to happen here though.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by nonoob View Post
    The 'while' and 'if' are equivalent since there is no increment operation inside the loop - therefore no implied traversing the array.
    Unless y = labels[y] is not permanent.... labels[] may be some global variable that gets altered in another thread that's co-executing. I don't think that's expected to happen here though.
    While it's true that y isn't incremented, it is nevertheless changed in the loop body, by setting it equal to labels[y], which we know is not the same as y because of the loop condition. It's not a straight traversal, but you can see from the output section in post #6, that using Tim's example array, the while loop does "traverse".

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yes indeed. Examples in post #6 do change 'y' within the loop. But not in the first post that started this thread. I was responding to the original issue as stated.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Reread again my friend.

  15. #15
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    Just to say THANK YOU to everyone who replied, I understand it much better now. I did a little reading on union-find stuff (i found this pdf quite useful) and I've managed to use it correctly in my cluster finding program, I'm very pleased!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me understand following statement/syntax in C++
    By sanddune008 in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2011, 10:29 PM
  2. Cannot understand this FOR statement!
    By MicroB in forum C Programming
    Replies: 3
    Last Post: 12-13-2010, 04:28 AM
  3. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  4. Need help to understand this statement
    By jk1998 in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2007, 02:42 PM
  5. help me understand...
    By stormfront in forum C Programming
    Replies: 2
    Last Post: 10-12-2005, 10:47 AM