Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,38 @@
proc dijkstra {graph origin} {
# Initialize
dict for {vertex distmap} $graph {
dict set dist $vertex Inf
dict set path $vertex {}
}
dict set dist $origin 0
dict set path $origin [list $origin]
while {[dict size $graph]} {
# Find unhandled node with least weight
set d Inf
dict for {uu -} $graph {
if {$d > [set dd [dict get $dist $uu]]} {
set u $uu
set d $dd
}
}
# No such node; graph must be disconnected
if {$d == Inf} break
# Update the weights for nodes lead to by the node we've picked
dict for {v dd} [dict get $graph $u] {
if {[dict exists $graph $v]} {
set alt [expr {$d + $dd}]
if {$alt < [dict get $dist $v]} {
dict set dist $v $alt
dict set path $v [list {*}[dict get $path $u] $v]
}
}
}
# Remove chosen node from graph still to be handled
dict unset graph $u
}
return [list $dist $path]
}

View file

@ -0,0 +1,16 @@
proc makeUndirectedGraph arcs {
# Assume that all nodes are connected to something
foreach arc $arcs {
lassign $arc v1 v2 cost
dict set graph $v1 $v2 $cost
dict set graph $v2 $v1 $cost
}
return $graph
}
set arcs {
{a b 7} {a c 9} {b c 10} {b d 15} {c d 11}
{d e 6} {a f 14} {c f 2} {e f 9}
}
lassign [dijkstra [makeUndirectedGraph $arcs] "a"] costs path
puts "path from a to e costs [dict get $costs e]"
puts "route from a to e is: [join [dict get $path e] { -> }]"