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

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