Indenting.
3 \page graphs How to use graphs
5 The following program demonstrates the basic features of HugoLib's graph
10 #include <hugo/list_graph.h>
16 typedef ListGraph Graph;
19 ListGraph is one of HugoLib's graph classes. It is based on linked lists,
20 therefore iterating throuh its edges and nodes is fast.
23 typedef Graph::Edge Edge;
24 typedef Graph::InEdgeIt InEdgeIt;
25 typedef Graph::OutEdgeIt OutEdgeIt;
26 typedef Graph::EdgeIt EdgeIt;
27 typedef Graph::Node Node;
28 typedef Graph::NodeIt NodeIt;
32 for (int i = 0; i < 3; i++)
35 for (NodeIt i(g); g.valid(i); g.next(i))
36 for (NodeIt j(g); g.valid(j); g.next(j))
37 if (i != j) g.addEdge(i, j);
40 After some convenience typedefs we create a graph and add three nodes to it.
41 Then we add edges to it to form a full graph.
44 std::cout << "Nodes:";
45 for (NodeIt i(g); g.valid(i); g.next(i))
46 std::cout << " " << g.id(i);
47 std::cout << std::endl;
50 Here we iterate through all nodes of the graph. We use a constructor of the
51 node iterator to initialize it to the first node. The next member function is
52 used to step to the next node, and valid is used to check if we have passed the
56 std::cout << "Nodes:";
58 for (g.first(n); n != INVALID; g.next(n))
59 std::cout << " " << g.id(n);
60 std::cout << std::endl;
63 Here you can see an alternative way to iterate through all nodes. Here we use a
64 member function of the graph to initialize the node iterator to the first node
65 of the graph. Using next on the iterator pointing to the last node invalidates
66 the iterator i.e. sets its value to INVALID. Checking for this value is
67 equivalent to using the valid member function.
69 Both of the previous code fragments print out the same:
76 std::cout << "Edges:";
77 for (EdgeIt i(g); g.valid(i); g.next(i))
78 std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")";
79 std::cout << std::endl;
83 Edges: (0,2) (1,2) (0,1) (2,1) (1,0) (2,0)
86 We can also iterate through all edges of the graph very similarly. The head and
87 tail member functions can be used to access the endpoints of an edge.
92 std::cout << "Out-edges of node " << g.id(first_node) << ":";
93 for (OutEdgeIt i(g, first_node); g.valid(i); g.next(i))
94 std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")";
95 std::cout << std::endl;
97 std::cout << "In-edges of node " << g.id(first_node) << ":";
98 for (InEdgeIt i(g, first_node); g.valid(i); g.next(i))
99 std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")";
100 std::cout << std::endl;
104 Out-edges of node 2: (2,0) (2,1)
105 In-edges of node 2: (0,2) (1,2)
108 We can also iterate through the in and out-edges of a node. In the above
109 example we print out the in and out-edges of the first node of the graph.
112 Graph::EdgeMap<int> m(g);
114 for (EdgeIt e(g); g.valid(e); g.next(e))
115 m.set(e, 10 - g.id(e));
117 std::cout << "Id Edge Value" << std::endl;
118 for (EdgeIt e(g); g.valid(e); g.next(e))
119 std::cout << g.id(e) << " (" << g.id(g.tail(e)) << "," << g.id(g.head(e))
120 << ") " << m[e] << std::endl;
133 In generic graph optimization programming graphs are not containers rather
134 incidence structures which are iterable in many ways. HugoLib introduces
135 concepts that allow us to attach containers to graphs. These containers are
138 In the example above we create an EdgeMap which assigns an int value to all
139 edges of the graph. We use the set member function of the map to write values
140 into the map and the operator[] to retrieve them.
142 Here we used the maps provided by the ListGraph class, but you can also write
143 your own maps. You can read more about using maps \ref maps "here".