documentationfor yFiles for HTML 2.6

SingleSourceShortestPaths

Finds the shortest path between one node and multiple other nodes in the graph (also known as the single-source shortest path problem).

Inheritance Hierarchy
SingleSourceShortestPaths

Remarks

The shortest paths will be determined either by the Bellman-Ford algorithm or Dijkstra's algorithm, depending on whether or not edge costs are provided and the exact values of those edge costs.

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 shortes path edges between a start node and multiple end nodes
// configure the shortest path algorithm
const algorithm = new SingleSourceShortestPaths({
  // single source
  source: startNode,
  // 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:
// calculate paths from startNode to all nodes in the graph
const result = algorithm.run(graph)
// for each end node
for (const targetNode of targetNodes) {
  // set its distance to the startNode as label text
  graph.setLabelText(
    targetNode.labels.get(0),
    `${result.getPathTo(targetNode).distance}`
  )
  // and mark the edge path from start to the end node
  for (const edge of result.getPathTo(targetNode).edges) {
    graph.setStyle(edge, highlightPathStyle)
  }
}// configure the shortest path algorithm
const algorithm = new SingleSourceShortestPaths({
  // single source
  source: startNode,
  // 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:
// calculate paths from startNode to all nodes in the graph
const result = algorithm.run(graph)
// for each end node
for (const targetNode of targetNodes) {
  // set its distance to the startNode as label text
  graph.setLabelText(
    targetNode.labels.get(0),
    `${result.getPathTo(targetNode)!.distance}`
  )
  // and mark the edge path from start to the end node
  for (const edge of result.getPathTo(targetNode)!.edges) {
    graph.setStyle(edge, highlightPathStyle)
  }
}

Type Details

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

See Also

Constructors

Properties

Methods