460 likes | 650 Views
Topological Sorting. Brief overview of topological sorting in graphs. Mihail Petrov. mihail-petrov.cloudvps.com. Trainer Candidate. Table of Contents. TS Why ? TS Examples and Warnings TS Algorithm explanation Source Removal Algorithm for TS TS with DFS. TS Why ?.
E N D
Topological Sorting Brief overview of topological sorting in graphs Mihail Petrov mihail-petrov.cloudvps.com TrainerCandidate
Table of Contents • TS Why ? • TS Examples and Warnings • TS Algorithm explanation • Source Removal Algorithm for TS • TS with DFS
TS Why ? Why we need to sort a graph topologically
University course graph Math Programing OOP Data Structures C# Java Spring JS Java EE Find the order in which all the courses can be taken !
Explanation • Using TP Sorting we could find the right sequence of element, represented as a gaph. • Given a digraph G = (V, E), we could find a linear ordering of its vertices such that for any edge (v, w) in E, v precedes w in the ordering.
Sample Graph #1 A C B E D F
Sample Sorting #1 • Actualy we can’t sort it: • What is the order, this: A B C D E • Or this: B A E D C
Sample #1 Conclusion ! We CAN NOT topologicallysort an unordered Graph
Sample Graph#2 A B C# Java Script D C WPF Front-End E Production
Sample Sort Rite Sort – Left Direction A B C D E Wrong Sort – Right Direction A B C E D
Sample #2 Conclusion ! Any linear ordering in which an arrow goes to the left is NOT a vald solution
Sample Graph#3 A B C# Java Script D C WPF Front-End E Production
Sample Sort#3 A B C D E
Sample #3 Conclusion ! A directed graph with a cycle CAN NOT be topologically sorted
TS Algorithm General Explanation of TS
Example: • We have the fouling graph, and we have to apply the TS for it. A B C E D
Step #1 • Identify and Select Node with no incoming edges. A B C E D
Step #1 Warning • If, no such node than: A B TPS Not Posible D C
Step #2 • Remove Selectde Node A B C E D Elements:
Step #2 • Repeat step#2 B C E D A Elements:
Step #2 • Repeat step #2 C E D A B Elements:
Step #2 • Repeat step #2 E D A B C Elements:
Result: • TS Result A B C E D
Graph Representation using Adjacency List A B E B D C C D D E F F E F -
Graph Representation using Adjacency Matrix The Node A is the only Node without Outgoing Edges
Source Removal Implementation • Implementation Steps: • Create an Empty List • Find a Noad withoutIncoming Edges • Add this Node to the End of the List • Remove the Edge from the Graph • Repeat Step 1 throw 4 until the Graph is empty
Step #1-2 The Node A is the only Node without Incoming Edges B C A D E F
Step #3-4 B C D E F L A
Step #2-3 B C D E F L A
Step #2-3 C D E F L A B
Step #2-3 C D E F L A B
Step #2-3 D E F L A B C
Step #2-3 D E F L A B C
Step #2-3 E F L A B C D
Step #2-3 E F L A B C D
Result:TS A B C D E F
TS using Source Removal Live Demo
TS using DFS Pretty much the same
DFS Implementation. • This algoritum is the oposite of SR algoritum • Realisation Steps: • Create ab empty List • Find a Noad without Outgoing Edges • 3.Mark the Node as visited • Reverse the List and get the TS, of the Elements
TS using DFS Live Demo
Combinatorics http://algoacademy.telerik.com
Useful Links • Topological sorting sudo code • http://en.wikipedia.org/wiki/Topological_sorting • Graph teory • http://cs.maycamp.com/?page_id=772 • Source removal and DFS explanation • http://www.youtube.com/watch?v=SwN4LgOw6mo TopCoder forum discusion about number of ways to topological sort: http://apps.topcoder.com/forums/?module=Thread&threadID=667031&start=0&mc=10#1208539