public final class Paths extends Object
k
from node u
to node v
in a graph is a sequence v0, v1, v2, ... , vk
of nodes such that u = v0, v = vk
and (vi-1, vi)
in E
, for each i = 1, 2, .. , k
.
2
.NP
-hard for undirected graphs, but can be solved in linear time for directed acyclic graphs.
Modifier and Type | Method and Description |
---|---|
static NodeList |
constructNodePath(EdgeList path)
Constructs a
path of nodes from a given path of edges . |
static EdgeList[] |
findAllChains(Graph graph,
boolean directed)
Returns all chains present in the given graph.
|
static EdgeList[] |
findAllPaths(Graph graph,
Node startNode,
Node endNode,
boolean directed)
Returns all simple directed or undirected paths that connect a
start node with an end node. |
static EdgeList[] |
findAllPaths(Graph graph,
Node startNode,
Node endNode,
boolean directed,
Predicate<EdgeList> filter)
A variant of
findAllPaths(Graph, Node, Node, boolean) which returns all simple directed or undirected paths
between two given nodes and, additionally, allows to specify a filter for the paths to be returned. |
static void |
findAllPaths(Graph graph,
Node startNode,
Node endNode,
IEdgeMap pathEdges)
Finds all edges that belong to a directed path from a
start node to an end node. |
static ICursor |
findAllPathsCursor(Graph graph,
Node startNode,
Node endNode,
boolean directed)
A variant of
findAllPaths(Graph, Node, Node, boolean) , which returns all simple directed or undirected paths between two
given nodes as a special cursor that calculates the next path in the sequence, only when needed. |
static EdgeList |
findLongestPath(Graph graph)
Returns the longest directed path in a given acyclic weighted graph.
|
static EdgeList |
findLongestPath(Graph graph,
IDataProvider edgeLength)
Returns the longest directed path in a given acyclic weighted graph.
|
static void |
findLongestPaths(Graph graph,
Node startNode,
IEdgeMap dist,
INodeMap maxDist,
IEdgeMap predicate)
Calculates the longest path from a given node to all other node in a given directed acyclic graph.
|
static EdgeList |
findLongPath(Graph graph)
Returns an
EdgeList containing the edges of an undirected simple path within the given graph. |
static boolean |
findPath(Graph graph,
NodeList topSort,
Node startNode,
Node endNode,
IEdgeMap predicate)
Returns whether or not a directed path from a start node to another node in an acyclic graph exists.
|
static EdgeList |
findPath(Graph graph,
Node startNode,
Node endNode,
boolean directed)
Returns an
EdgeList containing the edges of a path from the given start node to the given end node, if such a
path exists. |
public static final NodeList constructNodePath(EdgeList path)
path of nodes
from a given path of edges
.
The returned node path has length path.size()+1
, if the given path is not empty. Otherwise, the returned path
will be empty. The i
-th node in the returned path will be either source or target node of the
i
-th edge in the given path.
path
- the given path of edges
path of nodes
from the given path of edges
public static final EdgeList[] findAllChains(Graph graph, boolean directed)
A chain is a path of maximum length in which each internal node has degree 2
.
The internal nodes on directed chains all have in-degree 1
and out-degree 1
.
constructNodePath(EdgeList)
can be used for converting an edge path to a node path.O(graph.N() + graph.E())
graph
- the input graphdirected
- true
if the chain should be considered as directed, false
otherwiseEdgeList
s each of which contains the edges (at least two) that make up a chainconstructNodePath(EdgeList)
public static final EdgeList[] findAllPaths(Graph graph, Node startNode, Node endNode, boolean directed)
start
node with an end
node.findAllPathsCursor(Graph, Node, Node, boolean)
instead.start
node and end
node must not be null
.O(2^ (graph.N() + graph.E()))
graph
- the input graphstartNode
- the given start nodeendNode
- the given end nodedirected
- true
if the path should be considered as directed, false
otherwiseEdgeList
s each of which represents a path between the start and end nodepublic static final EdgeList[] findAllPaths(Graph graph, Node startNode, Node endNode, boolean directed, Predicate<EdgeList> filter)
findAllPaths(Graph, Node, Node, boolean)
which returns all simple directed or undirected paths
between two given nodes and, additionally, allows to specify a filter for the paths to be returned.start
node and end
node must not be null
.O(2^ (graph.N() + graph.E()))
graph
- the input graphstartNode
- the given start nodeendNode
- the given end nodedirected
- true
if the path should be considered as directed, false
otherwisefilter
- a Predicate
that accepts or rejects a found EdgeList
and adds it to the resultEdgeList
s each of which represents a path between the start and end node.public static final void findAllPaths(Graph graph, Node startNode, Node endNode, IEdgeMap pathEdges)
start
node to an end
node.O(graph.N() + graph.E())
graph
- the input graphstartNode
- the given start nodeendNode
- the given end nodepathEdges
- the IEdgeMap
that will be filled during the execution with a boolean value indicating whether or not an edge
belongs to a path connecting the two given nodespublic static final ICursor findAllPathsCursor(Graph graph, Node startNode, Node endNode, boolean directed)
findAllPaths(Graph, Node, Node, boolean)
, which returns all simple directed or undirected paths between two
given nodes as a special cursor that calculates the next path in the sequence, only when needed.
The returned cursor only supports the operation Ok
, Current
, Size
and ICursor.next()
.
start
node and end
node must not be null
.O(2^ (graph.N() + graph.E()))
graph
- the input graphstartNode
- the given start nodeendNode
- the given end nodedirected
- true
if the path should be considered as directed, false
otherwiseICursor
that calculates the next path in the sequencepublic static final EdgeList findLongestPath(Graph graph)
All edges of the graph have an integral length associated with them. The longest path is defined as one of all directed paths within the graph for which the edge lengths of all contained edges sum up to a maximum.
public static final EdgeList findLongestPath(Graph graph, IDataProvider edgeLength)
All edges of the graph have an integral length associated with them. The longest path is defined as one of all directed paths within the graph for which the edge lengths of all contained edges sum up to a maximum.
acyclic
.edgeLength.getInt(e) > 0
for all edges e
of the graphgraph
- a directed acyclic graphedgeLength
- the IDataProvider
that returns the non-negative integer length of each edgeEdgeList
containing the edges of the longest directed pathpublic static final void findLongestPaths(Graph graph, Node startNode, IEdgeMap dist, INodeMap maxDist, IEdgeMap predicate)
acyclic
.graph
- a directed acyclic graphstartNode
- the node for which the distances are calculateddist
- the IEdgeMap
that returns the distance (i.e. weight) of type double for each edgemaxDist
- the INodeMap
that will be filled during the execution and holds the maximum distance between the given node and
all other nodespredicate
- the IEdgeMap
that returns a boolean value indicating whether or not an edge should be considered during the path
searchpublic static final EdgeList findLongPath(Graph graph)
EdgeList
containing the edges of an undirected simple path within the given graph.
The edges are returned in the order that they appear in the found path.
A heuristic is used for finding a path that is long. It is not guaranteed, though, that the returned path is actually the longest path within the given graph, since that is a well known hard problem.
public static final boolean findPath(Graph graph, NodeList topSort, Node startNode, Node endNode, IEdgeMap predicate)
acyclic
.graph
- an acyclic graph which contains the two nodestopSort
- a list
of nodes sorted in topological orderpredicate
- the IEdgeMap
that returns a boolean value indicating whether or not an edge should be considered during the path
searchtrue
if a directed path from a start node to another node exists, false
otherwisepublic static final EdgeList findPath(Graph graph, Node startNode, Node endNode, boolean directed)
EdgeList
containing the edges of a path from the given start node to the given end node, if such a
path exists.
The edges are returned in the order that they appear in the found path. If the returned path is empty, no path between the given nodes was found.
startNode != endNode
O(graph.N() + graph.E())
graph
- the input graphstartNode
- the first node of the pathendNode
- the last node of the pathdirected
- true
if the path should be directed, false
otherwiseEdgeList
containing the path edges between the start node and the end node