Array of Numbers - Discrete Mathematics - Lecture Slides, Slides of Discrete Mathematics

During the study of discrete mathematics, I found this course very informative and applicable.The main points in these lecture slides are:Array of Numbers, Iterative Algorithms, Proof of Correctness, Reasonable Output for Function, Minimum Element of Array, Loop Counter, Running Time of Algorithm, Algorithm Complexity, Linear Search Algorithm

Typology: Slides

2012/2013

Uploaded on 04/27/2013

ashwini
ashwini 🇮🇳

4.5

(18)

167 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 173:
Discrete Mathematical Structures
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Array of Numbers - Discrete Mathematics - Lecture Slides and more Slides Discrete Mathematics in PDF only on Docsity!

CS 173:

Discrete Mathematical Structures

Algorithms

Algorithm MAX

Input: x 1 , x 2 , …, x n, an array of numbers

Output: y, the maximum of x 1 , x 2 , …, x n

1. for j = 1 to n-

2. if x j > x j+1 then

3. temp = x j+

4. x j+1 = xj

5. xj = temp

Complexity is O(n)

vars x 1 x 2 x 3 x (^4) input 3 2 4 1 j = 1 3  2  4 1 j = 2 2 3  4  1 j = 3 2 3 4  1  final 2 3 1 4

Algorithms

Algorithm MAX Input: x 1 , x 2 , …, x (^) n, an array of numbers Output: y, the maximum of x 1 , x 2 , …, x (^) n

  1. for j = 1 to n-
  2. if xj > xj+1 then
  3. temp = xj+
  4. xj+1 = xj
  5. xj = temp

Prove that x j+1 = max{x 1 , x 2 , …, x j+1 }

after the j th^ iteration of the loop.Note that the last

iteration (j = n-1) gives the result we really want.

Algorithms

Algorithm MAX Input: x 1 , x 2 , …, x (^) n, an array of numbers Output: x (^) n, the maximum of x 1 , x 2 , …, x (^) n

  1. for j = 1 to n-
  2. if xj > xj+1 then
  3. temp = xj+
  4. xj+1 = xj
  5. xj = temp

Base case (j = 0 th^ iteration): x 1 = max{x 1 }

IH: assume assertion holds for j = n th^ iteration.

On n+1 st^ iteration, x (^) n+1 is compared with x (^) n+2 and max is swapped into x (^) n+.

But x (^) n+2 = max{x (^) n+1 , x (^) n+2 } = max{ x 1 , x 2 , …, x (^) n+1, x (^) n+2} by IH.

Algorithms

Algorithm BUBBLE

Input: x 1 , x 2 , …, x (^) n, an array of numbers

Output: ??

  1. for j = n downto 2
  2. MAX(x 1 ,x 2 ,…,xj)
  3. output ??

The running time of this algorithm is:

A. O(n log n)

B. O(n)

C. O(n^2 )

D. None of the above.

Algorithm Complexity

How long does the “linear search algorithm” take?

Suppose the data is (2, 8, 3, 9, 12, 1, 6, 4, 10, 7) and we’re looking for # (^1284)

The running time of the algorithm depends on the particular input to the problem. In this case we have two different complexity measures: Worst case complexity - running time on worst input Average case - average running time among all inputs

Worst case for linear search is time n.

Average case for linear search is time (1+2+…+n)/n = n(n+1)/2n = (n+1)/2. Both are O(n)

Algorithm Complexity

How long does the “binary search algorithm” take?

Binary search

Input: a sorted array of numbers a 1 , a 2 , … an and a number x

Output: position i where ai = x, if x is in the list

  1. i = 1, j = n;
  2. while i < j

3. m = (i + j)/2; /* midpt of range (i,j)*/

  1. if x > am then i = m + 1
  2. else j = m
  3. if x = ai then output “x is in position ai ”
  4. else output “x is not in list”

If n is a power of 2, n = 2 k^ , then k = lg n iterations occur.

If n is not a power of 2, let k be the number so that 2k < n < 2 k+1^ , and imagine that the array has 2 k+1^ elements. Then k+1 < lg n + 1 = O(log n)

Running times

It’s fun to make comparisons about the running times of algorithms of various complexities.

Inp size

compxity

n .00001s .00002s .00003s .00004s .00005s .00006s

n^2 .0001s .0004s .0009s .0016s .0025s .0036s

n^5 .1s 3.2s 24.3s 1.7m 5.2m 13m

3 n^ .059s 58m 6.5y 3855c 2x10 8 c 1.3x^

13 c

But computers are getting faster! Maybe we can do better.

A dangling complexity question.

Why would we ever use “worst case complexity”?

Advantages: Easiest to analyze. Bounds the worst possible thing that could happen (conservative). Don’t have to decide or assume what typical inputs are (or find a distribution on inputs).

Disadvantage: Bizarre inputs could make worst-case running time seem horrible, when most inputs terminate in reasonable time.

On to recursive algorithms…

Factorial (n)

Input: an integer n > 0.

Output: n!

  1. If n = 1 then output 1
  2. else
  3. output n x Factorial(n-1)

Let T(n) denote the running time of the algorithm on input of size n.

T(n) = C + T(n-1)

T(1) = c

T(n) = C + (C + T(n-2))

= C + (C + (C + T(n-3))) … = nC = O(n)

On to recursive algorithms…

Now let’s analyze the running time.

T(n) = 2 T(n-1) + C Unroll a bit…

= 2 (2 T(n-2) + C) + C = 4 T(n-2) + 3C

= 4 (2 T(n-3) + C) + 3C = 8 T(n-3) + 7C

… = 2k^ T(n-k) + (2 k-1)C What is k when this bottoms out at 1?

T(1) = C

= 2 n-1^ T(1) + (2 n-1-1)C = (2n-1) C

= O(2n)

More general techniques investigated in a couple weeks

On to recursive algorithms…

Is the algorithm correct?

Does it do the right thing for 1 disk?

Assume it does the right thing for n-1 disks. (IH)

Yes

And finally, it DOES do the right thing for n disks: move n-1 out of the way, move the biggest disk, move n-1 back.

This doesn’t feel satisfying because we think about recursion inductively to begin with (as we should!).

On to recursive algorithms…

Write down the recurrence relation:

T(n) = 2 T(n/2) + cn

We’ll talk about techniques for solving it later.

On to recursive algorithms…

Recursive function for computing the nth Fibonacci number:

Iterative function for computing the nth Fibonacci number:

Which of these is better?Docsity.com