SlideShare a Scribd company logo
Design and Analysis of Algorithms
Lecture 12
• Algorithms
1
CS345A
for Directed Acyclic Graphs
Recap of last lecture
2
Problem Definition
Input: A directed graph with and a source vertex
Aim:
• Compute the shortest path to for all
Theorem:
There exists an time algorithm to build size data structure
that compactly stores shortest path to for all
3
Longest path to each vertex ?
No polynomial time algorithm
till date !
Imagine there were no research in Algorithms. And you are asked to write a program
to compute shortest paths from source s to every vertex. Your first attempt would
have been to enumerate all paths from source and then report the path which is
shortest. But the number of paths from source to any vertex can be exponential. So
you would have felt the hardness of the shortest paths problem. May be (if you were
like Dijkstra) you would have worked hard and with perseverance and designed the
algorithm which we call Dijkstra’s algorithm today.
But consider the situation now when the area of algorithm is quite developed ( very
active for more than 50 years). But the best algorithm the researchers have designed
till date for the longest paths problem takes exponential time ! What would be your
reaction to this reality ?
• Frustration, feeling down ? (not good for a person with true scientific spirit)
• Curiosity (natural)
There are hundreds of such algorithmic problems in computer science for which the
fastest algorithm takes only exponential time.
Towards the end of this course, we shall discuss a theory of such a family of problems.
“Theory of NP complete problems”
With this note, we begin today’s lecture.
4
Directed Acyclic Graphs
5
Directed Acyclic Graphs
Question: what is a cycle in ?
Answer: A sequence , ,…, such that (,) for all
and (,) .
6
𝒗 𝟏 𝒗 𝟐 𝒗 𝟑 𝒗𝒌 −𝟏 𝒗 𝒌
…
Directed Acyclic Graphs
Definition:
A directed graph is said to be acyclic if there is no cycle present in it.
Example:
7
z
u
b v
x w a
y
DAG
Topological ordering
Definition: a mapping : []
such that for each edge
() < ()
Theorem: There exists a topological ordering for every DAG.
1 2 3 4 5 6 7 8 9 10 11
Does there exist a
topological ordering for
every directed graph ?
Certainly No if there is any cycle
< < <
< < <
>
Is it possible to arrange vertices in a line so that each edge goes from left to right ?
How to formalize it ?
Topological ordering
Example:
This is indeed a valid topological ordering.
9
z
u
b v
x w a
y
u v a b w
x z y
1 2 3 4 5 6 7 8
How efficiently can we
determine if a given mapping
is indeed a topological
ordering ?
O() time
Topological ordering
Three questions
Question 1: Why does a topological ordering exist for every DAG ?
Question 2: How efficiently can we compute a topological ordering ?
Question 3: What is the use of topological ordering ?
10
APPLICATIONS OF
TOPOLOGICAL ORDERING
Question 3
11
Applications of Topological ordering
Almost every algorithmic problem on DAG exploits Topological ordering.
Examples:
• Single source shortest paths
(No need of Dijkstra’s algorithm)
• Single source longest paths
• Count no. of paths from a source to a destination
(If the no. is a polynomial of )
All these problems have a simple time algorithm for DAG
12
WHY DOES
TOPOLOGICAL ORDERING EXIST FOR EVERY DAG?
Question 1
13
Why does Topological ordering exist ?
Example:
14
z
u
b v
x w a
y
Is there any vertex for
which you can be sure of
its place in the ordering?
Any vertex of indegree=0 can
be given number 1 in a
topological ordering.
What is the guarantee
that such a vertex always
exists in a DAG ?
Why does Topological ordering exist ?
Lemma 1: Every DAG has at least one vertex with in-degree = 0.
Proof:
(an algorithmic proof)
Pick any vertex .
While in-degree 0 do
{ Let be an edge
 ;
}
return ;
Observation: This algorithm, if terminates will output a vertex of indegree =0.
15
a
c
x
c …
b y
cycle
But what is the guarantee that it
will terminate.?
The algorithm does indeed terminate.
In fact, no vertex will be processed twice in
while loop.
Why does Topological ordering exist ?
Lemma 1: Every DAG has at least one vertex with in-degree = 0.
16
z
u
b v
x w a
y
How to use
Lemma 1 to show
existence of a valid
?
Why does Topological ordering exist ?
17
z
w
y
u
a
x
v
u v a
b
b w
x z y
1 2 3 4 5 6 7 8
Why does Topological ordering exist ?
Time complexity of the algorithm: ?
18
Is empty ?
 a vertex with
in-degree = 0
 num;
num  num + 1;
Remove and all its
outgoing edges;
NO
Yes
Input ;
num 1;
A valid
time
HOW EFFICIENTLY CAN WE COMPUTE
TOPOLOGICAL ORDERING ?
19
Important Questions
Aim:
To design a more efficient implementation of the algorithm.
Question:
What is the most time consuming step of the algorithm ?
Answer: Searching a vertex of in-degree = 0 ?
Question:
Do we need to compute from scratch the next vertex of in-degree 0 every time ?
Answer: Perhaps not !
20
Let us explore closely the algorithm to see
“how and when the vertices with in-degree=0 get created” ?
Revisiting the example
The new vertices with indegree=0 are created during ?
21
z
w
y
u
a
x
v
u v a
b
b w
x z y
1 2 3 4 5 6 7 8
..but slowly this time…
the processing of the current vertex of indegree=0
Algorithm for Topological ordering ?
Question:
How to design a more efficient implementation of the algorithm ?
Main step of the current algorithm:
• Searching a vertex of in-degree = 0
Hints:
1. Maintain a set of vertices of In-degree=0
2. Keep an array In-degree[] that stores for each vertex the number of edges
entering it at any stage during the algorithm.
22
time.
Attempt as a Homework.
APPLICATIONS OF
TOPOLOGICAL ORDERING ?
23
Example: Single source shortest paths
: the distance from source .
=0;
=
A correct formulation for distances from in a directed graph.
But difficult to translate to algorithms for general graphs.
Ponder over it.
24
But, this formulation leads to a very simple and
efficient algorithm for distance in DAGs.
Now ponder over it.
Topological ordering
A mapping : [] such that
For each edge , () < ()
1 2 3 4 5 6 7 8 9 10 11
Applications of Topological ordering
I
A mapping : [] such that
For each edge , () < ()
Let be a function on that we wish to compute
If can be expressed in terms of ONLY
We can compute by processing vertices in increasing order of .
1 2 3 4 5 6 7 8 9 10 11
𝒚
It is useful to compute in this order
Applications of Topological ordering
I
A mapping : [] such that
For each edge , () < ()
Example: Single source shortest paths
: the distance from source .
=0;
=
1 2 3 4 5 6 7 8 9 10 11
𝒚
It is useful to compute in this order
time algo
Applications of Topological ordering
II
A mapping : [] such that
For each edge , () < ()
Let be a function on that we wish to compute
If can be expressed in terms of ONLY
We can compute by processing vertices in decreasing order of .
1 2 3 4 5 6 7 8 9 10 11
𝒚
It is useful to compute in this order
Applications of Topological ordering
II
A mapping : [] such that
For each edge , () < ()
Example: Number of paths to a vertex
: the number of paths to .
=1;
=
1 2 3 4 5 6 7 8 9 10 11
𝒚
time algo
It is useful to compute in this order
Homework
Design an time algorithm for the following problem:
Given a directed acyclic graph , and a sequence of vertices ,
does there exist a path in which looks like :
30
Moral of the story
Think of a problem on DAG
 Think of topological ordering

31

More Related Content

PPTX
Design and Analysis of Algorithms Lecture Notes
PPTX
Data Structure Algorithm -Algorithm Complexity
PDF
DAA Notes.pdf
PPT
Mit15 082 jf10_lec01
PDF
01A - Greatest Hits of CS111 Data structure and algorithm
PDF
Lecture 16 - Dijkstra's Algorithm.pdf
PPTX
Linear Programming- Leacture-16-lp1.pptx
PPT
Lecture 1 and 2 of Data Structures & Algorithms
Design and Analysis of Algorithms Lecture Notes
Data Structure Algorithm -Algorithm Complexity
DAA Notes.pdf
Mit15 082 jf10_lec01
01A - Greatest Hits of CS111 Data structure and algorithm
Lecture 16 - Dijkstra's Algorithm.pdf
Linear Programming- Leacture-16-lp1.pptx
Lecture 1 and 2 of Data Structures & Algorithms

Similar to Lecture-12-CS345A-2023 of Design and Analysis (20)

PPTX
Bidirectional graph search techniques for finding shortest path in image base...
PPTX
Asymptotic Notations
PDF
BCS401 ADA First IA Test Question Bank.pdf
PPT
36 greedy
DOCX
IntroductionTopological sorting is a common operation performed .docx
PPTX
Asymptotics 140510003721-phpapp02
PPTX
ICPC 2015, Tsukuba : Unofficial Commentary
PPTX
Undecidable Problems and Approximation Algorithms
PPTX
Introduction to Algorithms and Asymptotic Notation
PDF
A greedy algorithms
PPTX
Searching Algorithms
PPTX
Asymptotic Notations.pptx
PPTX
Data Structures and Algorithms for placements
PPTX
DS Unit-1.pptx very easy to understand..
PDF
Parallelising Dynamic Programming
PPTX
L1_Start_of_Learning_of_Algorithms_Basics.pptx
PDF
Algorithms of graph
PPTX
L1_DatabAlgorithm Basics with Design & Analysis.pptx
PDF
Algorithm Design and Analysis
PDF
Data Structure & Algorithms - Mathematical
Bidirectional graph search techniques for finding shortest path in image base...
Asymptotic Notations
BCS401 ADA First IA Test Question Bank.pdf
36 greedy
IntroductionTopological sorting is a common operation performed .docx
Asymptotics 140510003721-phpapp02
ICPC 2015, Tsukuba : Unofficial Commentary
Undecidable Problems and Approximation Algorithms
Introduction to Algorithms and Asymptotic Notation
A greedy algorithms
Searching Algorithms
Asymptotic Notations.pptx
Data Structures and Algorithms for placements
DS Unit-1.pptx very easy to understand..
Parallelising Dynamic Programming
L1_Start_of_Learning_of_Algorithms_Basics.pptx
Algorithms of graph
L1_DatabAlgorithm Basics with Design & Analysis.pptx
Algorithm Design and Analysis
Data Structure & Algorithms - Mathematical
Ad

More from ssuser9183b6 (8)

PPTX
DEsign and Analysis of ALgorithms slides of lecture 1, 2 and 3
PPTX
Lecture-13-CS345A-2023 of Design and Analysis
PPTX
Lecture-11-CS345A-2023 of Design and Analysis
PPTX
Lecture-7-CS345A-2023 of Design and Analysis
PPTX
Lecture-8-CS345A-2023 of Design and Analysis
PPTX
Lecture-10-CS345A-2023 of Design and Analysis
PPTX
Lecture-7-CS345A-2023 of Design and Analysis
PPTX
DiscreteMathematicsfor btechAsWeProgress.pptx
DEsign and Analysis of ALgorithms slides of lecture 1, 2 and 3
Lecture-13-CS345A-2023 of Design and Analysis
Lecture-11-CS345A-2023 of Design and Analysis
Lecture-7-CS345A-2023 of Design and Analysis
Lecture-8-CS345A-2023 of Design and Analysis
Lecture-10-CS345A-2023 of Design and Analysis
Lecture-7-CS345A-2023 of Design and Analysis
DiscreteMathematicsfor btechAsWeProgress.pptx
Ad

Recently uploaded (20)

PPTX
additive manufacturing of ss316l using mig welding
PPTX
Geodesy 1.pptx...............................................
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Construction Project Organization Group 2.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Sustainable Sites - Green Building Construction
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
PPT on Performance Review to get promotions
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
additive manufacturing of ss316l using mig welding
Geodesy 1.pptx...............................................
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Embodied AI: Ushering in the Next Era of Intelligent Systems
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Internet of Things (IOT) - A guide to understanding
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Construction Project Organization Group 2.pptx
UNIT 4 Total Quality Management .pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
OOP with Java - Java Introduction (Basics)
Sustainable Sites - Green Building Construction
R24 SURVEYING LAB MANUAL for civil enggi
PPT on Performance Review to get promotions
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...

Lecture-12-CS345A-2023 of Design and Analysis

  • 1. Design and Analysis of Algorithms Lecture 12 • Algorithms 1 CS345A for Directed Acyclic Graphs
  • 2. Recap of last lecture 2
  • 3. Problem Definition Input: A directed graph with and a source vertex Aim: • Compute the shortest path to for all Theorem: There exists an time algorithm to build size data structure that compactly stores shortest path to for all 3 Longest path to each vertex ? No polynomial time algorithm till date !
  • 4. Imagine there were no research in Algorithms. And you are asked to write a program to compute shortest paths from source s to every vertex. Your first attempt would have been to enumerate all paths from source and then report the path which is shortest. But the number of paths from source to any vertex can be exponential. So you would have felt the hardness of the shortest paths problem. May be (if you were like Dijkstra) you would have worked hard and with perseverance and designed the algorithm which we call Dijkstra’s algorithm today. But consider the situation now when the area of algorithm is quite developed ( very active for more than 50 years). But the best algorithm the researchers have designed till date for the longest paths problem takes exponential time ! What would be your reaction to this reality ? • Frustration, feeling down ? (not good for a person with true scientific spirit) • Curiosity (natural) There are hundreds of such algorithmic problems in computer science for which the fastest algorithm takes only exponential time. Towards the end of this course, we shall discuss a theory of such a family of problems. “Theory of NP complete problems” With this note, we begin today’s lecture. 4
  • 6. Directed Acyclic Graphs Question: what is a cycle in ? Answer: A sequence , ,…, such that (,) for all and (,) . 6 𝒗 𝟏 𝒗 𝟐 𝒗 𝟑 𝒗𝒌 −𝟏 𝒗 𝒌 …
  • 7. Directed Acyclic Graphs Definition: A directed graph is said to be acyclic if there is no cycle present in it. Example: 7 z u b v x w a y DAG
  • 8. Topological ordering Definition: a mapping : [] such that for each edge () < () Theorem: There exists a topological ordering for every DAG. 1 2 3 4 5 6 7 8 9 10 11 Does there exist a topological ordering for every directed graph ? Certainly No if there is any cycle < < < < < < > Is it possible to arrange vertices in a line so that each edge goes from left to right ? How to formalize it ?
  • 9. Topological ordering Example: This is indeed a valid topological ordering. 9 z u b v x w a y u v a b w x z y 1 2 3 4 5 6 7 8 How efficiently can we determine if a given mapping is indeed a topological ordering ? O() time
  • 10. Topological ordering Three questions Question 1: Why does a topological ordering exist for every DAG ? Question 2: How efficiently can we compute a topological ordering ? Question 3: What is the use of topological ordering ? 10
  • 12. Applications of Topological ordering Almost every algorithmic problem on DAG exploits Topological ordering. Examples: • Single source shortest paths (No need of Dijkstra’s algorithm) • Single source longest paths • Count no. of paths from a source to a destination (If the no. is a polynomial of ) All these problems have a simple time algorithm for DAG 12
  • 13. WHY DOES TOPOLOGICAL ORDERING EXIST FOR EVERY DAG? Question 1 13
  • 14. Why does Topological ordering exist ? Example: 14 z u b v x w a y Is there any vertex for which you can be sure of its place in the ordering? Any vertex of indegree=0 can be given number 1 in a topological ordering. What is the guarantee that such a vertex always exists in a DAG ?
  • 15. Why does Topological ordering exist ? Lemma 1: Every DAG has at least one vertex with in-degree = 0. Proof: (an algorithmic proof) Pick any vertex . While in-degree 0 do { Let be an edge  ; } return ; Observation: This algorithm, if terminates will output a vertex of indegree =0. 15 a c x c … b y cycle But what is the guarantee that it will terminate.? The algorithm does indeed terminate. In fact, no vertex will be processed twice in while loop.
  • 16. Why does Topological ordering exist ? Lemma 1: Every DAG has at least one vertex with in-degree = 0. 16 z u b v x w a y How to use Lemma 1 to show existence of a valid ?
  • 17. Why does Topological ordering exist ? 17 z w y u a x v u v a b b w x z y 1 2 3 4 5 6 7 8
  • 18. Why does Topological ordering exist ? Time complexity of the algorithm: ? 18 Is empty ?  a vertex with in-degree = 0  num; num  num + 1; Remove and all its outgoing edges; NO Yes Input ; num 1; A valid time
  • 19. HOW EFFICIENTLY CAN WE COMPUTE TOPOLOGICAL ORDERING ? 19
  • 20. Important Questions Aim: To design a more efficient implementation of the algorithm. Question: What is the most time consuming step of the algorithm ? Answer: Searching a vertex of in-degree = 0 ? Question: Do we need to compute from scratch the next vertex of in-degree 0 every time ? Answer: Perhaps not ! 20 Let us explore closely the algorithm to see “how and when the vertices with in-degree=0 get created” ?
  • 21. Revisiting the example The new vertices with indegree=0 are created during ? 21 z w y u a x v u v a b b w x z y 1 2 3 4 5 6 7 8 ..but slowly this time… the processing of the current vertex of indegree=0
  • 22. Algorithm for Topological ordering ? Question: How to design a more efficient implementation of the algorithm ? Main step of the current algorithm: • Searching a vertex of in-degree = 0 Hints: 1. Maintain a set of vertices of In-degree=0 2. Keep an array In-degree[] that stores for each vertex the number of edges entering it at any stage during the algorithm. 22 time. Attempt as a Homework.
  • 24. Example: Single source shortest paths : the distance from source . =0; = A correct formulation for distances from in a directed graph. But difficult to translate to algorithms for general graphs. Ponder over it. 24 But, this formulation leads to a very simple and efficient algorithm for distance in DAGs. Now ponder over it.
  • 25. Topological ordering A mapping : [] such that For each edge , () < () 1 2 3 4 5 6 7 8 9 10 11
  • 26. Applications of Topological ordering I A mapping : [] such that For each edge , () < () Let be a function on that we wish to compute If can be expressed in terms of ONLY We can compute by processing vertices in increasing order of . 1 2 3 4 5 6 7 8 9 10 11 𝒚 It is useful to compute in this order
  • 27. Applications of Topological ordering I A mapping : [] such that For each edge , () < () Example: Single source shortest paths : the distance from source . =0; = 1 2 3 4 5 6 7 8 9 10 11 𝒚 It is useful to compute in this order time algo
  • 28. Applications of Topological ordering II A mapping : [] such that For each edge , () < () Let be a function on that we wish to compute If can be expressed in terms of ONLY We can compute by processing vertices in decreasing order of . 1 2 3 4 5 6 7 8 9 10 11 𝒚 It is useful to compute in this order
  • 29. Applications of Topological ordering II A mapping : [] such that For each edge , () < () Example: Number of paths to a vertex : the number of paths to . =1; = 1 2 3 4 5 6 7 8 9 10 11 𝒚 time algo It is useful to compute in this order
  • 30. Homework Design an time algorithm for the following problem: Given a directed acyclic graph , and a sequence of vertices , does there exist a path in which looks like : 30
  • 31. Moral of the story Think of a problem on DAG  Think of topological ordering  31