user
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task.

**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.

For instance

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:

:   :   * IS-IS   (Intermediate System to Intermediate System)   and
        * OSPF   (Open Shortest Path First).

Important note

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

:   :   * an adjacency matrix or list,   and
        * a start node.

A destination node is not specified.

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

An example, starting with

```
                                         a──►b,  cost=7,   lastNode=a  
                                         a──►c,  cost=9,   lastNode=a 
                                         a──►d,  cost=NA,  lastNode=a
                                         a──►e,  cost=NA,  lastNode=a 
                                         a──►f,  cost=14,  lastNode=a

    The lowest cost is    a──►b    so    a──►b    is added to the output. 

    There is a connection from   b──►d   so the input is updated to: 
                                         a──►c,  cost=9,   lastNode=a 
                                         a──►d,  cost=22,  lastNode=b 
                                         a──►e,  cost=NA,  lastNode=a 
                                         a──►f,  cost=14,  lastNode=a

    The lowest cost is    a──►c    so    a──►c    is added to the output. 

    Paths to    d    and    f    are cheaper via    c    so the input is updated to:
                                         a──►d,  cost=20,  lastNode=c 
                                         a──►e,  cost=NA,  lastNode=a 
                                         a──►f,  cost=11,  lastNode=c

    The lowest cost is    a──►f    so    c──►f    is added to the output. 

    The input is updated to:
                                         a──►d,  cost=20,  lastNode=c 
                                         a──►e,  cost=NA,  lastNode=a

    The lowest cost is    a──►d    so    c──►d    is added to the output. 

    There is a connection from    d──►e    so the input is updated to:
                                         a──►e,  cost=26,  lastNode=d

    Which just leaves adding    d──►e    to the output.

    The output should now be:
                                       [ d──►e
                                         c──►d
                                         c──►f
                                         a──►c
                                         a──►b ]

```

Task

1. Implement a version of Dijkstra's algorithm that outputs a set of edges depicting the shortest path to each reachable node from an origin.
2. Run your program with the following directed graph starting at node    **a**.
3. Write a program which interprets the output from the above and use it to output the shortest path from node    **a**    to nodes    **e**    and  **f**.

:   :   :   :   :   :   :   :   :   Vertices

                                    | Number | Name |
                                    | --- | --- |
                                    | 1 | a |
                                    | 2 | b |
                                    | 3 | c |
                                    | 4 | d |
                                    | 5 | e |
                                    | 6 | f |

Edges

| Start | End | Cost |
| --- | --- | --- |
| a | b | 7 |
| a | c | 9 |
| a | f | 14 |
| b | c | 10 |
| b | d | 15 |
| c | d | 11 |
| c | f | 2 |
| d | e | 6 |
| e | f | 9 |

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

See also

* Dijkstra's Algorithm vs. A\* Search vs. Concurrent Dijkstra's Algorithm (youtube)
assistant
(formerly Perl 6)

**Works with**: Rakudo version 2018.03

```
class Graph {
  has (%.edges, %.nodes);

  method new(*@args){
    my (%edges, %nodes);
    for @args {
      %edges{.[0] ~ .[1]} = $_;
      %nodes{.[0]}.push( .[0] ~ .[1] );
      %nodes{.[1]}.push( .[0] ~ .[1] );
    }
    self.bless(edges => %edges, nodes => %nodes);
  }

  method neighbours ($source) {
    my (%neighbours, $edges);
    $edges = self.nodes{$source};
    for @$edges -> $x {
      for self.edges{$x}[0..1] -> $y {
        if $y ne $source {
          %neighbours{$y} = self.edges{$x}
        }
      }
    }
    return %neighbours
  }

  method dijkstra ($source, $dest) {
    my (%node_data, $v, $u);
    my @q = self.nodes.keys;

    for self.nodes.keys {
      %node_data{$_}{'dist'} = Inf;
      %node_data{$_}{'prev'} = '';
    }
    %node_data{$source}{'dist'} = 0;

    while @q {
      # %node_data.perl.say;
      my ($mindist, $idx) =
        @((map {[%node_data{@q[$_]}{'dist'},$_]},^@q).min(*[0]));
      $u = @q[$idx];

      if $mindist eq Inf {
        return ()
      }
      elsif $u eq $dest {
        my @s;
        while %node_data{$u}{'prev'} {
          @s.unshift($u);
          $u = %node_data{$u}{'prev'}
        }
        @s.unshift($source);
        return @s;
      }
      else {
        @q.splice($idx,1);
      }

      for self.neighbours($u).kv -> $v, $edge {
        my $alt = %node_data{$u}{'dist'} + $edge[2];
        if $alt < %node_data{$v}{'dist'} {
          %node_data{$v}{'dist'} = $alt;
          %node_data{$v}{'prev'} = $u
        }
      }
    }
  }
}

my $a = Graph.new([
  ["a", "b",  7],
  ["a", "c",  9],
  ["a", "f", 14],
  ["b", "c", 10],
  ["b", "d", 15],
  ["c", "d", 11],
  ["c", "f",  2],
  ["d", "e",  6],
  ["e", "f",  9]
]).dijkstra('a', 'e').say;

```

Output:

```
[a c f e]
```