Topological Sorting
description
Transcript of Topological Sorting
Topological SortingBrief overview of topological sorting in graphs
Mihail Petrov
mihail-petrov.cloudvps.com
Trainer Candidate
Table of Contents
1. TS Why ?
2. TS Examples and Warnings
3. TS Algorithm explanation
4. Source Removal Algorithm for TS
5. TS with DFS
2
TS Why ?Why we need to sort a graph
topologically
University course graph
4
Math
Programing OOP
C#
Data Structures
Java
JS
Spring
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.
5
TS Examples and Warnings
Sample Graph #1
7
A
E
B
C
D
F
Sample Sorting #1 Actualy we can’t sort it: What is the order, this:
8
A B C D E
Or this:
B A E D C
Sample #1 Conclusion
9
We CAN NOT topologically sort an unordered Graph
!
Sample Graph#2
C#
WPF
Production
Java Script
Front-End
A B
CD
E
Sample Sort
11
A B C D E
A B C DE
Rite Sort – Left Direction
Wrong Sort – Right Direction
Sample #2 Conclusion
12
Any linear ordering in which an arrow goes to the left is NOT a vald solution
!
Sample Graph#3
C#
WPF
Production
Java Script
Front-End
A B
CD
E
Sample Sort#3
14
A B C D E
Sample #3 Conclusion
15
A directed graph with a cycle CAN NOT be topologically sorted
!
TS AlgorithmGeneral Explanation of TS
Example:
A
E
C
B
D
We have the fouling graph, and we have to apply the TS for it.
Step #1
A
E
C
B
D
Identify and Select Node with no incoming edges.
Step #1 Warning If, no such node than:
19
A
D
B
C
TPS Not Posible
Step #2
A
E
C
B
D
Remove Selectde Node
Elements:
Step #2
A
E
C
B
D
Repeat step#2
Elements:
Step #2
A
E
C
B
D
Repeat step #2
Elements:
Step #2
A
E
CB
D
Repeat step #2
Elements:
Result:
A ECB D
TS Result
TS Program Implementation
Graph Representation using Adjacency List
26
A
B
C
D
E
B E
D C
D
E F
F
F -
Graph Representation using Adjacency Matrix
27
A B C D E FABCDEF
The Node A is the only Node without Outgoing Edges
TS using Source Removal
Source Removal Implementation
29
Implementation Steps:1. Create an Empty List 2. Find a Noad without Incoming Edges3. Add this Node to the End of the List4. Remove the Edge from the Graph5. Repeat Step 1 throw 4 until the
Graph is empty
Step #1-2
30
A
E
D
B
F
C
The Node A is the only Node without Incoming Edges
Step #3-4
31
A
E
D
B
F
C
L
Step #2-3
32
A
E
D
B
F
C
L
Step #2-3
33
A
E
D
B
F
C
L
Step #2-3
34
A
E
D
B
F
C
L
Step #2-3
35
A
E
D
B
F
CL
Step #2-3
36
A
E
D
B
F
CL
Step #2-3
37
A
E
DB
F
CL
Step #2-3
38
A
E
DB
F
CL
Result:TS
39
A EDB FC
TS using Source Removal
Live Demo
TS using DFSPretty much the same
42
DFS Implementation. This algoritum is the oposite of SR
algoritum Realisation Steps:1. Create ab empty List2. Find a Noad without Outgoing Edges3. 3.Mark the Node as visited4. Reverse the List and get the TS, of
the Elements
TS using DFSLive Demo
форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно
програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки
уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop
уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC
безплатен курс "Разработка на софтуер в cloud среда"
BG Coder - онлайн състезателна система - online judge
курсове и уроци по програмиране, книги – безплатно от Наков
безплатен курс "Качествен програмен код"
алго академия – състезателно програмиране, състезания
ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NETкурсове и уроци по програмиране – Телерик академия
курс мобилни приложения с iPhone, Android, WP7, PhoneGap
free C# book, безплатна книга C#, книга Java, книга C#Дончо Минков - сайт за програмиранеНиколай Костов - блог за програмиранеC# курс, програмиране, безплатно
?
? ? ??
?? ?
?
?
?
??
?
?
? ?
Questions?
?
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