Upload
hossain-md-shakhawat
View
209
Download
0
Embed Size (px)
Citation preview
Directed Acyclic Graph• A directed acyclic graph or DAG is a directed graph with no
directed cycles:
Topological Sorting Topological sort of a DAG:
Linear ordering of all vertices in graph G such that vertex u comes before vertex v if edge (u, v) ∈ G
Real-world application: Scheduling a dependent graph, Find a feasible course plan for university studies
Topological Sorting Assume a directed acyclic graph G = (V,E).
A topological sort of a DAG G=(V,E) is a linear ordering of all its vertices such that if G contains an edge (u,v), then u appears before v in the ordering.
We can view a topological sort of a graph as an ordering of its vertices along a horizontal line so that all directed edges go from left to right.
If the graph is cyclic, then such ordering is not possible. In other words the topological sorting can be applied on a graph if it is directed and acyclic.
Thus topological sort is different from the usual kind of "sorting"
Topological Sorting TOPOLOGICAL-SORT(G)1. Call DFS(G) to compute finishing time f[v] for
each vertex v. 2. As each vertex is finished, it is inserted into the
front of the linked list.3. Finally return the linked list of vertices
shirtshirt
tietie
jacketjacket
sockssocks
shoesshoeswatchwatch
undershortsundershorts
pantspants
beltbelt Assume that Bumstead can only do one thing at time.
Produce a list of tasks in order such that no task has an edge connecting it to a task earlier in the list.
Professor Bumstead Gets Dressed
Professor Bumstead Gets Dressed
shirtshirt
tietie
jacketjacket
sockssocks
shoesshoeswatchwatch
undershortsundershorts
pantspants
beltbelt
11/16
12/15
6/7
17/18
9/10
13/141/8
2/5
3/4
1. After applying the Depth first search . Timestamp
assigned as d/f
Topological Sort Example
shirtshirt tietie jacketjacketsockssocks shoesshoes watchwatchundershortsundershorts pantspants beltbelt
17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4
2. As each vertex is finished, it was inserted into the front of the linked list 3. Finally the linked list of vertices is returned
Topological Sort Example
shirtshirt tietie jacketjacketsockssocks shoesshoes watchwatchundershortsundershorts pantspants beltbelt
17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4
18 - socks16 - undershorts15 - pants14 - shoes10 - watch8 - shirt7 - belt5 - tie4 - jacket
Shorted According to the finishing time
10
Topological Sort: Definition• Consider the following graph of course prerequisities
111
201
123
213
205220 302
304
306
446
427
402Problem: Find an orderin which all thesecourses can be taken. • To take a course, all of its
prerequisites must be taken first
Example: 111, 123, 201, 213, 304, 306, 427, 205, 446, 220, 302, 402
Strongly Connected
• Every pair of vertices are reachable from each other• Graph G is strongly connected if, for every u and v in V,
there is some path from u to v and some path from v to u.
Strongly Connected
Not Strongly Connected
A B
C
D
E
A B
C
E
D
Strongly-Connected Components
A strongly connected component of a graph is a maximal subset of nodes (along with their associated edges) that is strongly connected. Nodes share a strongly connected component if they are inter-reachable.
Transpose of a Directed Graph
Transpose of G = (V,E): GT=(V, ET), where ET={(u, v): (v, u) ∈E}
If G is a DAG then GT is also a DAG If we print the topological order of G in the reverse
order, then it is a topological order of GT
Finding Strongly Connected Components
• Input: A directed graph G = (V,E)• Output: a partition of V into disjoint sets so that each
set defines a strongly connected component of G
Algorithm
• The intention is to: 1. Perform depth first search and label vertices in
post fix order2. compute reversed directed graph3. perform depth first search on reversed graph4. components of this forest are strongly connected
components
Algorithm
Strongly-Connected-Components(G)1. Perform depth first search on graph G that is call DFS(G)
to compute finishing times f[u] for each vertex u.
2. Compute reversed directed graph GT of graph G3. Perform depth first search on reversed graph that is call
DFS(GT), but in the main loop of DFS, consider the vertices in order of decreasing f[u]
4. Output the vertices of each tree in the depth-first forest of step 3 as a separate strongly connected component.
Ad=13f=14
Ad=13f=14
Bd=11f=16
Bd=11f=16
Cd=1f=10
Cd=1f=10
Dd=8f=9
Dd=8f=9
Ed=12f=15
Ed=12f=15
Fd=3f=4
Fd=3f=4
Gd=2f=7
Gd=2f=7
Hd=5f=6
Hd=5f=6
Strongly-Connected Components
DFS on G, starting at c.
Ad=2f=5π=B
Ad=2f=5π=B
Bd=1f=6
π=NIL
Bd=1f=6
π=NIL
Cd=7f=10
π=NIL
Cd=7f=10
π=NIL
Dd=8f=9π=C
Dd=8f=9π=C
Ed=3f=4π=A
Ed=3f=4π=A
Fd=12f=13π=G
Fd=12f=13π=G
Gd=11f=14
π=NIL
Gd=11f=14
π=NIL
Hd=16f=15
π=NIL
Hd=16f=15
π=NIL
DFS on GT
Ad=2f=5π=B
Ad=2f=5π=B
Bd=1f=6
π=NIL
Bd=1f=6
π=NIL
Cd=7f=10
π=NIL
Cd=7f=10
π=NIL
Dd=8f=9π=C
Dd=8f=9π=C
Ed=3f=4π=A
Ed=3f=4π=A
Fd=12f=13π=G
Fd=12f=13π=G
Gd=11f=14
π=NIL
Gd=11f=14
π=NIL
Hd=16f=15
π=NIL
Hd=16f=15
π=NIL
DFS on GT
This is GT, labeled after running DFS(GT). but in the main loop of DFS, the vertices are processed in order of decreasing f[u](from G). That is B E A C D G H F
GT:
Strongly-Connected ComponentsThese are the 4 trees that result, yielding the strongly connected
components. Finally, merge the nodes of any given tree into a super-node, and
draw links between them, showing the resultant acyclic component graph.
a
b c
d
e
f
g h
a b c d
e f g h
abe cd
fg h
Component Graph
Time Complexity AnalysisStrongly-Connected-Components(G)1. call DFS(G) to compute finishing times f[u] for each vertex u.
Cost: O(E+V)2. compute GT Cost: O(E+V)3. call DFS(GT), but in the main loop of DFS, consider the vertices in
order of decreasing f[u] Cost: O(E+V)4. output the vertices of each tree in the depth-first forest of step 3
as a separate strongly connected component.
The graph GT is the transpose of G, which is visualized byreversing the arrows on the digraph.• Cost: O(E+V)
CSE-201CSE-201
CSE 403CSE 403
CSE 404CSE 404
CSE-101CSE-101
CSE-203CSE-203CSE-202CSE-202
CSE-102CSE-102
CSE-103CSE-103
CSE 303CSE 303
Problem: Find an order in which all these courses can be taken.
• Introduction to Computer → CSE-101• Mathematics → CSE-102• Programming with C → CSE-103• Data Structure → CSE-201• Algorithm → CSE-202• OOP → CSE-203• Compiler Design → CSE 303• Software Engineering → CSE 403• Advanced Software Engineering → CSE 403
• Consider the following graph of course prerequisites
• To take a course, all of its prerequisites must be taken first
Desired Output
CSE 201CSE 201 CSE 403CSE 403 CSE 404CSE 404CSE-101CSE-101 CSE-203CSE-203 CSE-202CSE-202CSE-102CSE-102 CSE-103CSE-103 CSE 303CSE 303
17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4
1. Introduction to Computer → CSE-1012. Mathematics → CSE-1023. Programming with C → CSE-1034. OOP → CSE-2035. Algorithm → CSE-2026. Data Structure → CSE-2017. Compiler Design → CSE 3038. Software Engineering → CSE 4039. Advanced Software Engineering → CSE 403