algorithms

Repository of programs that demonstrate basic algorithms I've been learning
git clone git://git.samirparikh.com/algorithms
Log | Files | Refs | README

commit 0f054d0ec4d84eee111878db9b43436f76c20e2d
parent 7521278cdf574e2613d5bffa55298b2e0bee0856
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Thu, 30 Dec 2021 19:22:19 +0000

initial commit for dijkstra algorithm

Diffstat:
Adijkstra.pl | 27+++++++++++++++++++++++++++
1 file changed, 27 insertions(+), 0 deletions(-)

diff --git a/dijkstra.pl b/dijkstra.pl @@ -0,0 +1,27 @@ +#!/usr/local/bin/perl + +# +# implementation of Dijkstra's algorithm to find the shorted path of weighted +# graphs, as demonstrated in Chapter 7 of "Grokking Algorithms" +# https://www.manning.com/books/grokking-algorithms +# + +use warnings; +use strict; +use v5.22; +use Data::Dumper; + +# define the graph to store the neighbors and the cost to get to those +# neighbors. We will use a hash of a hash +my %graph; +$graph{'start'}{'a'} = 6; +$graph{'start'}{'b'} = 2; + +# get all the neighbors of Start: +say join ", " => keys %{$graph{'start'}}; +# get weights of those edges: +say join ", " => values %{$graph{'start'}}; +# get key-value pairs for neighbors of Start +foreach my $key (sort keys %{$graph{'start'}}) { + say "$key => $graph{'start'}{$key}"; +}