Loading...

PPT – Algorithms PowerPoint presentation | free to view - id: 9f4a2-ZWQ3M

The Adobe Flash plugin is needed to view this content

Algorithms

- Introductory Discrete Mathematics (CS/MAT165)

Algorithm

- A structured method of solving a problem
- Assertions
- Pre-condition
- Initial state represented as a predicate
- Post-condition
- Final state represented as a predicate

Statements

- Assignment
- Decision
- if-then-else
- Loops
- while
- repeat
- for

Assignment

- Associates a value with a variable
- Example
- X 5
- X is assigned the value 5

Assertion

- A predicate associated with a specific point in

an algorithm - Example
- X 10
- x 10
- X X 5
- x 15

Decisions

- Also called a branch
- Indicates one or two statements to execute based

on the value of a condition - If condition then then-statement else

else-statement - Example
- If X lt 5 then Y 1 else Y 2

Decisions

- if Score lt 60
- then Grade NP
- else Grade P
- What must be true for the then-statement to be

executed? - The else-statement?

Try this

- What if the grade breakdown is as follows
- A 90 100B 80 89F lt80
- What would the decision statement look like?

Try this

- If Score lt 80 then Grade F else if Score lt

90 then Grade B else Grade A

Loops

- While
- Repeat
- For

While Loop

- while (guard) body end while
- Guard Boolean expression
- Sometimes called a condition
- Body is repeated (iterated) as long as the guard

is true

While Loop

- X 1
- while x lt 10
- x x 1
- end while
- Q What is the value of X after the loop has

terminated?

Loop Invariant

- Predicate with domain a set of integers
- For each iteration of the loop,if the predicate

is true before the iteration,then it is true

after the iteration

Loop Invariant

- If the following are true, then the loop is

correct with respect to its pre- and

post-conditions - It is true before the first iteration
- If the loop terminates after a finite number of

iterations, the truth of the loop invariant

insures the truth of the post-condition

Try this

- Prove the correctness of the following
- Pre-condition A and B are integers,A gt B ? 0,

a A, b B, r B - while (b ? 0) r a mod b a b b rend

while - Post-condition a gcd(A,B)

Loop invariant?

For Loop

- Every for-loop can be re-written as a while-loop

for x 1 to 5 ltbodygtend for

x 1while (x ? 5) ltbodygt x x 1end while

Timing Function

- f(n)
- Represents the amount of work an algorithm does

in order to process n elements - Example
- for x 1 to n ltbodygtend for
- f(n) Cn

Big-?

- Upper bound
- A function that g(n) that approximates a timing

function f(n) - f(n) ?(g(n)) ??N, ?C such that f(n) ? Cg(n),

?n ? N

Big-?

- g(n) is a function that hugs f(n) just above for

all n ? N

Try this

- Given f(n) n2
- Which of the following could be g(n)?
- n
- n3
- 1/n

Try this

- Find a suitable big-?, g(n), for each of the

following - f(n) n n2 n3
- f(n) 1/n
- f(n) ln(n) n
- General ruleGiven a function consisting of the

sum of terms, a good g(n) candidate is the term

that grows the quickest

Proving Big-?

- Given f(n) n n2
- Try g(n) n2
- Proof
- N 1
- f(n) ? Cg(n)n n2 ? (C)(n2)1/n1 ? C1/1

1 ? C2 ? C

One more time

- Given f(n) n2
- Which of the following could be g(n)?
- n
- n3
- 1/n

Prove it!

Common Big-?s

- ?(1) constant
- ?(log n) logarithmic
- ?(n) linear
- ?(n log n) linearithmic
- ?(nc), c gt 1 polynomial
- ?(cn), c gt 1 exponential
- ?(n!) factorial

Power Functions

- f(n) nc
- Polynomials are made from power functions

c gt 1

c lt 0

c 0

0 lt c lt 1

c 1

Nested Loops

- Q How many times will the body execute in the

following algorithm? - while 1 ? a ? n ltbodygtend while
- A n times

Nested Loops

- Q How many times will the body execute in the

following algorithm? - while 1 ? a ? n while 1 ? b ? n ltbodygt end

whileend while - A n2 times

Nested Loops

- Q How many times will the body execute in the

following algorithm? - while 1 ? a ? n while 1 ? b ? a ltbodygt end

whileend while - Loop invariant?

Nested Loops

- Q How many times will the body execute in the

following algorithm? - while 1 ? a ? n while 1 ? b ? 5 ltbodygt end

whileend while

Big-?

- Lower bound
- A function that g(n) that approximates a timing

function f(n) - f(n) ?(g(n)) ??N, ?C such that f(n) ? Cg(n),

?n ? N

Big-?

- A function that g(n) that approximates a timing

function f(n) - f(n) ?(g(n)) ??N, ?A ,?B such that Bg(n) ?

f(n) ? Ag(n), ?n ? N

Properties

- If f(n) is O(g(n)) and g(n) is O(h(n)),then f(n)

is O(h(n)) (transitivity) - If f(n) is O(h(n)) and g(n) is O(h(n)),then

f(n)g(n) is O(h(n)) - The function ank is O(nk)
- The function nk is O(nkj) for any positive j

Properties

- If f(n) cg(n), then f(n) is O(g(n))
- The function loga n is O(logb n) for any positive

number a and b ? 1 - loga n is O(lg n) for any positive a ? 1,where

lg n log2 n

Insertion Sort

- Simple to implement
- Efficient on (quite) small data sets
- Efficient on data sets which are already

substantially sorted it runs in ?(n d) time,

where d is the number of inversions - Otherwise ?(n2)
- Why?

Insertion Sort

- subroutine insert (a, length, value)i length

1while (i ? 0 ? ai gt value) ai 1

ai i i 1end whileai 1 value - subroutine insertionSort (a, length)i 0while

(i lt length) insert(a, i, ai) i i 1end

while

Merge Sort

- Divide the unsorted list into two sublists of

about half the size - Sort each of the two sublists recursively until

we have list sizes of length 1, in which case the

list itself is returned - Merge the two sorted sublists back into one

sorted list

Merge Sort

- function mergesort(m) var list left, right,

result if length(m) 1 return m else

middle length(m) / 2 for each x in m up to

middle add x to left for each x in m after

middle add x to right left mergesort(left)

right mergesort(right) result merge(left,

right) return result

Merge Sort

- function merge(left,right) while (length(left))

gt 0 ? (length(right) gt 0) if first(left)

first(right) append first(left) to

result left rest(left) else append

first(right) to result right rest(right) end

ifend while if length(left) gt 0 append

rest(left) to resultend if if length(right) gt

0 append rest(right) to resultend if return

result

?(?)