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

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _p_embedding.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. #include <LEDA/planar_map.h>
  17.  
  18.  
  19. //-----------------------------------------------------------------------------
  20. //
  21. // Straight Line Embedding
  22. //
  23. //-----------------------------------------------------------------------------
  24.  
  25. /* declare(node_array,int)  declared in header file */
  26.  
  27. declare(node_array,list_item)
  28. declare(node_array,node)
  29.  
  30. static
  31. void label_node( planar_map& G, list(node)& L, 
  32.                                 node_array(int)& ord,
  33.                                 int& count,
  34.                                 node_array(int)& labelled,
  35.                                 list(node)& Al,
  36.                                 list(node)& Bl,
  37.                                 list(node)*& Il,
  38.                                 node_array(int)& Class,
  39.                                 node_array(list_item)& Classloc,
  40.                                 node_array(node)& first, 
  41.                                 node_array(node)& second,
  42.                                 node_array(node)& last,
  43.                                 int A, 
  44.                                 int B, 
  45.                                 node v, 
  46.                                 node c)
  47.  
  48. { // labels the node v; c is the special node which is to be labelled
  49.   // last; the details are described in lemma 10
  50.   edge e;
  51.   L.append(v);
  52.   ord[v]=count++; 
  53.   labelled[v]=1;
  54.   forall_adj_edges(e,v)
  55.   { edge e1 = G.reverse(e);
  56.     node tt = target(e);
  57.     int i;
  58.     if (labelled[tt] && !labelled[target(G.cyclic_adj_succ(e))])
  59.        {first[v]=tt; 
  60.         second[v]=target(G.cyclic_adj_pred(e));
  61.        };
  62.     if (labelled[tt] && !labelled[target(G.cyclic_adj_pred(e))])
  63.        last[v]=tt;
  64.     if (!labelled[tt] && (tt != c))
  65.        {if (Class[tt] == A) { Al.del(Classloc[tt]); 
  66.                             Classloc[tt] = Bl.push(tt);
  67.                             Class[tt]=B;
  68.                           }  
  69.         else {if (Class[tt] == B) { Bl.del(Classloc[tt]);
  70.             /* geaendert wegen */ int x=labelled[target(G.cyclic_adj_succ(e1))];
  71.             /*  inline Fehler  */ int y=labelled[target(G.cyclic_adj_pred(e1))];
  72.                                   i=2-x-y;
  73.                                    }
  74.              else { i=Class[tt];
  75.                     Il[i].del(Classloc[tt]);
  76.                     i=i+1-labelled[target(G.cyclic_adj_succ(e1))]
  77.                          -labelled[target(G.cyclic_adj_pred(e1))];
  78.                   }//end if Class[tt] = B
  79.              Class[tt]=i;
  80.              Classloc[tt]=Il[i].push(tt);
  81.              }//end else case
  82.       }//end if
  83.   }//end 
  84. }//end of label_node
  85.  
  86.  
  87. static
  88. void compute_labelling(planar_map& G, list(node)& L, list(node)& Pi,
  89.                                       node_array(int)&  ord,
  90.                                       node_array(node)& first, 
  91.                                       node_array(node)& second,
  92.                                       node_array(node)& last)
  93.  
  94. { /* computes the ordering of lemma 10 in List L ,the sequence pi
  95.      in List Pi, the function L^-1(v) in Array ord, and the functions
  96.      first,second,last of lemma 11 in the corresponding Arrays
  97.    */
  98.   node v,a,b,c;
  99.  
  100.   /* zuerst berechne ich die drei Knoten,die am Rand des aeusseren 
  101.      Gebiets liegen sollen
  102.    */
  103.  
  104.   a=G.first_node(); 
  105.   edgelist temp = G.adj_edges(a);
  106.   b = target(temp.pop());
  107.   c = target(temp.pop());
  108.  
  109.  
  110.   node_array(int) labelled(G);
  111.   forall_nodes(v,G) labelled[v]=0;
  112.   int count = 0;
  113.   
  114.   const int A = -2; const int B = -1; 
  115.   list(node) Al ;
  116.   node_array(int) Class(G); 
  117.   node_array(list_item) Classloc(G);
  118.   list(node) knoten = G.all_nodes();
  119.   forall(v,knoten) {Classloc[v] = Al.push(v);Class[v]=A;}
  120.   list(node) Bl;
  121.   list(node)* Il = new list(node)[G.number_of_nodes()];
  122.   
  123.   label_node(G, L,ord,count,labelled,Al,Bl,Il,Class,Classloc,
  124.              first,second,last,A,B,a,c);
  125.  
  126.   label_node(G, L,ord,count,labelled,Al,Bl,Il,Class,Classloc,
  127.              first,second,last,A,B,b,c);
  128.  
  129.   while (v=Il[1].pop()) 
  130.      label_node(G, L,ord,count,labelled,Al,Bl,Il,Class, Classloc,
  131.                 first, second,last,A,B,v,c);
  132.  
  133.   label_node(G, L,ord,count,labelled,Al,Bl,Il,Class,Classloc,
  134.              first,second,last,A,B,c,c);
  135.  
  136.    //nun berechne ich noch first second und last des Knoten c 
  137.   first[c]=a;
  138.   last[c]=b;
  139.  
  140.   edge e;
  141.   forall_adj_edges(e,c) if (target(e)==a) second[c]=target(G.cyclic_adj_pred(e));
  142.   
  143.  //nun die Folge Pi
  144.   node_array(list_item) Piloc(G);
  145.   Piloc[a] = Pi.push(a);
  146.   Piloc[b] = Pi.append(b);
  147.   forall(v,L) if (v != a && v != b) Piloc[v] = Pi.insert(v,Piloc[second[v]],-1);
  148.  
  149. }//end of compute_labelling 
  150.  
  151. int planar_map::straight_line_embedding(node_array(int)& x, node_array(int)& y)
  152. {
  153.  // computes a straight-line embedding of the planar map into
  154.  // the 2n by n grid. The coordinates of the nodes are returned
  155.  // in the Arrays x and y. Returns the maximal coordinate.
  156.  
  157. list(node) L;
  158. list(node) Pi;
  159. edgelist TL;
  160.  
  161. node v;
  162. edge e;
  163. int maxcoord=0;
  164.  
  165. node_array(int)  ord(*this);
  166. node_array(node) first(*this), second(*this), last(*this);
  167.  
  168. TL = triangulate();
  169.  
  170.  
  171. compute_labelling(*this,L,Pi,ord,first,second,last);
  172. /* computes the ordering of lemma 10 in List L ,the sequence pi
  173.    in list Pi, the function pi^-1(v) in Array ord, and the functions
  174.    first,second,last of lemma 11 in the corresponding Arrays
  175.  */
  176.  
  177. //I now embed the first three nodes
  178.  
  179. v = L.pop();
  180. x[v]=y[v]=0;
  181.  
  182. v=L.pop();
  183. x[v]=2;y[v]=0;
  184.  
  185. v=L.pop();
  186. x[v]=y[v]=1;
  187.  
  188. //I now embed the remaining nodes
  189.  
  190. while (v=L.pop())
  191.  { // I first move the nodes depending on second[v] by one unit
  192.    // and the the nodes depending on last[v] by another unit to the 
  193.    // right, i.e.,
  194.  
  195.  
  196.   //I increase the x-coordinate of all nodes which follow second[v] in 
  197.   //List Pi and precede v in List L,i.e., have a smaller ord value than v
  198.  
  199.   bool seen = false;
  200.   node z;
  201.   forall(z,Pi)
  202.   { if (z==second[v]) seen = true;
  203.     if (seen && (ord[z]<ord[v])) x[z]=x[z]+1;
  204.   }
  205.  
  206.   //I increase the x-coordinate of all nodes which follow last[v] in 
  207.   //List Pi and precede v in List L,i.e., have a smaller ord value than v
  208.  
  209.   seen = false;
  210.   forall(z,Pi)
  211.   { if (z==last[v]) seen = true;
  212.     if (seen && (ord[z]<ord[v])) x[z]=x[z]+1;
  213.   }
  214.  
  215.  
  216.    // I now embed v at the intersection of the line with slope +1
  217.    // through first[v] and the line with slope -1 through last[v]
  218.  
  219.    int x_first_v = x[first[v]];
  220.    int x_last_v =  x[last[v]];
  221.    int y_first_v = y[first[v]];
  222.    int y_last_v =  y[last[v]];
  223.  
  224.    x[v]=(y_last_v - y_first_v + x_first_v + x_last_v)/2;
  225.    y[v]=(x_last_v - x_first_v + y_first_v + y_last_v)/2;
  226.  }
  227.  
  228. // delete triangulation edges
  229.  
  230. forall(e,TL) del_edge(e);
  231.  
  232. forall_nodes(v,*this) maxcoord = Max(maxcoord,Max(x[v],y[v]));
  233. return maxcoord;
  234.  
  235. } //end of straight_embedding
  236.  
  237.  
  238. /*
  239. real planar_map::straight_line_embedding(node_array(real)& x, 
  240.                                          node_array(real)& y)
  241. { node v;
  242.   node_array(int) x0(*this), y0(*this);
  243.  
  244.   real result = straight_line_embedding(x0,y0);
  245.  
  246.   forall_nodes(v,*this) { x[v] = x0[v];
  247.                           y[v] = y0[v]; 
  248.                          }
  249.   return result;
  250. }
  251. */
  252.