How to resolve the algorithm Dijkstra's algorithm step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Dijkstra's algorithm step by step in the jq programming language

Table of Contents

Problem Statement

Dijkstra's algorithm, conceived by Dutch computer scientist Edsger Dijkstra in 1956 and published in 1959, is a graph search algorithm that solves the single-source shortest path problem for a graph with non-negative edge path costs, producing a shortest path tree.
This algorithm is often used in routing and as a subroutine in other graph algorithms.

For a given source vertex (node) in the graph, the algorithm finds the path with lowest cost (i.e. the shortest path) between that vertex and every other vertex.

If the vertices of the graph represent cities and edge path costs represent driving distances between pairs of cities connected by a direct road,   Dijkstra's algorithm can be used to find the shortest route between one city and all other cities.
As a result, the shortest path first is widely used in network routing protocols, most notably:

The inputs to Dijkstra's algorithm are a directed and weighted graph consisting of 2 or more nodes, generally represented by:

A destination node is not specified. The output is a set of edges depicting the shortest path to each destination node.

You can use numbers or names to identify vertices in your program.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Dijkstra's algorithm step by step in the jq programming language

Source code in the jq programming language

# (*) If using gojq, uncomment the following line:
# def keys_unsorted: keys;

# remove the first occurrence of $x from the input array
def rm($x):
  index($x) as $ix
  | if $ix then .[:$ix] + .[$ix+1:] else . end;

# Input: a Graph
# Output: a (possibly empty) stream of the neighbors of $node 
# that are also in the array $ary
def neighbors($node; $ary:
  .[$node]
  | select(.)
  | keys_unsorted[]
  | . as $n
  | select($ary | index($n));

# Input: a Graph
def vertices:
  [keys_unsorted[], (.[] | keys_unsorted[])] | unique;
  
# Input: a Graph
# Output: the final version of the scratchpad
def dijkstra($startname):
  . as $graph
  | vertices as $Q
  # scratchpad: { node: { prev, dist} }
  | reduce $Q[] as $v ({};
      . + { ($v): {prev: null, dist: infinite}} )
  | .[$startname].dist = 0
  | { scratchpad: ., $Q }
  | until( .Q|length == 0;
      .scratchpad as $scratchpad
      | ( .Q | min_by($scratchpad[.].dist)) as $u
      | .Q |= rm($u)
      | .Q as $Q
      # for each neighbor v of u still in Q:
      | reduce ($graph|neighbors($u; $Q)) as $v (.;
              (.scratchpad[$u].dist + $graph[$u][$v]) as $alt
              | if $alt < .scratchpad[$v].dist
                then .scratchpad[$v].dist = $alt
                | .scratchpad[$v].prev = $u
		else . end ) )
  | .scratchpad ;	

# Input: a Graph
# Output: the scratchpad
def Dijkstra($startname):
  if .[$startname] == null then "The graph does not contain start vertex \(startname)"
  else dijkstra($startname)
  end;

# Input: scratchpad, i.e. a dictionary with key:value pairs of the form:
#   node: {prev, dist}
# Output: an array, being
#   [optimal path from $node to $n, optimal distance from $node to $n]
def readout($node):
  . as $in
  | $node
  | [recurse($in[.].prev; .)]
  | [reverse, $in[$node].dist] ;

# Input: a graph
# Output: [path, value]
def Dijkstra($startname; $endname):
  Dijkstra($startname)
  | readout($endname) ;

def GRAPH: {
   "a": {"b": 7, "c": 9, "f": 14},
   "b": {"c": 10, "d": 15},
   "c": {"d": 11, "f": 2},
   "d": {"e": 6},
   "e": {"f": 9}
  };

# To produce the final version of the scratchpad:
# GRAPH | Dijkstra("a")

"\nThe shortest paths from a to e and to f:",
(GRAPH | Dijkstra("a"; "e", "f") | .[0])

  

You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Musical scale step by step in the Pure Data programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean/Calculate Pi step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the FBSL programming language
You may also check:How to resolve the algorithm Safe primes and unsafe primes step by step in the Python programming language