Graph Interface Primitives
|
What is a graph?
Formally, a graph is a pair G = (V, E) where V is a set of vertices and E is a set of edges. Each edge is a pair (u, v) with u, v in V.
The terms vertex, edge, source, target, and the directed vs. undirected distinction map directly onto the operations listed below. For a fuller introduction (degrees, paths, cycles, planarity), see Graph Theory Review. |
All BGL graph types share a common set of free functions for accessing and modifying the graph structure. These functions are how algorithms interact with graphs, regardless of the underlying data structure.
#include <boost/graph/adjacency_list.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>
using namespace boost;
using Graph = adjacency_list<vecS, vecS, directedS>;
int main() {
Graph g(4);
add_edge(0, 1, g);
add_edge(1, 2, g);
add_edge(2, 3, g);
for (auto v : make_iterator_range(vertices(g))) {
for (auto e : make_iterator_range(out_edges(v, g))) {
std::cout << source(e, g) << " -> " << target(e, g) << "\n";
}
}
}
0 -> 1
1 -> 2
2 -> 3
Traversal
| Function | Returns | Concept |
|---|---|---|
|
Pair of vertex iterators (all vertices) |
|
|
Pair of edge iterators (all edges) |
|
|
Pair of out-edge iterators |
|
|
Pair of in-edge iterators |
|
|
Pair of adjacency iterators |
All pairs can be used with make_iterator_range for range-based for loops (see example above).
|
Edge access
| Function | Returns | Concept |
|---|---|---|
|
Source vertex of edge |
|
|
Target vertex of edge |
|
|
Pair of (edge_descriptor, bool). Bool is true if the edge exists. |
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
using namespace boost;
using Graph = adjacency_list<vecS, vecS, directedS>;
int main() {
Graph g(3);
add_edge(0, 1, g);
auto result = edge(0, 1, g); // pair of (edge_descriptor, bool)
if (result.second) {
std::cout << source(result.first, g) << " -> " << target(result.first, g) << "\n";
}
}
0 -> 1
Counts
| Function | Returns | Concept |
|---|---|---|
|
Number of vertices |
|
|
Number of edges |
|
|
Number of out-edges of vertex |
|
|
Number of in-edges of vertex |
|
|
|
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
using namespace boost;
using Graph = adjacency_list<vecS, vecS, bidirectionalS>;
int main() {
Graph g(3);
add_edge(0, 1, g);
add_edge(2, 1, g);
std::cout << "num_vertices: " << num_vertices(g) << "\n";
std::cout << "num_edges: " << num_edges(g) << "\n";
std::cout << "out_degree(0): " << out_degree(0, g) << "\n";
std::cout << "in_degree(1): " << in_degree(1, g) << "\n";
std::cout << "degree(1): " << degree(1, g) << "\n";
}
num_vertices: 3
num_edges: 2
out_degree(0): 1
in_degree(1): 2
degree(1): 2
Mutation
| Function | Effect | Concept |
|---|---|---|
|
Add a vertex, return its descriptor |
|
|
Remove vertex |
|
|
Add edge from |
|
|
Remove edge(s) between |
|
|
Remove specific edge |
|
|
Remove all edges incident to |
With adjacency_list<vecS, vecS, …>, remove_vertex()
invalidates all vertex descriptors after the removed vertex. See
Graph Data Structures for details.
|
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
using namespace boost;
using Graph = adjacency_list<vecS, vecS, directedS>;
int main() {
Graph g(2);
add_edge(0, 1, g);
auto v = add_vertex(g);
add_edge(v, vertex(0, g), g);
clear_vertex(v, g); // remove all edges of v
remove_vertex(v, g); // remove v itself
std::cout << "vertices: " << num_vertices(g) << "\n";
}
vertices: 2
Property access
| Function | Returns |
|---|---|
|
Property map for bundled property member |
|
Value of |
|
Set value of |
|
Direct reference to bundled property struct of vertex |
|
Property map for internal property tag (legacy, prefer bundled properties) |
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
#include <string>
using namespace boost;
struct City { std::string name; };
struct Road { double km; };
using Graph = adjacency_list<vecS, vecS, directedS, City, Road>;
int main() {
Graph g(2);
add_edge(0, 1, Road{460.0}, g);
g[0].name = "Paris"; // direct struct access
auto km = get(&Road::km, g); // property map from bundled member
std::cout << get(km, edge(0, 1, g).first); // read through property map
}
460
Types
Obtained via graph_traits<Graph>:
| Type | Description |
|---|---|
|
Handle to a vertex. Opaque type, do not assume it is an integer. |
|
Handle to an edge. Contains source and target information. |
|
Iterator over all vertices. |
|
Iterator over all edges. |
|
Iterator over the out-edges of a vertex. |
|
Iterator over the in-edges of a vertex (requires |
|
Iterator over the adjacent vertices of a vertex. |
|
Unsigned integer type for vertex counts. |
|
Unsigned integer type for edge counts. |
|
Unsigned integer type for vertex degree. |