0% found this document useful (0 votes)
105 views10 pages

Topological Sort & Critical Path

Topological Sort & Critical Path
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views10 pages

Topological Sort & Critical Path

Topological Sort & Critical Path
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

DATASTRUCTURE & ALGORITHMS

Activity networks, topological


Sort & critical path
UNIT -2

S.Kavitha
Head & Assistant Professor
Department of Computer Science
Sri Sarada Niketan College of Science for
Women,Karur.
ACTIVITY NETWORK
Activity network is a digraph in which the vertices represent
activities or tasks and edges represent precedence relation
between tasks.
The topological sorting is arranging the vertices of the graph
on some order
 Activity network is also a type of topological ordering where
the vertices of a graph are connected based on some
dependencies.
Topological sorting is possible only for acyclic graphs.
EXAMPLE: Task Dependenc
ies
T1 None

T2 T1

T3 T1
T4 T1, T2
T5 T1
T6 T4, T5
T7 T6

Activity network T8 T7
T9 T6, T8
T10 T9
OPOLOGICAL SORT
A topological sort is a linear ordering of vertices in a directed acyclic
graph such that if there is a path from Vi to Vj, then Vj appears after Vi
in the linear ordering.
Topological ordering is not possible. If the graph has a cycle, since
for two vertices v and w on the cycle, v precedes w and w precedes v.
Any DAG has at least one topological ordering, and algorithms are
known for constructing a topological ordering of any DAG in linear
time.
For instance, the vertices of the graph may represent tasks to be
performed, and the edges may represent constraints that one task must
be performed before another; in this application, a topological ordering
is just a valid sequence for the tasks.
.
TO IMPLEMENT THE TOPOLOGICAL SORT,

PERFORM THE FOLLOWING STEPS .


 Step 1 : Find the indegree for every vertex.
 Step 2 : - Place the vertices whose indegree is ‘0’ on the empty
queue.
 Step 3 : Dequeue the vertex V and decrement the indegree’s of all
its adjacent vertices.
 Step 4 : - Enqueue the vertex on the queue, if its indegree falls to
zero.
 Step 5 : - Repeat from step 3 until the queue becomes empty.
 Step 6 : - The topological ordering is the order in which the vertices
dequeued
Example 2 :
V1 V2

V3 V4 V5

V6 V7
Adjacency Matrix :-
V1 V2 V3 V4 V5 V6 V7
V1 0 1 1 1 0 0 0
V2 0 0 0 1 1 0 0
V3 0 0 0 0 0 1 0
V4 0 0 1 0 0 1 1
V5 0 0 0 1 0 0 1
V6 0 0 0 0 0 0 0
V7 0 0 0 0 0 1 0
INDEGREE 0 1 2 3 1 3 2

Indegree [V1] = 0 Indegree [V2] = 1 Indegree [V3] = 2


Indegree [V4] = 3 Indegree [V5] = 1 Indegree [V6] = 3 Indegree [V7] = 2
INDEGREE BEFORE DEQUEUE
VERTEX 1 2 3 4 5 6 7
V1 0 0 0 0 0 0 0
V2 1 0 0 0 0 0 0
V3 2 1 1 1 0 0 0
V4 3 2 1 0 0 0 0
V5 1 1 0 0 0 0 0
V6 3 3 3 3 2 1 0
V7 2 2 2 1 0 0 0
ENQUEUE V1 V2 V5 V4 V3,V7 V6

DEQUEUE V1 V2 V5 V4 V3 V7 V6

Result of Applying Topological Sort To The Graph In


The topological order is V1, V2, V5, V4, V3, V7, V6 V1 V2

V3 V4 V5

V6 V7
CRITICAL PATH
The critical path for a directed acyclic graph is the most time-consuming path
originating from the root. Critical path analysis answers the following questions
What is the minimum amount of time needed to complete all activities?

For a given activity v, is it possible to delay the completion of that activity


without affecting the overall completion time? If yes, by how much can the
completion of activity v be delayed?
Given a Weighted Directed Acyclic Graph (DAG) and a source vertex s in
it, find the longest distances (critical path) from s to all other vertices in the
given graph. This is critical path.
The longest path problem for a general graph is not as easy as the shortest path
problem because the longest path problem doesn’t have optimal substructure
property. First perform topological sorting of the graph.. For every vertex being
processed, we update distances of its adjacent using distance of current vertex.
Routine to calculate longest-path(G) for
weighted graph

Topologically sort G
for each vertex v ∈ V in linearized order

do dist(v) = max(u,v)∈E {dist(u)


+ w(u, v)} return maxv∈V
{dist(v)}
ADVANTAGES OF
TOPOLOGICAL SORTING
 Finding cycle in a graph: Only directed acyclic
graphs may be ordered topologically (DAG). It is
impossible to arrange a circular graph topologically.
 Operation System deadlock detection: A deadlock
occurs when one process is waiting while another
holds the requested resource.
 Dependency resolution: Topological Sorting has been
proved to be very helpful in Dependency resolution.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy