Analysis of Array Max Algorithm in Python: Big-O Notation and Induction, Lecture notes of Computer Science

An analysis of the array max algorithm in python, explaining its purpose, consumption, production, and operation counts. It also includes big-o notation analysis and proof of induction. Both recursive and non-recursive approaches.

Typology: Lecture notes

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv 🇮🇳

4.3

(12)

194 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Operation Counting, Big-O and
(maybe) Induction
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download Analysis of Array Max Algorithm in Python: Big-O Notation and Induction and more Lecture notes Computer Science in PDF only on Docsity!

Operation Counting, Big-O and

(maybe) Induction

array max - python - non-recursive

# Note: Python uses 0 based indexing, so we've switched.

#!/usr/bin/python

def array_max(n, array) :

"""array_max: num * num[pos] -> num

Purpose: Finds the maximum value of the first n elements in

an array

Consumes: a nonnegative number n and an array of numbers

with at least n elements

Produces: a number

Example: array_max(4, [75, 4, 46, 12]) -> 75

m = array[0] # set m to the first element of the array

for i in range (1, n ): # for the rest of the

# elements

m = max(m, array[i]) # set m to the larger of itself

# and element we're checking

return m

What is the slope of the line: T( n ) = 5 + 4 * ( n – 1) A. ¼ B. 4/ C. 2 D. 4 E. 5

#!/usr/bin/python def array_max(n, array): """array_max: num * num[pos] -> num Purpose: Finds the maximum value of the first n elements in an array Consumes: a positive number n and an array of numbers with at least n elements Produces: a number Example: array_max(4, [75, 4, 46, 12]) -> 75 """ if n == 1: # if we are looking for the max of 1

element

return array[0] # return that element else: # otherwise, return the max of the last element

and the result of a recursive call to array_max

return max(array[n-1], array_max(n - 1, array)) array max - python - recursive

array max - python - recursive - op counts

Let A(n) = # of ops in array max(n, array) for any n

def array_max(n, array) : if n == 1: 1 op: comparison return array[0] 1 op: value return else: 0 ops: comparison already done return max(array[n-1], array_max(n - 1, array)) 4 ops: value return, math (max), function call, math (-) + A(n-1), the number of ops for array_max(n - 1, array)

  • A(1) = 2
  • A(n) = 5 + A(n-1)
  • How can we solve for A(n)?

What is the slope of the line: A( n ) = 5 * ( n - 1) + 2 A. ¼ B. 4/ C. 2 D. 4 E. 5

What is the slope of the line: A( n ) = 5 * ( n - 1) + 2 A. ¼ B. 4/ C. 2 D. 4 E. 5  5n – 3 O(n) recursive  4n + 1 O(n) iterative

Big O

• Seamcarve algs

– O (

sqrt (n)

– O(n)

Big O

• Seamcarve algs

– O (

sqrt (n)

– O(n)

Big O

  • f = O(g) f,g: Z + ->Z +
  • f(n) ≤ c g(n)

Big O

  • A(n) = 5 * (n-1) + 2
  • Prove A = O(n)
  • 5 * (n-1) + 2 ≤ c * n
  • 5 * n – 3 ≤ 5 * n // c=
  • So, A = O(n)