doc/graphs.dox
author deba
Thu, 11 Jan 2007 21:35:14 +0000
changeset 2342 4dd3eb348641
parent 2116 b6a68c15a6a3
child 2391 14a343be7a5a
permissions -rw-r--r--
Bug fix
ladanyi@666
     1
/*!
ladanyi@666
     2
ladanyi@1638
     3
\page graphs Graphs
ladanyi@666
     4
deba@2111
     5
\todo Write a new Graphs page. I think it should be contain the Graph,
deba@2111
     6
UGraph and BpUGraph concept. It should be describe the iterators and
deba@2111
     7
the basic functions and the differences of the implementations.
deba@2111
     8
alpar@921
     9
The primary data structures of LEMON are the graph classes. They all
alpar@756
    10
provide a node list - edge list interface, i.e. they have
alpar@756
    11
functionalities to list the nodes and the edges of the graph as well
deba@2116
    12
as  incoming and outgoing edges of a given node. 
alpar@756
    13
alpar@2260
    14
Each graph should meet the \ref lemon::concepts::Graph "Graph" concept.
deba@2116
    15
This concept does not make it possible to change the graph (i.e. it is
deba@2116
    16
not possible to add or delete edges or nodes). Most of the graph
deba@2116
    17
algorithms will run on these graphs.
alpar@756
    18
alpar@756
    19
deba@2116
    20
In case of graphs meeting the full feature
alpar@2260
    21
\ref lemon::concepts::ErasableGraph "ErasableGraph"
deba@2116
    22
concept
deba@2116
    23
you can also erase individual edges and nodes in arbitrary order.
deba@2116
    24
deba@2116
    25
The implemented graph structures are the following.
alpar@921
    26
\li \ref lemon::ListGraph "ListGraph" is the most versatile graph class. It meets
alpar@2260
    27
the \ref lemon::concepts::ErasableGraph "ErasableGraph" concept
athos@1168
    28
and it also has some convenient extra features.
alpar@921
    29
\li \ref lemon::SmartGraph "SmartGraph" is a more memory
alpar@921
    30
efficient version of \ref lemon::ListGraph "ListGraph". The
athos@1168
    31
price of this is that it only meets the
alpar@2260
    32
\ref lemon::concepts::ExtendableGraph "ExtendableGraph" concept,
alpar@756
    33
so you cannot delete individual edges or nodes.
alpar@921
    34
\li \ref lemon::FullGraph "FullGraph"
alpar@1200
    35
implements a complete graph. It is a
alpar@2260
    36
\ref lemon::concepts::Graph "Graph", so you cannot
alpar@756
    37
change the number of nodes once it is constructed. It is extremely memory
alpar@756
    38
efficient: it uses constant amount of memory independently from the number of
alpar@1043
    39
the nodes of the graph. Of course, the size of the \ref maps-page "NodeMap"'s and
alpar@1043
    40
\ref maps-page "EdgeMap"'s will depend on the number of nodes.
alpar@756
    41
alpar@921
    42
\li \ref lemon::NodeSet "NodeSet" implements a graph with no edges. This class
alpar@921
    43
can be used as a base class of \ref lemon::EdgeSet "EdgeSet".
alpar@921
    44
\li \ref lemon::EdgeSet "EdgeSet" can be used to create a new graph on
alpar@873
    45
the node set of another graph. The base graph can be an arbitrary graph and it
alpar@921
    46
is possible to attach several \ref lemon::EdgeSet "EdgeSet"'s to a base graph.
alpar@756
    47
alpar@756
    48
\todo Don't we need SmartNodeSet and SmartEdgeSet?
alpar@756
    49
\todo Some cross-refs are wrong.
alpar@756
    50
athos@1168
    51
The graph structures themselves can not store data attached
alpar@756
    52
to the edges and nodes. However they all provide
alpar@1043
    53
\ref maps-page "map classes"
alpar@756
    54
to dynamically attach data the to graph components.
alpar@756
    55
alpar@921
    56
The following program demonstrates the basic features of LEMON's graph
ladanyi@666
    57
structures.
ladanyi@666
    58
ladanyi@666
    59
\code
ladanyi@666
    60
#include <iostream>
alpar@921
    61
#include <lemon/list_graph.h>
ladanyi@666
    62
alpar@921
    63
using namespace lemon;
ladanyi@666
    64
ladanyi@666
    65
int main()
ladanyi@666
    66
{
ladanyi@666
    67
  typedef ListGraph Graph;
ladanyi@666
    68
\endcode
ladanyi@666
    69
alpar@921
    70
ListGraph is one of LEMON's graph classes. It is based on linked lists,
ladanyi@666
    71
therefore iterating throuh its edges and nodes is fast.
ladanyi@666
    72
ladanyi@666
    73
\code
ladanyi@666
    74
  typedef Graph::Edge Edge;
ladanyi@666
    75
  typedef Graph::InEdgeIt InEdgeIt;
ladanyi@666
    76
  typedef Graph::OutEdgeIt OutEdgeIt;
ladanyi@666
    77
  typedef Graph::EdgeIt EdgeIt;
ladanyi@666
    78
  typedef Graph::Node Node;
ladanyi@666
    79
  typedef Graph::NodeIt NodeIt;
ladanyi@666
    80
ladanyi@666
    81
  Graph g;
ladanyi@666
    82
  
ladanyi@666
    83
  for (int i = 0; i < 3; i++)
ladanyi@666
    84
    g.addNode();
ladanyi@666
    85
  
ladanyi@875
    86
  for (NodeIt i(g); i!=INVALID; ++i)
ladanyi@875
    87
    for (NodeIt j(g); j!=INVALID; ++j)
ladanyi@666
    88
      if (i != j) g.addEdge(i, j);
ladanyi@666
    89
\endcode
ladanyi@666
    90
athos@1168
    91
After some convenient typedefs we create a graph and add three nodes to it.
athos@1168
    92
Then we add edges to it to form a complete graph.
ladanyi@666
    93
ladanyi@666
    94
\code
ladanyi@666
    95
  std::cout << "Nodes:";
ladanyi@875
    96
  for (NodeIt i(g); i!=INVALID; ++i)
ladanyi@666
    97
    std::cout << " " << g.id(i);
ladanyi@666
    98
  std::cout << std::endl;
ladanyi@666
    99
\endcode
ladanyi@666
   100
ladanyi@666
   101
Here we iterate through all nodes of the graph. We use a constructor of the
ladanyi@875
   102
node iterator to initialize it to the first node. The operator++ is used to
ladanyi@875
   103
step to the next node. Using operator++ on the iterator pointing to the last
ladanyi@875
   104
node invalidates the iterator i.e. sets its value to
alpar@921
   105
\ref lemon::INVALID "INVALID". This is what we exploit in the stop condition.
ladanyi@666
   106
ladanyi@875
   107
The previous code fragment prints out the following:
ladanyi@666
   108
ladanyi@666
   109
\code
ladanyi@666
   110
Nodes: 2 1 0
ladanyi@666
   111
\endcode
ladanyi@666
   112
ladanyi@666
   113
\code
ladanyi@666
   114
  std::cout << "Edges:";
ladanyi@875
   115
  for (EdgeIt i(g); i!=INVALID; ++i)
alpar@986
   116
    std::cout << " (" << g.id(g.source(i)) << "," << g.id(g.target(i)) << ")";
ladanyi@666
   117
  std::cout << std::endl;
ladanyi@666
   118
\endcode
ladanyi@666
   119
ladanyi@666
   120
\code
ladanyi@666
   121
Edges: (0,2) (1,2) (0,1) (2,1) (1,0) (2,0)
ladanyi@666
   122
\endcode
ladanyi@666
   123
athos@1168
   124
We can also iterate through all edges of the graph very similarly. The 
athos@1168
   125
\c target and
athos@1168
   126
\c source member functions can be used to access the endpoints of an edge.
ladanyi@666
   127
ladanyi@666
   128
\code
ladanyi@666
   129
  NodeIt first_node(g);
ladanyi@666
   130
ladanyi@666
   131
  std::cout << "Out-edges of node " << g.id(first_node) << ":";
ladanyi@875
   132
  for (OutEdgeIt i(g, first_node); i!=INVALID; ++i)
alpar@986
   133
    std::cout << " (" << g.id(g.source(i)) << "," << g.id(g.target(i)) << ")"; 
ladanyi@666
   134
  std::cout << std::endl;
ladanyi@666
   135
ladanyi@666
   136
  std::cout << "In-edges of node " << g.id(first_node) << ":";
ladanyi@875
   137
  for (InEdgeIt i(g, first_node); i!=INVALID; ++i)
alpar@986
   138
    std::cout << " (" << g.id(g.source(i)) << "," << g.id(g.target(i)) << ")"; 
ladanyi@666
   139
  std::cout << std::endl;
ladanyi@666
   140
\endcode
ladanyi@666
   141
ladanyi@666
   142
\code
ladanyi@666
   143
Out-edges of node 2: (2,0) (2,1)
ladanyi@666
   144
In-edges of node 2: (0,2) (1,2)
ladanyi@666
   145
\endcode
ladanyi@666
   146
ladanyi@666
   147
We can also iterate through the in and out-edges of a node. In the above
ladanyi@666
   148
example we print out the in and out-edges of the first node of the graph.
ladanyi@666
   149
ladanyi@666
   150
\code
ladanyi@666
   151
  Graph::EdgeMap<int> m(g);
ladanyi@666
   152
ladanyi@875
   153
  for (EdgeIt e(g); e!=INVALID; ++e)
ladanyi@666
   154
    m.set(e, 10 - g.id(e));
ladanyi@666
   155
  
ladanyi@666
   156
  std::cout << "Id Edge  Value" << std::endl;
ladanyi@875
   157
  for (EdgeIt e(g); e!=INVALID; ++e)
alpar@986
   158
    std::cout << g.id(e) << "  (" << g.id(g.source(e)) << "," << g.id(g.target(e))
ladanyi@666
   159
      << ") " << m[e] << std::endl;
ladanyi@666
   160
\endcode
ladanyi@666
   161
ladanyi@666
   162
\code
ladanyi@666
   163
Id Edge  Value
ladanyi@666
   164
4  (0,2) 6
ladanyi@666
   165
2  (1,2) 8
ladanyi@666
   166
5  (0,1) 5
ladanyi@666
   167
0  (2,1) 10
ladanyi@666
   168
3  (1,0) 7
ladanyi@666
   169
1  (2,0) 9
ladanyi@666
   170
\endcode
ladanyi@666
   171
alpar@873
   172
As we mentioned above, graphs are not containers rather
alpar@921
   173
incidence structures which are iterable in many ways. LEMON introduces
ladanyi@666
   174
concepts that allow us to attach containers to graphs. These containers are
ladanyi@666
   175
called maps.
ladanyi@666
   176
athos@1168
   177
In the example above we create an EdgeMap which assigns an integer value to all
ladanyi@666
   178
edges of the graph. We use the set member function of the map to write values
ladanyi@666
   179
into the map and the operator[] to retrieve them.
ladanyi@666
   180
ladanyi@666
   181
Here we used the maps provided by the ListGraph class, but you can also write
alpar@1043
   182
your own maps. You can read more about using maps \ref maps-page "here".
ladanyi@666
   183
ladanyi@666
   184
*/