1 / 45

Topological Sorting

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

berny
Télécharger la présentation

Topological Sorting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Topological Sorting Brief overview of topological sorting in graphs Mihail Petrov mihail-petrov.cloudvps.com TrainerCandidate

  2. Table of Contents • TS Why ? • TS Examples and Warnings • TS Algorithm explanation • Source Removal Algorithm for TS • TS with DFS

  3. TS Why ? Why we need to sort a graph topologically

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

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

  6. TS Examples and Warnings

  7. Sample Graph #1 A C B E D F

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

  9. Sample #1 Conclusion ! We CAN NOT topologicallysort an unordered Graph

  10. Sample Graph#2 A B C# Java Script D C WPF Front-End E Production

  11. Sample Sort Rite Sort – Left Direction A B C D E Wrong Sort – Right Direction A B C E D

  12. Sample #2 Conclusion ! Any linear ordering in which an arrow goes to the left is NOT a vald solution

  13. Sample Graph#3 A B C# Java Script D C WPF Front-End E Production

  14. Sample Sort#3 A B C D E

  15. Sample #3 Conclusion ! A directed graph with a cycle CAN NOT be topologically sorted

  16. TS Algorithm General Explanation of TS

  17. Example: • We have the fouling graph, and we have to apply the TS for it. A B C E D

  18. Step #1 • Identify and Select Node with no incoming edges. A B C E D

  19. Step #1 Warning • If, no such node than: A B TPS Not Posible D C

  20. Step #2 • Remove Selectde Node A B C E D Elements:

  21. Step #2 • Repeat step#2 B C E D A Elements:

  22. Step #2 • Repeat step #2 C E D A B Elements:

  23. Step #2 • Repeat step #2 E D A B C Elements:

  24. Result: • TS Result A B C E D

  25. TS Program Implementation

  26. Graph Representation using Adjacency List A B E B D C C D D E F F E F -

  27. Graph Representation using Adjacency Matrix The Node A is the only Node without Outgoing Edges

  28. TS using Source Removal

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

  30. Step #1-2 The Node A is the only Node without Incoming Edges B C A D E F

  31. Step #3-4 B C D E F L A

  32. Step #2-3 B C D E F L A

  33. Step #2-3 C D E F L A B

  34. Step #2-3 C D E F L A B

  35. Step #2-3 D E F L A B C

  36. Step #2-3 D E F L A B C

  37. Step #2-3 E F L A B C D

  38. Step #2-3 E F L A B C D

  39. Result:TS A B C D E F

  40. TS using Source Removal Live Demo

  41. TS using DFS Pretty much the same

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

  43. TS using DFS Live Demo

  44. Combinatorics http://algoacademy.telerik.com

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

More Related