Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
37
Task/Topological-sort/Tcl/topological-sort-1.tcl
Normal file
37
Task/Topological-sort/Tcl/topological-sort-1.tcl
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package require Tcl 8.5
|
||||
proc topsort {data} {
|
||||
# Clean the data
|
||||
dict for {node depends} $data {
|
||||
if {[set i [lsearch -exact $depends $node]] >= 0} {
|
||||
set depends [lreplace $depends $i $i]
|
||||
dict set data $node $depends
|
||||
}
|
||||
foreach node $depends {dict lappend data $node}
|
||||
}
|
||||
# Do the sort
|
||||
set sorted {}
|
||||
while 1 {
|
||||
# Find available nodes
|
||||
set avail [dict keys [dict filter $data value {}]]
|
||||
if {![llength $avail]} {
|
||||
if {[dict size $data]} {
|
||||
error "graph is cyclic, possibly involving nodes \"[dict keys $data]\""
|
||||
}
|
||||
return $sorted
|
||||
}
|
||||
# Note that the lsort is only necessary for making the results more like other langs
|
||||
lappend sorted {*}[lsort $avail]
|
||||
# Remove from working copy of graph
|
||||
dict for {node depends} $data {
|
||||
foreach n $avail {
|
||||
if {[set i [lsearch -exact $depends $n]] >= 0} {
|
||||
set depends [lreplace $depends $i $i]
|
||||
dict set data $node $depends
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach node $avail {
|
||||
dict unset data $node
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Task/Topological-sort/Tcl/topological-sort-2.tcl
Normal file
20
Task/Topological-sort/Tcl/topological-sort-2.tcl
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
set inputData {
|
||||
des_system_lib std synopsys std_cell_lib des_system_lib dw02 dw01 ramlib ieee
|
||||
dw01 ieee dw01 dware gtech
|
||||
dw02 ieee dw02 dware
|
||||
dw03 std synopsys dware dw03 dw02 dw01 ieee gtech
|
||||
dw04 dw04 ieee dw01 dware gtech
|
||||
dw05 dw05 ieee dware
|
||||
dw06 dw06 ieee dware
|
||||
dw07 ieee dware
|
||||
dware ieee dware
|
||||
gtech ieee gtech
|
||||
ramlib std ieee
|
||||
std_cell_lib ieee std_cell_lib
|
||||
synopsys
|
||||
}
|
||||
foreach line [split $inputData \n] {
|
||||
if {[string trim $line] eq ""} continue
|
||||
dict set parsedData [lindex $line 0] [lrange $line 1 end]
|
||||
}
|
||||
puts [topsort $parsedData]
|
||||
Loading…
Add table
Add a link
Reference in a new issue