doc/graphs.dox
author klao
Fri, 04 Jun 2004 16:05:20 +0000
changeset 675 38755a4d4b51
child 756 c54cf1e83039
permissions -rw-r--r--
My notes.
Sources of information and software packages which could be interesting wrt
HUGO.
ladanyi@666
     1
/*!
ladanyi@666
     2
ladanyi@666
     3
\page graphs How to use graphs
ladanyi@666
     4
ladanyi@666
     5
The following program demonstrates the basic features of HugoLib's graph
ladanyi@666
     6
structures.
ladanyi@666
     7
ladanyi@666
     8
\code
ladanyi@666
     9
#include <iostream>
ladanyi@666
    10
#include <hugo/list_graph.h>
ladanyi@666
    11
ladanyi@666
    12
using namespace hugo;
ladanyi@666
    13
ladanyi@666
    14
int main()
ladanyi@666
    15
{
ladanyi@666
    16
  typedef ListGraph Graph;
ladanyi@666
    17
\endcode
ladanyi@666
    18
ladanyi@666
    19
ListGraph is one of HugoLib's graph classes. It is based on linked lists,
ladanyi@666
    20
therefore iterating throuh its edges and nodes is fast.
ladanyi@666
    21
ladanyi@666
    22
\code
ladanyi@666
    23
  typedef Graph::Edge Edge;
ladanyi@666
    24
  typedef Graph::InEdgeIt InEdgeIt;
ladanyi@666
    25
  typedef Graph::OutEdgeIt OutEdgeIt;
ladanyi@666
    26
  typedef Graph::EdgeIt EdgeIt;
ladanyi@666
    27
  typedef Graph::Node Node;
ladanyi@666
    28
  typedef Graph::NodeIt NodeIt;
ladanyi@666
    29
ladanyi@666
    30
  Graph g;
ladanyi@666
    31
  
ladanyi@666
    32
  for (int i = 0; i < 3; i++)
ladanyi@666
    33
    g.addNode();
ladanyi@666
    34
  
ladanyi@666
    35
  for (NodeIt i(g); g.valid(i); g.next(i))
ladanyi@666
    36
    for (NodeIt j(g); g.valid(j); g.next(j))
ladanyi@666
    37
      if (i != j) g.addEdge(i, j);
ladanyi@666
    38
\endcode
ladanyi@666
    39
ladanyi@666
    40
After some convenience typedefs we create a graph and add three nodes to it.
ladanyi@666
    41
Then we add edges to it to form a full graph.
ladanyi@666
    42
ladanyi@666
    43
\code
ladanyi@666
    44
  std::cout << "Nodes:";
ladanyi@666
    45
  for (NodeIt i(g); g.valid(i); g.next(i))
ladanyi@666
    46
    std::cout << " " << g.id(i);
ladanyi@666
    47
  std::cout << std::endl;
ladanyi@666
    48
\endcode
ladanyi@666
    49
ladanyi@666
    50
Here we iterate through all nodes of the graph. We use a constructor of the
ladanyi@666
    51
node iterator to initialize it to the first node. The next member function is
ladanyi@666
    52
used to step to the next node, and valid is used to check if we have passed the
ladanyi@666
    53
last one.
ladanyi@666
    54
ladanyi@666
    55
\code
ladanyi@666
    56
  std::cout << "Nodes:";
ladanyi@666
    57
  NodeIt n;
ladanyi@666
    58
  for (g.first(n); n != INVALID; g.next(n))
ladanyi@666
    59
    std::cout << " " << g.id(n);
ladanyi@666
    60
  std::cout << std::endl;
ladanyi@666
    61
\endcode
ladanyi@666
    62
ladanyi@666
    63
Here you can see an alternative way to iterate through all nodes. Here we use a
ladanyi@666
    64
member function of the graph to initialize the node iterator to the first node
ladanyi@666
    65
of the graph. Using next on the iterator pointing to the last node invalidates
ladanyi@666
    66
the iterator i.e. sets its value to INVALID. Checking for this value is
ladanyi@666
    67
equivalent to using the valid member function.
ladanyi@666
    68
ladanyi@666
    69
Both of the previous code fragments print out the same:
ladanyi@666
    70
ladanyi@666
    71
\code
ladanyi@666
    72
Nodes: 2 1 0
ladanyi@666
    73
\endcode
ladanyi@666
    74
ladanyi@666
    75
\code
ladanyi@666
    76
  std::cout << "Edges:";
ladanyi@666
    77
  for (EdgeIt i(g); g.valid(i); g.next(i))
ladanyi@666
    78
    std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")";
ladanyi@666
    79
  std::cout << std::endl;
ladanyi@666
    80
\endcode
ladanyi@666
    81
ladanyi@666
    82
\code
ladanyi@666
    83
Edges: (0,2) (1,2) (0,1) (2,1) (1,0) (2,0)
ladanyi@666
    84
\endcode
ladanyi@666
    85
ladanyi@666
    86
We can also iterate through all edges of the graph very similarly. The head and
ladanyi@666
    87
tail member functions can be used to access the endpoints of an edge.
ladanyi@666
    88
ladanyi@666
    89
\code
ladanyi@666
    90
  NodeIt first_node(g);
ladanyi@666
    91
ladanyi@666
    92
  std::cout << "Out-edges of node " << g.id(first_node) << ":";
ladanyi@666
    93
  for (OutEdgeIt i(g, first_node); g.valid(i); g.next(i))
ladanyi@666
    94
    std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")"; 
ladanyi@666
    95
  std::cout << std::endl;
ladanyi@666
    96
ladanyi@666
    97
  std::cout << "In-edges of node " << g.id(first_node) << ":";
ladanyi@666
    98
  for (InEdgeIt i(g, first_node); g.valid(i); g.next(i))
ladanyi@666
    99
    std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")"; 
ladanyi@666
   100
  std::cout << std::endl;
ladanyi@666
   101
\endcode
ladanyi@666
   102
ladanyi@666
   103
\code
ladanyi@666
   104
Out-edges of node 2: (2,0) (2,1)
ladanyi@666
   105
In-edges of node 2: (0,2) (1,2)
ladanyi@666
   106
\endcode
ladanyi@666
   107
ladanyi@666
   108
We can also iterate through the in and out-edges of a node. In the above
ladanyi@666
   109
example we print out the in and out-edges of the first node of the graph.
ladanyi@666
   110
ladanyi@666
   111
\code
ladanyi@666
   112
  Graph::EdgeMap<int> m(g);
ladanyi@666
   113
ladanyi@666
   114
  for (EdgeIt e(g); g.valid(e); g.next(e))
ladanyi@666
   115
    m.set(e, 10 - g.id(e));
ladanyi@666
   116
  
ladanyi@666
   117
  std::cout << "Id Edge  Value" << std::endl;
ladanyi@666
   118
  for (EdgeIt e(g); g.valid(e); g.next(e))
ladanyi@666
   119
    std::cout << g.id(e) << "  (" << g.id(g.tail(e)) << "," << g.id(g.head(e))
ladanyi@666
   120
      << ") " << m[e] << std::endl;
ladanyi@666
   121
\endcode
ladanyi@666
   122
ladanyi@666
   123
\code
ladanyi@666
   124
Id Edge  Value
ladanyi@666
   125
4  (0,2) 6
ladanyi@666
   126
2  (1,2) 8
ladanyi@666
   127
5  (0,1) 5
ladanyi@666
   128
0  (2,1) 10
ladanyi@666
   129
3  (1,0) 7
ladanyi@666
   130
1  (2,0) 9
ladanyi@666
   131
\endcode
ladanyi@666
   132
ladanyi@666
   133
In generic graph optimization programming graphs are not containers rather
ladanyi@666
   134
incidence structures which are iterable in many ways. HugoLib introduces
ladanyi@666
   135
concepts that allow us to attach containers to graphs. These containers are
ladanyi@666
   136
called maps.
ladanyi@666
   137
ladanyi@666
   138
In the example above we create an EdgeMap which assigns an int value to all
ladanyi@666
   139
edges of the graph. We use the set member function of the map to write values
ladanyi@666
   140
into the map and the operator[] to retrieve them.
ladanyi@666
   141
ladanyi@666
   142
Here we used the maps provided by the ListGraph class, but you can also write
ladanyi@666
   143
your own maps. You can read more about using maps \ref maps "here".
ladanyi@666
   144
ladanyi@666
   145
*/