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

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  ugraph.h
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. #ifndef LEDA_UGRAPH_H
  12. #define LEDA_UGRAPH_H
  13. #include <LEDA/graph.h>
  14. //-----------------------------------------------------------------------------
  15. // ugraph: base class for all undirected graphs
  16. //-----------------------------------------------------------------------------
  17. /*{Manpage {ugraph} {} {Undirected Graphs}}*/
  18. class ugraph : public graph {
  19. /*{Mdefinition
  20. An instance $G$ of the data type $ugraph$ is an undirected graph as defined
  21. in section ref{Graphs}. }*/
  22. protected:
  23. edge new_edge(node v,node w, GenPtr x) {return graph::new_edge(v,w,x);}
  24. public:
  25. /*{Mcreation U }*/
  26. ugraph()  { undirected = true; }
  27. /*{Mcreate creates an instance var of type name and initializes it to 
  28.             the empty undirected graph. }*/
  29. ugraph(const graph& a)  : graph(a) { undirected = true; }
  30. ugraph(const ugraph& a) : graph(a) { undirected = true; }
  31. ~ugraph() { /* ~graph does the job */ }
  32. //subgraph constructors
  33. ugraph(ugraph&, const list<node>&, const list<edge>&);
  34. ugraph(ugraph&, const list<edge>&);
  35. ugraph& operator=(const ugraph& a) 
  36. { graph::operator=(a); undirected = true; return *this; }
  37. ugraph& operator=(const graph& a)  
  38. { graph::operator=(a); undirected= true; return *this; }
  39. /*{Moperations 2 4.5 }*/
  40. /*{Mtext
  41. see section ref{Graphs}.
  42. }*/
  43. edge new_edge(node v, node w) 
  44. { GenPtr x; init_edge_entry(x);
  45.   return graph::new_edge(v,w,x);
  46.  }
  47. edge adj_succ(edge e,node v) const 
  48. { edge r = (v==e->s) ? graph::adj_succ(e) : graph::in_succ(e); 
  49.   if (r==nil && v==e->s) r = graph::first_in_edge(v); 
  50.   return r;
  51.  }
  52. /*{op        returns the successor of edge $e$ in the
  53.       adjacency list of $v$. }*/
  54. edge adj_pred(edge e,node v) const 
  55. { edge r = (v==e->s) ? graph::adj_succ(e) : graph::in_succ(e); 
  56.   if (r==nil && v==e->t) r = graph::last_adj_edge(v); 
  57.   return r;
  58.  }
  59. /*{op        returns the predecessor of edge $e$ in the
  60.       adjacency list of $v$. }*/
  61. edge cyclic_adj_succ(edge e,node v)  const
  62. { edge r = (v==e->s) ? graph::adj_succ(e) : graph::in_succ(e); 
  63.   if (r==nil) 
  64.      r = (v==e->s) ? graph::first_in_edge(v) : graph::first_adj_edge(v); 
  65.   return r;
  66.  }
  67. /*{opl      returns the cyclic successor of edge $e$ in the
  68.       adjacency list of $v$. }*/
  69. edge cyclic_adj_pred(edge e,node v) const
  70. { edge r = (v==e->s) ? graph::adj_pred(e) : graph::in_pred(e); 
  71.   if (r==nil) 
  72.      r = (v==e->s) ? graph::last_in_edge(v) : graph::last_adj_edge(v); 
  73.   return r;
  74.  }
  75. /*{opl       returns the cyclic predecessor of edge $e$ in the
  76.       adjacency list of $v$. }*/
  77. };
  78. /*{Mimplementation
  79. see section ref{Graphs}.
  80. }*/
  81. //------------------------------------------------------------------------------
  82. // UGRAPH: generic ugraphs
  83. //------------------------------------------------------------------------------
  84. /*{Manpage {UGRAPH} {vtype,etype} {Parameterized Ugraphs}}*/
  85. template<class vtype, class etype>
  86. class UGRAPH : public ugraph {
  87. /*{Mdefinition
  88. A parameterized undirected graph $G$ is an undirected graph whose nodes and
  89. contain additional (user defined) data (cf. ref{Parameterized Graphs}). Every 
  90. node contains an element of a data type $vtype$, called the node type of $G$ 
  91. and every edge contains an element of a data type $etype$ called the edge type 
  92. of $G$. }*/
  93. char* node_type()  const { return LEDA_TYPE_NAME(vtype); }
  94. char* edge_type()  const { return LEDA_TYPE_NAME(etype); }
  95. void copy_node_entry(GenPtr& x) const  { LEDA_COPY(vtype,x); }
  96. void copy_edge_entry(GenPtr& x) const  { LEDA_COPY(etype,x); }
  97. void clear_node_entry(GenPtr& x) const { LEDA_CLEAR(vtype,x); }
  98. void clear_edge_entry(GenPtr& x) const { LEDA_CLEAR(etype,x); }
  99. void write_node_entry(ostream& o, GenPtr& x) const
  100. { LEDA_PRINT(vtype,x,o); o << endl;}
  101. void write_edge_entry(ostream& o, GenPtr& x) const { LEDA_PRINT(etype,x,o);}
  102. void read_node_entry(istream& i, GenPtr& x) { vtype X; Read(X,i); x=Copy(X); }
  103. void read_edge_entry(istream& i, GenPtr& x) { etype Y; Read(Y,i); x=Copy(Y); }
  104. void init_node_entry(GenPtr& x) { LEDA_CREATE(vtype,x); }
  105. void init_edge_entry(GenPtr& x) { LEDA_CREATE(etype,x); }
  106. void print_node_entry(ostream& o, GenPtr& x)  const
  107.      { o << "("; LEDA_PRINT(vtype,x,o); o << ")"; }
  108. void print_edge_entry(ostream& o, GenPtr& x)  const
  109.      { o << "("; LEDA_PRINT(etype,x,o); o << ")"; }
  110. public:
  111. /*{creation G }*/
  112. UGRAPH() {}
  113. /*{Mcreate creates an instance var of type name and initializes it to the
  114.     empty undirected graph.}*/
  115. UGRAPH(const UGRAPH<vtype,etype>& a): ugraph(*(ugraph*)&a) {copy_all_entries();}
  116. UGRAPH(const graph& a) : ugraph(a) { copy_all_entries(); }
  117. ~UGRAPH() { if (parent==0) clear_all_entries(); }
  118. UGRAPH<vtype,etype>& operator=(const UGRAPH<vtype,etype>& a)
  119. { clear_all_entries();
  120.   ugraph::operator=(*(ugraph*)&a);
  121.   copy_all_entries();
  122.   return *this;}
  123. UGRAPH<vtype,etype>& operator=(const graph& a)
  124. { clear_all_entries();
  125.   ugraph::operator=(a);
  126.   copy_all_entries();
  127.   return *this;
  128.  }
  129. /*{Moperations 2 4.5}*/
  130. /*{Mtext
  131. see section ref{Parameterized Graphs}.
  132. }*/
  133. int cmp_node_entry(node x, node y) const { return compare(inf(x),inf(y)); }
  134. int cmp_edge_entry(edge x, edge y) const { return compare(inf(x),inf(y)); }
  135. vtype  inf(node v)         const { return LEDA_ACCESS(vtype,ugraph::inf(v)); }
  136. /*{Xop      returns the information of node $v$ }*/ 
  137. etype  inf(edge e)         const { return LEDA_ACCESS(etype,ugraph::inf(e)); }
  138. /*{Xop    returns the information of edge $e$ }*/ 
  139. vtype& operator[] (node v)       { return LEDA_ACCESS(vtype,entry(v)); }
  140. vtype  operator[] (node v) const { return LEDA_ACCESS(vtype,ugraph::inf(v)); }
  141. etype& operator[] (edge e)       { return LEDA_ACCESS(etype,entry(e)); }
  142. etype  operator[] (edge e) const { return LEDA_ACCESS(etype,ugraph::inf(e)); }
  143. void   assign(node v,vtype x) { operator[](v) = x; }
  144. /*{Xop    makes $x$ the information of node $v$ }*/
  145. void   assign(edge e,etype x) { operator[](e) = x; }
  146. /*{Xop    makes $x$ the information of edge $e$ }*/
  147. node new_node(vtype a) { return graph::new_node(Copy(a)); }
  148. /*{Xop    adds a new node $<x>$ to $G$ and returns it}*/
  149. edge new_edge(node v, node w) { return ugraph::new_edge(v,w); }
  150. /*{Xop    inserts the undirected edge $<{v,w},edef>$ into  
  151.    $G$ by appending it to the adjacency lists of  
  152.    both $v$ and $w$ and returns it. Here, $edef$ 
  153.            is the default value of type $etype$.}*/
  154. edge new_edge(node v, node w, etype a) { return ugraph::new_edge(v,w,Copy(a)); }
  155. /*{Xopl   inserts the undirected edge $<{v,w},x>$ into  
  156.    $G$ by appending it to the adjacency lists of  
  157.    both $v$ and $w$ and returns it }*/
  158. /*{Xopl   NOT  SUPPORTED IN CURRENT RELEASE !!!!
  159.            $after, rel_pos dir2=after$) 
  160.            inserts the undirected edge $<{v,w},x>$ after 
  161.    (if $dir1=after$) or before (if $dir1=before$)  
  162.    the edge $e1$ into the adjacency list of $v$ and  
  163.    after (if $dir2=after$) or before (if $dir2=$ 
  164.    $before$) the edge $e2$ into the adjacency list 
  165.    of $w$ and returns it. }*/
  166. void clear() { clear_all_entries(); ugraph::clear(); }
  167. };
  168. /*{Mimplementation
  169. see section ref{Parameterized Graphs}.
  170. .}*/
  171. extern void complete_ugraph(ugraph&,int);
  172. extern void random_ugraph(ugraph&,int,int);
  173. extern void test_ugraph(ugraph&);
  174. #ifndef __ZTC__
  175. inline void complete_graph(ugraph& U,int n)     { complete_ugraph(U,n); }
  176. inline void random_graph(ugraph& U,int n,int m) { random_ugraph(U,n,m); }
  177. inline void test_graph(ugraph& U)               { test_ugraph(U); }
  178. #endif
  179. #endif