How to resolve the algorithm Dijkstra's algorithm step by step in the Erlang programming language
How to resolve the algorithm Dijkstra's algorithm step by step in the Erlang 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 Erlang programming language
Source code in the erlang programming language
-module(dijkstra).
-include_lib("eunit/include/eunit.hrl").
-export([dijkstrafy/3]).
% just hide away recursion so we have a nice interface
dijkstrafy(Graph, Start, End) when is_map(Graph) ->
shortest_path(Graph, [{0, [Start]}], End, #{}).
shortest_path(_Graph, [], _End, _Visited) ->
% if we're not going anywhere, it's time to start going back
{0, []};
shortest_path(_Graph, [{Cost, [End | _] = Path} | _ ], End, _Visited) ->
% this is the base case, and finally returns the distance and the path
{Cost, lists:reverse(Path)};
shortest_path(Graph, [{Cost, [Node | _ ] = Path} | Routes], End, Visited) ->
% this is the recursive case.
% here we build a list of new "unvisited" routes, where the stucture is
% a tuple of cost, then a list of paths taken to get to that cost from the "Start"
NewRoutes = [{Cost + NewCost, [NewNode | Path]}
|| {NewCost, NewNode} <- maps:get(Node, Graph),
not maps:get(NewNode, Visited, false)],
shortest_path(
Graph,
% add the routes we ripped off earlier onto the new routes
% that we want to visit. sort the list of routes to get the
% shortest routes (lowest cost) at the beginning.
% Erlangs sort is already good enough, and it will sort the
% tuples by the number at the beginning of each (the cost).
lists:sort(NewRoutes ++ Routes),
End,
Visited#{Node => true}
).
basic_test() ->
Graph = #{
a => [{7,b},{9,c},{14,f}],
b => [{7,a},{10,c},{15,d}],
c => [{10,b},{9,c},{11,d},{2,f}],
d => [{15,b},{6,e},{11,c}],
e => [{9,f},{6,d}],
f => [{14,f},{2,c},{9,e}]
},
{Cost, Path} = dijkstrafy(Graph, a, e),
{20,[a,c,f,e]} = {Cost, Path},
io:format(user, "The total cost was ~p and the path was: ", [Cost]),
io:format(user, "~w~n", [Path]).
You may also check:How to resolve the algorithm Digital root/Multiplicative digital root step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Law of cosines - triples step by step in the RPL programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the Burlesque programming language
You may also check:How to resolve the algorithm Spelling of ordinal numbers step by step in the Factor programming language
You may also check:How to resolve the algorithm Temperature conversion step by step in the Phix programming language