Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Algorithms - 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:Algorithms, Finite Set of Precise Instructions, Performing Computation, Maximum Element, Linear Search, Binary Search, Endpoint of Search Interval, Binary Search Running Time, Growth of Functions, Measure Algorithms

Typology: Slides

2012/2013

Uploaded on 04/27/2013

ashwini
ashwini 🇮🇳

4.5

(18)

177 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSci 2011
Discrete Mathematics
Lecture 12
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Algorithms - Discrete Mathematics - Lecture Slides and more Slides Discrete Mathematics in PDF only on Docsity!

CSci 2011

Discrete Mathematics

Lecture 12

3.1 Algorithms

Algorithm 1: Maximum element

Algorithm for finding the maximum element
in a list:
procedure max ( a 1 , a 2 , …, an : integers)
max := a 1
for i := 2 to n
if max < ai then max := ai
{ max is the largest element}
How long does this take?
If the list has n elements, worst case
scenario is that it takes n “steps”

Algorithm 2: Linear search

Given a list, find a specific element in the list

 List does NOT have to be sorted!

procedure linear_search ( x : integer; a 1 , a 2 , …, an : integers) i := 1 while ( in and xai ) i := i + 1 if in then location := i else location := 0

{ location is the subscript of the term that equals x, or it is 0 if x is not found}

Binary search running time

How long does this take (worst case)?
If the list has 8 elements

 It takes 3 steps

If the list has 16 elements

 It takes 4 steps

If the list has 64 elements

 It takes 6 steps

If the list has n elements

 It takes log 2 n steps

ch3.

Growth of Functions

Binary Search running time

 The binary search takes log 2 n “steps”

 Let’s say the binary search takes the following number of steps on specific CPUs:  Intel Pentium IV CPU: 58* log 2 n /  Motorola CPU: 84.4(log 2 n + 1)/  Intel Pentium V CPU: 44(log 2 n)/

Notice that each has an log 2 n term

 As n increases, the other terms will drop out

As processors change, the constants will always
change

 The exponent on n will not

Big-Oh notation

If f(x) and g(x) are two functions of a single
variable, the statement f(x)∈O(g(x)) means that
∃k∈ R , ∃c∈ R , ∀x∈ R , x>k ⇒ 0 ≤|f(x)|≤c|g(x)|.
Note: O(g(x)): a set of functions
Informally

 c g(x) is greater than f(x) for sufficiently large x.  f(x) grows no faster than g(x), as x gets large.

How proof goes

 Need to find k and c to show f(x)∈O(g(x)).

Conventionally people use f(x)=O(g(x)).

Big-Oh proofs

A variant of the last question

Show that f ( x ) = 3 x +7 is O ( x^2 )

 In other words, show that 3 x +7 ≤ c* x^2

Function growth rates

 For input size n = 1000

 O(1) 1

 O(log n) ≈

 O(n) 103

 O(n log n) ≈10^4

 O(n 2 ) 106

 O(n 3 ) 109

 O(n 4 ) 1012

 O(n c^ ) 10 3*c^ c is a consant

 2 n^ ≈10^301

 n! ≈10^2568

 n n^103000 Many interesting
problems fall into
this category

More Examples

Show that x log x is O(x^2 ).

Proof) Take c = 1, k=1. Then, x 2 > x log x for all x > 1, since x log x = 0 when x = 1, while x 2 = 1.

Show that x^2 is not O(x log x).

Proof) Suppose x^2 is O(x log x). Then ∃ c and k such that c x log x ≥ x 2 for x > k. Note that c>2. Otherwise, cx log x < x 2 for x = 3. Consider x = c 3. Then lhs) cx log x = c 4 log c 3 = 2 c 4 log c rhs) x 2 = c 6. Since c>2, it is clear that c 2 > 2 log c. Therefore, cxlog x<x 2 for x=c 3. (Contradiction)

Little bit of Complexity

 Efficiency of your algorithm? Given input size,

 Time complexity: Time used by your computer to solve the problem  We use number of operations…  Memory (space) complexity: Memory used by your computer to solve the problem  We use number of variables allocated…

 Example: Find the maximal element in a set

max = a 1 , i = 2 While (i ≤ n) if (max < a (^) i ) max = a (^) i i++  Time complexity: 2 n - 2 comparison, n-1 addition, n+ assignment (worst case) = 4n - 2 operation = θ(n)  Memory complexity: 2 = θ(1)

 Other example: Hash Chain