Question

The algorithm below is written in pseudocode and for simplicity the storage of the actual route in the Data structure is not included.

LengthFromSrc   = 0;
    LengthFromDest  = 0;
    TotalNumberHops = 0;

    X = SRC;  /*Last Node Visited from Random walk starting at SRC;*/
    Y = DEST; /*Last Node Visited from Random walk starting at DEST;*/
    /* Randomly select a route length */
    do {
        Length = rand( ) % Max;
        while( Length < Min );


    while( TotalNumberHops < Length ) {           

      Next = Toss Coin to Pick Random Walk from Src or from Dest;

      if( Next == RandWalkFromSrc ) {
      Z = Randomly select an adjacent node to X;
      TotalNumberHops = 1 + LengthFromSrc + LengthFromDest
                            + shortest-path from Z to Y; 
      if( TotalNumberHops > Length )  
          break;
      X = Z;            /*include the node in the route*/
              Store X in the route data structure 
          LengthFromSrc++;
      }
      else {  /* Next = RandWalkFromDest */
      Z = Randomly select an adjacent node to Y;
      TotalNumberHops = 1 + LengthFromSrc + LengthFromDest
                            + shortest-path from Z to X; 
      if( TotalNumberHops > Length )  
          break;
      Y = Z;            
              Store Y in the route data structure 
          LengthFromDest++;
       }
      }

Would someone give me a brief analysis of the algorithm/or walk me through the code, as I would like to understand it better? My main problem is understanding the first part:

      do {
          Length = rand( ) % Max;
          while( Length < Min );


          while( TotalNumberHops < Length )

PS: my source is http://www.onion-router.net/Archives/Route/

Was it helpful?

Solution

I'd say that code is missing a } (although it is pseudo-code, so anything goes really)

do {
    Length = rand() % Max;
}
while( Length < Min );

rand() is a function is C++ which generates an integer between 0 and at least 32767 (although, for the purposes of this, I think we should assume that the maximum number than can be generated is greater than Max).

% Max gives the remaining of the number divided by Max, so Length will be between 0 and Max-1 (inclusive).

Then you repeat this until Length >= Min, so, at the end, Length will between Min and Max-1 (inclusive).

We can completely avoid the loop with this code:

Length = Min + rand() % (Max - Min);

Or, since this is pseudo-code:

Length = random number in the range [Min, Max)

The rest of the code generates two paths at the same time from the source and destination, and then stops when linking them (using the shortest possible path) would result in a walk longer than Length.

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