Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering of results based upon dependencies

Tags:

clojure

I'm looking at resolving compilation targets.. Say if I have a set of compilation targets, each with a set of its own dependencies.

A -> B C

B -> C E 

C -> E F

D -> NONE

E -> F

F -> NONE

The targets cannot be added to a pass unless its dependencies are are in a previous pass. i.e: I want to have a list of compilation steps looking like this:

[[D F] [E] [C] [B] [A]]

So, D and F are compiled, then E, then C, etc... How can this be done?

like image 268
zcaudate Avatar asked Apr 01 '14 20:04

zcaudate


People also ask

What is an example of dependency?

Dependency happens when you can't function without the help of someone or something. If you have a dependency on coffee, you need it to be human in the morning.

What are dependencies in database?

A dependency is a constraint that applies to or defines the relationship between attributes. It occurs in a database when information stored in the same database table uniquely determines other information stored in the same table.

What are dependencies in programming?

A software dependency is a code library or package that is reused in a new piece of software. For example, a machine learning project might call a Python library to build models. The benefit of software dependencies is that they allow developers to more quickly deliver software by building on previous work.


1 Answers

A map would be a natural way to represent your direct dependencies

(def direct-dependencies 
  {:a #{:b :c}, :b #{:c :e}, :c #{:e :f}, :d nil, :e #{:f}, :f nil})

Then a no-frills (no cycle check) topological sort

(defn tsort [m] 
  (let [depth (fn depth [x] 
                (if (empty? (m x)) 
                  0 
                  (->> x m (map depth) (apply max) inc)))]
    (map val (sort-by key (group-by depth (keys m))))))

With output as desired

(tsort direct-dependencies)
;=> ([:f :d] [:e] [:c] [:b] [:a])
like image 116
A. Webb Avatar answered Sep 25 '22 14:09

A. Webb