Graph Concepts
BGL algorithms are templates. They do not depend on a specific graph class. Instead, each algorithm specifies its requirements as a concept: a set of types and functions the graph must provide. Any graph type that satisfies the concept works with the algorithm.
The concepts are small and composable. An algorithm that only needs to walk out-edges requires IncidenceGraph. One that also needs to list all vertices requires VertexListGraph. This keeps algorithms reusable: a graph type only needs to support the operations the algorithm actually uses.
Which concept does my algorithm need?
| I need to… | Require this concept |
|---|---|
Walk out-edges of a vertex |
|
Walk both out-edges and in-edges |
|
List all vertices |
|
List all edges |
|
Check if an edge exists between two vertices |
|
Add/remove vertices and edges |
|
Read/write vertex or edge properties |
Concept hierarchy
Each concept refines (extends) a simpler one. IncidenceGraph refines Graph. BidirectionalGraph refines IncidenceGraph. An algorithm that requires IncidenceGraph accepts any type that models BidirectionalGraph, VertexListGraph, or any other refinement.
Quick reference
| Expression | Return type |
|---|---|
Graph (base concept) |
|
|
Vertex handle |
|
Edge handle |
|
|
|
|
IncidenceGraph refines Graph |
|
|
|
|
|
|
|
|
|
BidirectionalGraph refines IncidenceGraph |
|
|
|
|
|
|
|
AdjacencyGraph refines Graph |
|
|
|
VertexListGraph refines Graph |
|
|
|
|
|
EdgeListGraph refines Graph |
|
|
|
|
|
AdjacencyMatrix refines Graph |
|
|
|
MutableGraph refines Graph |
|
|
|
|
|
|
|
|
|
|
|
PropertyGraph refines Graph |
|
|
Property map object |
|
Property value for vertex/edge |
|
Set property value |
Undirected graphs
BGL uses the same interface for directed and undirected graphs. out_edges(),
source(), and target() work on undirected graphs too. In an undirected
graph, out_edges(v, g) returns all edges incident to v (there is no
distinction between "in" and "out").
The key difference is edge equality. In a directed graph, edge (u,v) is
never equal to (v,u). In an undirected graph without parallel edges, they
are the same edge. This also means edge properties are shared: weight(u,v)
and weight(v,u) return the same value.
Do not subclass BGL graph types. Because graph_traits and
property_map specializations are defined externally to the class, a subclass
will not inherit them and many BGL functions will fail to compile.
|