15
Digital Logic Design: a rigorous approach c Chapter 4: Directed Graphs Guy Even Moti Medina School of Electrical Engineering Tel-Aviv Univ. March 18, 2020 Book Homepage: http://www.eng.tau.ac.il/ ~ guy/Even-Medina 1 / 27

Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm

Digital Logic Design: a rigorous approach c�Chapter 4: Directed Graphs

Guy Even Moti Medina

School of Electrical Engineering Tel-Aviv Univ.

March 18, 2020

Book Homepage:http://www.eng.tau.ac.il/~guy/Even-Medina

1 / 27

Page 2: Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm

example: longest paths in DAGs

e12

v0

e1

v2

v6v4

v9

v7

e7

e6

e8

e2

e3

e5

e4

e9

e0

v1

v3v5

v10

v8

e10

e11

e13

15 / 27

Page 3: Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm

longest paths

We denote the length of a path � by |�|.

De�nition

A path � that ends in vertex v is a longest path ending in v if|��| � |�| for every path �� that ends in v .

Note: there may be multiple longest paths ending in v (hence “alongest path” rather than “the longest path”).

De�nition

A path � is a longest path in G if |��| � |�|, for every path �� in G .

Question

Does a longest path always exist in a directed graph?

16 / 27

Page 4: Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm

longest paths in DAGs

If a directed graph has a cycle, then there does not exist a longestpath. Indeed, one could walk around the cycle forever. However,longest paths do exist in DAGs.

Lemma

If G = (V ,E ) is a DAG, then there exists a longest path that endsin v , for every v . In addition, there exists a longest path in G .

Proof: The length of every path in a DAG is at most |V |� 1. [Or,every path is simple, hence, the number of paths is �nite.]

17 / 27

Page 5: Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm
Page 6: Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm

computing longest paths: speci�cation

Goal: compute, for every v in a DAG, a longest path that ends inv . We begin with the simpler task of computing the length of alongest path.

Speci�cation

Algorithm longest-path is speci�ed as follows.

input: A DAG G = (V ,E ).

output: A delay function d : V � N.

functionality: For every vertex v � V : d(v) equals the length of alongest path that ends in v .

Application: Model circuits by DAGs. Assume all gates completetheir computation in one unit of time. The delay of the output of agate v equals d(v)

18 / 27

Page 7: Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm

example: delay function

e12

v0

e1

v2

v6v4

v9

v7

e7

e6

e8

e2

e3

e5

e4

e9

e0

v1

v3v5

v10

v8

e10

e11

e13

19 / 27

Page 8: Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm

algorithm: longest path lengths

Algorithm 2 longest-path-lengths(V ,E ) - An algorithm for comput-ing the lengths of longest paths in a DAG. Returns a delay functiond(v).

1 topological sort: (v0, . . . , vn�1) � TS(V ,E ).2 For j = 0 to (n � 1) do

1 If vj is a source then d(vj) � 0.2 Else

d(vj) = 1 + max�

d(vi) | i < j and (vi , vj ) � E�

.

One could design a “single pass” algorithm; the two pass algorithmis easier to prove.

20 / 27

Page 9: Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm
Page 10: Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm

algorithm correctness

Let

d(v) � output of algorithm

�(v) � the length of a longest path that ends in v

Theorem

Algorithm correct: �j : d(vj) = �(vj ).

Proof: Complete induction on j . Basis for sources easy.

21 / 27

Page 11: Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm

algorithm correctness - cont.

We prove now that

1 �(vj+1) � d(vj+1), namely, there exists a path � that ends inv such that |�| � d(vj+1).

2 �(vj+1) � d(vj+1), namely, for every path � that ends in v wehave |�| � d(vj+1).

22 / 27

Page 12: Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm
Page 13: Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm
Page 14: Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm
Page 15: Chapter 4: Directed Graphs Guy Even Moti Medinahyde.eng.tau.ac.il/Even-Medina/Annotated-Slides/longest... · 2020. 3. 18. · Algorithm 2 longest-path-lengths(V,E) - An algorithm