lecture 4 sept 4 goals: chapter 1 (completion)

25
Lecture 4 Sept 4 Goals: • chapter 1 (completion) • 1-d array examples •Selection sorting •Insertion sorting •Max subsequence sum •Algorithm analysis (Chapter 2)

Upload: gaius

Post on 07-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2). Parameter passing call by value and reference: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Lecture 4 Sept 4

Goals:• chapter 1 (completion)

• 1-d array examples•Selection sorting•Insertion sorting•Max subsequence sum

•Algorithm analysis (Chapter 2)

Page 2: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Parameter passing• call by value and reference:

What are the outputs of the following program?

#include <iostream>

using namespace std;

int f(int x) { x++; cout << x << endl; return x*x; }

int main(){

int x = 5; cout << f(x) << endl; cout << x << endl;

}

#include <iostream>

using namespace std;

int f(int& x) { x++; cout << x << endl; return x*x; }

int main(){

int x = 5; cout << f(x) << endl; cout << x << endl;

}

Page 3: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Parameter passing

• In c++, all parameter passing is done by value unless & is used preceding the name of the variable.

• Advantage of call by value:• safer• easier to understand and debug

• Disadvantage of call by value:• not efficient (copying costs both in time and space)• especially a problem when a large object is passed as parameter.

• call by reference is complementary

Page 4: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Parameter passing

• call by const reference:• getting advantages of both conventions• should be used when passing objects (especially when it is large)

• call by const reference:

• getting advantages of both conventions• should be used when passing objects (especially when it is large)

•Constant return value from a function (The returned object can’t be modified.)

• Section 1.6 templates

Page 5: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Implementation of a list class

• list class with the following functionality:

• list is singly-linked, with a head pointer

• constructor(s)

• functions:• insert(int item, int posn)• length()• delete(int posn)• search(int item)

Page 6: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Implementing insert

To insert a key x as the k-th key

a k g c

x = ‘b’ and k = 5

a k g c

b

Page 7: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Recursive version of insert

void insert(int x, int k) { // insert x as the k-th item of the list // assume that the list has at least k – 1 items Node* tmp = new Node(k); if (k == 1) { tmp->next = head; head = tmp; } else { List temp(head->next); temp.insert(x, k – 1); }}

Page 8: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Insertion sorting

At the start of the k-th cycle: A[0 .. k–1] is sorted. At the end of the k-th cycle: A[k] is inserted into correct place so that A[0 .. k] is sorted.

Example: k = 5

Before: 1 5 9 13 21 11 23 20 4 31 8

After: 1 5 9 11 13 21 23 20 4 31 8

Page 9: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Insertion step

temp = A[j];for (k = j – 1; k >= 0 && A[k] > temp; --k) A[k+1] = A[k];A[k+1] = temp;

Page 10: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Insertion sorting

Repeat the insertion cycle for j = 1, 2, 3, …, n – 1.

The complete code is as follows:

for (j = 1; j < n; ++j) { temp = A[j]; for (k = j – 1; k >= 0 && A[k] > temp; --k) A[k+1] = A[k]; A[k+1] = temp; }

Page 11: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

The following link contains an applet to animate various sorting (and other) algorithms:

http://math.hws.edu/TMCM/java/xSortLab/

A screen shot is shown below:

Page 12: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Selection SortSelection Sorting Algorithm:• • During the j-th pass (j = 0, 1, …, n – 2), we will examine the elements of the array a[j] , a[j+1], …, a[n-1] and determine the index min of the smallest key. • Swap a[min] and a[j].

Reselection_sort(vector<int> a) { if (a.size() == 1) return; for (int j = 0; j < n – 1; ++j) { min = j; for (int k= j+1; k<=n-1; ++k) if (a[k] < a[min]) min = k; swap a[min] and a[j]; } }

Selection sorting animation can be found in the same site.

Page 13: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Algorithm analysis

• Analysis is the process of estimating the number of computational steps performed by a program (usually as a function of the input size).

• Useful to compare different approaches. Can be done before coding. Does not require a computer (paper and pencil).

• Order of magnitude (rough) estimate is often enough. We will introduce a notation to convey the estimates. (O notation).

Page 14: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Insertion sorting - analysis

for (j = 1; j < n; ++j) { temp = A[j]; for (k = j – 1; k >= 0 && A[k] > temp; --k) A[k+1] = A[k]; A[k+1] = temp; }

How many comparisons are performed?

• Consider the j-th iteration of the inner loop.

• In the worst-case, the loop will be iterated j times.

• Each time, two comparisons are performed. Total = 2j

Page 15: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

First iteration performs 2 x 1 comparisons

Second iteration performs 2 x 2 comparisons

. . . .

n – 1 th iteration performs 2 x (n – 1) comparisons

Total number of comparisons performed by insertion sorting

= 2 x (1 + 2 + … + n – 1) = n (n – 1) ~ n2 comparisons

Note the approximation we used:

When n = 1000, n2 = one million, n2 – n = 999000 so the error in our estimate is 0.1%

Page 16: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Analysis of selection sorting

Consider the program to find the min number in an array:

min = a[0]; for (j = 1; j < n; ++j) if (A[j] > min) min = A[j];

The number of comparisons performed is n – 1.

loop starts with j = 1 and ends with j = n so the number of iterations = n – 1.

In each iteration, one comparison is performed.

Page 17: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Selection sorting – analysis

The inner loop:

n – 1 comparisons during the first iteration of the inner loop

n – 2 comparisons during the 2nd iteration of the inner loop . . . .

1 comparison during the last iteration of the inner loop

Total number of comparisons = 1 + 2 + … + (n – 1) =

n(n – 1)/ 2

Page 18: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

General rule

• If there is a nested loop, analyze the inner loop and calculate the number of operations performed by the inner loop.

• Suppose the j-th iteration of the inner loop performs f(j) operations

• Suppose the value of j in the outer loop goes from 1 to m, then the total number of operations is

f(1) + f(2) + … + f(m)

Page 19: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Exercise: What is the number of operations (additions) by the following program?

for (j = 100; j < 200; ++j) if (j % 2 == 0) sum+= A[j];

Page 20: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Exercise: What is the number of operations (additions) by the following program?

for (j = 100; j < 200; ++j) if (j % 2 == 0) sum+= A[j];

Answer: The number of iterations of the loop = 100,But the addition is performed in every other iteration so the number of additions performed = 50.

Page 21: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

O (order) notationDefinition: Let f(n) and g(n) be two functions defined on the set of integers. If there is a c > 0 such that f(n) <= c g(n) for all large enough n. Then, we say f(n) = O(g(n)).

Example: n2 + 2n – 15 is O(n2)

Rule: When the expression involves a sum, keep only the term with the highest power, drop the rest. You can also drop constant multiplying terms.

(3n2 + 2 n + 1) (4 n – 5) is O(n3)

Page 22: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Max Subsequence Problem Given a sequence of integers A1, A2, …, An, find the

maximum possible value of a subsequence Ai, …, Aj.

Numbers can be negative.

You want a contiguous chunk with largest sum.

Example: -2, 11, -4, 13, -5, -2 The answer is 20 (subsequence A2 through A4).

We will discuss three different algorithms, with time complexities O(n3), O(n2), and O(n).

With n = 106, algorithm 1 may take > 10 years; algorithm 3 will take a fraction of a second!

Page 23: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

int maxSum = 0;

for( int i = 0; i < a.size( ); i++ )for( int j = i; j < a.size( ); j++ ){

int thisSum = 0;for( int k = i; k <= j; k++ )

thisSum += a[ k ];if( thisSum > maxSum )

maxSum = thisSum;}return maxSum;

Algorithm 1 for Max Subsequence Sum Given A1,…,An , find the maximum value of Ai+Ai+1+···+Aj

0 if the max value is negative

Time complexity: O(n3)

)1(O

( )O j i

)1(O

)1(O

)1(O

1

( ( ))n

j i

O j i

1 1

0

( ( ))n n

i j i

O j i

Page 24: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Algorithm 2 Idea: Given sum from i to j-1, we can compute

the sum from i to j in constant time. This eliminates one nested loop, and reduces the

running time to O(n2).

into maxSum = 0;

for( int i = 0; i < a.size( ); i++ )int thisSum = 0;for( int j = i; j < a.size( ); j+

+ ){ thisSum += a[ j ]; if( thisSum > maxSum )

maxSum = thisSum;}

return maxSum;

Page 25: Lecture 4                                                   Sept 4 Goals:  chapter 1 (completion)

Algorithm 3Algorithm 3

int maxSum = 0, thisSum = 0;

for( int j = 0; j < a.size( ); j++ ){

thisSum += a[ j ];

if ( thisSum > maxSum )maxSum = thisSum;

else if ( thisSum < 0 )thisSum = 0;

}return maxSum

The algorithm resets whenever prefix is < 0. Otherwise, it forms new sums and updates maxSum in one pass.