doc/graphs.dox
author deba
Fri, 11 Mar 2005 18:54:18 +0000
changeset 1214 39993ada11c7
parent 1168 66400041ef2a
child 1631 e15162d8eca1
permissions -rw-r--r--
Changing exception names.
Compile sentry fix.
ladanyi@666
     1
/*!
ladanyi@666
     2
ladanyi@666
     3
\page graphs How to use graphs
ladanyi@666
     4
alpar@921
     5
The primary data structures of LEMON 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
athos@1168
     8
as  incoming and outgoing edges of a given node. 
alpar@756
     9
alpar@756
    10
alpar@873
    11
Each graph should meet the
klao@959
    12
\ref lemon::concept::StaticGraph "StaticGraph" concept.
alpar@873
    13
This concept does not
athos@1168
    14
make 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
klao@959
    19
\ref lemon::concept::ExtendableGraph "ExtendableGraph"
alpar@873
    20
concept allow node and
athos@1168
    21
edge addition. You can also "clear" such a graph (i.e. erase all edges and nodes ).
alpar@756
    22
alpar@873
    23
In case of graphs meeting the full feature
klao@959
    24
\ref lemon::concept::ErasableGraph "ErasableGraph"
alpar@873
    25
concept
athos@1168
    26
you can also erase individual edges and nodes in arbitrary order.
alpar@756
    27
alpar@756
    28
The implemented graph structures are the following.
alpar@921
    29
\li \ref lemon::ListGraph "ListGraph" is the most versatile graph class. It meets
klao@959
    30
the \ref lemon::concept::ErasableGraph "ErasableGraph" concept
athos@1168
    31
and it also has some convenient extra features.
alpar@921
    32
\li \ref lemon::SmartGraph "SmartGraph" is a more memory
alpar@921
    33
efficient version of \ref lemon::ListGraph "ListGraph". The
athos@1168
    34
price of this is that it only meets the
klao@959
    35
\ref lemon::concept::ExtendableGraph "ExtendableGraph" concept,
alpar@756
    36
so you cannot delete individual edges or nodes.
alpar@921
    37
\li \ref lemon::SymListGraph "SymListGraph" and
alpar@921
    38
\ref lemon::SymSmartGraph "SymSmartGraph" classes are very similar to
alpar@921
    39
\ref lemon::ListGraph "ListGraph" and \ref lemon::SmartGraph "SmartGraph".
alpar@756
    40
The difference is that whenever you add a
alpar@756
    41
new edge to the graph, it actually adds a pair of oppositely directed edges.
alpar@756
    42
They are linked together so it is possible to access the counterpart of an
alpar@756
    43
edge. An even more important feature is that using these classes you can also
alpar@756
    44
attach data to the edges in such a way that the stored data
alpar@756
    45
are shared by the edge pairs. 
alpar@921
    46
\li \ref lemon::FullGraph "FullGraph"
alpar@1200
    47
implements a complete graph. It is a
alpar@1200
    48
\ref lemon::concept::StaticGraph "StaticGraph", 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@1043
    51
the nodes of the graph. Of course, the size of the \ref maps-page "NodeMap"'s and
alpar@1043
    52
\ref maps-page "EdgeMap"'s will depend on the number of nodes.
alpar@756
    53
alpar@921
    54
\li \ref lemon::NodeSet "NodeSet" implements a graph with no edges. This class
alpar@921
    55
can be used as a base class of \ref lemon::EdgeSet "EdgeSet".
alpar@921
    56
\li \ref lemon::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@921
    58
is possible to attach several \ref lemon::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
athos@1168
    63
The graph structures themselves can not store data attached
alpar@756
    64
to the edges and nodes. However they all provide
alpar@1043
    65
\ref maps-page "map classes"
alpar@756
    66
to dynamically attach data the to graph components.
alpar@756
    67
alpar@921
    68
The following program demonstrates the basic features of LEMON's graph
ladanyi@666
    69
structures.
ladanyi@666
    70
ladanyi@666
    71
\code
ladanyi@666
    72
#include <iostream>
alpar@921
    73
#include <lemon/list_graph.h>
ladanyi@666
    74
alpar@921
    75
using namespace lemon;
ladanyi@666
    76
ladanyi@666
    77
int main()
ladanyi@666
    78
{
ladanyi@666
    79
  typedef ListGraph Graph;
ladanyi@666
    80
\endcode
ladanyi@666
    81
alpar@921
    82
ListGraph is one of LEMON's graph classes. It is based on linked lists,
ladanyi@666
    83
therefore iterating throuh its edges and nodes is fast.
ladanyi@666
    84
ladanyi@666
    85
\code
ladanyi@666
    86
  typedef Graph::Edge Edge;
ladanyi@666
    87
  typedef Graph::InEdgeIt InEdgeIt;
ladanyi@666
    88
  typedef Graph::OutEdgeIt OutEdgeIt;
ladanyi@666
    89
  typedef Graph::EdgeIt EdgeIt;
ladanyi@666
    90
  typedef Graph::Node Node;
ladanyi@666
    91
  typedef Graph::NodeIt NodeIt;
ladanyi@666
    92
ladanyi@666
    93
  Graph g;
ladanyi@666
    94
  
ladanyi@666
    95
  for (int i = 0; i < 3; i++)
ladanyi@666
    96
    g.addNode();
ladanyi@666
    97
  
ladanyi@875
    98
  for (NodeIt i(g); i!=INVALID; ++i)
ladanyi@875
    99
    for (NodeIt j(g); j!=INVALID; ++j)
ladanyi@666
   100
      if (i != j) g.addEdge(i, j);
ladanyi@666
   101
\endcode
ladanyi@666
   102
athos@1168
   103
After some convenient typedefs we create a graph and add three nodes to it.
athos@1168
   104
Then we add edges to it to form a complete graph.
ladanyi@666
   105
ladanyi@666
   106
\code
ladanyi@666
   107
  std::cout << "Nodes:";
ladanyi@875
   108
  for (NodeIt i(g); i!=INVALID; ++i)
ladanyi@666
   109
    std::cout << " " << g.id(i);
ladanyi@666
   110
  std::cout << std::endl;
ladanyi@666
   111
\endcode
ladanyi@666
   112
ladanyi@666
   113
Here we iterate through all nodes of the graph. We use a constructor of the
ladanyi@875
   114
node iterator to initialize it to the first node. The operator++ is used to
ladanyi@875
   115
step to the next node. Using operator++ on the iterator pointing to the last
ladanyi@875
   116
node invalidates the iterator i.e. sets its value to
alpar@921
   117
\ref lemon::INVALID "INVALID". This is what we exploit in the stop condition.
ladanyi@666
   118
ladanyi@875
   119
The previous code fragment prints out the following:
ladanyi@666
   120
ladanyi@666
   121
\code
ladanyi@666
   122
Nodes: 2 1 0
ladanyi@666
   123
\endcode
ladanyi@666
   124
ladanyi@666
   125
\code
ladanyi@666
   126
  std::cout << "Edges:";
ladanyi@875
   127
  for (EdgeIt i(g); i!=INVALID; ++i)
alpar@986
   128
    std::cout << " (" << g.id(g.source(i)) << "," << g.id(g.target(i)) << ")";
ladanyi@666
   129
  std::cout << std::endl;
ladanyi@666
   130
\endcode
ladanyi@666
   131
ladanyi@666
   132
\code
ladanyi@666
   133
Edges: (0,2) (1,2) (0,1) (2,1) (1,0) (2,0)
ladanyi@666
   134
\endcode
ladanyi@666
   135
athos@1168
   136
We can also iterate through all edges of the graph very similarly. The 
athos@1168
   137
\c target and
athos@1168
   138
\c source member functions can be used to access the endpoints of an edge.
ladanyi@666
   139
ladanyi@666
   140
\code
ladanyi@666
   141
  NodeIt first_node(g);
ladanyi@666
   142
ladanyi@666
   143
  std::cout << "Out-edges of node " << g.id(first_node) << ":";
ladanyi@875
   144
  for (OutEdgeIt i(g, first_node); i!=INVALID; ++i)
alpar@986
   145
    std::cout << " (" << g.id(g.source(i)) << "," << g.id(g.target(i)) << ")"; 
ladanyi@666
   146
  std::cout << std::endl;
ladanyi@666
   147
ladanyi@666
   148
  std::cout << "In-edges of node " << g.id(first_node) << ":";
ladanyi@875
   149
  for (InEdgeIt i(g, first_node); i!=INVALID; ++i)
alpar@986
   150
    std::cout << " (" << g.id(g.source(i)) << "," << g.id(g.target(i)) << ")"; 
ladanyi@666
   151
  std::cout << std::endl;
ladanyi@666
   152
\endcode
ladanyi@666
   153
ladanyi@666
   154
\code
ladanyi@666
   155
Out-edges of node 2: (2,0) (2,1)
ladanyi@666
   156
In-edges of node 2: (0,2) (1,2)
ladanyi@666
   157
\endcode
ladanyi@666
   158
ladanyi@666
   159
We can also iterate through the in and out-edges of a node. In the above
ladanyi@666
   160
example we print out the in and out-edges of the first node of the graph.
ladanyi@666
   161
ladanyi@666
   162
\code
ladanyi@666
   163
  Graph::EdgeMap<int> m(g);
ladanyi@666
   164
ladanyi@875
   165
  for (EdgeIt e(g); e!=INVALID; ++e)
ladanyi@666
   166
    m.set(e, 10 - g.id(e));
ladanyi@666
   167
  
ladanyi@666
   168
  std::cout << "Id Edge  Value" << std::endl;
ladanyi@875
   169
  for (EdgeIt e(g); e!=INVALID; ++e)
alpar@986
   170
    std::cout << g.id(e) << "  (" << g.id(g.source(e)) << "," << g.id(g.target(e))
ladanyi@666
   171
      << ") " << m[e] << std::endl;
ladanyi@666
   172
\endcode
ladanyi@666
   173
ladanyi@666
   174
\code
ladanyi@666
   175
Id Edge  Value
ladanyi@666
   176
4  (0,2) 6
ladanyi@666
   177
2  (1,2) 8
ladanyi@666
   178
5  (0,1) 5
ladanyi@666
   179
0  (2,1) 10
ladanyi@666
   180
3  (1,0) 7
ladanyi@666
   181
1  (2,0) 9
ladanyi@666
   182
\endcode
ladanyi@666
   183
alpar@873
   184
As we mentioned above, graphs are not containers rather
alpar@921
   185
incidence structures which are iterable in many ways. LEMON introduces
ladanyi@666
   186
concepts that allow us to attach containers to graphs. These containers are
ladanyi@666
   187
called maps.
ladanyi@666
   188
athos@1168
   189
In the example above we create an EdgeMap which assigns an integer value to all
ladanyi@666
   190
edges of the graph. We use the set member function of the map to write values
ladanyi@666
   191
into the map and the operator[] to retrieve them.
ladanyi@666
   192
ladanyi@666
   193
Here we used the maps provided by the ListGraph class, but you can also write
alpar@1043
   194
your own maps. You can read more about using maps \ref maps-page "here".
ladanyi@666
   195
ladanyi@666
   196
*/