topological-sort

Tail recursive algorithm for generating all topological orderings in a graph

有些话、适合烂在心里 提交于 2019-12-23 15:14:23
问题 Given a graph i need to generate all topological orderings. For instance, given the following graph: i want to generate all topological orderings, which are: 2 4 7 5 2 7 4 5 2 4 5 7 Because many topological orderings may exist, I need to generate them lazily. Currently, I have a working implementation that is recursive and works on top of the scala-graph library: import scalax.collection.Graph import scalax.collection.GraphPredef._ import scalax.collection.GraphEdge._ import scala.collection

Can we use the output of one recursive query into another recursive query?

元气小坏坏 提交于 2019-12-23 05:29:05
问题 I wanted to find the topological sort of a DAG. create table topo( v1 int, v2 int ); Insert into topo values (1,3),(2,5),(3,4),(4,5),(4,6),(5,7),(6,5),(7,null) WITH RECURSIVE path(S,d) AS( select t1.v1, 0 from topo t1 left outer join topo as t2 on t1.v1=t2.v2 where t2.v2 IS null UNION ALL select distinct t1.v2, path.d + 1 from path inner join topo as t1 on t1.v1=path.S ) select S from path group by S order by MAX(d); This code gives the output of the topological order of a graph. Now i want

Will a source-removal sort always return a maximal cycle?

霸气de小男生 提交于 2019-12-21 21:18:37
问题 I wrote a source-removal algorithm to sort some dependencies between tables in our database, and it turns out we have a cycle. For simplicity, let's say we have tables A, B, C, and D. The edges are like this: (A, B) (B, A) (B, C) (C, D) (D, A) As you can see, there are two cycles here. One is between A and B and another is between all four of them. Will this type of sort always choke on the largest cycle? Or is that not necessarily the case? 回答1: By source-removal I presume you mean at each

Examples for Topological Sorting on Large DAGs

有些话、适合烂在心里 提交于 2019-12-20 18:44:21
问题 I am looking for real world applications where topological sorting is performed on large graph sizes. Some fields where I image you could find such instances would be bioinformatics, dependency resolution, databases, hardware design, data warehousing... but I hope some of you may have encountered or heard of any specific algorithms/projects/applications/datasets that require topsort. Even if the data/project may not be publicly accessible any hints (and estimates on the order of magnitude of

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

允我心安 提交于 2019-12-19 10:07:53
问题 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>() {

Topological order using bfs

情到浓时终转凉″ 提交于 2019-12-18 08:49:15
问题 The following question was found in Sedgewick and Wayne book about algorithms in java: 4.2.19 Topological sort and BFS. Explain why the following algorithm does not necessarily produce a topological order: Run BFS, and label the vertices by increasing distance to their respective source. I was trying to prove it finding a counter example. But, everytime I try, I get a topological order. I mean, I don't understand why this not work: If the source of a vertex comes before it, why don't we have

Topological Sorting using LINQ

允我心安 提交于 2019-12-17 18:50:58
问题 I have a list of items that have a partial order relation, i. e, the list can be considered a partially ordered set. I want to sort this list in the same way as in this question. As correctly answered there, this is known as topological sorting. There's a reasonably simple known algorithm to solve the problem. I want a LINQ-like implementation of it. I already tried to use OrderBy extension method, but I'm quite sure it's not able to make topological sorting. The problem is that the IComparer

Transforming recursive DFS-based topological sort into a non-recusive algorithm (without losing cycle detection)

◇◆丶佛笑我妖孽 提交于 2019-12-08 10:10:26
问题 Here is a pseudo-code for topological sort from Wikipedia: L ← Empty list that will contain the sorted nodes while there are unmarked nodes do select an unmarked node n visit(n) function visit(node n) if n has a temporary mark then stop (not a DAG) if n is not marked (i.e. has not been visited yet) then mark n temporarily for each node m with an edge from n to m do visit(m) mark n permanently unmark n temporarily add n to head of L I want to write it non-recursively without losing cicle

Partial order sorting?

我们两清 提交于 2019-12-05 11:41:36
问题 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

Will a source-removal sort always return a maximal cycle?

≡放荡痞女 提交于 2019-12-04 19:07:46
I wrote a source-removal algorithm to sort some dependencies between tables in our database, and it turns out we have a cycle. For simplicity, let's say we have tables A, B, C, and D. The edges are like this: (A, B) (B, A) (B, C) (C, D) (D, A) As you can see, there are two cycles here. One is between A and B and another is between all four of them. Will this type of sort always choke on the largest cycle? Or is that not necessarily the case? By source-removal I presume you mean at each step removing a node with no incoming edges. What I think you are asking for is finding the maximal Euler