doc/graphs.dox
author alpar
Mon, 06 Sep 2004 08:59:25 +0000
changeset 807 ce85435185c3
parent 666 410a1419e86b
child 808 9cabbdd73375
permissions -rw-r--r--
Correction in doc: skeleton/path.h has been moved to the 'skeletons' module.
     1 /*!
     2 
     3 \page graphs How to use graphs
     4 
     5 The primary data structures of HugoLib are the graph classes. They all
     6 provide a node list - edge list interface, i.e. they have
     7 functionalities to list the nodes and the edges of the graph as well
     8 as in incoming and outgoing edges of a given node. 
     9 
    10 
    11 Each graph should meet the \ref ConstGraph concept. This concept does
    12 makes it possible to change the graph (i.e. it is not possible to add
    13 or delete edges or nodes). Most of the graph algorithms will run on
    14 these graphs.
    15 
    16 The graphs meeting the \ref ExtendableGraph concept allow node and
    17 edge addition. You can also "clear" (i.e. erase all edges and nodes)
    18 such a graph.
    19 
    20 In case of graphs meeting the full feature \ref ErasableGraph concept
    21 you can also erase individual edges and node in arbitrary order.
    22 
    23 The implemented graph structures are the following.
    24 \li \ref hugo::ListGraph "ListGraph" is the most versatile graph class. It meets
    25 the ErasableGraph concept and it also have some convenience features.
    26 \li \ref hugo::SmartGraph "SmartGraph" is a more memory
    27 efficient version of \ref hugo::ListGraph "ListGraph". The
    28 price of it is that it only meets the \ref ExtendableGraph concept,
    29 so you cannot delete individual edges or nodes.
    30 \li \ref hugo::SymListGraph "SymListGraph" and
    31 \ref hugo::SymSmartGraph "SymSmartGraph" classes are very similar to
    32 \ref hugo::ListGraph "ListGraph" and \ref hugo::SmartGraph "SmartGraph".
    33 The difference is that whenever you add a
    34 new edge to the graph, it actually adds a pair of oppositely directed edges.
    35 They are linked together so it is possible to access the counterpart of an
    36 edge. An even more important feature is that using these classes you can also
    37 attach data to the edges in such a way that the stored data
    38 are shared by the edge pairs. 
    39 \li \ref hugo::FullGraph "FullGraph"
    40 implements a full graph. It is a \ref ConstGraph, so you cannot
    41 change the number of nodes once it is constructed. It is extremely memory
    42 efficient: it uses constant amount of memory independently from the number of
    43 the nodes of the graph. Of course, the size of the \ref maps "NodeMap"'s and
    44 \ref maps "EdgeMap"'s will depend on the number of nodes.
    45 
    46 \li \ref hugo::NodeSet "NodeSet" implements a graph with no edges. This class
    47 can be used as a base class of \ref hugo::EdgeSet "EdgeSet".
    48 \li \ref hugo::EdgeSet "EdgeSet" can be used to create a new graph on
    49 the edge set of another graph. The base graph can be an arbitrary graph and it
    50 is possible to attach several \ref hugo::EdgeSet "EdgeSet"'s to a base graph.
    51 
    52 \todo Don't we need SmartNodeSet and SmartEdgeSet?
    53 \todo Some cross-refs are wrong.
    54 
    55 
    56 The graph structures itself can not store data attached
    57 to the edges and nodes. However they all provide
    58 \ref maps "map classes"
    59 to dynamically attach data the to graph components.
    60 
    61 
    62 
    63 
    64 The following program demonstrates the basic features of HugoLib's graph
    65 structures.
    66 
    67 \code
    68 #include <iostream>
    69 #include <hugo/list_graph.h>
    70 
    71 using namespace hugo;
    72 
    73 int main()
    74 {
    75   typedef ListGraph Graph;
    76 \endcode
    77 
    78 ListGraph is one of HugoLib's graph classes. It is based on linked lists,
    79 therefore iterating throuh its edges and nodes is fast.
    80 
    81 \code
    82   typedef Graph::Edge Edge;
    83   typedef Graph::InEdgeIt InEdgeIt;
    84   typedef Graph::OutEdgeIt OutEdgeIt;
    85   typedef Graph::EdgeIt EdgeIt;
    86   typedef Graph::Node Node;
    87   typedef Graph::NodeIt NodeIt;
    88 
    89   Graph g;
    90   
    91   for (int i = 0; i < 3; i++)
    92     g.addNode();
    93   
    94   for (NodeIt i(g); g.valid(i); g.next(i))
    95     for (NodeIt j(g); g.valid(j); g.next(j))
    96       if (i != j) g.addEdge(i, j);
    97 \endcode
    98 
    99 After some convenience typedefs we create a graph and add three nodes to it.
   100 Then we add edges to it to form a full graph.
   101 
   102 \code
   103   std::cout << "Nodes:";
   104   for (NodeIt i(g); g.valid(i); g.next(i))
   105     std::cout << " " << g.id(i);
   106   std::cout << std::endl;
   107 \endcode
   108 
   109 Here we iterate through all nodes of the graph. We use a constructor of the
   110 node iterator to initialize it to the first node. The next member function is
   111 used to step to the next node, and valid is used to check if we have passed the
   112 last one.
   113 
   114 \code
   115   std::cout << "Nodes:";
   116   NodeIt n;
   117   for (g.first(n); n != INVALID; g.next(n))
   118     std::cout << " " << g.id(n);
   119   std::cout << std::endl;
   120 \endcode
   121 
   122 Here you can see an alternative way to iterate through all nodes. Here we use a
   123 member function of the graph to initialize the node iterator to the first node
   124 of the graph. Using next on the iterator pointing to the last node invalidates
   125 the iterator i.e. sets its value to INVALID. Checking for this value is
   126 equivalent to using the valid member function.
   127 
   128 Both of the previous code fragments print out the same:
   129 
   130 \code
   131 Nodes: 2 1 0
   132 \endcode
   133 
   134 \code
   135   std::cout << "Edges:";
   136   for (EdgeIt i(g); g.valid(i); g.next(i))
   137     std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")";
   138   std::cout << std::endl;
   139 \endcode
   140 
   141 \code
   142 Edges: (0,2) (1,2) (0,1) (2,1) (1,0) (2,0)
   143 \endcode
   144 
   145 We can also iterate through all edges of the graph very similarly. The head and
   146 tail member functions can be used to access the endpoints of an edge.
   147 
   148 \code
   149   NodeIt first_node(g);
   150 
   151   std::cout << "Out-edges of node " << g.id(first_node) << ":";
   152   for (OutEdgeIt i(g, first_node); g.valid(i); g.next(i))
   153     std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")"; 
   154   std::cout << std::endl;
   155 
   156   std::cout << "In-edges of node " << g.id(first_node) << ":";
   157   for (InEdgeIt i(g, first_node); g.valid(i); g.next(i))
   158     std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")"; 
   159   std::cout << std::endl;
   160 \endcode
   161 
   162 \code
   163 Out-edges of node 2: (2,0) (2,1)
   164 In-edges of node 2: (0,2) (1,2)
   165 \endcode
   166 
   167 We can also iterate through the in and out-edges of a node. In the above
   168 example we print out the in and out-edges of the first node of the graph.
   169 
   170 \code
   171   Graph::EdgeMap<int> m(g);
   172 
   173   for (EdgeIt e(g); g.valid(e); g.next(e))
   174     m.set(e, 10 - g.id(e));
   175   
   176   std::cout << "Id Edge  Value" << std::endl;
   177   for (EdgeIt e(g); g.valid(e); g.next(e))
   178     std::cout << g.id(e) << "  (" << g.id(g.tail(e)) << "," << g.id(g.head(e))
   179       << ") " << m[e] << std::endl;
   180 \endcode
   181 
   182 \code
   183 Id Edge  Value
   184 4  (0,2) 6
   185 2  (1,2) 8
   186 5  (0,1) 5
   187 0  (2,1) 10
   188 3  (1,0) 7
   189 1  (2,0) 9
   190 \endcode
   191 
   192 In generic graph optimization programming graphs are not containers rather
   193 incidence structures which are iterable in many ways. HugoLib introduces
   194 concepts that allow us to attach containers to graphs. These containers are
   195 called maps.
   196 
   197 In the example above we create an EdgeMap which assigns an int value to all
   198 edges of the graph. We use the set member function of the map to write values
   199 into the map and the operator[] to retrieve them.
   200 
   201 Here we used the maps provided by the ListGraph class, but you can also write
   202 your own maps. You can read more about using maps \ref maps "here".
   203 
   204 */