template<typename NodeIndexType = int32, typename ArcIndexType = int32>
class util::ListGraph< NodeIndexType, ArcIndexType >
Basic graph implementation without reverse arc.
This class also serves as a documentation for the generic graph interface (minus the part related to reverse arcs).
This implementation uses a linked list and compared to StaticGraph:
- Is a bit faster to construct (if the arcs are not ordered by tail).
- Does not require calling Build().
- Has slower outgoing arc iteration.
- Uses more memory: ArcIndexType * node_capacity()
- Has an efficient Tail() but need an extra NodeIndexType/arc memory for it.
- Never changes the initial arc index returned by AddArc().
Definition at line 297 of file graph.h.
template<typename NodeIndexType = int32, typename ArcIndexType = int32>
Reserve space for the graph at construction and do not allow it to grow beyond that, see FreezeCapacities().
This constructor also makes any nodes in [0, num_nodes) valid.
Definition at line 312 of file graph.h.
template<typename NodeIndexType , typename ArcIndexType >
| ArcIndexType util::ListGraph< NodeIndexType, ArcIndexType >::AddArc |
( |
NodeIndexType |
tail, |
|
|
NodeIndexType |
head |
|
) |
| |
Adds an arc to the graph and returns its current index which will always be num_arcs() - 1.
It will also automatically call AddNode(tail) and AddNode(head). It will fail in DEBUG mode if the capacities are fixed and this cause the graph to grow beyond them.
- Note
- Self referencing arcs and duplicate arcs are supported.
Definition at line 1137 of file graph.h.
template<typename NodeIndexType , typename ArcIndexType >
| void util::ListGraph< NodeIndexType, ArcIndexType >::AddNode |
( |
NodeIndexType |
node | ) |
|
If node is not a valid node, sets num_nodes_ to node + 1 so that the given node becomes valid.
It will fail in DEBUG mode if the capacities are fixed and the new node is out of range.
Definition at line 1129 of file graph.h.
template<typename NodeIndexType = int32, typename ArcIndexType = int32>
Some graph implementations need to be finalized with Build() before they can be used.
After Build() is called, the arc indices (which had been the return values of previous AddArc() calls) may change: the new index of former arc #i will be stored in permutation[i] if #i is smaller than permutation.size() or will be unchanged otherwise. If you don't care about these, just call the simple no-output version Build().
- Note
- some implementations become immutable after calling Build().
Definition at line 339 of file graph.h.
| void util::BaseGraph< NodeIndexType, ArcIndexType, HasReverseArcs >::BuildStartAndForwardHead |
( |
SVector< NodeIndexType > * |
head, |
|
|
std::vector< ArcIndexType > * |
start, |
|
|
std::vector< ArcIndexType > * |
permutation |
|
) |
| |
|
protectedinherited |
Given the tail of arc #i in (*head)[i] and the head of arc #i in (*head)[~i].
- Reorder the arc by increasing tail.
- Put the head of the new arc #i in (*head)[i].
- Put in start[i] the index of the first arc with tail >= i.
- Update "permutation" to reflect the change, unless it is NULL.
Computes the outgoing degree of each nodes and check if we need to permute something or not. Note that the tails are currently stored in the positive range of the SVector head.
Abort early if we do not need the permutation: we only need to put the heads in the positive range.
Computes the forward arc permutation.
- Note
- this temporarily alters the start vector.
Restore in (*start)[i] the index of the first arc with tail >= i.
Permutes the head into their final position in head. We do not need the tails anymore at this point.
Definition at line 995 of file graph.h.
template<typename NodeIndexType , typename ArcIndexType >
| ArcIndexType util::ListGraph< NodeIndexType, ArcIndexType >::OutDegree |
( |
NodeIndexType |
node | ) |
const |
Graph jargon: the "degree" of a node is its number of arcs.
The out-degree is the number of outgoing arcs. The in-degree is the number of incoming arcs, and is only available for some graph implementations, below.
ListGraph<>::OutDegree() works in O(degree).
Definition at line 1121 of file graph.h.
template<typename NodeIndexType , typename ArcIndexType >
| void util::ListGraph< NodeIndexType, ArcIndexType >::ReserveNodes |
( |
NodeIndexType |
bound | ) |
|
|
overridevirtual |
Changes the graph capacities.
The functions will fail in debug mode if:
- const_capacities_ is true.
- A valid node does not fall into the new node range.
- A valid arc does not fall into the new arc range. In non-debug mode, const_capacities_ is ignored and nothing will happen if the new capacity value for the arcs or the nodes is too small.
Reimplemented from util::BaseGraph< NodeIndexType, ArcIndexType, false >.
Definition at line 1151 of file graph.h.