COIN-OR::LEMON - Graph Library

source: lemon-0.x/doc/graphs.dox @ 939:1559584ca634

Last change on this file since 939:1559584ca634 was 921:818510fa3d99, checked in by Alpar Juttner, 20 years ago

hugo -> lemon

File size: 6.4 KB
Line 
1/*!
2
3\page graphs How to use graphs
4
5The primary data structures of LEMON 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
12\ref lemon::skeleton::StaticGraph "StaticGraph" concept.
13This concept does not
14makes it possible to change the graph (i.e. it is not possible to add
15or delete edges or nodes). Most of the graph algorithms will run on
16these graphs.
17
18The graphs meeting the
19\ref lemon::skeleton::ExtendableGraph "ExtendableGraph"
20concept allow node and
21edge addition. You can also "clear" (i.e. erase all edges and nodes)
22such a graph.
23
24In case of graphs meeting the full feature
25\ref lemon::skeleton::ErasableGraph "ErasableGraph"
26concept
27you can also erase individual edges and node in arbitrary order.
28
29The implemented graph structures are the following.
30\li \ref lemon::ListGraph "ListGraph" is the most versatile graph class. It meets
31the \ref lemon::skeleton::ErasableGraph "ErasableGraph" concept
32and it also have some convenience features.
33\li \ref lemon::SmartGraph "SmartGraph" is a more memory
34efficient version of \ref lemon::ListGraph "ListGraph". The
35price of it is that it only meets the
36\ref lemon::skeleton::ExtendableGraph "ExtendableGraph" concept,
37so you cannot delete individual edges or nodes.
38\li \ref lemon::SymListGraph "SymListGraph" and
39\ref lemon::SymSmartGraph "SymSmartGraph" classes are very similar to
40\ref lemon::ListGraph "ListGraph" and \ref lemon::SmartGraph "SmartGraph".
41The difference is that whenever you add a
42new edge to the graph, it actually adds a pair of oppositely directed edges.
43They are linked together so it is possible to access the counterpart of an
44edge. An even more important feature is that using these classes you can also
45attach data to the edges in such a way that the stored data
46are shared by the edge pairs.
47\li \ref lemon::FullGraph "FullGraph"
48implements a full graph. It is a \ref lemon::skeleton::StaticGraph, so you cannot
49change the number of nodes once it is constructed. It is extremely memory
50efficient: it uses constant amount of memory independently from the number of
51the nodes of the graph. Of course, the size of the \ref maps "NodeMap"'s and
52\ref maps "EdgeMap"'s will depend on the number of nodes.
53
54\li \ref lemon::NodeSet "NodeSet" implements a graph with no edges. This class
55can be used as a base class of \ref lemon::EdgeSet "EdgeSet".
56\li \ref lemon::EdgeSet "EdgeSet" can be used to create a new graph on
57the node set of another graph. The base graph can be an arbitrary graph and it
58is possible to attach several \ref lemon::EdgeSet "EdgeSet"'s to a base graph.
59
60\todo Don't we need SmartNodeSet and SmartEdgeSet?
61\todo Some cross-refs are wrong.
62
63The graph structures itself can not store data attached
64to the edges and nodes. However they all provide
65\ref maps "map classes"
66to dynamically attach data the to graph components.
67
68The following program demonstrates the basic features of LEMON's graph
69structures.
70
71\code
72#include <iostream>
73#include <lemon/list_graph.h>
74
75using namespace lemon;
76
77int main()
78{
79  typedef ListGraph Graph;
80\endcode
81
82ListGraph is one of LEMON's graph classes. It is based on linked lists,
83therefore iterating throuh its edges and nodes is fast.
84
85\code
86  typedef Graph::Edge Edge;
87  typedef Graph::InEdgeIt InEdgeIt;
88  typedef Graph::OutEdgeIt OutEdgeIt;
89  typedef Graph::EdgeIt EdgeIt;
90  typedef Graph::Node Node;
91  typedef Graph::NodeIt NodeIt;
92
93  Graph g;
94 
95  for (int i = 0; i < 3; i++)
96    g.addNode();
97 
98  for (NodeIt i(g); i!=INVALID; ++i)
99    for (NodeIt j(g); j!=INVALID; ++j)
100      if (i != j) g.addEdge(i, j);
101\endcode
102
103After some convenience typedefs we create a graph and add three nodes to it.
104Then we add edges to it to form a full graph.
105
106\code
107  std::cout << "Nodes:";
108  for (NodeIt i(g); i!=INVALID; ++i)
109    std::cout << " " << g.id(i);
110  std::cout << std::endl;
111\endcode
112
113Here we iterate through all nodes of the graph. We use a constructor of the
114node iterator to initialize it to the first node. The operator++ is used to
115step to the next node. Using operator++ on the iterator pointing to the last
116node invalidates the iterator i.e. sets its value to
117\ref lemon::INVALID "INVALID". This is what we exploit in the stop condition.
118
119The previous code fragment prints out the following:
120
121\code
122Nodes: 2 1 0
123\endcode
124
125\code
126  std::cout << "Edges:";
127  for (EdgeIt i(g); i!=INVALID; ++i)
128    std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")";
129  std::cout << std::endl;
130\endcode
131
132\code
133Edges: (0,2) (1,2) (0,1) (2,1) (1,0) (2,0)
134\endcode
135
136We can also iterate through all edges of the graph very similarly. The head and
137tail member functions can be used to access the endpoints of an edge.
138
139\code
140  NodeIt first_node(g);
141
142  std::cout << "Out-edges of node " << g.id(first_node) << ":";
143  for (OutEdgeIt i(g, first_node); i!=INVALID; ++i)
144    std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")";
145  std::cout << std::endl;
146
147  std::cout << "In-edges of node " << g.id(first_node) << ":";
148  for (InEdgeIt i(g, first_node); i!=INVALID; ++i)
149    std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")";
150  std::cout << std::endl;
151\endcode
152
153\code
154Out-edges of node 2: (2,0) (2,1)
155In-edges of node 2: (0,2) (1,2)
156\endcode
157
158We can also iterate through the in and out-edges of a node. In the above
159example we print out the in and out-edges of the first node of the graph.
160
161\code
162  Graph::EdgeMap<int> m(g);
163
164  for (EdgeIt e(g); e!=INVALID; ++e)
165    m.set(e, 10 - g.id(e));
166 
167  std::cout << "Id Edge  Value" << std::endl;
168  for (EdgeIt e(g); e!=INVALID; ++e)
169    std::cout << g.id(e) << "  (" << g.id(g.tail(e)) << "," << g.id(g.head(e))
170      << ") " << m[e] << std::endl;
171\endcode
172
173\code
174Id Edge  Value
1754  (0,2) 6
1762  (1,2) 8
1775  (0,1) 5
1780  (2,1) 10
1793  (1,0) 7
1801  (2,0) 9
181\endcode
182
183As we mentioned above, graphs are not containers rather
184incidence structures which are iterable in many ways. LEMON introduces
185concepts that allow us to attach containers to graphs. These containers are
186called maps.
187
188In the example above we create an EdgeMap which assigns an int value to all
189edges of the graph. We use the set member function of the map to write values
190into the map and the operator[] to retrieve them.
191
192Here we used the maps provided by the ListGraph class, but you can also write
193your own maps. You can read more about using maps \ref maps "here".
194
195*/
Note: See TracBrowser for help on using the repository browser.