cse 101: design and analysis of algorithms lecture 16 · cse 101: design and analysis of algorithms...
TRANSCRIPT
![Page 1: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/1.jpg)
Divide and Conquer Algorithms, and Backtracking
CSE 101: Design and Analysis of Algorithms
Lecture 16
![Page 2: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/2.jpg)
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
![Page 3: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/3.jpg)
GREATEST OVERLAP Divide and conquer example
CSE 101, Fall 2018 3
![Page 4: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/4.jpg)
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?
CSE 101, Fall 2018 4 Based on slides courtesy of Miles Jones
![Page 5: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/5.jpg)
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: return 0
else: if b ≤ d:
return b – c + 1
if b > d: return d – c + 1
CSE 101, Fall 2018 5
[a b] [c d]
[a b] [c d]
[a b] [c d]
![Page 6: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/6.jpg)
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
![Page 7: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/7.jpg)
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])
return olap
CSE 101, Fall 2018 7
𝑂(𝑛2) Can we do better?
![Page 8: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/8.jpg)
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
![Page 9: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/9.jpg)
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
![Page 10: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/10.jpg)
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?
CSE 101, Fall 2018 10
0 5 10 15 20 25 30 35 40 45 50 55 60
![Page 11: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/11.jpg)
Greatest overlap
• Break the problem into smaller pieces
– Would it be helpful to break the problem into two depending on the starting value?
CSE 101, Fall 2018 11
0 5 10 15 20 25 30 35 40 45 50 55 60
![Page 12: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/12.jpg)
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]
CSE 101, Fall 2018 12
0 5 10 15 20 25 30 35 40 45 50 55 60
![Page 13: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/13.jpg)
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
CSE 101, Fall 2018 13 0 5 10 15 20 25 30 35 40 45 50 55 60
35-27+1=9 29-17+1=13
![Page 14: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/14.jpg)
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
CSE 101, Fall 2018 14 0 5 10 15 20 25 30 35 40 45 50 55 60
35-27+1=9 29-17+1=13
![Page 15: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/15.jpg)
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
![Page 16: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/16.jpg)
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?
CSE 101, Fall 2018 16 0 5 10 15 20 25 30 35 40 45 50 55 60
![Page 17: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/17.jpg)
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?
CSE 101, Fall 2018 17 0 5 10 15 20 25 30 35 40 45 50 55 60
Compare green interval with all blue intervals
![Page 18: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/18.jpg)
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 < 𝑏𝑖: maxb = 𝑏𝑖
for 𝑗 from 1 to k: if olap < overlap([minc,maxb],[𝑐𝑘, 𝑑𝑘]):
olap = overlap([minc,maxb],[𝑐𝑘, 𝑑𝑘])
return olap
CSE 101, Fall 2018 18
Find the latest ending red. O(n/2)
Compare it with blues. O(n/2)
Total runtime O(n)
![Page 19: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/19.jpg)
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
![Page 20: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/20.jpg)
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. 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 20
𝑂(𝑛 log 𝑛)
𝑂(𝑛)
𝑇(𝑛/2) 𝑇(𝑛/2)
𝑇 𝑛 = 2𝑇𝑛
2+ 𝑂 𝑛 log 𝑛
𝑇(𝑛)
But each recursion sorts intervals that are already sorted
![Page 21: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/21.jpg)
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 (sort([𝑎1, 𝑏1], … [𝑎𝑛, 𝑏𝑛])) if n==1 then return 0. mid = 𝑛/2 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
Sort first
Do not sort here
![Page 22: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/22.jpg)
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 (sort([𝑎1, 𝑏1], … [𝑎𝑛, 𝑏𝑛])) if n==1 then return 0. mid = 𝑛/2 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 22
𝑇(𝑛)
𝑂(𝑛)
𝑇(𝑛/2) 𝑇(𝑛/2)
𝑂(𝑛 log 𝑛)
2𝑇𝑛
2+ 𝑂 𝑛 = 𝑂 𝑛 log 𝑛 by master theorem
𝑇 𝑛 = 𝑂 𝑛 log 𝑛 + 𝑂 𝑛 log 𝑛 = 𝑂 𝑛 log 𝑛
Sort first
![Page 23: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/23.jpg)
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
![Page 24: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/24.jpg)
MINIMUM DISTANCE Divide and conquer example
CSE 101, Fall 2018 24
![Page 25: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/25.jpg)
Minimum distance
• Given a list of coordinates [ 𝑥1, 𝑦1 , … , 𝑥𝑛, 𝑦𝑛 ], find the distance between the closest pair
• distance( 𝑥𝑖 , 𝑦𝑖 , (𝑥𝑗 , 𝑦𝑗)) = 𝑥𝑖 − 𝑥𝑗2
+ 𝑦𝑖 − 𝑦𝑗2
CSE 101, Fall 2018 25
![Page 26: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/26.jpg)
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
![Page 27: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/27.jpg)
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
![Page 28: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/28.jpg)
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
![Page 29: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/29.jpg)
Minimum distance
• Base case
– What happens if there is only two points?
CSE 101, Fall 2018 29
![Page 30: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/30.jpg)
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
![Page 31: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/31.jpg)
Minimum distance
• Break the problem up
– How would you break the problem into smaller pieces?
CSE 101, Fall 2018 31
![Page 32: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/32.jpg)
Example
𝑦
𝑥 CSE 101, Fall 2018 32
How would you break the problem into smaller pieces?
![Page 33: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/33.jpg)
Minimum distance • Break the problem up
– Usually the smaller pieces are each of size n/2 – We will break the problem in half, sort the points by their 𝑥
values, then find a value 𝑥𝑚 such that half of the 𝑥 values are on the left and half are on the right
– Perform the algorithm on each side • Assume the algorithm works for the subproblems (have faith)
– What does that give us? – It gives us the distance of the closest pair on the left and on the
right; let’s call them 𝑑𝐿 and 𝑑𝑅
CSE 101, Fall 2018 33
![Page 34: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/34.jpg)
CSE 101, Fall 2018
Example
𝑦
𝑥 34 𝑥𝑚
![Page 35: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/35.jpg)
Minimum distance • Break the problem up
– Usually the smaller pieces are each of size n/2 – We will break the problem in half, sort the points by their 𝑥
values, then find a value 𝑥𝑚 such that half of the 𝑥 values are on the left and half are on the right
– Perform the algorithm on each side • Assume the algorithm works for the subproblems (have faith)
– What does that give us? – It gives us the distance of the closest pair on the left and on the
right; let’s call them 𝑑𝐿 and 𝑑𝑅
CSE 101, Fall 2018 35
![Page 36: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/36.jpg)
Minimum distance • Break the problem up
– Usually the smaller pieces are each of size n/2 – We will break the problem in half, sort the points by their 𝑥
values, then find a value 𝑥𝑚 such that half of the 𝑥 values are on the left and half are on the right
– Perform the algorithm on each side • Assume the algorithm works for the subproblems (have faith)
– What does that give us? – It gives us the distance of the closest pair on the left and on the
right; let’s call them 𝑑𝐿 and 𝑑𝑅
CSE 101, Fall 2018 36
![Page 37: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/37.jpg)
CSE 101, Fall 2018
Example
𝑦
𝑥
𝑑𝐿
𝑑𝑅
37 𝑥𝑚
![Page 38: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/38.jpg)
Minimum distance • Combine the results
– How will we use this information to find the distance of the closest pair in the whole set?
– We must consider if there is a closest pair where one point is in the left half and one is in the right half
– How do we do this? – Let 𝑑 = min(𝑑𝐿, 𝑑𝑅) and compare only the points (𝑥𝑖 , 𝑦𝑖)
such that 𝑥𝑚 − 𝑑 ≤ 𝑥𝑖 and 𝑥𝑖 ≤ 𝑥𝑚 + 𝑑. – Worst case, how many points could this be?
CSE 101, Fall 2018 38
![Page 39: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/39.jpg)
Minimum distance • Combine the results
– How will we use this information to find the distance of the closest pair in the whole set?
– We must consider if there is a closest pair where one point is in the left half and one is in the right half
– How do we do this? – Let 𝑑 = min(𝑑𝐿, 𝑑𝑅) and compare only the points (𝑥𝑖 , 𝑦𝑖)
such that 𝑥𝑚 − 𝑑 ≤ 𝑥𝑖 and 𝑥𝑖 ≤ 𝑥𝑚 + 𝑑. – Worst case, how many points could this be?
CSE 101, Fall 2018 39
![Page 40: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/40.jpg)
Minimum distance • Combine the results
– How will we use this information to find the distance of the closest pair in the whole set?
– We must consider if there is a closest pair where one point is in the left half and one is in the right half
– How do we do this? – Let 𝑑 = min(𝑑𝐿, 𝑑𝑅) and compare only the points (𝑥𝑖 , 𝑦𝑖)
such that 𝑥𝑚 − 𝑑 ≤ 𝑥𝑖 and 𝑥𝑖 ≤ 𝑥𝑚 + 𝑑 – Worst case, how many points could this be?
CSE 101, Fall 2018 40
![Page 41: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/41.jpg)
CSE 101, Fall 2018
Example
𝑦
𝑥 𝑥𝑚
𝑑𝐿
𝑑𝑅
𝑑
41
Worst case, how many points could this be?
![Page 42: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/42.jpg)
Minimum distance • Combine the results
– Let 𝑃𝑚 be the set of points within 𝑑 of 𝑥𝑚 – Then, 𝑃𝑚 may contain as many as 𝑛 different points
– So, to compare all the points in 𝑃𝑚 with each other would take 𝑛2
many comparisons
• So the runtime recursion is
𝑇 𝑛 = 2𝑇𝑛
2+ 𝑂 𝑛2
𝑇 𝑛 = 𝑂 𝑛2
• Can we improve the combine term?
CSE 101, Fall 2018 42
by master theorem
![Page 43: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/43.jpg)
Minimum distance
• Combine the results
– Let 𝑃𝑚 be the set of points within 𝑑 of 𝑥𝑚
– Then, 𝑃𝑚 may contain as many as 𝑛 different points
– Lets only compare the points i, j in 𝑃𝑚 with each
other if 𝑦𝑖 − 𝑦𝑗 ≤ 𝑑
– How many such comparisons are there?
– How can we find all such pairs?
CSE 101, Fall 2018 43
![Page 44: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/44.jpg)
CSE 101, Fall 2018
Example
𝑦
𝑥 𝑥𝑚
𝑑𝐿
𝑑𝑅
𝑑
44
![Page 45: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/45.jpg)
CSE 101, Fall 2018
Example
𝑦
𝑥 𝑥𝑚
𝑑𝐿
𝑑𝑅
𝑑
45
![Page 46: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/46.jpg)
Minimum distance • Combine the results
– Given a point 𝑥, 𝑦 ∈ 𝑃𝑚, all the points we would need to compare it to are in a 2𝑑 × 𝑑 rectangle with that point at its upper boundary
– How many points could possibly be in this rectangle?
– There could not be more than 8 points total because if we divide the rectangle into 8 𝑑
2×
𝑑
2 squares then
there can never be more than one point per square
CSE 101, Fall 2018 46
𝑥, 𝑦
![Page 47: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/47.jpg)
Minimum distance • Combine the results
– Given a point 𝑥, 𝑦 ∈ 𝑃𝑚, all the points we would need to compare it to are in a 2𝑑 × 𝑑 rectangle with that point at its upper boundary
– How many points could possibly be in this rectangle?
– There could not be more than 8 points total because if we divide the rectangle into eight 𝑑
2×
𝑑
2 squares then
there can never be more than one point per square
CSE 101, Fall 2018 47
𝑥, 𝑦
To learn more about this, investigate geometric point packing bound
![Page 48: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/48.jpg)
Minimum distance • Combine the results
– So instead of comparing (𝑥, 𝑦) with every other point in 𝑃𝑚 we only have to compare it with at most 7 points lower than it (smaller y)
– To gain quick access to these points, let’s sort the points in 𝑃𝑚 by 𝑦 values – The points above must be in the 7 points before our current point in this sorted list – Now, if there are 𝑘 vertices in 𝑃𝑚 we have to sort the vertices in 𝑂(𝑘log 𝑘) time and make at
most 7𝑘 comparisons in 𝑂(𝑘) time for a total combine step of 𝑂 𝑘 log 𝑘 – But, we said in the worst case, there are 𝑛 vertices in 𝑃𝑚 and so worst case, the combine step
takes 𝑂(𝑛 log 𝑛) time
• So the runtime recursion is
𝑇 𝑛 = 2𝑇𝑛
2+ 𝑂 𝑛 log 𝑛
CSE 101, Fall 2018 48
![Page 49: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/49.jpg)
BACKTRACKING
CSE 101, Fall 2018 49
![Page 50: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/50.jpg)
Backtracking • Backtracking is a generic method that can be applied to many
problems that have a solution set that is exponentially large, e.g., search and optimization problems
• Backtracking can often be a first step towards finding a greedy or dynamic programming algorithm
• Backtracking often gives a more efficient runtime over exhaustive search or brute force but may not result in a polynomial time algorithm, and is usually an improved exponential time
• On the other hand, it applies even to NP-complete problems, where we do not expect to find less than exponential time algorithms
• Often, they are better on typical inputs than their worst-case
CSE 101, Fall 2018 50
![Page 51: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/51.jpg)
Search and optimization problems • Many problems involve finding the best solution
from among a large space of possibilities • Instance: what does the input look like? • Solution format: what does an output look like? • Constraints: what properties must a solution
have? • Objective function: what makes a solution better
or worse?
CSE 101, Fall 2018 51
![Page 52: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/52.jpg)
Global search vs local searchs
• Like greedy algorithms, backtracking algorithms break the massive global search for a solution into a series of simpler local searches for part of the solution (Which edge do we take first? Then second? …)
• While greedy algorithms guess the “best” local choice and only consider this possibility, in backtracking we use exhaustive search to try out all combinations of local decisions
CSE 101, Fall 2018 52
![Page 53: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/53.jpg)
Global search vs local searchs
• However, we can often use the constraints of the problem to prune cases that are dead ends. Applying this recursively, we get a substantial savings over exhaustive search.
• This might take a long time to do. What are some other ideas in general?
CSE 101, Fall 2018 53
![Page 54: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/54.jpg)
Search and optimization problems • Many problems involve finding the best solution
from among a large space of possibilities • Instance: what does the input look like? • Solution format: what does an output look like? • Constraints: what properties must a solution
have? • Objective function: what makes a solution better
or worse?
CSE 101, Fall 2018 54
![Page 55: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/55.jpg)
Search and optimization problems • Many problems involve finding the best solution from
among a large space of possibilities • Instance: what does the input look like? • Solution format: exhaustive search generally loops through
all possibilities that satisfy the solution format • Constraints: backtracking uses the constraint to eliminate
impossible solutions (usually early on in their development) • Objective function: what makes a solution better or worse?
CSE 101, Fall 2018 55
![Page 56: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/56.jpg)
8 QUEENS PUZZLE Backtracking example
CSE 101, Fall 2018 56
![Page 57: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/57.jpg)
8 queens puzzle
• Put 8 queens on a chessboard such that no two are attacking
• Solution format: what does an output look like?
• Constraints: what properties must a solution have?
CSE 101, Fall 2018 57
![Page 58: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/58.jpg)
8 queens puzzle
• Put 8 queens on a chessboard such that no two are attacking
• Solution format: Arrangement of 8 queens on chessboard
• Constraints: No two queens are attacking
CSE 101, Fall 2018 58
![Page 59: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/59.jpg)
8 queens puzzle
• Put 8 queens on a chessboard such that no two are attacking
• Brute force
– Put all possible arrangements of queens on the chessboard
– 864 = 281474976710656
CSE 101, Fall 2018 59
![Page 60: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/60.jpg)
8 queens puzzle • Put 8 queens on a chessboard
such that no two are attacking • Brute force (better)
– Put all possible arrangements of queens on the chessboard such that no two queens occupy the same square
– 648
= 4426165368
CSE 101, Fall 2018 60
![Page 61: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/61.jpg)
8 queens puzzle • Put 8 queens on a chessboard
such that no two are attacking • Brute force (better++)
– Put all possible arrangements of queens on the chessboard such that no two queens occupy the same row
– 88 = 16777216
CSE 101, Fall 2018 61
![Page 62: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/62.jpg)
8 queens puzzle • Put 8 queens on a chessboard
such that no two are attacking • Brute force (better+++)
– Put all possible arrangements of queens on the chessboard such that no two queens occupy the same row or column
– 8! = 40320
CSE 101, Fall 2018 62
![Page 63: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/63.jpg)
8 queens puzzle • Put 8 queens on a chessboard such
that no two are attacking • The backtracking method attacks the
problem by considering one row at a time, eliminating or “pruning” possible non-solution (dead-end) chessboard positions early in their construction – Using this method, there are only 15720
possible queen placements
CSE 101, Fall 2018 63
![Page 64: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/64.jpg)
8 queens puzzle
CSE 101, Fall 2018 64
![Page 65: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/65.jpg)
8 queens puzzle
CSE 101, Fall 2018 65
![Page 66: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/66.jpg)
8 queens puzzle
CSE 101, Fall 2018 66
Q
![Page 67: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/67.jpg)
8 queens puzzle
CSE 101, Fall 2018 67
Q
![Page 68: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/68.jpg)
8 queens puzzle
CSE 101, Fall 2018 68
Q X
X
![Page 69: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/69.jpg)
8 queens puzzle
CSE 101, Fall 2018 69
Q X
X
Q
![Page 70: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/70.jpg)
8 queens puzzle
CSE 101, Fall 2018 70
Q X
X
Q
![Page 71: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/71.jpg)
8 queens puzzle
CSE 101, Fall 2018 71
Q X X
X X
Q X
X
![Page 72: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/72.jpg)
8 queens puzzle
CSE 101, Fall 2018 72
Q X X
X X
Q X
X
Q
![Page 73: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/73.jpg)
8 queens puzzle
CSE 101, Fall 2018 73
Q X X
X X
Q X
X
Q
![Page 74: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/74.jpg)
8 queens puzzle
CSE 101, Fall 2018 74
Q X X X
X X
Q X X
X X
Q X
X
![Page 75: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/75.jpg)
8 queens puzzle
CSE 101, Fall 2018 75
Q X X X
X X Q
Q X X
X X
Q X
X
![Page 76: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/76.jpg)
8 queens puzzle
CSE 101, Fall 2018 76
Q X X X
X X Q
Q X X
X X
Q X
X
![Page 77: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/77.jpg)
8 queens puzzle
CSE 101, Fall 2018 77
Q X X X X
X X Q X
Q X X X
X X
Q X X
X X
X
![Page 78: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/78.jpg)
8 queens puzzle
CSE 101, Fall 2018 78
Q X X X X
X X Q X
Q X X X
X X Q
Q X X
X X
X
![Page 79: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/79.jpg)
8 queens puzzle
CSE 101, Fall 2018 79
Q X X X X
X X Q X
Q X X X
X X Q
Q X X
X X
X
![Page 80: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/80.jpg)
8 queens puzzle
CSE 101, Fall 2018 80
Q X X X X X
X X Q X X
Q X X X X
X X Q X
Q X X X
X X X
X X
X
Cannot possibly be completed to a valid solution
![Page 81: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/81.jpg)
8 queens puzzle
CSE 101, Fall 2018 81
Q X X X X
X X Q X
Q X X X
X X X
Q X X
X X
X
Backtrack
![Page 82: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/82.jpg)
8 queens puzzle
CSE 101, Fall 2018 82
Q X X X X
X X Q X
Q X X X
X X X
Q X X
X X
X
Q
![Page 83: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/83.jpg)
8 queens puzzle
CSE 101, Fall 2018 83
Q X X X X
X X Q X
Q X X X
X X X
Q X X
X X
X
Q
![Page 84: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/84.jpg)
8 queens puzzle
CSE 101, Fall 2018 84
Q X X X X X
X X Q X X
Q X X X X
X X X X
Q X X X
X X X
X X
Q X
Cannot possibly be completed to a valid solution
![Page 85: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/85.jpg)
8 queens puzzle
CSE 101, Fall 2018 85
Q X X X X
X X Q X
Q X X X
X X X
Q X X
X X
X
X
Backtrack
![Page 86: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/86.jpg)
8 queens puzzle
CSE 101, Fall 2018 86
Q X X X X
X X Q X
Q X X X
X X X
Q X X
X X
X
X
Cannot possibly be completed to a valid solution
![Page 87: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/87.jpg)
8 queens puzzle
CSE 101, Fall 2018 87
Q X X X
X X X
Q X X
X X
Q X
X
Backtrack
![Page 88: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/88.jpg)
8 queens puzzle
CSE 101, Fall 2018 88
Q X X X
X X X
Q X X
X X
Q X
X
Q
![Page 89: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/89.jpg)
8 queens puzzle
CSE 101, Fall 2018 89
Q X X X
X X X
Q X X
X X
Q X
X
Q
And so on
https://youtu.be/V4qSux-M8N4
![Page 90: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/90.jpg)
SUDOKU Backtracking example
CSE 101, Fall 2018 90
![Page 91: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/91.jpg)
Search and optimization problems • Many problems involve finding the best solution
from among a large space of possibilities • Instance: what does the input look like? • Solution format: what does an output look like? • Constraints: what properties must a solution
have? • Objective function: what makes a solution better
or worse?
CSE 101, Fall 2018 91
![Page 92: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/92.jpg)
Sudoku
• Instance: a partially filled in puzzle
• Solution format: a grid with all squares filled with the numbers 1 through 9
• Constraint: there can be no repeats of numbers in each sub-square, row or column
• Objective: find a solution with the constraint
CSE 101, Fall 2018 92
![Page 93: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/93.jpg)
Sudoku • Backtracking strategy: fill in the
first available cell with the least possible number and recurse until there is a cell that cannot be filled in
• Go back to the last decision point and try the next biggest possible number if available
CSE 101, Fall 2018 93
![Page 94: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/94.jpg)
MAXIMAL INDEPENDENT SET
CSE 101, Fall 2018 94
![Page 95: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/95.jpg)
Maximal independent set
• Given a graph with nodes representing people, and there is an edge between A in B if A and B are enemies, find the largest set of people such that no two are enemies. In other words, given an undirected graph, find the largest set of vertices such that no two are connected with an edge.
CSE 101, Fall 2018 95
![Page 96: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/96.jpg)
• Given a graph with nodes representing people, and there is an edge between A in B if A and B are enemies, find the largest set of people such that no two are enemies. In other words, given an undirected graph, find the largest set of vertices such that no two are connected with an edge.
• Instance: what does the input look like? • Solution format: what does an output look like? • Constraints: what properties must a solution have? • Objective function: what makes a solution better or worse?
Maximal independent set
CSE 101, Fall 2018 96
![Page 97: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/97.jpg)
• Given a graph with nodes representing people, and there is an edge between A in B if A and B are enemies, find the largest set of people such that no two are enemies. In other words, given an undirected graph, find the largest set of vertices such that no two are connected with an edge.
• Instance: an undirected graph • Solution format: set of vertices S • Constraints: 𝑢, 𝑣 ∈ 𝑆 ⇒ (𝑢, 𝑣) ∉ 𝐸 • Objective function: maximize |S|
Maximal independent set
CSE 101, Fall 2018 97
![Page 98: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/98.jpg)
Maximal independent set
• Greedy approaches?
• One may be tempted to choose the person with the fewest enemies, remove all of his enemies and recurse on the remaining graph
• This is fast, however it does not always find the best solution
CSE 101, Fall 2018 98
![Page 99: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/99.jpg)
Example
A
B C D
E F
G H I
J
K L
CSE 101, Fall 2018 99
Greedy: all degree 3, pick any, say E
![Page 100: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/100.jpg)
Example
Greedy: all degree 3, pick any, say E
Neighbors (enemies) of E forced out of set
CSE 101, Fall 2018 100
A
B C D
E F
G H I
J
K L
![Page 101: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/101.jpg)
Example
Greedy: all degree 3, pick any, say E
Neighbors (enemies) of E forced out of set
CSE 101, Fall 2018 101
A
B C D
E F
G H I
J
K L
A now is lowest degree
![Page 102: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/102.jpg)
Example
Greedy: all degree 3, pick any, say E
Neighbors (enemies) of E forced out of set
CSE 101, Fall 2018 102
B C D
E F
G H I
J
K L
A now is lowest degree
Neighbors (enemies) of A forced out of set
A
![Page 103: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/103.jpg)
Example
Greedy: all degree 3, pick any, say E
Neighbors (enemies) of E forced out of set
CSE 101, Fall 2018 103
B C D
E F
G H I
J
K L
A now is lowest degree
Neighbors (enemies) of A forced out of set
A
Multiple degree 2, pick any, say G
![Page 104: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/104.jpg)
Example
Greedy: all degree 3, pick any, say E
Neighbors (enemies) of E forced out of set
CSE 101, Fall 2018 104
B C D
E F
G H I
J
K L
A now is lowest degree
Neighbors (enemies) of A forced out of set
A
Multiple degree 2, pick any, say G
Neighbors (enemies) of G forced out of set
![Page 105: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/105.jpg)
Example
Greedy: all degree 3, pick any, say E
Neighbors (enemies) of E forced out of set
CSE 101, Fall 2018 105
B C D
E F
G H I
J
K L
A now is lowest degree
Neighbors (enemies) of A forced out of set
A
Multiple degree 2, pick any, say G
Neighbors (enemies) of G forced out of set
All degree 2, pick any, say J
![Page 106: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/106.jpg)
Example
Greedy: all degree 3, pick any, say E
Neighbors (enemies) of E forced out of set
CSE 101, Fall 2018 106
B C D
E F
G H I
J
K L
A now is lowest degree
Neighbors (enemies) of A forced out of set
A
Multiple degree 2, pick any, say G
Neighbors (enemies) of G forced out of set
All degree 2, pick any, say J
Neighbors (enemies) of J forced out of set
Solution found by greedy is size 4
![Page 107: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/107.jpg)
Example
A
B C D
E F
G H I
J
K L
CSE 101, Fall 2018 107
Better solution: size 5
![Page 108: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/108.jpg)
• Given a graph with nodes representing people, and there is an edge between A in B if A and B are enemies, find the largest set of people such that no two are enemies. In other words, given an undirected graph, find the largest set of vertices such that no two are connected with an edge.
• Instance: an undirected graph • Solution format: set of vertices S • Constraints: 𝑢, 𝑣 ∈ 𝑆 ⇒ (𝑢, 𝑣) ∉ 𝐸 • Objective function: maximize |S|
Maximal independent set
CSE 101, Fall 2018 108
![Page 109: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/109.jpg)
Maximal independent set • Given a graph with nodes representing people, and there is an edge
between A in B if A and B are enemies, find the largest set of people such that no two are enemies. In other words, given an undirected graph, find the largest set of vertices such that no two are connected with an edge.
• What is the solution space? All subsets S of V
• How much is exhaustive search? 2 𝑉 • What are the constraints? For each edge e={u,v}, if u is in S, v is not
in S • What is the objective? maximize |S|
CSE 101, Fall 2018 109
![Page 110: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/110.jpg)
Maximal independent set • Given a graph with nodes representing people, and there is an edge
between A in B if A and B are enemies, find the largest set of people such that no two are enemies. In other words, given an undirected graph, find the largest set of vertices such that no two are connected with an edge.
• What is the solution space? all subsets S of V
• How much is exhaustive search? 2 𝑉 • What are the constraints? for each edge e={u,v}, if u is in S, v is not
in S • What is the objective? maximize |S|
CSE 101, Fall 2018 110
![Page 111: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/111.jpg)
Maximal independent set • Backtracking: Do exhaustive search locally, but use
constraints to simplify problem as we go • What is a local decision? Do we pick vertex E or not? • What are the possible answers to this decision? Yes or no • How do the answers affect the problem to be solved in the
future? – If we pick E, then recurse on subgraph 𝐺𝑟 − 𝐸 ∪ {𝐸’s neighbors}
(and add 1) – If we do not pick E, then recurse on subgraph 𝐺𝑟 − 𝐸
CSE 101, Fall 2018 111
![Page 112: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/112.jpg)
Example
A
B C D
E F
G H I
J
K L
Local decision: do we pick vertex E or not?
Yes No A
B
E G H I
J
K L
A
B C D
F G H
I J
K L
CSE 101, Fall 2018 112
Recurse on subgraph 𝐺𝑟 − 𝐸 ∪ {𝐸’s neighbors} (and add 1 for E)
Recurse on subgraph 𝐺𝑟 − 𝐸
![Page 113: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/113.jpg)
Case analysis as recursion
MIS1(G= (V,E))
IF |V|=0 return the empty set
Pick vertex v:
𝑆1 := MIS1(G-v-N(v))
𝑆2 := MIS1(G-v)
IF |𝑆2| > |𝑆1| return 𝑆2, else return 𝑆1
CSE 101, Fall 2018 113
Case 1: optimal solution with v
Case 2: optimal solution without v
![Page 114: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/114.jpg)
Correctness
• Strong induction:
• n=0. MIS1 correctly returns empty set.
• Otherwise, use strong induction:
– 𝑆1 is maximal independent set containing v
– 𝑆2 is maximal independent set not containing v
– Better of two is maximal independent set in G
CSE 101, Fall 2018 114
![Page 115: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/115.jpg)
Time analysis
MIS1(G= (V,E))
IF |V|=0 return the empty set
Pick vertex v: 𝑆1 := MIS1(G-v-N(v))
𝑆2 := MIS1(G-v)
IF |𝑆2| > |𝑆1| return 𝑆2, else return 𝑆1
CSE 101, Fall 2018 115
𝑇(𝑛)
𝑇(𝑛 − 1)
worst case 𝑇(𝑛 − 1)
𝑂(1) 𝑇 𝑛 ≤ 2𝑇 𝑛 − 1 + 𝑂 1 𝑇(𝑛) ≤ 𝑂(2𝑛)
reduce and conquer
![Page 116: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/116.jpg)
Maximal independent set
• What is the worse case for MIS1? – An empty graph with no edges, i.e., the whole graph is
an independent set
– But then we should just return all vertices without trying cases
– More generally, if a vertex has no neighbors, the case when we include it v+MIS(G-v) is always better than the case when we do not include it, MIS(G-v)
CSE 101, Fall 2018 116
![Page 117: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/117.jpg)
Maximal independent set
• What is the worse case for MIS1? – An empty graph with no edges, i.e., the whole graph is
an independent set
– But then we should just return all vertices without trying cases
– More generally, if a vertex has no neighbors, the case when we include it v+MIS(G-v) is always better than the case when we do not include it, MIS(G-v)
CSE 101, Fall 2018 117
![Page 118: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/118.jpg)
MIS2(G= (V,E)) IF |V|=0 return the empty set Pick vertex v:
𝑆1 := MIS2(G-v-N(v)) IF deg(v) = 0 return 𝑆1 𝑆2 := MIS2(G-v)
IF |𝑆2| > |𝑆1| return 𝑆2, else return 𝑆1
Getting rid of that worst case
CSE 101, Fall 2018 118
Correctness: if deg(v) =0, then 𝑆2 < |𝑆1|, so we’d return 𝑆1 anyway.
So, does same thing as MIS1
![Page 119: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/119.jpg)
Maximal independent set
• What is the worse case for MIS2?
– One line. If we always pick the end, then we recurse on one line of size n-1 and one of size n-2
CSE 101, Fall 2018 119
![Page 120: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/120.jpg)
Maximal independent set
• What is the worse case for MIS2?
– One line. If we always pick the end, then we recurse on one line of size n-1 and one of size n-2
CSE 101, Fall 2018 120
T(n) = T(n-1) + T(n-2) + poly(n) T(n) = O(Fib(n)) = O(2.7n) Still exponential but for medium sized n, makes huge difference n=80: 256 = minute of computer time, 280 = 16 million minutes
![Page 121: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/121.jpg)
• Can we do better? • In the example, we argued that we should add vertices
of degree 1 as well • Modify-the-solution proof:
– If v has one neighbor u, let 𝑆2 be the largest independent set with v not in 𝑆2. Let S’= 𝑆2 - {u}+{v}. S’ is an independent set, is at least as big as 𝑆2, and contains v. Thus, 𝑆1 is at least as big as S’, which is at least as big as 𝑆2. So don’t bother computing 𝑆2 in this case.
Maximal independent set
CSE 101, Fall 2018 121
![Page 122: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/122.jpg)
MIS3(G= (V,E)) IF |V|=0 return the empty set Pick vertex v:
𝑆1 := MIS3(G-v-N(v)) IF deg(v) = 0 or 1 return 𝑆1 𝑆2 := MIS3(G-v)
IF |𝑆2| > |𝑆1| return 𝑆2, else return 𝑆1
Improved algorithm
CSE 101, Fall 2018 122
Correctness: if deg(v) =0 or 1, then 𝑆2 ≤ |𝑆1|, so we’d return 𝑆1 anyway.
So, does same thing as MIS1
![Page 123: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/123.jpg)
Time analysis
• T(n) is at most T(n-2) + T(n-1) + small amount
• Similar to Fibonacci numbers, but a bit better, about 2.6n rather than 2.7n
• n=80: 2.6n=248, less than a second.
• n=100: 260 = 16 minutes, 270=16,000 minutes
• So while still exponential, big win for moderate n
CSE 101, Fall 2018 123
![Page 124: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/124.jpg)
Is this tight? • I do not know whether there is any graph where MIS3 is
that bad • Best known MIS algorithm around 2n/4, by Robson, building
on Tarjan and Trojanowski. Does much more elaborate case analysis for small degree vertices.
• Interesting research question: is there a limit to improvements?
• This question leads to Exponential Time Hypothesis, which has interesting ramifications whether true or false
CSE 101, Fall 2018 124
![Page 125: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/125.jpg)
WEIGHTED EVENT SCHEDULING
CSE 101, Fall 2018 125
![Page 126: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/126.jpg)
Weighted event scheduling
CSE 101, Fall 2018 126
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
$25
$6
$11 $33
$20 $15
$22 $24
$11 $35
$33 $2
$12
$5
![Page 127: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/127.jpg)
• Instance: what does the input look like?
• Solution format: what does an output look like?
• Constraints: what properties must a solution have?
• Objective function: what makes a solution better or worse?
Weighted event scheduling
CSE 101, Fall 2018 127
![Page 128: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/128.jpg)
Weighted event scheduling
• Instance: list of n intervals I= (s,f), with associated values v
• Solution format: subset of intervals
• Constraints: cannot pick intersecting intervals
• Objective function: maximize total value of intervals chosen
CSE 101, Fall 2018 128
![Page 129: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/129.jpg)
Weighted event scheduling
• No known greedy algorithm
– In fact, Borodin, Nielsen, and Rackoff formally prove no greedy algorithm even approximates
• Brute force?
CSE 101, Fall 2018 129
![Page 130: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/130.jpg)
Weighted event scheduling
• No known greedy algorithm
– In fact, Borodin, Nielsen, and Rackoff formally prove no greedy algorithm even approximates
• Brute force? 2n subsets
CSE 101, Fall 2018 130
![Page 131: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/131.jpg)
Backtracking
• Sort events by start time
• Pick first to start. 𝐼1 not necessarily good to include, so we will try both possibilities: – Case 1: we exclude 𝐼1, recurse on 𝐼2, … , 𝐼𝑛
– Case 2: we include 𝐼1, recurse on the set of all intervals that do not conflict with 𝐼1
• Is there a better way to describe this set? All events that start after 𝐼1 finished, 𝐼J, … , 𝐼𝑛 for some J
CSE 101, Fall 2018 131
![Page 132: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/132.jpg)
Backtracking
• Sort events by start time
• Pick first to start. 𝐼1 not necessarily good to include, so we will try both possibilities: – Case 1: we exclude 𝐼1, recurse on 𝐼2, … , 𝐼𝑛
– Case 2: we include 𝐼1, recurse on the set of all intervals that do not conflict with 𝐼1
• Is there a better way to describe this set? All events that start after 𝐼1 finished, 𝐼J, … , 𝐼𝑛 for some J
CSE 101, Fall 2018 132
![Page 133: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/133.jpg)
Backtracking • Sort events by start time • Pick first to start. 𝐼1 not necessarily good to include, so we will try both
possibilities: exclude 𝐼1 and include 𝐼1 BTWES (𝐼1 … 𝐼𝑛): in order of start times
If n=0 return 0 If n=1 return 𝑉1
Exclude:= BTWES(𝐼2. . 𝐼𝑛) J:=2 Until (J > n or s_J > f_1) do J++
Include:= 𝑉1 + BTWES(𝐼𝐽. . 𝐼𝑛) return Max(Include, Exclude)
CSE 101, Fall 2018 133
![Page 134: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/134.jpg)
Backtracking, runtime • Sort events by start time • Pick first to start. 𝐼1 not necessarily good to include, so we will try both
possibilities: exclude 𝐼1 and include 𝐼1 BTWES (𝐼1 … 𝐼𝑛): in order of start times
If n=0 return 0 If n=1 return 𝑉1
Exclude:= BTWES(𝐼2. . 𝐼𝑛) J:=2 Until (J > n or s_J > f_1) do J++
Include:= 𝑉1 + BTWES(𝐼𝐽. . 𝐼𝑛) return Max(Include, Exclude)
CSE 101, Fall 2018 134
𝑇(𝑛)
𝑇(𝑛 − 1)
𝑇(𝑛 − 𝐽)
𝑇 𝑛 = 𝑇 𝑛 − 1 + 𝑇 𝑛 − 𝐽 + 𝑂 poly 𝑇 𝑛 = 𝑂(2𝑛)
![Page 135: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/135.jpg)
Backtracking, runtime
• 𝑂(2𝑛) worst case time, same as exhaustive search
• We could try to improve or use dynamic programming
CSE 101, Fall 2018 135
![Page 136: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/136.jpg)
Example • 𝐼1 = (1,5), 𝑉1 = 4
• 𝐼2 = (2,4), 𝑉2 = 3
• 𝐼3 = (3, 7), 𝑉3 = 5
• 𝐼4 = (4,9), 𝑉4 = 6
• 𝐼5 = (5,8), 𝑉5 = 3
• 𝐼6 = (6,11), 𝑉6 = 4
• 𝐼7 = (9,13), 𝑉7 = 5
• 𝐼8 = (10,12), 𝑉8 = 3
CSE 101, Fall 2018 136
![Page 137: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/137.jpg)
Total number of calls vs number of distinct calls
• We make up to 2𝑛 recursive calls in our algorithm
• But every recursive call has the form 𝐼𝐽 … 𝐼𝑛
• Thus, there are at most 𝑛 + 1 different calls throughout
• Memorization: Store and reuse the answers, do not recompute
CSE 101, Fall 2018 137
![Page 138: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/138.jpg)
𝐼1 = (1,5), 𝑉1 = 4
𝐼2 = (2,4), 𝑉2 = 3
𝐼3 = (3, 7), 𝑉3 = 5
𝐼4 = (4,9), 𝑉4 = 6
𝐼5 = (5,8), 𝑉5 = 3
𝐼6 = (6,11), 𝑉6 = 4
𝐼7 = (9,13), 𝑉7 = 5
𝐼8 = (10,12), 𝑉8 = 3
Example
CSE 101, Fall 2018 138
𝐼1…8, 𝐼2…8, 𝐼3…8, 𝐼4…8, 𝐼5…8, 𝐼6…8, 𝐼7…8, 𝐼8, none
Distinct calls:
𝐼1…8
Do 𝐼1: 𝐼5…8
Do 𝐼5: 𝐼7…8
none
Skip 𝐼1: 𝐼2…8
Skip 𝐼5: 𝐼6…8
none 𝐼7…8
none 𝐼8
𝐼4…8 𝐼3…8
𝐼7…8 𝐼5…8
none 𝐼8 𝐼7…8
none none 𝐼8
𝐼6…8
𝐼7…8
none 𝐼8
𝐼7…8
none
𝐼8
𝐼8
𝐼4…8
*
*
![Page 139: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/139.jpg)
Characterize calls made • All of the recursive calls BTWES makes are to
arrays of the form 𝐼𝐾…𝑛 or empty with K=1…n • So, of the 2𝑛 recursive calls we might make, only
𝑛 + 1 distinct calls are made • Just like Fibonacci numbers: many calls made
exponentially often • Solution same: create array to store and re-use
answers, rather than repeatedly solving them
CSE 101, Fall 2018 139
![Page 140: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/140.jpg)
Dynamic programming steps • Step 1: Define sub-problems and corresponding array • Step 2: What are the base cases • Step 3: Give recursion for sub-problems • Step 4: Find bottom-up order • Step 5: What is the final output? • Step 6: Put it all together into an iterative algorithm that fills in the array
step by step
• For analysis: • Step 7: correctness proof • Step 8: runtime analysis
CSE 101, Fall 2018 140
![Page 141: 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,](https://reader030.vdocument.in/reader030/viewer/2022040309/5f0e2ea37e708231d43e00ce/html5/thumbnails/141.jpg)
Next lecture
• Dynamic programming
– Reading: Chapter 6
CSE 101, Fall 2018 141