home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / src / basic / _partiti.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  1.5 KB  |  73 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _partition.c
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. //------------------------------------------------------------------------------
  17. // partition.c
  18. // implementation of data type partition
  19. // (union find with weighted union rule and path compression)
  20. //
  21. // S. Naeher     1989
  22. //------------------------------------------------------------------------------
  23.  
  24. #include <LEDA/partition.h>
  25.  
  26.  
  27. void partition::clear()
  28. { // delete all used items
  29.  
  30.   partition_item p = used_items; 
  31.   while (used_items)
  32.   { p = used_items;
  33.     used_items = used_items->next;
  34.     clear_inf(p->info);
  35.     delete p;
  36.    }
  37.  }
  38.   
  39.  
  40.  
  41. partition_item partition::find(partition_item it) 
  42. { // find with path compression
  43.  
  44.   partition_item x,root = it;
  45.  
  46.   while (root->father) root = root->father;
  47.  
  48.   while (it!=root)
  49.   { x = it->father;
  50.     it->father = root;
  51.     it = x;
  52.    }
  53.   return root;
  54.  }
  55.  
  56. void partition::union_blocks(partition_item a, partition_item b)
  57. { // weighted union
  58.  
  59.   a = find(a);
  60.   b = find(b);
  61.  
  62.   if (a==b) return;
  63.  
  64.   if (a->size > b->size)
  65.        { b->father = a;
  66.          a->size += b->size; }
  67.   else { a->father = b;
  68.          b->size += a->size; }
  69.  
  70.  }
  71.  
  72.