topological-sort

Does Tarjan's SCC algorithm give a topological sort of the SCC?

时光怂恿深爱的人放手 提交于 2019-12-04 18:48:51
问题 I've been studying SCC and algorithms about them, and I've seen that people almost always mention that Kosaraju's algorithm finds the SCC and also gives them ordered in a (reversed) topological sort. My question is: doesn't Tarjan's algorithm also find a (reversed) topological sort? I've found that it isn't mentioned (at least from where I've read, except wikipedia). I've been thinking about it and make perfect sense. When tarjans_dfs is called on some node u, all SCCs that are reachable from

Algorithm for topological sorting if cycles exist

允我心安 提交于 2019-12-04 07:54:44
问题 Some programming languages (like haskell) allow cyclic dependencies between modules. Since the compiler needs to know all definitions of all modules imported while compiling one module, it usually has to do some extra work if some modules import each other mutually or any other kind of cycle occurs. In that case, the compiler may not be able to optimize code as much as in modules that have no import cycles, since imported functions may have not yet been analyzed. Usually only one module of a

Partial order sorting?

允我心安 提交于 2019-12-03 23:22:23
Say, we have some items, and each defines some partial sorting rules, like this: I'm A and I want to be before B I'm C and I want to be after A but before D So we have items A,B,C,D with these rules: A>B C<A , C>D nothing else! So, B and D have no 'preferences' in ordering and are considered equal. As you see, transitive relation rules are not working here. However, if A>B it still means that B<A . So, there can be multiple possible results of sorting: A B C D A C D B A C B D A B C D How can I implement a sorting algorithm that handles such a situation? The reason: there're multiple loadable

What is the best way to sort a partially ordered list?

这一生的挚爱 提交于 2019-12-03 14:00:00
Probably best illustrated with a small example. Given the relations A < B < C A < P < Q Correct outputs would be ABCPQ or APQBC or APBCQ ... etc. In other words, any ordering is valid in which the given relationships hold. I am most interested in the solution that is easiest to implement, but the best O(n) in speed and time is interesting as well. This is called topological sorting . The standard algorithm is to output a minimal element, then remove it and repeat until done. Do several sorts. First sort according to the first rule, then according to the second one and so on. Should work,

Finding Minimum Completion Time of Scheduled Tasks with Topological Sort

浪子不回头ぞ 提交于 2019-12-03 11:04:01
问题 Assume that there are an unlimited number of workers each of which can complete one task, each of which takes some time. There are also precedence constraints where one task cannot be completed until another is. What is the minimum amount of time needed for each task to be completed while respecting the precedence order? As an example, lets say you have 3 tasks. The first task takes 10 units of time to complete, the second takes 5 units of time to complete, and the third takes 6 units of time

Algorithm for topological sorting if cycles exist

北城余情 提交于 2019-12-02 18:27:45
Some programming languages (like haskell ) allow cyclic dependencies between modules. Since the compiler needs to know all definitions of all modules imported while compiling one module, it usually has to do some extra work if some modules import each other mutually or any other kind of cycle occurs. In that case, the compiler may not be able to optimize code as much as in modules that have no import cycles, since imported functions may have not yet been analyzed. Usually only one module of a cycle has to be compiled that way, as a binary object has no dependecies. Let's call such a module

Algorithm for computing partial orderings of dependency graphs

霸气de小男生 提交于 2019-12-01 15:48:54
I'm trying to compute a partial "topological sort" of a dependency graph, which is actually a DAG (Directed Acyclic Graph) to be precise; so as to execute tasks without conflicting dependencies in parallel. I came up with this simple algorithm because what I found on Google wasn't all that helpful (I keep finding only algorithms that themselves run in parallel to compute a normal topological sort). visit(node) { maxdist = 0; foreach (e : in_edge(node)) { maxdist = max(maxdist, 1 + visit(source(e))) } distance[node] = maxdist; return distance[node]; } make_partial_ordering(node) { set all node

Algorithm for computing partial orderings of dependency graphs

拈花ヽ惹草 提交于 2019-12-01 14:47:51
问题 I'm trying to compute a partial "topological sort" of a dependency graph, which is actually a DAG (Directed Acyclic Graph) to be precise; so as to execute tasks without conflicting dependencies in parallel. I came up with this simple algorithm because what I found on Google wasn't all that helpful (I keep finding only algorithms that themselves run in parallel to compute a normal topological sort). visit(node) { maxdist = 0; foreach (e : in_edge(node)) { maxdist = max(maxdist, 1 + visit

Java: Access local variables from anon inner class? (PriorityQueue)

放肆的年华 提交于 2019-12-01 10:36:38
I want to use a PriorityQueue to do a topological sort on a graph. For brevity, I'd like to use an anonymous inner class for the comparator. However, I need access to the graph g in order to determine the in degree of the nodes I'm looking at. Is this possible? /** * topological sort * @param g must be a dag */ public static Queue<String> topoSort(DirectedGraph<String, DefaultEdge> g) { Queue<String> result = new PriorityQueue<String>(g.vertexSet().size(), new Comparator<String>() { DirectedGraph<String, DefaultEdge> g; @Override public int compare(String arg0, String arg1) { if (g.inDegreeOf

Topological sort in OCaml

时光总嘲笑我的痴心妄想 提交于 2019-11-30 12:49:42
问题 I'm trying to write topological sorting in ocaml, but I'm a beginner (in OCaml & graphs algorithms) and I can't do this by myself. It's easier for me to think about topological sorting in, for example, C++ (and there is a lot examples of topological sorting in C++ on the Internet), but I want to learn something new. Moreover, I've found some examples of topological sorting written in OCaml, but I don't understand them, to be frankly. Let's say I have a list (int * int list) list , for example