Top Banner
1 / 81 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 15 Programming Fundamentals using Java 1
81

COP 3503 FALL 2012 Shayan Javed Lecture 15

Jan 01, 2016

Download

Documents

shea-puckett

COP 3503 FALL 2012 Shayan Javed Lecture 15. Programming Fundamentals using Java. Algorithms Searching. So far. Focused on Object-Oriented concepts in Java Going to look at some basic computer science concepts: algorithms and more data structures. Algorithm. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: COP 3503  FALL 2012 Shayan Javed Lecture 15

1 / 81

COP 3503 FALL 2012SHAYAN JAVED

LECTURE 15

Programming Fundamentals using Java

1

Page 2: COP 3503  FALL 2012 Shayan Javed Lecture 15

2 / 81

Algorithms

Searching

Page 3: COP 3503  FALL 2012 Shayan Javed Lecture 15

3 / 81

So far...

Focused on Object-Oriented concepts in Java

Going to look at some basic computer science concepts: algorithms and more data structures.

Page 4: COP 3503  FALL 2012 Shayan Javed Lecture 15

4 / 81

Algorithm

An algorithm is an effective procedure for solving a problem expressed as a finite sequence of instructions.

“effective procedure” = program

Page 5: COP 3503  FALL 2012 Shayan Javed Lecture 15

5 / 81

Pseudocode

A compact and informal description of algorithms using the structural conventions of a programming language and meant for human reading.

Page 6: COP 3503  FALL 2012 Shayan Javed Lecture 15

6 / 81

Pseudocode

A compact and informal description of algorithms using the structural conventions of a programming language and meant for human reading.

A text-based design tool that helps programmers to develop algorithms.

Page 7: COP 3503  FALL 2012 Shayan Javed Lecture 15

7 / 81

Pseudocode

A compact and informal description of algorithms using the structural conventions of a programming language and meant for human reading.

A text-based design tool that helps programmers to develop algorithms. Basically, write out the algorithm in steps which

humans can understand and can be converted to a programming language

Page 8: COP 3503  FALL 2012 Shayan Javed Lecture 15

8 / 81

Searching

Ability to search data is extremely crucial.

Page 9: COP 3503  FALL 2012 Shayan Javed Lecture 15

9 / 81

Searching

Ability to search data is extremely crucial.

Searching the whole internet is always a problem. (Google/Bing/...Altavista).

Page 10: COP 3503  FALL 2012 Shayan Javed Lecture 15

10 / 81

Searching

Ability to search data is extremely crucial.

Searching the whole internet is always a problem. (Google/Bing/...Altavista). Too complex of course.

Page 11: COP 3503  FALL 2012 Shayan Javed Lecture 15

11 / 81

Searching

The problem: Search an array A.

Page 12: COP 3503  FALL 2012 Shayan Javed Lecture 15

12 / 81

Searching

The problem: Search an array A.

N – Number of elements in the array

Page 13: COP 3503  FALL 2012 Shayan Javed Lecture 15

13 / 81

Searching

The problem: Search an array A.

N – Number of elements in the array k – the value to be searched

Page 14: COP 3503  FALL 2012 Shayan Javed Lecture 15

14 / 81

Searching

The problem: Search an array A.

N – Number of elements in the array k – the value to be searched

Does k appear in A?

Page 15: COP 3503  FALL 2012 Shayan Javed Lecture 15

15 / 81

Searching

The problem: Search an array A.

N – Number of elements in the array k – the value to be searched

Does k appear in A? Output: Return index of k in A

Page 16: COP 3503  FALL 2012 Shayan Javed Lecture 15

16 / 81

Searching

The problem: Search an array A.

N – Number of elements in the array k – the value to be searched

Does k appear in A? Output: Return index of k in A Return -1 if k does not appear in A

Page 17: COP 3503  FALL 2012 Shayan Javed Lecture 15

17 / 81

Linear Search

How would you search this array of integers?

(For ex., we want to search for 87)

i 0 1 2 3 4 5

A 55 19 100 45 87 33

Page 18: COP 3503  FALL 2012 Shayan Javed Lecture 15

18 / 81

Linear Search

How would you search this array of integers?

(For ex., we want to search for 87) Pseudocode:

for i = 0 till N: if A[i] == k return ireturn -1

i 0 1 2 3 4 5

A 55 19 100 45 87 33

Page 19: COP 3503  FALL 2012 Shayan Javed Lecture 15

19 / 81

Linear Search

int linearSearch (int[] A, int key, int start, int end)

{

for (int i = start; i < end; i++) {

if (key == A[i]) {

return i; // found - return index

}

}

// key not found

return -1;

}

Page 20: COP 3503  FALL 2012 Shayan Javed Lecture 15

20 / 81

Linear Search

int linearSearch (int[] A, int key, int start, int end)

{

for (int i = start; i < end; i++) {

if (key == A[i]) {

return i; // found - return index

}

}

// key not found

return -1;

}

int index = linearSearch(A, 87, 0, A.length);

Page 21: COP 3503  FALL 2012 Shayan Javed Lecture 15

21 / 81

Linear Search

Advantages: Straightforward algorithm.

Page 22: COP 3503  FALL 2012 Shayan Javed Lecture 15

22 / 81

Linear Search

Advantages: Straightforward algorithm. Array can be in any order.

Page 23: COP 3503  FALL 2012 Shayan Javed Lecture 15

23 / 81

Linear Search

Advantages: Straightforward algorithm. Array can be in any order.

Disadvantages: Slow and inefficient (if array is very large)

Page 24: COP 3503  FALL 2012 Shayan Javed Lecture 15

24 / 81

Linear Search

Advantages: Straightforward algorithm. Array can be in any order.

Disadvantages: Slow and inefficient (if array is very large)

N/2 elements on average (4 comparisons for 45)

Page 25: COP 3503  FALL 2012 Shayan Javed Lecture 15

25 / 81

Linear Search

Advantages: Straightforward algorithm. Array can be in any order.

Disadvantages: Slow and inefficient (if array is very large)

N/2 elements on average (4 comparisons for 45) Have to go through N elements in worst-case

Page 26: COP 3503  FALL 2012 Shayan Javed Lecture 15

26 / 81

Algorithm Efficiency

How do we judge if an algorithm is good enough?

Page 27: COP 3503  FALL 2012 Shayan Javed Lecture 15

27 / 81

Algorithm Efficiency

How do we judge if an algorithm is good enough?

Need a way to describe algorithm efficiency.

Page 28: COP 3503  FALL 2012 Shayan Javed Lecture 15

28 / 81

Algorithm Efficiency

How do we judge if an algorithm is good enough?

Need a way to describe algorithm efficiency.

We use the Big O notation

Page 29: COP 3503  FALL 2012 Shayan Javed Lecture 15

29 / 81

Big O notation

Estimates the execution time in relation to the input size.

Page 30: COP 3503  FALL 2012 Shayan Javed Lecture 15

30 / 81

Big O notation

Estimates the execution time in relation to the input size.

Upper bound on the growth rate of the function. (In terms of the worst-case)

Page 31: COP 3503  FALL 2012 Shayan Javed Lecture 15

31 / 81

Big O notation

Estimates the execution time in relation to the input size.

Upper bound on the growth rate of the function. (In terms of the worst-case)

Written as O(x), where x = in terms of the input size.

Page 32: COP 3503  FALL 2012 Shayan Javed Lecture 15

32 / 81

Big O notation

If execution time is not related to input size, algorithm takes constant time: O(1).

Page 33: COP 3503  FALL 2012 Shayan Javed Lecture 15

33 / 81

Big O notation

If execution time is not related to input size, algorithm takes constant time: O(1). Ex.: Looking up value in an array – A[3]

Page 34: COP 3503  FALL 2012 Shayan Javed Lecture 15

34 / 81

Big O notation

If execution time is not related to input size, algorithm takes constant time: O(1). Ex.: Looking up value in an array – A[3]

Big O for Linear Search?

Page 35: COP 3503  FALL 2012 Shayan Javed Lecture 15

35 / 81

Big O notation

If execution time is not related to input size, algorithm takes constant time: O(1). Ex.: Looking up value in an array – A[3]

Big O for Linear Search? O(N) = have to search through at least N values of the

Array A

Page 36: COP 3503  FALL 2012 Shayan Javed Lecture 15

36 / 81

Big O notation

If execution time is not related to input size, algorithm takes constant time: O(1). Ex.: Looking up value in an array – A[3]

Big O for Linear Search? O(N) = have to search through at least N values of the

Array A Execution time proportional to the size of the array.

Page 37: COP 3503  FALL 2012 Shayan Javed Lecture 15

37 / 81

Big O notation

Another example problem: Find max value in an array A of size N.

Page 38: COP 3503  FALL 2012 Shayan Javed Lecture 15

38 / 81

Big O notation

Another example problem: Find max value in an array A of size N.

int max = A[0];

for i = 1 till i = N

if (A[i] > max)

max = A[i]

Page 39: COP 3503  FALL 2012 Shayan Javed Lecture 15

39 / 81

Big O notation

Another example problem: Find max value in an array A of size N.

int max = A[0];

for i = 1 till i = N

if (A[i] > max)

max = A[i]

How many comparisons?

N – 1 comparisons

Efficiency: O(n-1)

O(n) [Ignore non-dominating part]

Page 40: COP 3503  FALL 2012 Shayan Javed Lecture 15

40 / 81

Big O notation

Another example problem: Find max value in an array A of size N.

int max = A[0];

for i = 1 till i = N

if (A[i] > max)

max = A[i]

How many comparisons?

N – 1 comparisons

Efficiency: O(n-1)

O(n) [Ignore non-dominating part]

Page 41: COP 3503  FALL 2012 Shayan Javed Lecture 15

41 / 81

Big O notation

Another example problem: Find max value in an array A of size N.

int max = A[0];

for i = 1 till i = N

if (A[i] > max)

max = A[i]

How many comparisons?

N – 1 comparisons

Efficiency: O(N-1)

O(n) [Ignore non-dominating part]

Page 42: COP 3503  FALL 2012 Shayan Javed Lecture 15

42 / 81

Big O notation

Another example problem: Find max value in an array A of size N.

int max = A[0];

for i = 1 till i = N

if (A[i] > max)

max = A[i]

How many comparisons?

N – 1 comparisons

Efficiency: O(N-1)

O(N) [Ignore non-dominating part]

Page 43: COP 3503  FALL 2012 Shayan Javed Lecture 15

43 / 81

Back to Search...

Linear Search is the simplest way to search an array.

Page 44: COP 3503  FALL 2012 Shayan Javed Lecture 15

44 / 81

Back to Search...

Linear Search is the simplest way to search an array.

Other data structures will make it easier to search for data.

Page 45: COP 3503  FALL 2012 Shayan Javed Lecture 15

45 / 81

Back to Search...

Linear Search is the simplest way to search an array.

Other data structures will make it easier to search for data.

But what if data is sorted?

Page 46: COP 3503  FALL 2012 Shayan Javed Lecture 15

46 / 81

Search

Array A of size 6: (search for k = 87)

A sorted:

i 0 1 2 3 4 5

A 19 33 45 55 87 100

i 0 1 2 3 4 5

A 55 19 100 45 87 33

Page 47: COP 3503  FALL 2012 Shayan Javed Lecture 15

47 / 81

Search

A sorted: k = 87

How can you search this more efficiently?

i 0 1 2 3 4 5

A 19 33 45 55 87 100

Page 48: COP 3503  FALL 2012 Shayan Javed Lecture 15

48 / 81

Search

A sorted: k = 87

How can you search this more efficiently?

Let’s start by looking at the middle index: I = N/2 = 3

i 0 1 2 3 4 5

A 19 33 45 55 87 100

Page 49: COP 3503  FALL 2012 Shayan Javed Lecture 15

49 / 81

Search

A sorted: k = 87

How can you search this more efficiently?

Let’s start by looking at the middle index: I = N/2 = 3

i 0 1 2 3 4 5

A 19 55 33 45 87 100

i 0 1 2 3 4 5

A 19 33 45 55 87 100

Page 50: COP 3503  FALL 2012 Shayan Javed Lecture 15

50 / 81

Search

A sorted: k = 87, I = 3

is 87 == A[I] ? No.

i 0 1 2 3 4 5

A 19 55 33 45 87 100

i 0 1 2 3 4 5

A 19 33 45 55 87 100

Page 51: COP 3503  FALL 2012 Shayan Javed Lecture 15

51 / 81

Search

A sorted: k = 87, I = 3

is 87 == A[I] ? No.

is 87 < A[I] ? No. (So 87 is NOT in index 0 through 3).

i 0 1 2 3 4 5

A 19 55 33 45 87 100

i 0 1 2 3 4 5

A 19 33 45 55 87 100

Page 52: COP 3503  FALL 2012 Shayan Javed Lecture 15

52 / 81

Search

A sorted: k = 87, I = 3

is 87 == A[I] ? No.

is 87 < A[I] ? No. (So 87 is NOT in index 0 through 3).

is 87 > A[I] ? Yes. (Now search from index 4 through 5).

i 0 1 2 3 4 5

A 19 33 45 55 87 100

Page 53: COP 3503  FALL 2012 Shayan Javed Lecture 15

53 / 81

Search

A sorted: k = 87

New index: I = (3 + N)/2 = 4.

i 0 1 2 3 4 5

A 19 33 45 55 87 100

Page 54: COP 3503  FALL 2012 Shayan Javed Lecture 15

54 / 81

Search

A sorted: k = 87, I = 4

New index: I = (3 + N)/2 = 4. Repeat the process.

i 0 1 2 3 4 5

A 19 55 33 45 87 100

i 0 1 2 3 4 5

A 19 33 45 55 87 100

Page 55: COP 3503  FALL 2012 Shayan Javed Lecture 15

55 / 81

Search

A sorted: k = 87, I = 4

New index: I = (3 + N)/2 = 4. Repeat the process. is 87 == A[I] ?

Yes! We found the index. Return I

i 0 1 2 3 4 5

A 19 55 33 45 87 100

i 0 1 2 3 4 5

A 19 33 45 55 87 100

Page 56: COP 3503  FALL 2012 Shayan Javed Lecture 15

56 / 81

Search

A sorted: k = 87, I = 4

New index: I = (3 + N)/2 = 4. Repeat the process. is 87 == A[I] ?

Yes! We found the index. Return I

This algorithm is called Binary Search

i 0 1 2 3 4 5

A 19 33 45 55 87 100

Page 57: COP 3503  FALL 2012 Shayan Javed Lecture 15

57 / 81

Search

Comparisons for Binary Search: 2

i 0 1 2 3 4 5

A 19 55 33 45 87 100

i 0 1 2 3 4 5

A 19 33 45 55 87 100

Page 58: COP 3503  FALL 2012 Shayan Javed Lecture 15

58 / 81

Search

Comparisons for Binary Search: 2

Comparisons for Linear Search: 5

i 0 1 2 3 4 5

A 19 55 33 45 87 100

i 0 1 2 3 4 5

A 19 33 45 55 87 100

Page 59: COP 3503  FALL 2012 Shayan Javed Lecture 15

59 / 81

Binary Search

For sorted data

Page 60: COP 3503  FALL 2012 Shayan Javed Lecture 15

60 / 81

Binary Search

For sorted data

Idea: Start at the middle index, then halve the search space for each pass.

Page 61: COP 3503  FALL 2012 Shayan Javed Lecture 15

61 / 81

Binary Search

Algorithm:

Page 62: COP 3503  FALL 2012 Shayan Javed Lecture 15

62 / 81

Binary Search

Algorithm:1. Get the middle element M between

index O and N

Page 63: COP 3503  FALL 2012 Shayan Javed Lecture 15

63 / 81

Binary Search

Algorithm:1. Get the middle element M between

index O and N2. if M = k, stop.

Page 64: COP 3503  FALL 2012 Shayan Javed Lecture 15

64 / 81

Binary Search

Algorithm:1. Get the middle element M between

index O and N2. if M = k, stop.3. Otherwise two cases:

Page 65: COP 3503  FALL 2012 Shayan Javed Lecture 15

65 / 81

Binary Search

Algorithm:1. Get the middle element M between

index O and N2. if M = k, stop.3. Otherwise two cases:

1. k < M, repeat from step 1 between O and M

Page 66: COP 3503  FALL 2012 Shayan Javed Lecture 15

66 / 81

Binary Search

Algorithm:1. Get the middle element M between

index O and N2. if M = k, stop.3. Otherwise two cases:

1. k < M, repeat from step 1 between O and M2. k > M, repeat from step 1 between M and N

Page 67: COP 3503  FALL 2012 Shayan Javed Lecture 15

67 / 81

Binary Search

int binarySearch(int[] array, int key, int left, int right){

while (left < right) {

int middle = (left + right)/2; // Compute mid point

if (key < array[mid]) {

right = mid; // repeat search in bottom half

} else if (key > array[mid]) {

left = mid + 1; // Repeat search in top half

} else {

return mid; // found!

}

}

return -1; // Not found

}

Page 68: COP 3503  FALL 2012 Shayan Javed Lecture 15

68 / 81

Binary Search

int binarySearch(int[] array, int key, int left, int right){

while (left <= right) {

int middle = (left + right)/2; // Compute mid point

if (key < array[mid]) {

right = mid; // repeat search in bottom half

} else if (key > array[mid]) {

left = mid + 1; // Repeat search in top half

} else {

return mid; // found!

}

}

return -1; // Not found

}

Page 69: COP 3503  FALL 2012 Shayan Javed Lecture 15

69 / 81

Binary Search

int binarySearch(int[] array, int key, int left, int right){

while (left <= right) {

int middle = (left + right)/2; // Compute mid point

if (key < array[mid]) {

right = mid; // repeat search in bottom half

} else if (key > array[mid]) {

left = mid + 1; // Repeat search in top half

} else {

return mid; // found!

}

}

return -1; // Not found

}

Page 70: COP 3503  FALL 2012 Shayan Javed Lecture 15

70 / 81

Binary Search

int binarySearch(int[] array, int key, int left, int right){

while (left <= right) {

int middle = (left + right)/2; // Compute mid point

if (key < array[mid]) {

right = mid-1; // repeat search in bottom half

} else if (key > array[mid]) {

left = mid + 1; // Repeat search in top half

} else {

return mid; // found!

}

}

return -1; // Not found

}

Page 71: COP 3503  FALL 2012 Shayan Javed Lecture 15

71 / 81

Binary Search

int binarySearch(int[] array, int key, int left, int right){

while (left <= right) {

int middle = (left + right)/2; // Compute mid point

if (key < array[mid]) {

right = mid-1; // repeat search in bottom half

} else if (key > array[mid]) {

left = mid + 1; // Repeat search in top half

} else {

return mid; // found!

}

}

return -1; // Not found

}

Page 72: COP 3503  FALL 2012 Shayan Javed Lecture 15

72 / 81

Binary Search

int binarySearch(int[] array, int key, int left, int right){

while (left <= right) {

int middle = (left + right)/2; // Compute mid point

if (key < array[mid]) {

right = mid-1; // repeat search in bottom half

} else if (key > array[mid]) {

left = mid + 1; // Repeat search in top half

} else {

return mid; // found!

}

}

return -1; // Not found

}

Page 73: COP 3503  FALL 2012 Shayan Javed Lecture 15

73 / 81

Binary Search

Exercise: Implement binary search recursively.

Page 74: COP 3503  FALL 2012 Shayan Javed Lecture 15

74 / 81

Binary Search

Advantages: Fast and efficient

Disadvantages: Has to be sorted first. (Going to look at sorting later)

Page 75: COP 3503  FALL 2012 Shayan Javed Lecture 15

75 / 81

Binary Search Efficiency

O(log2N)

Page 76: COP 3503  FALL 2012 Shayan Javed Lecture 15

76 / 81

Binary Search Efficiency

O(log2N)

Much faster than O(N).

Page 77: COP 3503  FALL 2012 Shayan Javed Lecture 15

77 / 81

Binary Search Efficiency

O(log2N)

Much faster than O(N). For N = 6, at most 2 comparisons. ⌊log2(N) + 1⌋

Page 78: COP 3503  FALL 2012 Shayan Javed Lecture 15

78 / 81

Binary Search Efficiency

O(log2N)

Much faster than O(N). For N = 6, at most 2 comparisons. ⌊log2(N) + 1⌋

N = 1 million.

Page 79: COP 3503  FALL 2012 Shayan Javed Lecture 15

79 / 81

Binary Search Efficiency

O(log2N)

Much faster than O(N). For N = 6, at most 2 comparisons. ⌊log2(N) + 1⌋

N = 1 million. Linear search = 1 million iterations

Page 80: COP 3503  FALL 2012 Shayan Javed Lecture 15

80 / 81

Binary Search Efficiency

O(log2N)

Much faster than O(N). For N = 6, at most 2 comparisons. ⌊log2(N) + 1⌋

N = 1 million. Linear search = 1 million iterations Binary search = 20 iterations

Page 81: COP 3503  FALL 2012 Shayan Javed Lecture 15

81 / 81

Summary

Search is an essential problem.

Linear search for an unsorted array.

Binary search for a sorted array.

Next Lecture: Sorting arrays