faculty.cse.tamu.edufaculty.cse.tamu.edu/.../topologicalsortingnotes.docx · web viewdfs...
TRANSCRIPT
DFS Topological Sorting
Theory If G = (E, V) is a Directed Acyclic Graph (DAG), then a topological sorting
of V is a linear ordering of V such that for each edge (u, v) in the DAG, u appears before v in the linear ordering.
The nodes on a graph can represent a task that needs to be done It accomplishes this using the depth-first search (DFS) algorithm, which
starts at a root, and explores as far as possible down the tree before backtracking
The edges linking the nodes can represent constraints that one task has on another.
The algorithm is recursive. This algorithm can only be used when the graph is acyclic and directed
Overall theory view of Topological Graph
1
Topological Graph start to finishDAG Topological Order
2
3
Setup for this algorithm As the graph is traversed, the nodes will be marked. The first time a node is visited, it will be temporarily marked, meaning that
node’s children still need to be looked at. After all the current node’s children have been visited, the node will be permanently marked.
Marking of Vertices during Sorting
An empty list will be needed, with size equal to the number of nodes in the graph
The example graph given would need this list Any time a node is marked Permanently, it is added to the head of the list
Exhausted Vertices stored in Reversed Queue
4
Step by Step Start with the lowest value vertex
o if another segment(s) is unreachable from the current, start again with the next lowest valued vertex
will notice how recursion and queue-ish data structure are used Searches for the shortest path from a starting vertex to all others Once a non-visited vertex is identified, move to that vertex and recursively
continue this until we have to stop DFS Topological uses a stack to determine the last vertex visited example
1st Topological Sort Example
5
6
7
8
9
10
11
12
Complete the problem below: Answerb:Call Stack Topo Order Stack
13
Try this one too. Answerb:
14
How to read the Topological Order Stack The stack that contains the Topological Order is filled with information if you
can decipher it vertices higher in the stack must be completed before some of the items below
Understanding the Topological ResultsTopological Order As a reconnected tree
This is no coincidence that all edges are flowing in one direction2 requires 0 AND 1 to be completed first (look at the original graph to confirm)7 requires what?
15
Try this one on your own. Show ALL stacks and Topological Tree. Answerb:
16
Code Vector used so we can add to the HEAD of the list notice the recursion
topsort in Java
17
Complexity V is the number of nodes E is the number of edges Each vertex is enqueued once at most, so all queue operations run at O(|V|)
constant time. The body of the loop is executed at most once per edge, so the running time is
O(|E|). The initialization is proportional to the size of the graph used, so it runs at O(|
E| + |V|). Add(u,v) – O(1) getDegree(u) – O( |V| ) getAdjacent(u) - O( |V| + |E| ) isAdjacentTo(u,v) – O(1) Worst Case - O(|E| + |V|)
o Graph is undirected and not connected. Might fail to visit some vertices on first DFS call. Then have to search for unvisited vertex and apply a DFS there and continue until all vertices have been visited
Best Case - O(|E|)o Graph is a tree. Start at top of tree, can get to all other vertices just by
looking at edges
18
Applications a DFS based topological sort is the perfect solution for breaking down
directed acyclic graphs into dependency fulfilling sequences a good example of its application (relevant to our studies here) are the
prerequisites for certain computer science courses.
Course requirements for 1999 UMBC CS major
by using a DFS topological sort, we can construct a sequence in which to take these courses that would ensure the prerequisites are always fulfilled.
For exampleo CMSC102 has a prerequisite of CMSC101 as well as MATH151
that means in any sequence of nodes, CMSC101 and MATH151 must precede CMSC 102
o Since CMSC101 has a prerequisite of MATH150, MATH150 must also always precede CMSC101.
possible sequences for CMSC102 include the following
MATH 150, MATH 151, CMSC 101, CMSC 102MATH 151, MATH 150, CMSC 101, CMSC 102MATH 150, CMSC 101, MATH 151, CMSC 102
Note that the order the prerequisites appear in is irrelevant as long as it holds that no item appears in the list without having all of its prerequisites appear before it.
19
AnswersTopological Exercise (in video)
20
Exercise #1
21
Exercise #2
22
Sourceshttp://www.just.edu.jo/~basel/algorithms/Algo%20Slides/algo_ch22_graph_part3.pdfhttp://cs.anu.edu.au/~Alistair.Rendell/Teaching/apac_comp3600/module4/basic_graph_algorithms.xhtmlhttp://www.cs.umbc.edu/courses/undergraduate/341/Lectures/Graphs/Graphs.ppthttp://www.cs.usfca.edu/~galles/visualization/TopoSortDFS.htmlhttp://www.cse.cuhk.edu.hk/~taoyf/course/2100sum11/lec14.pdfhttp://www.cse.ust.hk/faculty/golin/COMP271Sp03/Notes/MyL08.pdfhttp://www.cs.nott.ac.uk/~nza/G5BADS03/graphs3.pdfCMSC Image SourceData Structures and Algorithm Analysis in Java 3rd Edition by Mark Allen Weisshttp://www.cs.princeton.edu/courses/archive/spr10/cos226/lectures.html (topological sort under Demos)https://www.youtube.com/watch?v=jksMzq4LgfM
Topological Graph Creator (with some edits)Dia
23