fn() dagShortestPathComputes shortest paths from a single source in a directed acyclic graph (DAG).
Computes shortest paths from a single source in a directed acyclic graph (DAG).
Defined in | <seqan/graph_algorithms.h> |
---|---|
Signature |
void dagShortestPath(predecessor, distance, g, source, weight);
|
Parameters
predecessor
|
A property map. A property map that represents predecessor relationships among vertices. It determines a shortest-paths tree. |
---|---|
distance
|
A property map. Indicates for each vertex th distance from the source. do exist. |
g
|
A directed acyclic graph. Types: Directed Graph |
source
|
A source vertex. Types: VertexDescriptor |
weight
|
A weight map. In a directed acyclic graph edge weights can be negative because no cycles |
Detailed Description
Example
#include <iostream>
#include <seqan/graph_algorithms.h>
using namespace seqan2;
int main()
{
typedef Graph<Directed<> > TGraph;
typedef VertexDescriptor<TGraph>::Type TVertexDescriptor;
typedef Size<TGraph>::Type TSize;
// Create graph with 10 directed edges (0,2), (0,1), ...
TSize numEdges = 10;
TVertexDescriptor edges[] = {0, 2, 0, 1, 1, 3, 1, 2, 2, 5, 2, 4, 2, 3, 3, 5, 3, 4, 4, 5};
TGraph g;
addEdges(g, edges, numEdges);
// Print graph to stdout.
std::cout << g << "\n";
// Create external edge property map with edge weights.
int weights[] = {3, 5, 6, 2, 2, 4, 7, 1, -1, -2};
String<int> weightMap;
assignEdgeMap(weightMap, g, weights);
// Run DAG shortest path computation from vertex with descriptor 1.
String<unsigned> predMap;
String<unsigned> distMap;
dagShortestPath(predMap, distMap, g, 1, weightMap);
// Print result to stdout.
std::cout << "Single-Source Shortest Paths in DAG: \n";
typedef Iterator<TGraph, VertexIterator>::Type TVertexIterator;
TVertexIterator it(g);
while (!atEnd(it))
{
std::cout << "Path from 1 to " << getValue(it) << ": ";
_printPath(g, predMap, (TVertexDescriptor)1, getValue(it));
std::cout << " (Distance: " << getProperty(distMap, getValue(it)) << ")\n";
goNext(it);
}
return 0;
}
Adjacency list: 0 -> 1,2, 1 -> 2,3, 2 -> 3,4,5, 3 -> 4,5, 4 -> 5, 5 -> Edge list: Source: 0,Target: 1 (Id: 1) Source: 0,Target: 2 (Id: 0) Source: 1,Target: 2 (Id: 3) Source: 1,Target: 3 (Id: 2) Source: 2,Target: 3 (Id: 6) Source: 2,Target: 4 (Id: 5) Source: 2,Target: 5 (Id: 4) Source: 3,Target: 4 (Id: 8) Source: 3,Target: 5 (Id: 7) Source: 4,Target: 5 (Id: 9) Single-Source Shortest Paths in DAG: Path from 1 to 0: No path from 1 to 0 exists. (Distance: 1073741823) Path from 1 to 1: 1 (Distance: 0) Path from 1 to 2: 1,2 (Distance: 2) Path from 1 to 3: 1,3 (Distance: 6) Path from 1 to 4: 1,3,4 (Distance: 5) Path from 1 to 5: 1,3,4,5 (Distance: 3)
Data Races
If not stated otherwise, concurrent invocation is not guaranteed to be thread-safe.