Topological sort
You are encouraged to solve this task according to the task description, using any language you may know.
Given a mapping between items, and items they depend on, a topological sort orders items so that no item precedes an item it depends upon.
The compiling of a library in the VHDL language has the constraint that a library must be compiled after any library it depends on. A tool exists that extracts library dependencies. The task is to write a function that will return a valid compile order of VHDL libraries from their dependencies.
- Assume library names are single words.
- Items mentioned as only dependants, (sic), have no dependants of their own, but their order of compiling must be given.
- Any self dependencies should be ignored.
- Any un-orderable dependencies should be flagged.
Use the following data as an example:
LIBRARY LIBRARY DEPENDENCIES ======= ==================== 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
Note: the above data would be un-orderable if, for example, dw04
is added to the list of dependencies of dw01
.
E
<lang e>def makeQueue := <elib:vat.makeQueue>
def topoSort(data :Map[any, Set[any]]) {
# Tables of nodes and edges def forwardEdges := [].asMap().diverge() def reverseCount := [].asMap().diverge() def init(node) { reverseCount[node] := 0 forwardEdges[node] := [].asSet().diverge() } for node => deps in data { init(node) for dep in deps { init(dep) } } # 'data' holds the dependencies. Compute the other direction. for node => deps in data { for dep ? (dep != node) in deps { forwardEdges[dep].addElement(node) reverseCount[node] += 1 } }
# Queue containing all elements that have no (initial or remaining) incoming edges def ready := makeQueue() for node => ==0 in reverseCount { ready.enqueue(node) } var result := [] while (ready.optDequeue() =~ node :notNull) { result with= node for next in forwardEdges[node] { # Decrease count of incoming edges and enqueue if none if ((reverseCount[next] -= 1).isZero()) { ready.enqueue(next) } } forwardEdges.removeKey(node) } if (forwardEdges.size().aboveZero()) { throw(`Topological sort failed: $forwardEdges remains`) } return result
}</lang>
<lang e>pragma.enable("accumulator")
def dataText := "\ 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\ "
def data := accum [].asMap() for rx`(@item.{17})(@deps.*)` in dataText.split("\n") { _.with(item.trim(), deps.split(" ").asSet()) }
println(topoSort(data))</lang>
Output: ["std", "synopsys", "ieee", "dware", "gtech", "ramlib", "std_cell_lib", "dw02", "dw05", "dw06", "dw07", "dw01", "des_system_lib", "dw03", "dw04"]
Python
<lang python>try:
from functools import reduce
except:
pass
from pprint import pprint as pp from copy import deepcopy
class CyclicDependencyError(Exception): pass
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
data = {
'des_system_lib': set('std synopsys std_cell_lib des_system_lib dw02 dw01 ramlib ieee'.split()), 'dw01': set('ieee dw01 dware gtech'.split()), 'dw02': set('ieee dw02 dware'.split()), 'dw03': set('std synopsys dware dw03 dw02 dw01 ieee gtech'.split()), 'dw04': set('dw04 ieee dw01 dware gtech'.split()), 'dw05': set('dw05 ieee dware'.split()), 'dw06': set('dw06 ieee dware'.split()), 'dw07': set('ieee dware'.split()), 'dware': set('ieee dware'.split()), 'gtech': set('ieee gtech'.split()), 'ramlib': set('std ieee'.split()), 'std_cell_lib': set('ieee std_cell_lib'.split()), 'synopsys': set(), }
def toposort(dependencies):
givenchildren = set(dependencies.keys()) givenparents = reduce(set.union, ( set(p for p in parents) for child, parents in dependencies.items() ) ) data = deepcopy(dependencies) # Every parent is also a child, sometimes of nothing. originalchildren = givenparents - givenchildren for child in originalchildren: data[child] = set() # No parents # Self dependencies are no dependencies for child, parents in data.items(): parents.discard(child)
order = list()
while data: nocurrentdependencies = [child for child, parents in data.items() if not parents] if not nocurrentdependencies and data: #raise CyclicDependencyError, "Does not involve items: %s" % order raise CyclicDependencyError("Involving items from: %s" % data.keys()) order += sorted(nocurrentdependencies) nocurrentdependencies = set(nocurrentdependencies) for child, parents in data.items(): parents.difference_update(nocurrentdependencies) for child in nocurrentdependencies: del data[child]
return order
print (', '.join( toposort(data) )) </lang>
Ordered output:
ieee, std, synopsys, dware, gtech, ramlib, std_cell_lib, dw01, dw02, dw05, dw06, dw07, des_system_lib, dw03, dw04
If dw04 is added to the set of dependencies of dw01 to make the data un-orderable, an exception is raised:
Traceback (most recent call last): File "C:\Documents and Settings\All Users\Documents\Paddys\topological_sort.py", line 77, in <module> print (', '.join( toposort(data) )) File "C:\Documents and Settings\All Users\Documents\Paddys\topological_sort.py", line 67, in toposort raise CyclicDependencyError("Involving items from: %s" % data.keys()) CyclicDependencyError: Involving items from: ['des_system_lib', 'dw04', 'dw03', 'dw01']
Ruby
Uses the TSort module from the Ruby stdlib. <lang ruby>require 'tsort' class Hash
include TSort alias tsort_each_node each_key def tsort_each_child(node, &block) fetch(node).each(&block) end
end
depends = {} DATA.each do |line|
libs = line.split(' ') key = libs.shift depends[key] = libs libs.each {|lib| depends[lib] ||= []}
end
begin
p depends.tsort depends["dw01"] << "dw04" p depends.tsort
rescue TSort::Cyclic => e
puts "cycle detected: #{e}"
end
__END__ 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</lang> Produces:
["ieee", "dware", "gtech", "dw01", "dw02", "std", "synopsys", "dw03", "dw04", "dw05", "std_cell_lib", "ramlib", "des_system_lib", "dw06", "dw07"] cycle detected: topological sort failed: ["dw01", "dw04"]
Tcl
<lang tcl>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 }
}
}</lang> Demonstration code (which parses it from the format that the puzzle was posed in): <lang tcl>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]</lang> Sample output:
ieee std synopsys dware gtech ramlib std_cell_lib dw01 dw02 dw05 dw06 dw07 des_system_lib dw03 dw04
If the suggested extra arc is added, this is the error output:
graph is cyclic, possibly involving nodes "des_system_lib dw01 dw03 dw04"
UNIX Shell
The unix tsort utility does a topological sort where dependencies on multiple items must be reformatted as multiple lines of dependencies of an item and only one dependant.
bash$ tsort <<! > des_system_lib des_system_lib > des_system_lib dw01 > des_system_lib dw02 > des_system_lib ieee > des_system_lib ramlib > des_system_lib std > des_system_lib std_cell_lib > des_system_lib synopsys > dw01 dw01 > dw01 dware > dw01 gtech > dw01 ieee > dw02 dw02 > dw02 dware > dw02 ieee > dw03 dw01 > dw03 dw02 > dw03 dw03 > dw03 dware > dw03 gtech > dw03 ieee > dw03 std > dw03 synopsys > dw04 dw01 > dw04 dw04 > dw04 dware > dw04 gtech > dw04 ieee > dw05 dw05 > dw05 dware > dw05 ieee > dw06 dw06 > dw06 dware > dw06 ieee > dw07 dware > dw07 ieee > dware dware > dware ieee > gtech gtech > gtech ieee > ramlib ieee > ramlib std > std_cell_lib ieee > std_cell_lib std_cell_lib ! des_system_lib dw03 dw04 dw05 dw06 dw07 std_cell_lib ramlib synopsys dw02 dw01 std gtech dware ieee bash$