Is_Bidirected.w
上传用户:gzelex
上传日期:2007-01-07
资源大小:707k
文件大小:3k
开发平台:

MultiPlatform

  1. documentstyle[11pt,comments]{cweb}
  2. textwidth=6.5in
  3. textheight=9in
  4. oddsidemargin=0in
  5. topmargin=0in
  6. begin{document}
  7. markright{fbox{protecttiny version of today}}
  8. pagestyle{myheadings}
  9. @* Testing Bidirectedness.
  10. noindent Let $G$ be a simple graph. We test whether $G$ is bidirected
  11. and (if required) also establish the correspondence between edges and
  12. their reversals. We first bucket--sort the edges according to their
  13. target node, then according to their source node. The result is 
  14. maintained in a list. Afterwards we sort the edges again, but first 
  15. according to their source node and then according to their target node.
  16. The result is maintained in a second list. Since |bucket sort| is 
  17. stable,
  18. in a bidirected graph the position of an edge in the first list
  19. corresponds to the position of the reversal edge in the second
  20. list. The following procedure 
  21. checks this relation. 
  22. As soon as the tested edges do not correspond, false is returned. 
  23. @c
  24. bool is_bidirected(const graph& G)     
  25. {
  26.  // computes for every edge e = (v,w) in G its reversal reversal[e] = (w,v)
  27.  // in G ( nil if not present). Returns true if every edge has a
  28.  // reversal and false otherwise.
  29.   int n = G.max_i_node();
  30.   int count = 0;
  31.   edge e,r;
  32.   list<edge> El = G.all_edges();
  33.   El.bucket_sort(0,n,&edge_ord2);
  34.   El.bucket_sort(0,n,&edge_ord1);
  35.   
  36.   list<edge> El1 = G.all_edges();
  37.   El1.bucket_sort(0,n,&edge_ord1);
  38.   El1.bucket_sort(0,n,&edge_ord2);
  39.   // merge El and El1 to find corresponding edges
  40.   while (! El.empty() && ! El1.empty())
  41.   { e = El.head();
  42.     r = El1.head();
  43.     if ((target(r) == source(e))&&(source(r) == target(e))) 
  44.          { El1.pop();
  45.            El.pop();
  46.           }
  47.     else
  48.          return false;
  49.   }
  50.   return true;
  51. }
  52. @ The following procedure 
  53. also establishes the correspondence between edges and their reversals.
  54. For every edge |e = (v, w)| in $G$ it computes its reversal
  55. |reversal[e] = (w,v)| if existing in $G$ (|nil| otherwise). If every
  56. edge has a reversal it returns |true| and |false| otherwise.
  57. @c
  58. bool is_bidirected(const graph& G, edge_array<edge>& reversal)     
  59. {
  60.  // computes for every edge e = (v,w) in G its reversal reversal[e] = (w,v)
  61.  // in G ( nil if not present). Returns true if every edge has a
  62.  // reversal and false otherwise.
  63.   int n = G.max_i_node();
  64.   int count = 0;
  65.   edge e,r;
  66.   forall_edges(e,G) reversal[e] = 0;
  67.   list<edge> El = G.all_edges();
  68.   El.bucket_sort(0,n,&edge_ord2);
  69.   El.bucket_sort(0,n,&edge_ord1);
  70.   
  71.   list<edge> El1 = G.all_edges();
  72.   El1.bucket_sort(0,n,&edge_ord1);
  73.   El1.bucket_sort(0,n,&edge_ord2);
  74.   // merge El and El1 to find corresponding edges
  75.   while (! El.empty() && ! El1.empty())
  76.   { e = El.head();
  77.     r = El1.head();
  78.     if (target(r) == source(e))
  79.       if (source(r) == target(e))
  80.          { reversal[e] = r;
  81.            El1.pop();
  82.            El.pop();
  83.            count++;
  84.           }
  85.       else
  86.          if (index(source(r)) < index(target(e)))
  87.              El1.pop();
  88.          else
  89.              El.pop();
  90.     else
  91.       if (index(target(r)) < index(source(e)))
  92.           El1.pop();
  93.       else
  94.           El.pop();
  95.     }
  96.   return (count == G.number_of_edges());
  97. }
  98.  
  99. @
  100. end{document}