documentationfor yFiles for HTML 2.6

ShortestPath

Finds the shortest path between two nodes (also known as the single-source single-sink shortest path problem).

Inheritance Hierarchy
ShortestPath

Remarks

The shortest path will be determined either by Dijkstra's algorithm, or an implementation of A*, depending on whether a heuristic exists for estimating the remaining distance between a node and the sink node.

Other Shortest Path Algorithms

yFiles for HTML supports a number of other algorithms that compute shortest paths in a graph:

Other Path-Related Algorithms

yFiles for HTML also supports a number of other algorithms related to paths in a graph:

  • Paths – finds all paths between a set of source and a set of target nodes
  • Chains – finds all chains, that is, sequences of nodes that are each connected with just an edge without branches
  • Cycle – finds a cycle if one exists
  • LongestPath – finds the longest path in the graph

Examples

Highlighting the shortest path edges between a start and an end node
// configure the shortest path algorithm
const algorithm = new ShortestPath({
  // single source - single sink
  source: startNode,
  sink: endNode,
  // add edge cost mapping which returns the actual length of the edge
  costs: (edge) =>
    edge.style.renderer
      .getPathGeometry(edge, edge.style)
      .getPath()
      .getLength()
})
// run the algorithm
const result = algorithm.run(graph)
// highlight the edge path
for (const edge of result.edges) {
  graph.setStyle(edge, highlightPathStyle)
}// configure the shortest path algorithm
const algorithm = new ShortestPath({
  // single source - single sink
  source: startNode,
  sink: endNode,
  // add edge cost mapping which returns the actual length of the edge
  costs: (edge) =>
    edge.style.renderer
      .getPathGeometry(edge, edge.style)
      .getPath()!
      .getLength()
})
// run the algorithm
const result = algorithm.run(graph)
// highlight the edge path
for (const edge of result.edges) {
  graph.setStyle(edge, highlightPathStyle)
}

Type Details

yfiles module
view-layout-bridge
yfiles-umd modules
view-layout-bridge
Legacy UMD name
yfiles.analysis.ShortestPath

See Also

Constructors

Properties

Methods