COIN-OR::LEMON - Graph Library

source: lemon-0.x/doc/graphs.dox @ 771:ad7dff9ee2fd

Last change on this file since 771:ad7dff9ee2fd was 756:c54cf1e83039, checked in by Alpar Juttner, 20 years ago
  • A summary of the implemented graph structures.
  • Some words on the different (and still nonexisting) graph concepts.
File size: 6.6 KB
Line 
1/*!
2
3\page graphs How to use graphs
4
5The primary data structures of HugoLib are the graph classes. They all
6provide a node list - edge list interface, i.e. they have
7functionalities to list the nodes and the edges of the graph as well
8as in incoming and outgoing edges of a given node.
9
10
11Each graph should meet the \ref ConstGraph concept. This concept does
12makes it possible to change the graph (i.e. it is not possible to add
13or delete edges or nodes). Most of the graph algorithms will run on
14these graphs.
15
16The graphs meeting the \ref ExtendableGraph concept allow node and
17edge addition. You can also "clear" (i.e. erase all edges and nodes)
18such a graph.
19
20In case of graphs meeting the full feature \ref ErasableGraph concept
21you can also erase individual edges and node in arbitrary order.
22
23The implemented graph structures are the following.
24\li \ref hugo::ListGraph "ListGraph" is the most versatile graph class. It meets
25the ErasableGraph concept and it also have some convenience features.
26\li \ref hugo::SmartGraph "SmartGraph" is a more memory
27efficient version of \ref hugo::ListGraph "ListGraph". The
28price of it is that it only meets the \ref ExtendableGraph concept,
29so 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".
33The difference is that whenever you add a
34new edge to the graph, it actually adds a pair of oppositely directed edges.
35They are linked together so it is possible to access the counterpart of an
36edge. An even more important feature is that using these classes you can also
37attach data to the edges in such a way that the stored data
38are shared by the edge pairs.
39\li \ref hugo::FullGraph "FullGraph"
40implements a full graph. It is a \ref ConstGraph, so you cannot
41change the number of nodes once it is constructed. It is extremely memory
42efficient: it uses constant amount of memory independently from the number of
43the 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
47can 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
49the edge set of another graph. The base graph can be an arbitrary graph and it
50is 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
56The graph structures itself can not store data attached
57to the edges and nodes. However they all provide
58\ref maps "map classes"
59to dynamically attach data the to graph components.
60
61
62
63
64The following program demonstrates the basic features of HugoLib's graph
65structures.
66
67\code
68#include <iostream>
69#include <hugo/list_graph.h>
70
71using namespace hugo;
72
73int main()
74{
75  typedef ListGraph Graph;
76\endcode
77
78ListGraph is one of HugoLib's graph classes. It is based on linked lists,
79therefore 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
99After some convenience typedefs we create a graph and add three nodes to it.
100Then 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
109Here we iterate through all nodes of the graph. We use a constructor of the
110node iterator to initialize it to the first node. The next member function is
111used to step to the next node, and valid is used to check if we have passed the
112last 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
122Here you can see an alternative way to iterate through all nodes. Here we use a
123member function of the graph to initialize the node iterator to the first node
124of the graph. Using next on the iterator pointing to the last node invalidates
125the iterator i.e. sets its value to INVALID. Checking for this value is
126equivalent to using the valid member function.
127
128Both of the previous code fragments print out the same:
129
130\code
131Nodes: 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
142Edges: (0,2) (1,2) (0,1) (2,1) (1,0) (2,0)
143\endcode
144
145We can also iterate through all edges of the graph very similarly. The head and
146tail 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
163Out-edges of node 2: (2,0) (2,1)
164In-edges of node 2: (0,2) (1,2)
165\endcode
166
167We can also iterate through the in and out-edges of a node. In the above
168example 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
183Id Edge  Value
1844  (0,2) 6
1852  (1,2) 8
1865  (0,1) 5
1870  (2,1) 10
1883  (1,0) 7
1891  (2,0) 9
190\endcode
191
192In generic graph optimization programming graphs are not containers rather
193incidence structures which are iterable in many ways. HugoLib introduces
194concepts that allow us to attach containers to graphs. These containers are
195called maps.
196
197In the example above we create an EdgeMap which assigns an int value to all
198edges of the graph. We use the set member function of the map to write values
199into the map and the operator[] to retrieve them.
200
201Here we used the maps provided by the ListGraph class, but you can also write
202your own maps. You can read more about using maps \ref maps "here".
203
204*/
Note: See TracBrowser for help on using the repository browser.