58 is possible to attach several \ref hugo::EdgeSet "EdgeSet"'s to a base graph. |
58 is possible to attach several \ref hugo::EdgeSet "EdgeSet"'s to a base graph. |
59 |
59 |
60 \todo Don't we need SmartNodeSet and SmartEdgeSet? |
60 \todo Don't we need SmartNodeSet and SmartEdgeSet? |
61 \todo Some cross-refs are wrong. |
61 \todo Some cross-refs are wrong. |
62 |
62 |
63 \bug This file must be updated accordig to the new style iterators. |
|
64 |
|
65 The graph structures itself can not store data attached |
63 The graph structures itself can not store data attached |
66 to the edges and nodes. However they all provide |
64 to the edges and nodes. However they all provide |
67 \ref maps "map classes" |
65 \ref maps "map classes" |
68 to dynamically attach data the to graph components. |
66 to dynamically attach data the to graph components. |
69 |
67 |
95 Graph g; |
93 Graph g; |
96 |
94 |
97 for (int i = 0; i < 3; i++) |
95 for (int i = 0; i < 3; i++) |
98 g.addNode(); |
96 g.addNode(); |
99 |
97 |
100 for (NodeIt i(g); g.valid(i); g.next(i)) |
98 for (NodeIt i(g); i!=INVALID; ++i) |
101 for (NodeIt j(g); g.valid(j); g.next(j)) |
99 for (NodeIt j(g); j!=INVALID; ++j) |
102 if (i != j) g.addEdge(i, j); |
100 if (i != j) g.addEdge(i, j); |
103 \endcode |
101 \endcode |
104 |
102 |
105 After some convenience typedefs we create a graph and add three nodes to it. |
103 After some convenience typedefs we create a graph and add three nodes to it. |
106 Then we add edges to it to form a full graph. |
104 Then we add edges to it to form a full graph. |
107 |
105 |
108 \code |
106 \code |
109 std::cout << "Nodes:"; |
107 std::cout << "Nodes:"; |
110 for (NodeIt i(g); g.valid(i); g.next(i)) |
108 for (NodeIt i(g); i!=INVALID; ++i) |
111 std::cout << " " << g.id(i); |
109 std::cout << " " << g.id(i); |
112 std::cout << std::endl; |
110 std::cout << std::endl; |
113 \endcode |
111 \endcode |
114 |
112 |
115 Here we iterate through all nodes of the graph. We use a constructor of the |
113 Here we iterate through all nodes of the graph. We use a constructor of the |
116 node iterator to initialize it to the first node. The next member function is |
114 node iterator to initialize it to the first node. The operator++ is used to |
117 used to step to the next node, and valid is used to check if we have passed the |
115 step to the next node. Using operator++ on the iterator pointing to the last |
118 last one. |
116 node invalidates the iterator i.e. sets its value to |
|
117 \ref hugo::INVALID "INVALID". This is what we exploit in the stop condition. |
119 |
118 |
120 \code |
119 The previous code fragment prints out the following: |
121 std::cout << "Nodes:"; |
|
122 NodeIt n; |
|
123 for (g.first(n); n != INVALID; g.next(n)) |
|
124 std::cout << " " << g.id(n); |
|
125 std::cout << std::endl; |
|
126 \endcode |
|
127 |
|
128 Here you can see an alternative way to iterate through all nodes. Here we use a |
|
129 member function of the graph to initialize the node iterator to the first node |
|
130 of the graph. Using next on the iterator pointing to the last node invalidates |
|
131 the iterator i.e. sets its value to INVALID. Checking for this value is |
|
132 equivalent to using the valid member function. |
|
133 |
|
134 Both of the previous code fragments print out the same: |
|
135 |
120 |
136 \code |
121 \code |
137 Nodes: 2 1 0 |
122 Nodes: 2 1 0 |
138 \endcode |
123 \endcode |
139 |
124 |
140 \code |
125 \code |
141 std::cout << "Edges:"; |
126 std::cout << "Edges:"; |
142 for (EdgeIt i(g); g.valid(i); g.next(i)) |
127 for (EdgeIt i(g); i!=INVALID; ++i) |
143 std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")"; |
128 std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")"; |
144 std::cout << std::endl; |
129 std::cout << std::endl; |
145 \endcode |
130 \endcode |
146 |
131 |
147 \code |
132 \code |
153 |
138 |
154 \code |
139 \code |
155 NodeIt first_node(g); |
140 NodeIt first_node(g); |
156 |
141 |
157 std::cout << "Out-edges of node " << g.id(first_node) << ":"; |
142 std::cout << "Out-edges of node " << g.id(first_node) << ":"; |
158 for (OutEdgeIt i(g, first_node); g.valid(i); g.next(i)) |
143 for (OutEdgeIt i(g, first_node); i!=INVALID; ++i) |
159 std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")"; |
144 std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")"; |
160 std::cout << std::endl; |
145 std::cout << std::endl; |
161 |
146 |
162 std::cout << "In-edges of node " << g.id(first_node) << ":"; |
147 std::cout << "In-edges of node " << g.id(first_node) << ":"; |
163 for (InEdgeIt i(g, first_node); g.valid(i); g.next(i)) |
148 for (InEdgeIt i(g, first_node); i!=INVALID; ++i) |
164 std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")"; |
149 std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")"; |
165 std::cout << std::endl; |
150 std::cout << std::endl; |
166 \endcode |
151 \endcode |
167 |
152 |
168 \code |
153 \code |
174 example we print out the in and out-edges of the first node of the graph. |
159 example we print out the in and out-edges of the first node of the graph. |
175 |
160 |
176 \code |
161 \code |
177 Graph::EdgeMap<int> m(g); |
162 Graph::EdgeMap<int> m(g); |
178 |
163 |
179 for (EdgeIt e(g); g.valid(e); g.next(e)) |
164 for (EdgeIt e(g); e!=INVALID; ++e) |
180 m.set(e, 10 - g.id(e)); |
165 m.set(e, 10 - g.id(e)); |
181 |
166 |
182 std::cout << "Id Edge Value" << std::endl; |
167 std::cout << "Id Edge Value" << std::endl; |
183 for (EdgeIt e(g); g.valid(e); g.next(e)) |
168 for (EdgeIt e(g); e!=INVALID; ++e) |
184 std::cout << g.id(e) << " (" << g.id(g.tail(e)) << "," << g.id(g.head(e)) |
169 std::cout << g.id(e) << " (" << g.id(g.tail(e)) << "," << g.id(g.head(e)) |
185 << ") " << m[e] << std::endl; |
170 << ") " << m[e] << std::endl; |
186 \endcode |
171 \endcode |
187 |
172 |
188 \code |
173 \code |