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