Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Topological sort, but with a certain kind of grouping

It seems this must be a common scheduling problem, but I don't see the solution or even what to call the problem. It's like a topological sort, but different....

Given some dependencies, say

A -> B -> D -- that is, A must come before B, which must come before D
A -> C -> D

there might be multiple solutions to a topological sort:

    A, B, C, D
and A, C, B, D

are both solutions.

I need an algorithm that returns this:

(A) -> (B,C) -> (D)

That is, do A, then all of B and C, then you can do D. All the ambiguities or don't-cares are grouped.

I think algorithms such as those at Topological Sort with Grouping won't correctly handle cases like the following.

A -> B -> C -> D -> E
A - - - > M - - - > E

For this, the algorithm should return

(A) -> (B, C, D, M) -> (E)

This

A -> B -> D -> F
A -> C -> E -> F

should return

(A) -> (B, D, C, E) -> (F)

While this

A -> B -> D -> F
A -> C -> E -> F
     C -> D
     B -> E

should return

(A) -> (B, C) -> (D, E) -> (F)    

And this

A -> B -> D -> F
A -> C -> E -> F
A -> L -> M -> F
     C -> D
     C -> M
     B -> E
     B -> M
     L -> D
     L -> E

should return

(A) -> (B, C, L) -> (D, E, M) -> (F)    

Is there a name and a conventional solution to this problem? (And do the algorithms posted at Topological Sort with Grouping correctly handle this?)

Edit to answer requests for more examples:

A->B->C
A->C 

should return

(A) -> (B) -> (C). That would be a straight topological sort.

And

A->B->D
A->C->D
A->D

should return

(A) -> (B, C) -> (D)

And

A->B->C
A->C
A->D

should return

(A) -> (B,C,D)
like image 789
JPM Avatar asked Jan 11 '12 23:01

JPM


People also ask

When topological sorting is not possible?

Note: Topological Sorting for a graph is not possible if the graph is not a DAG. For example, a topological sorting of the following graph is “5 4 2 3 1 0”. There can be more than one topological sorting for a graph. Another topological sorting of the following graph is “4 5 2 3 1 0”.

What happens if we apply topological sort in a cyclic graph?

In Topological Sort, the idea is to visit the parent node followed by the child node. If the given graph contains a cycle, then there is at least one node which is a parent as well as a child so this will break Topological Order.

Is topological sorting possible in cyclic graph?

Precisely, a topological sort is a graph traversal in which each node v is visited only after all its dependencies are visited. A topological ordering is possible if and only if the graph has no directed cycles, that is, if it is a directed acyclic graph (DAG).

Can you do topological sort with BFS?

Yes, you can do topological sorting using BFS.


1 Answers

Let G be the transitive closure of the graph. Let G' be the undirected graph that results from removing the orientation from G and taking the complement. The connected components of the G' are the sets you are looking for.

like image 95
wye.bee Avatar answered Oct 07 '22 23:10

wye.bee