topological-sort

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

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 int...

java: implementation of topological sort, from a reputable source

I'm looking for a reputable Java implementation of a topological sort, given a directed graph of dependencies (node #7 depends on node #2, node #2 depends on note #4, etc.), that will detect the presence of a cycle so I can report an error if a cycle occurs. I assume Apache ant has to do this so I'm hoping I could just make use of it, b...

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

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<...

Topological Sorting using LINQ

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 impleme...

Imposing a Partial Ordering on Git Commit IDs

I'm converting the infrastructure at my workplace to use git instead of svn. The overall migration is going well, but we have a tool that I developed to do our SQL schema migrations. In order to deal with individual schema change dependencies, the migrations script used subversion keyword replacement to put the last-changed revision nu...

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

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 ...

Question from Interview, Retrieve alphabetic order from dictionary

My girlfriend got this question in an interview, and I liked it so much I thought I'd share it... Write an algorithm that receives a dictionary (Array of words). The array is sorted lexicographically, but the abc order can be anything. For example, it could be z, y, x, .., c, b, a. Or it could be completely messed up: d, g, w, y, ... It ...

How to sort a list of inter-linked tuples?

lst = [(u'course', u'session'), (u'instructor', u'session'), (u'session', u'trainee'), (u'person', u'trainee'), (u'person', u'instructor'), (u'course', u'instructor')] I've above list of tuple, I need to sort it with following logic.... each tuple's 2nd element is dependent on 1st element, e.g. (course, session) -> session is dependent...

Topological sort variant algorithm

I have a set of data on which I need to perform a topological sort, with a few assumptions and constraints, and I was wondering if anyone knew an existing, efficient algorithm that would be appropriate for this. The data relationships are known to form a DAG (so no cycles to worry about). An edge from A to B indicates that A depends on...

Can the Jung2 graph library traverse a digraph

Does anyone know if the Java Jung2 graph library provides the in-built capability to traverse a Digraph given a start Vector? I did see that there's a BFSDistanceLabeler class that returns a map of distances, which could do, but I then need to sort the values (highest distance first) and iterate through the sorted set. I'm creating a de...