Algorithms - PowerPoint PPT Presentation

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

The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
Title:

Algorithms

Description:

Algorithm. A structured method of solving a problem. Assertions ... Represents the amount of work an algorithm does in order to process n elements. Example: ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 39
Provided by: joe49
Category:
Tags:
Transcript and Presenter's Notes

Title: Algorithms

1
Algorithms
• Introductory Discrete Mathematics (CS/MAT165)

2
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

3
Statements
• Assignment
• Decision
• if-then-else
• Loops
• while
• repeat
• for

4
Assignment
• Associates a value with a variable
• Example
• X 5
• X is assigned the value 5

5
Assertion
• A predicate associated with a specific point in
an algorithm
• Example
• X 10
• x 10
• X X 5
• x 15

6
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

7
Decisions
• if Score lt 60
• What must be true for the then-statement to be
executed?
• The else-statement?

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

9
Try this
• If Score lt 80 then Grade F else if Score lt

10
Loops
• While
• Repeat
• For

11
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

12
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?

13
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

14
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

15
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?
16
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
17
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

18
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

19
Big-?
• g(n) is a function that hugs f(n) just above for
all n ? N

20
Try this
• Given f(n) n2
• Which of the following could be g(n)?
• n
• n3
• 1/n

21
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

22
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

23
One more time
• Given f(n) n2
• Which of the following could be g(n)?
• n
• n3
• 1/n

Prove it!
24
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

25
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
26
Nested Loops
• Q How many times will the body execute in the
following algorithm?
• while 1 ? a ? n ltbodygtend while
• A n times

27
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

28
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?

29
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

30
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

31
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

32
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

33
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

34
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?

35
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

36
Merge Sort
• Divide the unsorted list into two sublists of
• 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

37
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

38
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

?(?)