CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture...

101
CSE 548: Analysis of Algorithms Rezaul A. Chowdhury Department of Computer Science SUNY Stony Brook Spring 2015 Ο Ω I Asymptotic Stickman ( by Aleksandra Patrzalek, SUNY Buffalo )

Transcript of CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture...

Page 1: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

CSE 548: Analysis of Algorithms

Rezaul A. Chowdhury

Department of Computer Science

SUNY Stony Brook

Spring 2015

ΟΩI

Asymptotic Stickman

( by Aleksandra Patrzalek, SUNY Buffalo )

Page 2: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Some Mostly Useless Information

― Lecture Time: TuTh 4:00 pm - 5:20 pm

― Location: Computer Teaching Lab ( CS 2120 ), West Campus

― Instructor: Rezaul A. Chowdhury

― Office Hours: TuTh 12:00 pm - 1:30 pm, 1421 Computer Science

― Email: [email protected]

― TA: TBA

― Class Webpage:

http://www3.cs.stonybrook.edu/~rezaul/CSE548-S15.html

Page 3: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Prerequisites

― Required: Some background ( undergrad level ) in the design and

analysis of algorithms and data structures

― fundamental data structures (e.g., lists, stacks, queues

and arrays)

― discrete mathematical structures (e.g., graphs, trees, and

their adjacency lists & adjacency matrix representations)

― fundamental programming techniques (e.g., recursion,

divide-and-conquer, and dynamic programming)

― basic sorting and searching algorithms

― fundamentals of asymptotic analysis (e.g., O( · ), Ω( · )

and Θ( · ) notations)

― Required: Some background in programming languages ( C / C++ )

Page 4: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Topics to be Covered

The following topics will be covered ( hopefully )

― recurrence relations and divide-and-conquer algorithms

― dynamic programming

― graph algorithms (e.g., network flow)

― amortized analysis

― advanced data structures (e.g., Fibonacci heaps)

― cache-efficient and external-memory algorithms

― high probability bounds and randomized algorithms

― parallel algorithms and multithreaded computations

― NP-completeness and approximation algorithms

― the alpha technique (e.g., disjoint sets, partial sums)

― FFT ( Fast Fourier Transforms )

Page 5: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Grading Policy

― Four Homework Problem Sets

( highest score 15%, lowest score 5%, and others 10% each ): 40%

― Two Exams ( higher one 30%, lower one 15% ): 45%

― Midterm ( in-class ): Mar 12

― Final ( in-class ): May 7

― Scribe note ( one lecture ): 10%

― Class participation & attendance: 5%

Page 6: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Textbooks

Required

― Thomas Cormen, Charles Leiserson, Ronald Rivest, and Clifford Stein.

Introduction to Algorithms (3rd Edition), MIT Press, 2009.

Recommended

― Sanjoy Dasgupta, Christos Papadimitriou, and Umesh Vazirani.

Algorithms (1st Edition), McGraw-Hill, 2006.

― Jon Kleinberg and Éva Tardos.

Algorithm Design (1st Edition), Addison Wesley, 2005.

― Rajeev Motwani and Prabhakar Raghavan.

Randomized Algorithms (1st Edition), Cambridge University Press, 1995.

― Vijay Vazirani.

Approximation Algorithms, Springer, 2010.

― Joseph JáJá.

An Introduction to Parallel Algorithms (1st Edition), Addison Wesley, 1992.

Page 7: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

What is an Algorithm?

An algorithm is a well-defined computational procedure that solves

a well-specified computational problem.

It accepts a value or set of values as input, and produces a value or

set of values as output

Example: mergesort solves the sorting problem specified as a

relationship between the input and the output as follows.

Input: A sequence of numbers , , … , .

Output: A permutation ′, ′, … , ′ of the input sequence

such that ′ ′ ⋯ ′.

Page 8: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Desirable Properties of an Algorithm

√ Correctness

― Designing an incorrect algorithm is straight-forward

√ Efficiency

― Efficiency is easily achievable if we give up on correctness

Surprisingly, sometimes incorrect algorithms can also be useful!

― If you can control the error rate

― Tradeoff between correctness and efficiency:

Randomized algorithms

( Monte Carlo: always efficient but sometimes incorrect,

Las Vegas: always correct but sometimes inefficient )

Approximation algorithms

( always incorrect! )

Page 9: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

How Do You Measure Efficiency?

We often want algorithms that can use the available resources

efficiently.

Some measures of efficiency

― time complexity

― space complexity

― cache complexity

― I/O complexity

― energy usage

― number of processors/cores used

― network bandwidth

Page 10: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Goal of Algorithm Analysis

Goal is to predict the behavior of an algorithm without

implementing it on a real machine.

But predicting the exact behavior is not always possible

as there are too many influencing factors.

Runtime on a serial machine is the most commonly used measure.

We need to model the machine first in order to analyze runtimes.

But an exact model will make the analysis too complicated!

So we use an approximate model ( e.g., assume unit-cost Random

Access Machine model or RAM model ).

We may need to approximate even further: e.g., for a sorting

algorithm we may count the comparison operations only.

So the predicted running time will only be an approximation!

Page 11: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Performance Bounds

― worst-case complexity: maximum complexity over all inputs of a

given size

― average complexity: average complexity over all inputs of a given

size

― amortized complexity: worst-case bound on a sequence of

operations

― expected complexity: for algorithms that make random choices

during execution ( randomized algorithms )

― high-probability bound: when the probability that the complexity

holds is 1

for input size , positive constant and some

constant 1

Page 12: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

You are given an grid

1: , 1: , where 2 1

for some integer 0.

Each grid cell contains a number.

The numbers in each row are

sorted in non-decreasing order

from left to right.

The numbers in each column are

sorted in non-decreasing order

from top to bottom.

Page 13: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 1 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 1 ( SEARCH FOR ):

Scan the entire grid row by row

until either is found, or you are

done scanning the entire grid.

Let number of

comparisons performed on an

grid.

Then

Page 14: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 2 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 2 ( SEARCH FOR ):

Let be the number at the

center of the grid ( i.e., at the

intersection of the mid row and

mid column ).

• : you found the item

Page 15: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 2 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 2 ( SEARCH FOR ):

Let be the number at the

center of the grid ( i.e., at the

intersection of the mid row and

mid column ).

• : you found the item

Page 16: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 2 )

ALGORITHM 2 ( SEARCH FOR ):

Let be the number at the

center of the grid ( i.e., at the

intersection of the mid row and

mid column ).

• : you found the item

• : the item cannot be in

, and .

Search for in and , and

recursively in , & .

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

Page 17: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 2 )

ALGORITHM 2 ( SEARCH FOR ):

Let be the number at the

center of the grid ( i.e., at the

intersection of the mid row and

mid column ).

• : you found the item

• : the item cannot be in

, and .

Search for in and , and

recursively in , & .

• : the item cannot be in

, and .

Search for in and , and

recursively in , & .

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

Page 18: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 2 )

1 ! 2 ! 1

2 1

!3 ! 1

2 1

Let number of

comparisons performed on an

grid.

⇒ 3 ! 1

2 1 !

Solving: 2 ! 1 %&'()

2 ! 1 .+

Then

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

Page 19: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Page 20: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 21: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 22: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 23: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 24: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 25: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 26: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 27: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 28: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 29: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 30: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 31: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 32: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 33: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 34: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 35: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 36: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 37: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 38: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 39: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 40: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 41: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 42: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 3 ( SEARCH FOR ):

Starting from the top row

perform a binary search for in

each row until is found.

,-./ ← 1

1234/ ←

542,-,-./ 1234/67

26 ←89:;<=>?@;

2. 2,26 /4-

1-/A1"2/-.7A6"

-,C-2. 2,26 /4-

,-./ ← 26 ! 1

-,C-1234/ ← 26 1

-6542,-

1-/A1"2/-7/.7A6"

Binary search in row 2 of :

Search for 35

Page 43: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 3 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

Search for 35

) log ! 1

Let ) number of

comparisons performed on an

grid.

So,

Binary search on each row

performs comparisons.

Page 44: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 4 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Page 45: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 4 )

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Page 46: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 47: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 48: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 49: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 50: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 51: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 52: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 53: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 54: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 55: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 56: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 57: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 58: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 59: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 60: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 61: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 62: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 63: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 64: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 65: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 66: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 67: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 68: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

ALGORITHM 4 ( SEARCH FOR ):

Start the search for from the

bottom-left corner.

Keep performing the steps below

until either you find or you fall

off the grid:

Let be the number at current

location.

• : you found the item

• :move to the right

• :move to the cell above

Search for 68

Searching in a Sorted Grid ( Algorithm 4 )

Page 69: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 4 )

J 2 1 2

Let J number of

comparisons performed on an

grid.

Then

2 5 10 11 20 22 26 30 31 31 34 34 37 40 45

4 6 15 18 27 30 31 38 39 40 42 42 44 48 48

7 9 16 21 27 31 39 41 41 41 48 50 55 55 59

8 13 22 22 27 34 40 45 48 50 50 50 58 58 65

11 14 29 31 35 36 41 49 55 55 58 59 61 62 67

15 20 30 32 39 42 42 50 59 60 60 60 65 68 71

16 21 35 41 41 43 44 58 62 69 69 70 70 70 75

20 22 36 41 42 50 50 61 65 70 75 75 76 78 78

21 25 37 44 44 59 60 62 70 72 75 76 78 78 80

22 28 39 48 50 61 62 66 71 75 75 76 78 81 85

26 31 40 56 65 65 65 69 75 78 78 80 82 82 88

29 34 41 61 66 69 72 72 78 80 80 81 82 84 88

31 41 45 66 67 70 72 72 78 82 84 84 85 85 91

32 45 49 67 67 72 78 80 81 86 85 86 86 88 95

40 55 56 70 71 75 81 81 81 86 86 88 91 93 98

2 1

2 1

Search for 68

Page 70: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Comparing the Four ( 4 ) Grid Search Algorithms

) J

Page 71: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 1 )

Let K running time of algorithm 1 on an grid.

Though we were able to compute an exact worst-case bound for , the same

cannot be done for K because it depends on many other external factors such

as CPU speed, programming style, compiler and optimization level used, etc.

But for large values of , K ’s worst-case value will be within a constant factor

of that of . That constant is generally unknown, and depends on the specific

hardware and compiler used, expertise of the programmer, etc.

ALGORITHM 1 ( SEARCH FOR ):

1. .712 1/767

2. .71L 1/767

3. 2. 2, L /4-1-/A1"2/-.7A6“

4. -6.71

5. -6.71

6. 1-/A1"2/-7/.7A6"

Page 72: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Searching in a Sorted Grid ( Algorithm 1 )

ALGORITHM 1 ( SEARCH FOR ):

1. .712 1/767

2. .71L 1/767

3. 2. 2, L /4-1-/A1"2/-.7A6“

4. -6.71

5. -6.71

6. 1-/A1"2/-7/.7A6"

In the worst case,

― line 3 will be executed times,

― variable L in line 2 will be updated times,

― variable 2 in line 1 will be updated times, and

― line 6 will be executed will be executed 1 time.

Hence, K ! ! ), where , and ) are constants.

Clearly, K ! 1 ! 1 , when ! ).

Page 73: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Why Lower Order Terms Can be Dropped

Page 74: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Why Lower Order Terms Can be Dropped

Page 75: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Why Lower Order Terms Can be Dropped

Page 76: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Why Lower Order Terms Can be Dropped

Page 77: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Running Times of the Four ( 4 ) Algorithms for Large n

GRID SEARCHING

ALGORITHM

WORST-CASE BOUND ON

#COMPARISONS

WORST-CASE BOUND ON

RUNNING TIMES

ALGORITHM 1 K

ALGORITHM 2 2 ! 1 .+ K ! 1.+

ALGORITHM 3 ) log ! 1 K) ) log ! 1

ALGORITHM 4 J 2 KJ J

, , )6J1-7C//C

Page 78: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Why Faster Algorithms?

As the input gets large a faster algorithm run on a slow computer

will eventually beat a slower algorithm run on a fast computer!

Suppose we run ALGORITHM 4 on computer that can execute only

1 million instructions per second. The algorithm was implemented

by an inexperienced programmer, and so J 10.

Suppose we run ALGORITHM 1 on computer O that is 1000 times

faster than , and the algorithm was implemented by an expert

programmer, and so 1.

Let’s run both algorithm on a large grid with 100,000.

Then ALGORITHM 1 will require up to PPPPP (

PPPPPPPPP 10 seconds,

while ALGORITHM 4 will terminate in only PPPPPP

PPPPPP 1 second!

Page 79: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Asymptotic Bounds

We compute performance bounds as functions of input size .

Asymptotic bounds are obtained when → ∞.

Several types of asymptotic bounds

― upper bound ( O-notation )

― strict upper bound ( o-notation )

― lower bound ( Ω-notation )

― strict lower bound ( S-notation )

― tight bound ( Θ-notation )

ΟΩ

I

Asymptotic Stickman

( by Aleksandra Patrzalek )

Page 80: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Asymptotic Upper Bound ( O-notation )

n

f(n)

cg(n)

n0

T 3 . : /4-1--2C/U7C2/2V-7C//C6PCA4/4/

0 . 3 .71,, P

T 3

. : /4-1--2C/CU7C2/2V-7C//CA4/4/

lim→Y

.

3

Page 81: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Asymptotic Upper Bound ( O-notation )

Recall that for Algorithm 1 we had, K . , where,

. ! ! ), for constants , and ).

We will now derive asymptotic bounds for . .

Suppose, 5, 2 and ) 9.

Then . 5 ! 2 ! 9.

Page 82: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Asymptotic Upper Bound ( O-notation )

P

Let 3 .

Then . T because:

0 . 3 for 6 and 5.

Page 83: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Asymptotic Upper Bound ( O-notation )

Let 3 ).

Then . T ) because:

0 . 3 for 1 and 6.

P

Page 84: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Asymptotic Upper Bound ( O-notation )

Let 3 .

Then . [ T because:

. 3 for any and

\.

P P P

Page 85: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

n

f(n)

cg(n)

n0

Asymptotic Lower Bound ( ΩΩΩΩ-notation )

Ω 3 . : /4-1--2C/U7C2/2V-7C//C6PCA4/4/

0 3 . .71,, P

Ω 3

. : /4-1--2C/CU7C2/2V-7C//CA4/4/

lim→Y

.

3

Page 86: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Asymptotic Lower Bound ( ΩΩΩΩ-notation )

Let 3 .

Then . Ω because:

0 3 . for 5 and 1.

P

Page 87: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Let 3 ).

Then . [ Ω ) because:

3 . for any and \

.

P

Asymptotic Lower Bound ( ΩΩΩΩ-notation )

PP

Page 88: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Asymptotic Lower Bound ( ΩΩΩΩ-notation )

Let 3 .

Then . Ω because:

0 3 . for 30 and 6.

P

Page 89: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

n

f(n)

c1g(n)

n0

c2g(n)

Asymptotic Tight Bound ( ΘΘΘΘ-notation )

Θ 3 . : /4-1--2C/U7C2/2V-7C//C, 6PCA4/4/

0 3 . 3 .71,, P

Θ 3

. : /4-1--2C/U7C2/2V-7C//C6CA4/4/

lim→Y

.

3

Page 90: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

n

f(n)

c1g(n)

n0

c2g(n)

Asymptotic Tight Bound ( ΘΘΘΘ-notation )

. O 3 ∧ . Ω 3 ⇔ . Θ 3

Page 91: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Asymptotic Tight Bound ( ΘΘΘΘ-notation )

. 5 ! 2 ! 9

. Θ because both . T and . Ω hold.

. [ Θ ) because though . T ) holds, . [ Ω ) .

. [ Θ because though . Ω holds, . [ O .

Page 92: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Asymptotic Strict Upper Bound ( o-notation )

T 3 . : /4-1--2C/U7C2/2V-7C//C6PCA4/4/

0 . 3 .71,, P

T 3

. : /4-1--2C/CU7C2/2V-7C//CA4/4/

lim→Y

.

3

7 3 . : lim→Y

.

3 0

Page 93: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Asymptotic Strict Lower Bound ( ωωωω-notation )

Ω 3 . : /4-1--2C/U7C2/2V-7C//C6PCA4/4/

0 3 . .71,, P

Ω 3

. : /4-1--2C/CU7C2/2V-7C//CA4/4/

lim→Y

.

3

S 3 . : lim→Y

3

. 0

Page 94: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Comparing Functions: Transitivity

. Θ 3 and 3 Θ 4 ⇒ . Θ 4

. O 3 and 3 O 4 ⇒ . O 4

. Ω 3 and 3 Ω 4 ⇒ . Ω 4

. o 3 and 3 o 4 ⇒ . o 4

. S 3 and 3 S 4 ⇒ . S 4

Page 95: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Comparing Functions: Reflexivity

. Θ .

. O .

. Ω .

Page 96: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Comparing Functions: Symmetry

. Θ 3 if and only if 3 Θ .

Page 97: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Comparing Functions: Transpose Symmetry

. O 3 if and only if 3 Ω .

. Ω 3 if and only if 3 O .

Page 98: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Adding Functions

O . ! O 3 O max . , 3

Ω . ! Ω 3 Ω max . , 3

Θ . ! Θ 3 Θ max . , 3

Page 99: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Multiplying Functions by Constants

O . O .

Ω . Ω .

Θ . Θ .

Page 100: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Multiplying Two Functions

O . O 3 O . 3

Ω . Ω 3 Ω . 3

Θ . Θ 3 Θ . 3

Page 101: CSE 548: Analysis of Algorithms - Stony Brook Universityrezaul/Spring-2015/CSE548/CSE548-lecture … · CSE 548: Analysis of Algorithms ... analysis of algorithms and data structures

Division of Functions

O .

Θ 3 O

.

3

Ω .

Θ 3 Ω

.

3

Θ .

Θ 3 Θ

.

3

O .

Ω 3 O

.

3

Θ .

Ω 3 O

.

3

Ω .

O 3 Ω

.

3

Θ .

O 3 Ω

.

3