update
Former-commit-id: bdede6ed1b6f578f2ef046c338caf02d0b29d453 [formerly 7187de361b53e9c8ec121df379b762f2db736ea2] Former-commit-id: 447d58460fbbfd05ffe08428a1288e392637561d
This commit is contained in:
40
_embed/public/ace/demo/kitchen-sink/docs/tcl.tcl
Normal file
40
_embed/public/ace/demo/kitchen-sink/docs/tcl.tcl
Normal file
@@ -0,0 +1,40 @@
|
||||
|
||||
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]
|
||||
}
|
||||
Reference in New Issue
Block a user