|
Search this API | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object y.algo.ShortestPaths
public class ShortestPaths
Provides diverse algorithms and helper methods for solving the shortest path problem on weighted graphs.
Method Summary | |
---|---|
static boolean |
acyclic(Graph graph,
Node s,
DataProvider cost,
NodeMap dist,
NodeMap pred)
Like acyclic(Graph, Node, double[], double[], Edge[])
but uses NodeMaps and DataProviders instead of arrays. |
static boolean |
acyclic(Graph graph,
Node s,
double[] cost,
double[] dist)
This method solves the single-source shortest path problem for acyclic directed graphs. |
static boolean |
acyclic(Graph graph,
Node s,
double[] cost,
double[] dist,
Edge[] pred)
Like acyclic(Graph, Node, double[], double[]) but additionally this method
yields the path edges of each calculated shortest path. |
static boolean |
allPairs(Graph graph,
boolean directed,
double[] cost,
double[][] dist)
This method solves the all-pairs shortest path problem for graphs with arbitrary edge costs. |
static boolean |
bellmanFord(Graph graph,
Node s,
boolean directed,
DataProvider cost,
NodeMap dist,
NodeMap pred)
Like bellmanFord(Graph, Node, boolean, double[], double[], Edge[])
but uses NodeMaps and DataProviders instead of arrays. |
static boolean |
bellmanFord(Graph graph,
Node s,
boolean directed,
double[] cost,
double[] dist)
This method solves the single-source shortest path problem for arbitrary graphs. |
static boolean |
bellmanFord(Graph graph,
Node s,
boolean directed,
double[] cost,
double[] dist,
Edge[] pred)
Like bellmanFord(Graph, Node, boolean, double[], double[]) but additionally this method
yields the path edges of each calculated shortest path. |
static EdgeList |
constructEdgePath(Node s,
Node t,
DataProvider pred)
Like constructEdgePath(Node,Node,Edge[]) with the difference that
the path edges are given by a DataProvider. |
static EdgeList |
constructEdgePath(Node s,
Node t,
Edge[] pred)
Convenience method that constructs an explicit edge path from the result yielded by one of the shortest paths methods defined in this class. |
static NodeList |
constructNodePath(Node s,
Node t,
DataProvider pred)
Like constructNodePath(Node,Node,Edge[]) with the difference that
the path edges are given by a DataProvider. |
static NodeList |
constructNodePath(Node s,
Node t,
Edge[] pred)
Convenience method that constructs an explicit node path from the result yielded by one of the shortest paths methods defined in this class. |
static void |
dijkstra(Graph graph,
Node s,
boolean directed,
DataProvider cost,
NodeMap dist,
NodeMap pred)
Like dijkstra(Graph, Node, boolean, double[], double[], Edge[])
but uses NodeMaps and DataProviders instead of arrays. |
static void |
dijkstra(Graph graph,
Node s,
boolean directed,
double[] cost,
double[] dist)
This method solves the single-source shortest path problem for arbitrary graphs. |
static void |
dijkstra(Graph graph,
Node s,
boolean directed,
double[] cost,
double[] dist,
Edge[] pred)
Like dijkstra(Graph, Node, boolean, double[], double[]) but additionally this method
yields the path edges of each calculated shortest path. |
static void |
findShortestUniformPaths(Graph graph,
Node start,
DataProvider targetMap,
boolean directed,
int maxLength,
EdgeList pathEdges,
NodeList pathNodes)
Finds all nodes and edges that belong to a shortest path from start to a set of target nodes in the graph not
farther away than a given distance. |
static void |
findShortestUniformPaths(Graph graph,
Node start,
Node end,
boolean directed,
EdgeMap pathMap)
Marks all edges that belong to a shortest path from start to end node. |
static YList |
kShortestPaths(Graph graph,
DataProvider costDP,
Node start,
Node end,
int k)
This method finds the k shortest paths
connecting a pair of nodes in a directed graph with non-negative edge costs. |
static YCursor |
kShortestPathsCursor(Graph graph,
DataProvider costDP,
Node start,
Node end,
int k)
A variant of kShortestPaths(Graph, DataProvider, Node, Node, int)
that returns its result not as a list but as a special cursor that calculates
the next path in the sequence only when needed. |
static EdgeList[] |
shortestPair(Graph graph,
Node source,
Node target,
boolean directed,
DataProvider costDP)
Returns two edge-disjoint paths from in a nonnegatively-weighted directed graph, so that both paths connect s and t and have minimum total length. |
static boolean |
singleSource(Graph graph,
Node s,
boolean directed,
DataProvider cost,
NodeMap dist,
NodeMap pred)
Like singleSource(Graph, Node, boolean, double[], double[], Edge[])
but uses NodeMaps and DataProviders instead of arrays. |
static boolean |
singleSource(Graph graph,
Node s,
boolean directed,
double[] cost,
double[] dist)
This method solves the single-source shortest path problem for arbitrary graphs. |
static boolean |
singleSource(Graph graph,
Node s,
boolean directed,
double[] cost,
double[] dist,
Edge[] pred)
Like singleSource(Graph, Node, boolean, double[], double[]) but additionally this method
yields the path edges of each calculated shortest path. |
static EdgeList |
singleSourceSingleSink(Graph graph,
Node s,
Node t,
boolean directed,
DataProvider cost)
Similar to singleSourceSingleSink(Graph,Node,Node,boolean,DataProvider,NodeMap)
but instead of returning the shortest distance between the source and sink
the actual shortest edge path between these nodes will be returned. |
static double |
singleSourceSingleSink(Graph graph,
Node s,
Node t,
boolean directed,
DataProvider cost,
NodeMap pred)
Like singleSourceSingleSink(Graph, Node, Node, boolean, double[], Edge[])
but uses NodeMaps and DataProviders instead of arrays. |
static EdgeList |
singleSourceSingleSink(Graph graph,
Node s,
Node t,
boolean directed,
double[] cost)
Similar to singleSourceSingleSink(Graph,Node,Node,boolean,double[],Edge[])
but instead of returning the shortest distance between the source and sink
the actual shortest edge path between these nodes will be returned. |
static double |
singleSourceSingleSink(Graph graph,
Node s,
Node t,
boolean directed,
double[] cost,
Edge[] pred)
This method solves the single-source single-sink shortest path problem for arbitrary graphs. |
static void |
uniform(Graph graph,
Node s,
boolean directed,
double[] dist)
This method solves the single-source shortest path problem for arbitrary graphs where each edge has a uniform cost of 1.0. |
static void |
uniform(Graph graph,
Node s,
boolean directed,
double[] dist,
Edge[] pred)
Like uniform(Graph, Node, boolean, double[]) but additionally this method
yields the path edges of each calculated shortest path. |
static void |
uniform(Graph graph,
Node s,
boolean directed,
NodeMap dist,
NodeMap pred)
Like uniform(Graph, Node, boolean, double[], Edge[]) but uses NodeMaps instead of
arrays. |
static double[] |
uniformCost(Graph graph)
Convenience method that returns an array containing uniform edge costs of 1.0 for each edge
of the given graph. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static void uniform(Graph graph, Node s, boolean directed, double[] dist)
s
to all other nodes.
graph
- the graph being acted upons
- the start node for the shortest path searchdirected
- whether or not to consider the graph as directed. If the graph is
to be considered undirected then each edge can be traversed in both directions and
the returned shortest paths can thus be undirected.dist
- return value that will hold the shortest distance from node s
to
all other nodes. The distance from s
to v
is
dist[v.index()]
. If there is no path from s
to v
then dist[v.index()] == Double.POSITIVE_INFINITY
.public static void uniform(Graph graph, Node s, boolean directed, double[] dist, Edge[] pred)
uniform(Graph, Node, boolean, double[])
but additionally this method
yields the path edges of each calculated shortest path.
pred
- return value that holds for each node t
the shortest
path edge pred[t.index()]
which is the last edge on the shortest
path from s
to t
. If t == s
or if there
is no shortest path from s
to t
then
pred[t.index()] == null
.constructNodePath(Node, Node, Edge[])
,
constructEdgePath(Node, Node, Edge[])
public static void uniform(Graph graph, Node s, boolean directed, NodeMap dist, NodeMap pred)
uniform(Graph, Node, boolean, double[], Edge[])
but uses NodeMaps instead of
arrays.
dist
- return value. the map will provide a double value for each node.pred
- return value. the map will provide an Edge for each node.public static boolean acyclic(Graph graph, Node s, double[] cost, double[] dist)
s
to all other nodes.
graph
- the graph being acted upons
- the start node for the shortest path searchcost
- holds the costs for traversing each edge. Edge e
has cost cost[e.index()]
.dist
- return value that will hold the shortest distance from node s
to
all other nodes. The distance from s
to v
is
dist[v.index()]
. If there is no path from s
to v
then dist[v.index()] == Double.POSITIVE_INFINITY
.
false
if the input graph was not acyclic.public static boolean acyclic(Graph graph, Node s, double[] cost, double[] dist, Edge[] pred)
acyclic(Graph, Node, double[], double[])
but additionally this method
yields the path edges of each calculated shortest path.
pred
- return value that holds for each node t
the shortest
path edge pred[t.index()]
which is the last edge on the shortest
path from s
to t
. If t == s
or if there
is no shortest path from s
to t
then
pred[t.index()] == null
.constructNodePath(Node, Node, Edge[])
,
constructEdgePath(Node, Node, Edge[])
public static boolean acyclic(Graph graph, Node s, DataProvider cost, NodeMap dist, NodeMap pred)
acyclic(Graph, Node, double[], double[], Edge[])
but uses NodeMaps and DataProviders instead of arrays.
cost
- must provide a double value for each edge.dist
- return value. the map will provide a double value for each node.pred
- return value. the map will provide an Edge for each node.public static void dijkstra(Graph graph, Node s, boolean directed, double[] cost, double[] dist)
s
to all other nodes.
graph
- the graph being acted upons
- the start node for the shortest path searchdirected
- whether or not to consider the graph as directed. If the graph is
to be considered undirected then each edge can be traversed in both directions and
the returned shortest paths can thus be undirected.cost
- holds the costs for traversing each edge. Edge e
has cost cost[e.index()]
.dist
- return value that will hold the shortest distance from node s
to
all other nodes. The distance from s
to v
is
dist[v.index()]
. If there is no path from s
to v
then dist[v.index()] == Double.POSITIVE_INFINITY
.public static void dijkstra(Graph graph, Node s, boolean directed, double[] cost, double[] dist, Edge[] pred)
dijkstra(Graph, Node, boolean, double[], double[])
but additionally this method
yields the path edges of each calculated shortest path.
pred
- return value that holds for each node t
the shortest
path edge pred[t.index()]
which is the last edge on the shortest
path from s
to t
. If t == s
or if there
is no shortest path from s
to t
then
pred[t.index()] == null
.constructNodePath(Node, Node, Edge[])
,
constructEdgePath(Node, Node, Edge[])
public static void dijkstra(Graph graph, Node s, boolean directed, DataProvider cost, NodeMap dist, NodeMap pred)
dijkstra(Graph, Node, boolean, double[], double[], Edge[])
but uses NodeMaps and DataProviders instead of arrays.
cost
- must provide a double value for each edge.dist
- return value. the map will provide a double value for each node.pred
- return value. the map will provide an Edge for each node.public static double singleSourceSingleSink(Graph graph, Node s, Node t, boolean directed, double[] cost, Edge[] pred)
s
to node t
.
It also returns information to construct the actual path between these to nodes.
graph
- the graph being acted upons
- the source node for the shortest path searcht
- the sink node for the shortest path searchdirected
- whether or not to consider the graph as directed. If the graph is
to be considered undirected then each edge can be traversed in both directions and
the returned shortest paths can thus be undirected.cost
- holds the costs for traversing each edge. Edge e
has cost cost[e.index()]
.pred
- return value that holds for each node v
on the
the shortest the path from s
to t
an edge
pred[v.index()]
which is the last edge on
the shortest path from s
to v
. If v == s
or if there
is no shortest path from s
to v
then
pred[v.index()] == null
.
s
and t
if a path between these two
nodes exist and Double.POSITIVE_INFINITY
otherwise.constructNodePath(Node, Node, Edge[])
,
constructEdgePath(Node, Node, Edge[])
public static EdgeList singleSourceSingleSink(Graph graph, Node s, Node t, boolean directed, double[] cost)
singleSourceSingleSink(Graph,Node,Node,boolean,double[],Edge[])
but instead of returning the shortest distance between the source and sink
the actual shortest edge path between these nodes will be returned.
If the returned path is empty then there is no path between the nodes.
public static EdgeList singleSourceSingleSink(Graph graph, Node s, Node t, boolean directed, DataProvider cost)
singleSourceSingleSink(Graph,Node,Node,boolean,DataProvider,NodeMap)
but instead of returning the shortest distance between the source and sink
the actual shortest edge path between these nodes will be returned.
If the returned path is empty then there is no path between the nodes.
public static double singleSourceSingleSink(Graph graph, Node s, Node t, boolean directed, DataProvider cost, NodeMap pred)
singleSourceSingleSink(Graph, Node, Node, boolean, double[], Edge[])
but uses NodeMaps and DataProviders instead of arrays.
cost
- must provide a double value for each edge.pred
- return value. the map will provide an Edge for each node.public static boolean bellmanFord(Graph graph, Node s, boolean directed, double[] cost, double[] dist)
s
to all other nodes.
If, on the other hand, the given graph contains negative-cost cycles this method will yield
no reasonable result which will be indicated by the return value false
.
graph
- the graph being acted upons
- the start node for the shortest path searchdirected
- whether or not to consider the graph as directed. If the graph is
to be considered undirected then each edge can be traversed in both directions and
the returned shortest paths can thus be undirected.cost
- holds the costs for traversing each edge. Edge e
has cost cost[e.index()]
.dist
- return value that will hold the shortest distance from node s
to
all other nodes. The distance from s
to v
is
dist[v.index()]
. If there is no path from s
to v
then dist[v.index()] == Double.POSITIVE_INFINITY
.
false
if this weighted graph contains a negative cost cycle,
true
otherwise.public static boolean bellmanFord(Graph graph, Node s, boolean directed, double[] cost, double[] dist, Edge[] pred)
bellmanFord(Graph, Node, boolean, double[], double[])
but additionally this method
yields the path edges of each calculated shortest path.
pred
- return value that holds for each node t
the shortest
path edge pred[t.index()]
which is the last edge on the shortest
path from s
to t
. If t == s
or if there
is no shortest path from s
to t
then
pred[t.index()] == null
.constructNodePath(Node, Node, Edge[])
,
constructEdgePath(Node, Node, Edge[])
public static boolean bellmanFord(Graph graph, Node s, boolean directed, DataProvider cost, NodeMap dist, NodeMap pred)
bellmanFord(Graph, Node, boolean, double[], double[], Edge[])
but uses NodeMaps and DataProviders instead of arrays.
cost
- must provide a double value for each edge.dist
- return value. the map will provide a double value for each node.pred
- return value. the map will provide an Edge for each node.public static boolean singleSource(Graph graph, Node s, boolean directed, double[] cost, double[] dist)
graph
- the graph being acted upons
- the start node for the shortest path searchdirected
- whether or not to consider the graph as directed. If the graph is
to be considered undirected then each edge can be traversed in both directions and
the returned shortest paths can thus be undirected.cost
- holds the costs for traversing each edge. Edge e
has cost cost[e.index()]
.dist
- return value that will hold the shortest distance from node s
to
all other nodes. The distance from s
to v
is
dist[v.index()]
. If there is no path from s
to v
then dist[v.index()] == Double.POSITIVE_INFINITY
.
false
if this weighted graph contains a negative cost cycle,
true
otherwise.public static boolean singleSource(Graph graph, Node s, boolean directed, double[] cost, double[] dist, Edge[] pred)
singleSource(Graph, Node, boolean, double[], double[])
but additionally this method
yields the path edges of each calculated shortest path.
pred
- return value that holds for each node t
the shortest
path edge pred[t.index()]
which is the last edge on the shortest
path from s
to t
. If t == s
or if there
is no shortest path from s
to t
then
pred[t.index()] == null
.constructNodePath(Node, Node, Edge[])
,
constructEdgePath(Node, Node, Edge[])
public static boolean singleSource(Graph graph, Node s, boolean directed, DataProvider cost, NodeMap dist, NodeMap pred)
singleSource(Graph, Node, boolean, double[], double[], Edge[])
but uses NodeMaps and DataProviders instead of arrays.
cost
- must provide a double value for each edge.dist
- return value. the map will provide a double value for each node.pred
- return value. the map will provide an Edge for each node.public static boolean allPairs(Graph graph, boolean directed, double[] cost, double[][] dist)
false
is
returned and the values returned in dist
are left unspecified.
graph
- the graph being acted upondirected
- whether or not to consider the graph as directed. If the graph is
to be considered undirected then each edge can be traversed in both directions and
the returned shortest paths can thus be undirected.cost
- holds the costs for traversing each edge. Edge e
has cost cost[e.index()]
.dist
- return value that will hold the shortest path distances from all pairs of
nodes s
and t
in the graph.
The distance from s
to t
is
dist[s.index()][t.index()]
. If there is no path from s
to t
then dist[s.index()][t.index()] == Double.POSITIVE_INFINITY
.
public static void findShortestUniformPaths(Graph graph, Node start, Node end, boolean directed, EdgeMap pathMap)
start
to end
node.
This method assumes that each edge of the input graph has a cost of 1.0.
graph
- the input graphstart
- the start nodeend
- the end nodedirected
- whether or not to consider the graph as directed. If the graph is
to be considered undirected then each edge can be traversed in both directions and
the returned shortest paths can thus be undirected.pathMap
- the result. For each edge a boolean value will indicate whether or not
it belongs to a shortest path connecting the two nodes.public static YList kShortestPaths(Graph graph, DataProvider costDP, Node start, Node end, int k)
k
shortest paths
connecting a pair of nodes in a directed graph with non-negative edge costs.
The result will be returned as a list of EdgeList objects.
Note that the returned paths are not required to be simple, i.e. they may contain
a node or an edge multiple times.
graph
- the graph being acted uponcostDP
- a data provider that provides a double-valued cost for each edge
of the input graph.start
- start node of the shortest pathsend
- the end node of the shortest pathsk
-
start
to end
node. The i-th path in the
list contains the i-th shortest path between start
and end
node. Note that the returned list may contain less than k
paths in case
there are fewer directed paths between start and end node.public static YCursor kShortestPathsCursor(Graph graph, DataProvider costDP, Node start, Node end, int k)
kShortestPaths(Graph, DataProvider, Node, Node, int)
that returns its result not as a list but as a special cursor that calculates
the next path in the sequence only when needed.
The returned cursor only supports the operation YCursor.ok()
,
YCursor.current()
and YCursor.next()
.
public static double[] uniformCost(Graph graph)
1.0
for each edge
of the given graph.
cost[]
that contains uniform
edge costs of 1.0 for each edge e: cost[e.index()] == 1.0
.public static NodeList constructNodePath(Node s, Node t, Edge[] pred)
s
- the start node of the shortest path. This must be the
same start node that was specified when pred
was calculated.t
- the end node of the pathpred
- the shortest path edge result array returned by one of the
shortest path edge methods defined in this class.
s
to t
in the correct order. If there
is no path from s
to t
then an empty
list is returned.public static NodeList constructNodePath(Node s, Node t, DataProvider pred)
constructNodePath(Node,Node,Edge[])
with the difference that
the path edges are given by a DataProvider.
pred
- the shortest path edge result DataProvider returned by one of the
shortest path edge methods defined in this class.public static EdgeList constructEdgePath(Node s, Node t, Edge[] pred)
s
- the start node of the shortest path. This must be the
same start node that was specified when pred
was calculated.t
- the end node of the pathpred
- the shortest path edge result array returned by one of the
shortest path edge methods defined in this class.
s
to t
in the correct order. If there
is no path from s
to t
then an empty
list is returned.public static EdgeList constructEdgePath(Node s, Node t, DataProvider pred)
constructEdgePath(Node,Node,Edge[])
with the difference that
the path edges are given by a DataProvider.
pred
- the shortest path edge result DataProvider returned by one of the
shortest path edge methods defined in this class.public static void findShortestUniformPaths(Graph graph, Node start, DataProvider targetMap, boolean directed, int maxLength, EdgeList pathEdges, NodeList pathNodes)
start
to a set of target nodes in the graph not
farther away than a given distance.
This method assumes that each edge of the input graph has a cost of 1.0.
graph
- the input graphstart
- the start nodetargetMap
- a boolean data provider that marks the target nodes. If the data provider is null
all nodes in the graph are assumed to be target nodes.directed
- whether or not to work on directed edgesmaxLength
- the maximum edge length of the shortest paths. Shortest paths
that are longer than this value will not be considered.pathEdges
- a return value. If this parameter is not null, this algorithm first clears the list and then adds
all edges that belong to the detected shortest paths.pathNodes
- a return value. If this parameter is not null, this algorithm first clears the list and then adds
all nodes that belong to the detected shortest paths.public static final EdgeList[] shortestPair(Graph graph, Node source, Node target, boolean directed, DataProvider costDP)
s
and t
and have minimum total length.
graph
- the graph being acted uponsource
- source node of the shortest pairtarget
- end node of the shortest pairdirected
- whether or not to interpret the edges as directed or undirectedcostDP
- a data provider that provides a double-valued cost for each edge
of the input graph.
null
if no such
edge-disjoint paths exist.
|
© Copyright 2000-2013, yWorks GmbH. All rights reserved. |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |