CSE202: Design and Analysis of Algorithms Ragesh Jaiswal, CSE, UCSD - - PowerPoint PPT Presentation

cse202 design and analysis of algorithms
SMART_READER_LITE
LIVE PREVIEW

CSE202: Design and Analysis of Algorithms Ragesh Jaiswal, CSE, UCSD - - PowerPoint PPT Presentation

CSE202: Design and Analysis of Algorithms Ragesh Jaiswal, CSE, UCSD Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms Course Overview Graph Algorithms Algorithm Design Techniques: Greedy Algorithms Divide and Conquer


slide-1
SLIDE 1

CSE202: Design and Analysis of Algorithms

Ragesh Jaiswal, CSE, UCSD

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-2
SLIDE 2

Course Overview

Graph Algorithms Algorithm Design Techniques:

Greedy Algorithms Divide and Conquer Dynamic Programming Network Flows

Computational Intractability

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-3
SLIDE 3

Dynamic Programming

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-4
SLIDE 4

Dynamic Programming

Main Ideas

Main idea: Break the given problem into sub-problems and combine the solutions of the smaller sub-problems to get solutions to larger ones. How is it different than Divide and Conquer?

Here you are allowed overlapping sub-problems.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-5
SLIDE 5

Dynamic Programming

Main Ideas

Main idea: Break the given problem into sub-problems and combine the solutions of the smaller sub-problems to get solutions to larger ones. How is it different than Divide and Conquer?

Here you are allowed overlapping sub-problems.

Suppose your recursive algorithm gives a recursion tree that has many common sub-problems (e.g., recursion for computing Fibonacci numbers), then it helps to save the solution of sub-problems and use this solution whenever the same sub-problem is called. Dynamic programming algorithms are also called table-filling algorithms

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-6
SLIDE 6

Dynamic Programming

Longest increasing subsequence

Problem Longest increasing subsequence: You are given a sequence of integers A[1], A[2], ..., A[n] and you are asked to find a longest increasing subsequence of integers. Example: The longest increasing subsequence of the sequence (7, 2, 8, 6, 3, 6, 9, 7) is ?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-7
SLIDE 7

Dynamic Programming

Longest increasing subsequence

Problem Longest increasing subsequence: You are given a sequence of integers A[1], A[2], ..., A[n] and you are asked to find a longest increasing subsequence of integers. Example: The longest increasing subsequence of the sequence (7, 2, 8, 6, 3, 6, 9, 7) is (2, 3, 6, 7) Let L(i) denote the length of the longest increasing subsequence that ends with the number A[i] What is L(1)?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-8
SLIDE 8

Dynamic Programming

Longest increasing subsequence

Problem Longest increasing subsequence: You are given a sequence of integers A[1], A[2], ..., A[n] and you are asked to find a longest increasing subsequence of integers. Example: The longest increasing subsequence of the sequence (7, 2, 8, 6, 3, 6, 9, 7) is (2, 3, 6, 7) Let L(i) denote the length of the longest increasing subsequence that ends with the number A[i] What is L(1)? L(1) = 1 What is the value of L(i) in terms of L(1), ...L(i − 1)?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-9
SLIDE 9

Dynamic Programming

Longest increasing subsequence

Problem Longest increasing subsequence: You are given a sequence of integers A[1], A[2], ..., A[n] and you are asked to find a longest increasing subsequence of integers. Example: The longest increasing subsequence of the sequence (7, 2, 8, 6, 3, 6, 9, 7) is (2, 3, 6, 7) Let L(i) denote the length of the longest increasing subsequence that ends with the number A[i] What is L(1)? L(1) = 1 What is the value of L(i) in terms of L(1), ...L(i − 1)? L(i) = 1 + max

{j:j<i and A[j]<A[i]} {L(j)}

Note that if the set {j : j < i and A[j] < A[i]} is empty, then the second term on the RHS is 0.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-10
SLIDE 10

Dynamic Programming

Longest increasing subsequence

Let n = 9 and (A[1], ..., A[9]) = (7, 2, 8, 6, 3, 1, 10, 9, 11)

L(1) =? L(2) =? L(3) =? L(4) =? L(5) =? L(6) =? L(7) =? L(8) =? L(9) =?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-11
SLIDE 11

Dynamic Programming

Longest increasing subsequence

Let n = 9 and (A[1], ..., A[9]) = (7, 2, 8, 6, 3, 1, 10, 9, 11)

L(1) = 1 L(2) = 1 L(3) = 2 L(4) = 2 L(5) = 2 L(6) = 1 L(7) = 1 + max{1, 1, 2, 2, 2, 1} = 3 L(8) = 1 + max{1, 1, 2, 2, 2, 1} = 3 L(9) = 1 + max{1, 1, 2, 2, 2, 1, 3, 3} = 4

What is the length of the longest increasing subsequence?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-12
SLIDE 12

Dynamic Programming

Longest increasing subsequence

Let n = 9 and (A[1], ..., A[9]) = (7, 2, 8, 6, 3, 1, 10, 9, 11)

L(1) = 1 L(2) = 1 L(3) = 2 L(4) = 2 L(5) = 2 L(6) = 1 L(7) = 1 + max{1, 1, 2, 2, 2, 1} = 3 L(8) = 1 + max{1, 1, 2, 2, 2, 1} = 3 L(9) = 1 + max{1, 1, 2, 2, 2, 1, 3, 3} = 4

What is the length of the longest increasing subsequence? max

1≤j≤n L(j)

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-13
SLIDE 13

Dynamic Programming

Longest increasing subsequence

Algorithm Length-LIS-recursive(A, n)

  • If (n = 1) return(1)
  • max ← 1
  • For j = (n − 1) to 1
  • If (A[j] < A[n])
  • s ← Length-LIS-recursive(A, j)
  • If (max < s + 1) max ← s + 1
  • return(max)

What is the running time of this algorithm?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-14
SLIDE 14

Dynamic Programming

Longest increasing subsequence

Algorithm Length-LIS-recursive(A, n)

  • If (n = 1) return(1)
  • max ← 1
  • For j = (n − 1) to 1
  • If (A[j] < A[n])
  • s ← Length-LIS-recursive(A, j)
  • If (max < s + 1) max ← s + 1
  • return(max)

What is the running time of this algorithm?

T(n) ≤ T(n − 1) + T(n − 2) + ... + T(1) + cn; T(1) ≤ c

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-15
SLIDE 15

Dynamic Programming

Longest increasing subsequence

Algorithm Length-LIS-recursive(A, n)

  • If (n = 1) return(1)
  • max ← 1
  • For j = (n − 1) to 1
  • If (A[j] < A[n])
  • s ← Length-LIS-recursive(A, j)
  • If (max < s + 1) max ← s + 1
  • return(max)

What is the running time of this algorithm?

T(n) ≤ T(n − 1) + T(n − 2) + ... + T(1) + cn; T(1) ≤ c T(n) = O(n · 2n)

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-16
SLIDE 16

Dynamic Programming

Longest increasing subsequence

Algorithm Length-LIS-recursive(A, n)

  • If (n = 1) return(1)
  • max ← 1
  • For j = (n − 1) to 1
  • If (A[j] < A[n])
  • s ← Length-LIS-recursive(A, j)
  • If (max < s + 1) max ← s + 1
  • return(max)

What is the running time of this algorithm?

T(n) =≤ T(n − 1) + T(n − 2) + ... + T(1) + cn; T(1) ≤ c T(n) = O(n · 2n)

Lot of nodes in the recursion tree are repeated.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-17
SLIDE 17

Dynamic Programming

Longest increasing subsequence

Algorithm Length-LIS(A, n)

  • For i = 1 to n
  • max ← 1
  • For j = 1 to (i − 1)
  • If (A[j] < A[i])
  • If (max < L[j] + 1) max ← L[j] + 1
  • L[i] ← max
  • return the maximum of L[i]’s

What is the running time of this algorithm?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-18
SLIDE 18

Dynamic Programming

Longest increasing subsequence

Algorithm Length-LIS(A, n)

  • For i = 1 to n
  • max ← 1
  • For j = 1 to (i − 1)
  • If (A[j] < A[i])
  • If (max < L[j] + 1) max ← L[j] + 1
  • L[i] ← max
  • return the maximum of L[i]’s

What is the running time of this algorithm? O(n2) But the problem was to find a longest increasing subsequence and not the length!

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-19
SLIDE 19

Dynamic Programming

Longest increasing subsequence

Algorithm LIS(A, n)

  • For i = 1 to n
  • max ← 1
  • P[i] ← i
  • For j = 1 to (i − 1)
  • If (A[j] < A[i])
  • If (max < L[j] + 1)
  • max ← L[j] + 1
  • P[i] ← j
  • L[i] ← max
  • ... // Use P to output a longest increasing subsequence

But the problem was to find a longest increasing subsequence and not the length! For each number, we just note down the index of the number preceding this number in a longest increasing subsequence.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-20
SLIDE 20

Dynamic Programming

Longest increasing subsequence Algorithm LIS(A, n)

  • For i = 1 to n
  • max ← 1
  • P[i] ← i
  • For j = 1 to (i − 1)
  • If (A[j] < A[i])
  • If (max < L[j] + 1)
  • max ← L[j] + 1
  • P[i] ← j
  • L[i] ← max
  • OutputSequence(A, L, P, n)

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-21
SLIDE 21

Dynamic Programming

Longest increasing subsequence

Algorithm OutputSequence(A, L, P, n)

  • Let j be the index such that L[j] is maximized
  • i ← 1
  • While (P[j] = j)
  • B[i] ← A[j]
  • j ← P[j]
  • i ← i + 1
  • B[i] ← A[j]
  • Print B in reverse order

So, one of the longest increasing subsequence is (7, 8, 9, 10).

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-22
SLIDE 22

Dynamic Programming

Longest common subsequence

Problem Let S and T be strings of characters. S is of length n and T is of length m. Find a longest common subsequence in S and T. This is a longest sequence of characters (not necessarily contiguous) that appear in both S and T. Example S = XYXZPQ, T = YXQYXP

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-23
SLIDE 23

Dynamic Programming

Longest common subsequence

Problem Let S and T be strings of characters. S is of length n and T is of length m. Find a longest common subsequence in S and T. This is a longest sequence of characters (not necessarily contiguous) that appear in both S and T. Example S = XYXZPQ, T = YXQYXP

The longest common subsequence is XYXP S = XYXZPQ, T = YXQYXP

How do we define the subproblems?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-24
SLIDE 24

Dynamic Programming

Longest common subsequence

Problem Let S and T be strings of characters. S is of length n and T is of length m. Find a longest common subsequence in S and T. This is a longest sequence of characters (not necessarily contiguous) that appear in both S and T. Example S = XYXZPQ, T = YXQYXP

The longest common subsequence is XYXP S = XYXZPQ, T = YXQYXP

Let L(i, j) denote the length of the longest common subsequence in strings S[1], ..., S[i] and T[1], ..., T[j]. What is L(1, j) for 1 < j ≤ m?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-25
SLIDE 25

Dynamic Programming

Longest common subsequence

Problem Let S and T be strings of characters. S is of length n and T is of length m. Find a longest common subsequence in S and T. This is a longest sequence of characters (not necessarily contiguous) that appear in both S and T. Example S = XYXZPQ, T = YXQYXP

The longest common subsequence is XYXP S = XYXZPQ, T = YXQYXP

Let L(i, j) denote the length of the longest common subsequence in strings S[1], ..., S[i] and T[1], ..., T[j]. What is L(1, j) for 1 < j ≤ m?

1 if S[1] is present in the string T[1], ..., T[j], 0 otherwise.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-26
SLIDE 26

Dynamic Programming

Longest common subsequence

Problem Let S and T be strings of characters. S is of length n and T is of length m. Find a longest common subsequence in S and T. This is a longest sequence of characters (not necessarily contiguous) that appear in both S and T. Example S = XYXZPQ, T = YXQYXP

The longest common subsequence is XYXP S = XYXZPQ, T = YXQYXP

Let L(i, j) denote the length of the longest common subsequence in strings S[1], ..., S[i] and T[1], ..., T[j]. What is L(1, j) for 1 < j ≤ m?

1 if S[1] is present in the string T[1], ..., T[j], 0 otherwise. 1 if S[1] = T[j] else L(1, j) = L(1, j − 1) (with L(1, 0) = 0)

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-27
SLIDE 27

Dynamic Programming

Longest common subsequence

Example S = XYXZPQ, T = YXQYXP

The longest common subsequence is XYXP S = XYXZPQ, T = YXQYXP

Let L(i, j) denote the length of the longest common subsequence in strings S[1], ..., S[i] and T[1], ..., T[j]. What is L(1, j) for 1 < j ≤ m?

1 if S[1] is present in the string T[1], ..., T[j], 0 otherwise. 1 if S[1] = T[j] else L(1, j) = L(1, j − 1) (with L(1, 0) = 0)

Similarly, we can define L(i, 1) for 1 < i ≤ n. Can we say something similar for L(i, j) for i, j = 1?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-28
SLIDE 28

Dynamic Programming

Longest common subsequence

Example S = XYXZPQ, T = YXQYXP

The longest common subsequence is XYXP S = XYXZPQ, T = YXQYXP

Let L(i, j) denote the length of the longest common subsequence in strings S[1], ..., S[i] and T[1], ..., T[j]. What is L(1, j) for 1 < j ≤ m?

1 if S[1] is present in the string T[1], ..., T[j], 0 otherwise. 1 if S[1] = T[j] else L(1, j) = L(1, j − 1) (with L(1, 0) = 0)

Similarly, we can define L(i, 1) for 1 < i ≤ n. Can we say something similar for L(i, j) for i, j = 1?

Claim 1: If S[i] = T[j], then L(i, j) = 1 + L(i − 1, j − 1). Claim 2: If S[i] = T[j], then L(i, j) = max {L(i − 1, j), L(i, j − 1)}.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-29
SLIDE 29

Dynamic Programming

Longest common subsequence

What is L(1, j) for 1 < j ≤ m?

1 if S[1] is present in the string T[1], ..., T[j], 0 otherwise. 1 if S[1] = T[j] else L(1, j) = L(1, j − 1) (with L(1, 0) = 0)

Similarly, we can define L(i, 1) for 1 < i ≤ n. Can we say something similar for L(i, j) for i, j = 1?

Claim 1: If S[i] = T[j], then L(i, j) = 1 + L(i − 1, j − 1). Claim 2: If S[i] = T[j], then L(i, j) = max {L(i − 1, j), L(i, j − 1)}. Figure: The arrows show the dependencies between subproblems.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-30
SLIDE 30

Dynamic Programming

Longest common subsequence

Algorithm Length-LCS(S, T)

  • If (S[1] = T[1]), then L[1, 1] ← 1 else L[1, 1] ← 0
  • For j = 2 to m
  • If (S[1] = T[j]), then L[1, j] ← 1 else L[1, j] ← L[1, j − 1]
  • For i = 2 to n
  • If (S[i] = T[1]), then L[i, 1] ← 1 else L[i, 1] ← L[i − 1, 1]
  • For i = 2 to n
  • For j = 2 to m
  • If (S[i] = T[j]) then L[i, j] ← 1 + L[i − 1, j − 1]

else L[i, j] ← max {L[i − 1, j], L[i, j − 1]}

  • Return(L[n, m])

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-31
SLIDE 31

Dynamic Programming

Longest common subsequence

Algorithm Length-LCS(S, T)

  • If (S[1] = T[1]), then L[1, 1] ← 1 else L[1, 1] ← 0
  • For j = 2 to m
  • If (S[1] = T[j]), then L[1, j] ← 1 else L[1, j] ← L[1, j − 1]
  • For i = 2 to n
  • If (S[i] = T[1]), then L[i, 1] ← 1 else L[i, 1] ← L[i − 1, 1]
  • For i = 2 to n
  • For j = 2 to m
  • If (S[i] = T[j]) then L[i, j] ← 1 + L[i − 1, j − 1]

else L[i, j] ← max {L[i − 1, j], L[i, j − 1]}

  • Return(L[n, m])

What is the running time of the above table-filling algorithm?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-32
SLIDE 32

Dynamic Programming

Longest common subsequence

Algorithm Length-LCS(S, T)

  • If (S[1] = T[1]), then L[1, 1] ← 1 else L[1, 1] ← 0
  • For j = 2 to m
  • If (S[1] = T[j]), then L[1, j] ← 1 else L[1, j] ← L[1, j − 1]
  • For i = 2 to n
  • If (S[i] = T[1]), then L[i, 1] ← 1 else L[i, 1] ← L[i − 1, 1]
  • For i = 2 to n
  • For j = 2 to m
  • If (S[i] = T[j]) then L[i, j] ← 1 + L[i − 1, j − 1]

else L[i, j] ← max {L[i − 1, j], L[i, j − 1]}

  • Return(L[n, m])

What is the running time of the above table-filling algorithm? O(nm)

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-33
SLIDE 33

Dynamic Programming

Longest common subsequence

How do we find a longest common subsequence?

Figure: Array P is used to maintain the pointers to the appropriate

  • subproblem. The blue squares give the position of the characters in a longest

common subsequence.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-34
SLIDE 34

Dynamic Programming

Longest common subsequence

Example: S = XYXZPQ, T = YXQYXP

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-35
SLIDE 35

Dynamic Programming

Longest common subsequence

Example: S = XYXZPQ, T = YXQYXP

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-36
SLIDE 36

Dynamic Programming

Longest common subsequence

Example: S = XYXZPQ, T = YXQYXP

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-37
SLIDE 37

Dynamic Programming

Longest common subsequence

Example: S = XYXZPQ, T = YXQYXP

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-38
SLIDE 38

Dynamic Programming

Longest common subsequence

Example: S = XYXZPQ, T = YXQYXP

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-39
SLIDE 39

Dynamic Programming

Longest common subsequence

Example: S = XYXZPQ, T = YXQYXP

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-40
SLIDE 40

Dynamic Programming

Longest common subsequence

Problem Let S and T be strings of characters. S is of length n and T is of length m. Find a longest common subsequence in S and T. This is a longest sequence of characters (not necessarily contiguous) that appear in both S and T. Claim 1: If i = 0 or j = 0, then L(i, j) = 0. Claim 2: If S[i] = T[j], then L(i, j) = 1 + L(i − 1, j − 1). Claim 3: If S[i] = T[j], then L(i, j) = max {L(i − 1, j), L(i, j − 1)}.

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-41
SLIDE 41

Dynamic Programming

Longest common subsequence

Claim 1: If i = 0 or j = 0, then L(i, j) = 0. Claim 2: If S[i] = T[j], then L(i, j) = 1 + L(i − 1, j − 1). Claim 3: If S[i] = T[j], then L(i, j) = max {L(i − 1, j), L(i, j − 1)}. Here is a simple recursive program to find the length of the longest common subsequence. Algorithm LCS-rec(S, n, T, m)

  • If (n = 0 OR m = 0) then return(0)
  • If (S[n] = S[m]) return(1 + LCS-rec(S, n − 1, T, m − 1))
  • If (S[n] = T[m])

return(max{LCS-rec(S, n, T, m − 1), LCS-rec(S, n − 1, T, m)})

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-42
SLIDE 42

Dynamic Programming

Longest common subsequence

Algorithm LCS-rec(S, n, T, m)

  • If (n = 0 OR m = 0) then return(0)
  • If (S[n] = S[m]) return(1 + LCS-rec(S, n − 1, T, m − 1))
  • If (S[n] = T[m])

return(max{LCS-rec(S, n, T, m − 1), LCS-rec(S, n − 1, T, m)}) What is the running time of this algorithm?

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-43
SLIDE 43

Dynamic Programming

Longest common subsequence

Algorithm LCS-rec(S, n, T, m)

  • If (n = 0 OR m = 0) then return(0)
  • If (S[n] = S[m]) return(1 + LCS-rec(S, n − 1, T, m − 1))
  • If (S[n] = T[m])

return(max{LCS-rec(S, n, T, m − 1), LCS-rec(S, n − 1, T, m)}) What is the running time of this algorithm?

This is exponentially large!

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms

slide-44
SLIDE 44

End

Ragesh Jaiswal, CSE, UCSD CSE202: Design and Analysis of Algorithms