cpsc 490 problem solving in computer science
play

CPSC 490: Problem Solving in Computer Science Presentations are - PowerPoint PPT Presentation

Lecture 10: Tree DP, Smaller-to-larger optimization, DP on graphs Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-02-05 University of British Columbia CPSC 490: Problem Solving in Computer Science Presentations are


  1. Lecture 10: Tree DP, Smaller-to-larger optimization, DP on graphs Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-02-05 University of British Columbia CPSC 490: Problem Solving in Computer Science

  2. • Presentations are next week (Feb 12 and 14). • Presentation topic choices are due Wednesday. • (Part of) assignment 4 is released (due Mar 1). 1 Announcements

  3. v child of u f v Recall that Dynamic Programming = break down problem recursively into similar sub-problems and then combining their answers When we need to do something on a tree... • What are the sub-problems? • What are the base cases? • How do we do our computation? Tree DP example: compute the size of the tree • Leaf (i.e. base case): f u 1 • Non-leaf (i.e. recurrence): f u 1 • Answer: f root • Time complexity: O N 2 You already know how to do tree DP!

  4. v child of u f v Recall that Dynamic Programming = break down problem recursively into similar sub-problems and then combining their answers When we need to do something on a tree... • What are the sub-problems? • What are the base cases? • How do we do our computation? Tree DP example: compute the size of the tree • Leaf (i.e. base case): f u 1 • Non-leaf (i.e. recurrence): f u 1 • Answer: f root • Time complexity: O N 2 You already know how to do tree DP!

  5. v child of u f v Recall that Dynamic Programming = break down problem recursively into similar sub-problems and then combining their answers When we need to do something on a tree... • What are the sub-problems? Subtrees! • What are the base cases? • How do we do our computation? Tree DP example: compute the size of the tree • Leaf (i.e. base case): f u 1 • Non-leaf (i.e. recurrence): f u 1 • Answer: f root • Time complexity: O N 2 You already know how to do tree DP!

  6. v child of u f v Recall that Dynamic Programming = break down problem recursively into similar sub-problems and then combining their answers When we need to do something on a tree... • What are the sub-problems? Subtrees! • What are the base cases? Leaf nodes! • How do we do our computation? Tree DP example: compute the size of the tree • Leaf (i.e. base case): f u 1 • Non-leaf (i.e. recurrence): f u 1 • Answer: f root • Time complexity: O N 2 You already know how to do tree DP!

  7. v child of u f v Recall that Dynamic Programming = break down problem recursively into similar sub-problems and then combining their answers When we need to do something on a tree... • What are the sub-problems? Subtrees! • What are the base cases? Leaf nodes! • How do we do our computation? DFS! Tree DP example: compute the size of the tree • Leaf (i.e. base case): f u 1 • Non-leaf (i.e. recurrence): f u 1 • Answer: f root • Time complexity: O N 2 You already know how to do tree DP!

  8. Recall that Dynamic Programming = break down problem recursively into similar sub-problems and then combining their answers When we need to do something on a tree... • What are the sub-problems? Subtrees! • What are the base cases? Leaf nodes! • How do we do our computation? DFS! Tree DP example: compute the size of the tree 2 You already know how to do tree DP! • Leaf (i.e. base case): f ( u ) = 1 • Non-leaf (i.e. recurrence): f ( u ) = 1 + ∑ v child of u f ( v ) • Answer: f ( root ) • Time complexity: O ( N )

  9. Modify your favourite balanced binary search tree (e.g. AVL, red-black, splay) to support 3 Problem 1 – Order statistics finding the k th largest element in O (log n ) time.

  10. 4 4 • Update operation (e.g. rotations): update child if necessary, then recompute size of current node 7 1 6 2 5 3 • In every node, keep track of the size of subtree Problem 1 – Solution • Find k -th largest element: def find(T, k): if T.right.size == k - 1: return T.value if T.right.size >= k: return find(T.right, k) else : return find(T.left, k - T.right.size - 1) • All operations (insert, delete, find, get k -th largest) are still O (log n )

  11. • Size • Height • Diameter (length of longest simple path) • Average of values • Median of values • k -th largest value 5 Problem 2 – Bookkeeping Input : a rooted tree with N ≤ 10 5 nodes, with each node containing an integer value. Output : the following statistics for every subtree and for the entire tree:

  12. 6 • g u Time complexity: O N two largest values of g child 2 f child • f node 0 g leaf • f leaf length of longest path that starts in subtree and ends at u length of the diameter of the subtree rooted at u Size of each subtree • f u • Idea: keep two numbers per node Diameter of the tree f child 1 1, f node • f leaf Height of each subtree Problem 2 – Solution (Part 1) • f ( leaf ) = 1, f ( node ) = 1 + ∑ f ( child )

  13. 6 • f leaf Time complexity: O N two largest values of g child 2 f child • f node 0 g leaf length of longest path that starts in subtree and ends at u Size of each subtree • g u length of the diameter of the subtree rooted at u • f u • Idea: keep two numbers per node Diameter of the tree Height of each subtree Problem 2 – Solution (Part 1) • f ( leaf ) = 1, f ( node ) = 1 + ∑ f ( child ) • f ( leaf ) = 1, f ( node ) = 1 + max f ( child )

  14. Size of each subtree Height of each subtree Diameter of the tree • Idea: keep two numbers per node 6 Problem 2 – Solution (Part 1) • f ( leaf ) = 1, f ( node ) = 1 + ∑ f ( child ) • f ( leaf ) = 1, f ( node ) = 1 + max f ( child ) • f ( u ) = length of the diameter of the subtree rooted at u • g ( u ) = length of longest path that starts in subtree and ends at u • f ( leaf ) = g ( leaf ) = 0 • f ( node ) = max { f ( child ) , 2 + two largest values of g ( child ) } Time complexity: O ( N )

  15. Average Median, k -th largest value • A v sorted list of values in v ’s subtree • Answer for v ’s subtree the k th element in the list • To compute A v recursively, we need to merge all the lists of v ’s children • How do we merge? • What data structure do we use? 7 Problem 2 – Solution (Part 2) • f ( node ) = val ( node ) + ∑ f ( child ) • Avg ( node ) = f ( node ) / size ( node ) Time complexity: O ( N )

  16. Average Median, k -th largest value • How do we merge? • What data structure do we use? 7 Problem 2 – Solution (Part 2) • f ( node ) = val ( node ) + ∑ f ( child ) • Avg ( node ) = f ( node ) / size ( node ) Time complexity: O ( N ) • A ( v ) = sorted list of values in v ’s subtree • Answer for v ’s subtree = the k th element in the list • To compute A ( v ) recursively, we need to merge all the lists of v ’s children

  17. 2 N in total 8 • Each time an element moves, it gets into a set twice as big O N • N times Each element is moved at most O • Time complexity Naive approach: smaller set to the larger set • While there is more than 1 set: pick any arbitrary pair, add all elements from the • Let’s store each set using... a set! Merging sets with BSTs • The problem is that we keep moving items we’ve already merged Merging sets • Fill up an array, then call sort() • Time complexity: up to O ( N log N ) per sort, so possibly O ( N 2 log N ) in a deep tree

  18. 2 N in total 8 • Each time an element moves, it gets into a set twice as big O N • N times Each element is moved at most O • Time complexity Naive approach: smaller set to the larger set • While there is more than 1 set: pick any arbitrary pair, add all elements from the • Let’s store each set using... a set! Merging sets with BSTs • The problem is that we keep moving items we’ve already merged Merging sets • Fill up an array, then call sort() • Time complexity: up to O ( N log N ) per sort, so possibly O ( N 2 log N ) in a deep tree

  19. Naive approach: • The problem is that we keep moving items we’ve already merged Merging sets with BSTs • Let’s store each set using... a set! • While there is more than 1 set: pick any arbitrary pair, add all elements from the smaller set to the larger set Time complexity • Each time an element moves, it gets into a set twice as big 8 Merging sets • Fill up an array, then call sort() • Time complexity: up to O ( N log N ) per sort, so possibly O ( N 2 log N ) in a deep tree • ⇒ Each element is moved at most O (log N ) times • ⇒ O ( N log 2 N ) in total

  20. How do we use this to compute our k th largest value statistic? • Start with a set at every leaf, storing the value at that leaf. • For the vertex v , merge all of v ’s children and add the value at v . 9 Problem 2 – Solution (Part 3) • We can query for the k th largest element in O (log n ) time to get the answer.

  21. You are the head of the a large organization of 10000 members. You are planning to hold a retreat involving some of the members. The organization can be represented (in modern terminology) as a tree of managers. You want to select a subset of people that are connected in this tree. • How many ways can you form the group if you are part of it? • What if you are not part of the group? 10 Problem 3 – Adding states • What if you want to limit the group size to K ? Assume K ≤ 100.

  22. 11 (exactly one way). Problem 3 – Solution (Part 1) Let f ( u ) = number of connected subgraphs of subtree rooted at u that contain u • For each child v of u , we either choose to include it ( f ( v ) ways), or we exclude it • f ( u ) = ∏ child v ( 1 + f ( v )) f ( root ) gives the number of ways to select a group that contains you! Time complexity: O ( N )

Recommend


More recommend