matt schierholtz. method for solving problems with finite steps algorithm example: error check for...

10
Matt Schierholtz Algorithms

Upload: theodore-blankenship

Post on 29-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Matt Schierholtz. Method for solving problems with finite steps Algorithm example: Error Check for problem Solve problem Must terminate

Matt Schierholtz

Algorithms

Page 2: Matt Schierholtz. Method for solving problems with finite steps Algorithm example: Error Check for problem Solve problem Must terminate

Method for solving problems with finite stepsAlgorithm example:

ErrorCheck for problemSolve problem

Must terminate

Overview

Page 3: Matt Schierholtz. Method for solving problems with finite steps Algorithm example: Error Check for problem Solve problem Must terminate

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

Page 4: Matt Schierholtz. Method for solving problems with finite steps Algorithm example: Error Check for problem Solve problem Must terminate

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

Page 5: Matt Schierholtz. Method for solving problems with finite steps Algorithm example: Error Check for problem Solve problem Must terminate

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

Page 6: Matt Schierholtz. Method for solving problems with finite steps Algorithm example: Error Check for problem Solve problem Must terminate

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

Page 7: Matt Schierholtz. Method for solving problems with finite steps Algorithm example: Error Check for problem Solve problem Must terminate

Easier to write than Binary searchBut takes more

timeThink recursivelySuppose:

Efficient algorithm for arrays < k known

What if you have array < k?

Merge Sort

Page 8: Matt Schierholtz. Method for solving problems with finite steps Algorithm example: Error Check for problem Solve problem Must terminate

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

Page 9: Matt Schierholtz. Method for solving problems with finite steps Algorithm example: Error Check for problem Solve problem Must terminate

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

Page 10: Matt Schierholtz. Method for solving problems with finite steps Algorithm example: Error Check for problem Solve problem Must terminate

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