Matt Schierholtz
Algorithms
Method for solving problems with finite stepsAlgorithm example:
ErrorCheck for problemSolve problem
Must terminate
Overview
Algorithms vary greatly in either memory space or time required
Sorted into worst, best and average performance
Big O, Big Θ and Big Ω notationO(f(x)) is at most f(x)
Efficiency
Nested loops = 0
for i = 1 to n for j = 1 to i
s = s + j(i – j + 1)next j
next iFind the number of steps this algorithm takesCompare efficiency of particular algorithms
Algorithm Types
Logbx = a is very useful in computer calculationsFor example,
Log21024 = 10 and log21048576 = 20⌊a is the number of bits used to represent a number x in ⌋
binary, or ternary, or any number base ya want
Algorithms and Logarithms
Takes less time than sequential searchTrades time for organization
ExampleWhile (top >= bot and index = 0)
Mid = (bot + top) / 2 ⌊ ⌋If a[mid] = x then index = mid
If a[mid] > xThen top = mid - 1
Else bot = mid + 1End while
Binary Search
Easier to write than Binary searchBut takes more
timeThink recursivelySuppose:
Efficient algorithm for arrays < k known
What if you have array < k?
Merge Sort
Sort smaller parts!Then you merge
More Merge Sort
2 3 5 6 7 8 9
2 5 6 8
3 6 7 9
6
3
9
7
Initial number group
Get sorted
Merge
Algorithms with exponential orderEx: Tower of Hanoi
If it has 64 disksNumber of moves = 264 – 1For a computer, moves / second = 109 This will take 584 years
Tractable & Intractable Problems
Polynomial algorithms are class PThese are tractableEven if they take a very long time
Problems not solved in polynomial time are…IntractableClass NP (nondeterministic polynomial)$1,000,000, to anyone who proves P=NP
NP-CompleteIf one NP problem is solvable, all of them are
P vs. NP