algorithm

a sequence of unambiguous instructions for solving a problem

principles of an algorithm

finiteness, difiniteness, input, output, effectiveness

euclid's algorithm

gcd(m, n) = gcd(n, m % n)

steps in design and analysis of algorithms

- understand the problem

- choose between approximate and exact problem solving

- decide on data structures

- algorithm design technique

- methods of specifying an algorithm

- prove algorithm correctness

- analyze the algorithm

- code the algorithm

- choose between approximate and exact problem solving

- decide on data structures

- algorithm design technique

- methods of specifying an algorithm

- prove algorithm correctness

- analyze the algorithm

- code the algorithm

why study algorithms

theoretical importance, practical importance

algorithm design strategies

- brute force

- divide and conquer

- decrease and conquer

- transform and conquer

- greedy approach

- dynamic programming

- backtracking and brand and bound

- space and time tradeoffs

- divide and conquer

- decrease and conquer

- transform and conquer

- greedy approach

- dynamic programming

- backtracking and brand and bound

- space and time tradeoffs

worst case

maximum over inputs of size n -> O(n)

best case

minimum over inputs of size n -> O(1)

average case

expected number of basic operations repetitions considered as a random variable under some assumption about the probability distribution of all possible inputs of size n

T(n) < g(n)

0

T(n) = g(n)

c>0

T(n) > g(n)

infinity

steps in mathematical analysis of nonrecursive algorithms

- decide on input size

- identify algorithm's basic operation

- determine worst, average, and best cases for input size n

- set up summation for C(n) reflecting algorithm's loop structure

- simplify summation using standard formulas

- identify algorithm's basic operation

- determine worst, average, and best cases for input size n

- set up summation for C(n) reflecting algorithm's loop structure

- simplify summation using standard formulas

steps in mathematical analysis of recursive algorithms

- decide on input size

- identify algorithm's basic operation

- determine worst, average, and best cases for input size n

- set up a recurrence relation and initial condition(s) for C(n)

- solve the recurrence to obtain a closed form or estimate the order of magnitude of the solution

- identify algorithm's basic operation

- determine worst, average, and best cases for input size n

- set up a recurrence relation and initial condition(s) for C(n)

- solve the recurrence to obtain a closed form or estimate the order of magnitude of the solution

t(n) = t(n-1) + c; t(1) = d

t(n) = (n-1)c + d

t(n-1) +cn; t(1) = d

t(n) = [n(n+1)/2 - 1] c + d

t(n) = t(n/2) + c; t(1) = d

t(n) = c lg n + d

t(n) = 2t(n/2) + cn; t(1) = d

t(n) = cn lg n + dn

brute force

a straightforward approach usually based on problem statement and definitions of the concepts

selection sort

sort for the smallest in a list of size n and swap it with the first element in the list, reduce the list size by 1 and repeat until n-1 passes are made

bubble sort

compare adjacent elements and swap if out of order, repeat n-1 times.

brute force strengths

wide applicability - simplicity, yields reasonable algorithms for some important problems, yields standard algorithms for simple computational tasks

brute force weaknesses

rarely yields efficient algorithms, some brute force algorithms unacceptably slow, not as constructive/creative as some other design techniques.

convex

a set of points in the plane are called convex if for any two points p and q in the set, the entire line segment with endpoints at p and q belongs to the set.

convex hull

the smallest convex set containing the set S

undirected graph

edges are unordered pairs (v0, v1) = (v1, v0)

complete graph

a graph with the maximum number of edges

decrease and conquer

1) reduce problem instance to smaller instance of the same problem and extend solution

2) solve smaller instance

3) extend solution of smaller instance to obtain solution to original problem

2) solve smaller instance

3) extend solution of smaller instance to obtain solution to original problem

Russian peasant multiplication

if n is even: n/2 * 2m

if n is odd: (n-1)/2 * 2m + m

if n is odd: (n-1)/2 * 2m + m

interpolation search

like binary search except use linear interpolation on l and r to get the comparison value: x = l + floor[((v-A[l])(r-l)) / A[r]-A[l])]

l is left boundary, r is right boundary, v is search value

loglogn + 1

l is left boundary, r is right boundary, v is search value

loglogn + 1