Divide and Conquer Algorithms, and Backtracking

Divide and Conquer Algorithms, and Backtracking

Divide and Conquer Algorithms, and Backtracking CSE 101: Design and Analysis of Algorithms Lecture 16 CSE 101: Design and analysis of algorithms • Divide and conquer algorithms – Reading: Chapter 2 • Homework 6 is due today, 11:59 PM • Quiz 3 is Nov 27, in class – Greedy algorithms and divide and conquer algorithms – Study guide released tomorrow • Homework 6 will be assigned Nov 29 – No homework this week CSE 101, Fall 2018 2 Divide and conquer example GREATEST OVERLAP CSE 101, Fall 2018 3 Greatest overlap • Given a list of intervals [a_1,b_1],…,[a_n,b_n] write pseudocode for a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals • An interval [a,b] is a set of integers starting at a and ending at b. For example: [16,23] = {16,17,18,19,20,21,22,23} • An overlap between two intervals [a,b] and [c,d] is their intersection • Given two intervals [a,b] and [c,d], how would you compute the length of their overlap? Based on slides courtesy of Miles Jones CSE 101, Fall 2018 4 Greatest overlap • Given two intervals [a,b] and [c,d], how would you compute the length of their overlap? procedure overlap([a,b],[c,d]) [assume that a ≤ c] if b < c: [a b] return 0 [c d] else: if b ≤ d: [a b] return b – c + 1 [c d] if b > d: [a b] return d – c + 1 [c d] CSE 101, Fall 2018 5 Greatest overlap • Given a list of intervals [a_1,b_1],…,[a_n,b_n] write pseudocode for a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals • Example: What is the greatest overlap of the intervals [45,57],[17,50],[10,29],[12,22],[23,51],[31,32],[10,15],[23,35] 0 5 10 15 20 25 30 35 40 45 50 55 60 CSE 101, Fall 2018 6 Greatest overlap • Given a list of intervals [a_1,b_1],…,[a_n,b_n] write pseudocode for a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals • Simple solution: compute all overlap pairs and find maximum olap:=0 for i from 1 to n-1 for j from i+1 to n if overlap([a_i,b_i],[a_j,b_j])>olap then olap:=overlap([a_i,b_i],[a_j,b_j]) 푂(푛2) Can we do better? return olap CSE 101, Fall 2018 7 Greatest overlap • Given a list of intervals [a_1,b_1],…,[a_n,b_n] write pseudocode for a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals – Compose your base case – Break the problem into smaller pieces – Recursively call the algorithm on the smaller pieces – Combine the results CSE 101, Fall 2018 8 Greatest overlap • Compose your base case – What happens if there is only one interval? • If n=1, then return 0 CSE 101, Fall 2018 9 Greatest overlap • Break the problem into smaller pieces – Would knowing the result on smaller problems help with knowing the solution on the original problem? – In this stage, let’s keep the combine part in mind – How would you break the problem into smaller pieces? 0 5 10 15 20 25 30 35 40 45 50 55 60 CSE 101, Fall 2018 10 Greatest overlap • Break the problem into smaller pieces – Would it be helpful to break the problem into two depending on the starting value? 0 5 10 15 20 25 30 35 40 45 50 55 60 CSE 101, Fall 2018 11 Greatest overlap • Break the problem into smaller pieces – Sort the list and break it into lists each of size n/2 [10,15],[10,29],[12,22],[17,50],[23,51],[27,35],[31,32],[45,57] 0 5 10 15 20 25 30 35 40 45 50 55 60 CSE 101, Fall 2018 12 Greatest overlap • Break the problem into smaller pieces – Let’s assume we can get a divide and conquer algorithm to work. Then what information would it give us to recursively call each subproblem? • overlapDC([10,15],[10,29],[12,22],[17,50])=13 • overlapDC([23,51],[27,35],[31,32],[45,57])=9 35-27+1=9 29-17+1=13 0 5 10 15 20 25 30 35 40 45 50 55 60 CSE 101, Fall 2018 13 Greatest overlap • Break the problem into smaller pieces • overlapDC([10,15],[10,29],[12,22],[17,50])=13 • overlapDC([23,51],[27,35],[31,32],[45,57])=9 – The greatest overlap overall may be contained entirely in one sublist or it may be an overlap of one interval from either side 35-27+1=9 29-17+1=13 0 5 10 15 20 25 30 35 40 45 50 55 60 CSE 101, Fall 2018 14 Greatest overlap • Combine the results – So far, we have split up the set of intervals and recursively called the algorithm on both sides. The runtime of this algorithm satisfies a recurrence that looks something like 푛 푇 푛 = 2푇 + 푂(? ) 2 – What goes into the 푂(? )? – How long does it take to “combine”. In other words, how long does it take to check if there is not a bigger overlap between sublists? CSE 101, Fall 2018 15 Greatest overlap • Combine the results – What is an efficient way to determine the greatest overlap of intervals where one is red and the other is blue? 0 5 10 15 20 25 30 35 40 45 50 55 60 CSE 101, Fall 2018 16 Greatest overlap • Combine the results – What is an efficient way to determine the greatest overlap of intervals where one is red and the Compare other is blue? green interval with all blue intervals 0 5 10 15 20 25 30 35 40 45 50 55 60 CSE 101, Fall 2018 17 Greatest overlap between sets • Let’s formalize our algorithm that finds the greatest overlap of two intervals such that they come from different sets sorted by starting point procedure overlapbetween ([[푎1, 푏1], … [푎ℓ, 푏ℓ]], [[푐1, 푑1], … [푐푘, 푑푘]]) (푎1 ≤ 푎2 ≤ ⋯ ≤ 푎ℓ ≤ 푐1 ≤ 푐2 ≤ ⋯ ≤ 푐푘) if k==0 or ℓ == 0 then return 0 minc = 푐1 maxb = 0 olap = 0 for 푖 from 1 to ℓ: if maxb < 푏푖: Find the latest ending red. O(n/2) maxb = 푏푖 for 푗 from 1 to k: if olap < overlap([minc,maxb],[푐푘, 푑푘]): Compare it with blues. O(n/2) olap = overlap([minc,maxb],[푐푘, 푑푘]) return olap Total runtime O(n) CSE 101, Fall 2018 18 Greatest overlap • Given a list of intervals [a_1,b_1],…,[a_n,b_n] write pseudocode for a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals procedure overlapdc ([푎1, 푏1], … [푎푛, 푏푛]) if n==1 then return 0. sort([푎1, 푏1], … [푎푛, 푏푛]) by a values. mid = 푛/2 LS = [[푎1, 푏1], … [푎푚푖푑, 푏푚푖푑]] RS = [[푎푚푖푑+1, 푏푚푖푑+1], … [푎푛, 푏푛]] olap1 = overlapdc(LS) olap2 = overlapdc(RS) olap3 = overlapbetween(LS,RS) return max(olap1,olap2,olap3) CSE 101, Fall 2018 19 Greatest overlap, runtime • Given a list of intervals [a_1,b_1],…,[a_n,b_n] write pseudocode for a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals procedure overlapdc ([푎1, 푏1], … [푎푛, 푏푛]) 푇(푛) if n==1 then return 0. sort([푎1, 푏1], … [푎푛, 푏푛]) by a values. 푂(푛 log 푛) mid = 푛/2 LS = [[푎1, 푏1], … [푎푚푖푑, 푏푚푖푑]] RS = [[푎푚푖푑+1, 푏푚푖푑+1], … [푎푛, 푏푛]] olap1 = overlapdc(LS) 푇(푛/2) olap2 = overlapdc(RS) 푇(푛/2) olap3 = overlapbetween(LS,RS) 푂(푛) 푛 return max(olap1,olap2,olap3) 푇 푛 = 2푇 + 푂 푛 log 푛 2 But each recursion sorts CSE 101, Fall 2018 intervals that are already sorted 20 Greatest overlap • Given a list of intervals [a_1,b_1],…,[a_n,b_n] write pseudocode for a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals Sort first procedure overlapdc (sort([푎1, 푏1], … [푎푛, 푏푛])) if n==1 then return 0. mid = 푛/2 Do not sort here LS = [[푎1, 푏1], … [푎푚푖푑, 푏푚푖푑]] RS = [[푎푚푖푑+1, 푏푚푖푑+1], … [푎푛, 푏푛]] olap1 = overlapdc(LS) olap2 = overlapdc(RS) olap3 = overlapbetween(LS,RS) return max(olap1,olap2,olap3) a CSE 101, Fall 2018 21 Greatest overlap, runtime • Given a list of intervals [a_1,b_1],…,[a_n,b_n] write pseudocode for a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals 푂(푛 log 푛) Sort first procedure overlapdc (sort([푎1, 푏1], … [푎푛, 푏푛])) 푇(푛) if n==1 then return 0. mid = 푛/2 LS = [[푎1, 푏1], … [푎푚푖푑, 푏푚푖푑]] RS = [[푎푚푖푑+1, 푏푚푖푑+1], … [푎푛, 푏푛]] olap1 = overlapdc(LS) 푇(푛/2) 푛 olap2 = overlapdc(RS) 푇(푛/2) 2푇 + 푂 푛 = 푂 푛 log 푛 by master theorem 2 olap3 = overlapbetween(LS,RS) 푂(푛) return max(olap1,olap2,olap3) a 푇 푛 = 푂 푛 log 푛 + 푂 푛 log 푛 = 푂 푛 log 푛 CSE 101, Fall 2018 22 Greatest overlap • Given a list of intervals [a_1,b_1],…,[a_n,b_n] write pseudocode for a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals – It is clear that the greatest overlap will be from two intervals in the left half, two from the right half, or one from the left and one from the right – Our algorithm finds all three of these values and outputs the max CSE 101, Fall 2018 23 Divide and conquer example MINIMUM DISTANCE CSE 101, Fall 2018 24 Minimum distance • Given a list of coordinates [ 푥1, 푦1 , … , 푥푛, 푦푛 ], find the distance between the closest pair 2 2 • distance( 푥푖, 푦푖 , (푥푗, 푦푗)) = 푥푖 − 푥푗 + 푦푖 − 푦푗 CSE 101, Fall 2018 25 Minimum distance • Given a list of coordinates [ 푥1, 푦1 , … , 푥푛, 푦푛 ], find the distance between the closest pair • Brute force solution? min = ∞ for i from 1 to n-1: for j from i+1 to n: d = distance( 푥푖, 푦푖 , (푥푗, 푦푗)) if min > d, min = d return min CSE 101, Fall 2018 26 Minimum distance • Given a list of coordinates [ 푥1, 푦1 , … , 푥푛, 푦푛 ], find the distance between the closest pair • Brute force solution min = ∞ for i from 1 to n-1: for j from i+1 to n: d = distance( 푥푖, 푦푖 , (푥푗, 푦푗)) if min > d, min = d return min CSE 101, Fall 2018 27 Minimum distance • Given a list of coordinates [ 푥1, 푦1 , … , 푥푛, 푦푛 ], find the distance between the closest pair – Base case – Break the problem up – Recursively solve each problem • Assume the algorithm works for the subproblems – Combine the results CSE 101, Fall 2018 28 Minimum distance • Base case – What happens if there is only two points? CSE 101, Fall 2018 29 Minimum distance • Base case – What happens if there is only two points? • If n=2, then return distance( 푥1, 푦1 , (푥2, 푦2)) CSE 101, Fall 2018 30 Minimum

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    141 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us