backtracking and memoization
play

Backtracking and Memoization Lecture 12 February 28, 2017 Chandra - PowerPoint PPT Presentation

CS 374: Algorithms & Models of Computation, Spring 2017 Backtracking and Memoization Lecture 12 February 28, 2017 Chandra Chekuri (UIUC) CS374 1 Spring 2017 1 / 35 Recursion Reduction: Reduce one problem to another Recursion A


  1. CS 374: Algorithms & Models of Computation, Spring 2017 Backtracking and Memoization Lecture 12 February 28, 2017 Chandra Chekuri (UIUC) CS374 1 Spring 2017 1 / 35

  2. Recursion Reduction: Reduce one problem to another Recursion A special case of reduction reduce problem to a smaller instance of itself 1 self-reduction 2 Problem instance of size n is reduced to one or more instances 1 of size n − 1 or less. For termination, problem instances of small size are solved by 2 some other method as base cases . Chandra Chekuri (UIUC) CS374 2 Spring 2017 2 / 35

  3. Recursion in Algorithm Design Tail Recursion : problem reduced to a single recursive call after 1 some work. Easy to convert algorithm into iterative or greedy algorithms. Examples: Interval scheduling, MST algorithms, etc. Divide and Conquer : Problem reduced to multiple 2 independent sub-problems that are solved separately. Conquer step puts together solution for bigger problem. Examples: Closest pair, deterministic median selection, quick sort. Backtracking : Refinement of brute force search. Build solution 3 incrementally by invoking recursion to try all possibilities for the decision in each step. Dynamic Programming : problem reduced to multiple 4 (typically) dependent or overlapping sub-problems. Use memoization to avoid recomputation of common solutions leading to iterative bottom-up algorithm. Chandra Chekuri (UIUC) CS374 3 Spring 2017 3 / 35

  4. Part I Brute Force Search, Recursion and Backtracking Chandra Chekuri (UIUC) CS374 4 Spring 2017 4 / 35

  5. Maximum Independent Set in a Graph Definition Given undirected graph G = (V , E) a subset of nodes S ⊆ V is an independent set (also called a stable set) if for there are no edges between nodes in S . That is, if u , v ∈ S then (u , v) �∈ E . B C A F E D Some independent sets in graph above: { D } , { A , C } , { B , E , F } Chandra Chekuri (UIUC) CS374 5 Spring 2017 5 / 35

  6. Maximum Independent Set Problem Input Graph G = (V , E) Goal Find maximum sized independent set in G B C A F E D Chandra Chekuri (UIUC) CS374 6 Spring 2017 6 / 35

  7. Maximum Weight Independent Set Problem Input Graph G = (V , E) , weights w(v) ≥ 0 for v ∈ V Goal Find maximum weight independent set in G B C A F E D Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 35

  8. Maximum Weight Independent Set Problem No one knows an efficient (polynomial time) algorithm for this 1 problem Problem is NP-Complete and it is believed that there is no 2 polynomial time algorithm Brute-force algorithm: Try all subsets of vertices. Chandra Chekuri (UIUC) CS374 8 Spring 2017 8 / 35

  9. Brute-force enumeration Algorithm to find the size of the maximum weight independent set. MaxIndSet ( G = (V , E) ): max = 0 for each subset S ⊆ V do check if S is an independent set if S is an independent set and w(S) > max then max = w(S) Output max Chandra Chekuri (UIUC) CS374 9 Spring 2017 9 / 35

  10. Brute-force enumeration Algorithm to find the size of the maximum weight independent set. MaxIndSet ( G = (V , E) ): max = 0 for each subset S ⊆ V do check if S is an independent set if S is an independent set and w(S) > max then max = w(S) Output max Running time: suppose G has n vertices and m edges 2 n subsets of V 1 checking each subset S takes O(m) time 2 total time is O(m2 n ) 3 Chandra Chekuri (UIUC) CS374 9 Spring 2017 9 / 35

  11. A Recursive Algorithm Let V = { v 1 , v 2 , . . . , v n } . For a vertex u let N(u) be its neighbors. Chandra Chekuri (UIUC) CS374 10 Spring 2017 10 / 35

  12. A Recursive Algorithm Let V = { v 1 , v 2 , . . . , v n } . For a vertex u let N(u) be its neighbors. Observation v 1 : vertex in the graph. One of the following two cases is true Case 1 v 1 is in some maximum independent set. Case 2 v 1 is in no maximum independent set. We can try both cases to “reduce” the size of the problem Chandra Chekuri (UIUC) CS374 10 Spring 2017 10 / 35

  13. A Recursive Algorithm Let V = { v 1 , v 2 , . . . , v n } . For a vertex u let N(u) be its neighbors. Observation v 1 : vertex in the graph. One of the following two cases is true Case 1 v 1 is in some maximum independent set. Case 2 v 1 is in no maximum independent set. We can try both cases to “reduce” the size of the problem G 1 = G − v 1 obtained by removing v 1 and incident edges from G G 2 = G − v 1 − N(v 1 ) obtained by removing N(v 1 ) ∪ v 1 from G MIS(G) = max { MIS(G 1 ) , MIS(G 2 ) + w(v 1 ) } Chandra Chekuri (UIUC) CS374 10 Spring 2017 10 / 35

  14. A Recursive Algorithm RecursiveMIS ( G ): if G is empty then Output 0 a = RecursiveMIS ( G − v 1 ) b = w(v 1 ) + RecursiveMIS ( G − v 1 − N(v n ) ) Output max(a , b) Chandra Chekuri (UIUC) CS374 11 Spring 2017 11 / 35

  15. Example Chandra Chekuri (UIUC) CS374 12 Spring 2017 12 / 35

  16. Recursive Algorithms ..for Maximum Independent Set Running time: � � T(n) = T(n − 1) + T n − 1 − deg(v 1 ) + O(1 + deg(v 1 )) where deg(v 1 ) is the degree of v 1 . T(0) = T(1) = 1 is base case. Worst case is when deg(v 1 ) = 0 when the recurrence becomes T(n) = 2T(n − 1) + O(1) Solution to this is T(n) = O(2 n ) . Chandra Chekuri (UIUC) CS374 13 Spring 2017 13 / 35

  17. Backtrack Search via Recursion Recursive algorithm generates a tree of computation where each 1 node is a smaller problem (subproblem) Simple recursive algorithm computes/explores the whole tree 2 blindly in some order. Backtrack search is a way to explore the tree intelligently to 3 prune the search space Some subproblems may be so simple that we can stop the 1 recursive algorithm and solve it directly by some other method Memoization to avoid recomputing same problem 2 Stop the recursion at a subproblem if it is clear that there is no 3 need to explore further. Leads to a number of heuristics that are widely used in practice 4 although the worst case running time may still be exponential. Chandra Chekuri (UIUC) CS374 14 Spring 2017 14 / 35

  18. Sequences Definition Sequence : an ordered list a 1 , a 2 , . . . , a n . Length of a sequence is number of elements in the list. Definition a i 1 , . . . , a i k is a subsequence of a 1 , . . . , a n if 1 ≤ i 1 < i 2 < . . . < i k ≤ n . Definition A sequence is increasing if a 1 < a 2 < . . . < a n . It is non-decreasing if a 1 ≤ a 2 ≤ . . . ≤ a n . Similarly decreasing and non-increasing . Chandra Chekuri (UIUC) CS374 15 Spring 2017 15 / 35

  19. Sequences Example... Example Sequence: 6 , 3 , 5 , 2 , 7 , 8 , 1 , 9 1 Subsequence of above sequence: 5 , 2 , 1 2 Increasing sequence: 3 , 5 , 9 , 17 , 54 3 Decreasing sequence: 34 , 21 , 7 , 5 , 1 4 Increasing subsequence of the first sequence: 2 , 7 , 9 . 5 Chandra Chekuri (UIUC) CS374 16 Spring 2017 16 / 35

  20. Longest Increasing Subsequence Problem Input A sequence of numbers a 1 , a 2 , . . . , a n Goal Find an increasing subsequence a i 1 , a i 2 , . . . , a i k of maximum length Chandra Chekuri (UIUC) CS374 17 Spring 2017 17 / 35

  21. Longest Increasing Subsequence Problem Input A sequence of numbers a 1 , a 2 , . . . , a n Goal Find an increasing subsequence a i 1 , a i 2 , . . . , a i k of maximum length Example Sequence: 6, 3, 5, 2, 7, 8, 1 1 Increasing subsequences: 6, 7, 8 and 3, 5, 7, 8 and 2, 7 etc 2 Longest increasing subsequence: 3, 5, 7, 8 3 Chandra Chekuri (UIUC) CS374 17 Spring 2017 17 / 35

  22. Na¨ ıve Enumeration Assume a 1 , a 2 , . . . , a n is contained in an array A algLISNaive ( A[1 .. n] ): max = 0 for each subsequence B of A do if B is increasing and | B | > max then max = | B | Output max Chandra Chekuri (UIUC) CS374 18 Spring 2017 18 / 35

  23. Na¨ ıve Enumeration Assume a 1 , a 2 , . . . , a n is contained in an array A algLISNaive ( A[1 .. n] ): max = 0 for each subsequence B of A do if B is increasing and | B | > max then max = | B | Output max Running time: Chandra Chekuri (UIUC) CS374 18 Spring 2017 18 / 35

  24. Na¨ ıve Enumeration Assume a 1 , a 2 , . . . , a n is contained in an array A algLISNaive ( A[1 .. n] ): max = 0 for each subsequence B of A do if B is increasing and | B | > max then max = | B | Output max Running time: O(n2 n ) . 2 n subsequences of a sequence of length n and O(n) time to check if a given sequence is increasing. Chandra Chekuri (UIUC) CS374 18 Spring 2017 18 / 35

  25. Recursive Approach: Take 1 LIS : Longest increasing subsequence Can we find a recursive algorithm for LIS ? LIS ( A[1 .. n] ): Chandra Chekuri (UIUC) CS374 19 Spring 2017 19 / 35

  26. Recursive Approach: Take 1 LIS : Longest increasing subsequence Can we find a recursive algorithm for LIS ? LIS ( A[1 .. n] ): Case 1: Does not contain A[n] in which case 1 LIS ( A[1 .. n] ) = LIS ( A[1 .. (n − 1)] ) Case 2: contains A[n] in which case LIS ( A[1 .. n] ) is 2 Chandra Chekuri (UIUC) CS374 19 Spring 2017 19 / 35

  27. Recursive Approach: Take 1 LIS : Longest increasing subsequence Can we find a recursive algorithm for LIS ? LIS ( A[1 .. n] ): Case 1: Does not contain A[n] in which case 1 LIS ( A[1 .. n] ) = LIS ( A[1 .. (n − 1)] ) Case 2: contains A[n] in which case LIS ( A[1 .. n] ) is not so 2 clear. Chandra Chekuri (UIUC) CS374 19 Spring 2017 19 / 35

Recommend


More recommend