Complexity © 2014 Project Lead The Way, Inc.Computer Science and Software Engineering.

Post on 08-Jan-2018

217 views 0 download

description

Time efficiency: How much time does a program take to run? Time complexity of a solution: How much more time will a program take to run if given more bytes as input? How Fast Is a Solution?

transcript

Complexity

© 2014 Project Lead The Way, Inc.Computer Science and Software Engineering

• Constraints:–Limited time to execute–Limited bandwidth –Limited memory to store the

program–Limited memory for input/output

data• These constraints trade off against

each other• Often readability is more important

than any of these

Computers Have Finite Resources

• Time efficiency: How much time does a program take to run?

• Time complexity of a solution: How much more time will a program take to run if given more bytes as input?

How Fast Is a Solution?

• Time complexity described by the type of curve showing time vs. input bytes

• Brute force, an algorithm in which you try all possibilities, takes exponentially longer with longer passwords

How Fast Is a Solution?

• Consider passwords with a-z,A-Z,0-9 which allows 26+26+10=62 characters

Why Is Brute Force Exponential?

bytes passwords to try

1 622 622

3 623

4 624

n 62n

• Exponential growth will outpace any other kind of growth

• Make brute force impractical by using long passwords

Exponential Curve Gets Ahead of Others

• Time complexity of a problem is the time complexity of its best solution

• The algorithm might be quick with special input even if it is long. We describe difficulty using the worst-case input.

How Hard Is a Problem?

• Some problems are so hard the program might never finish.

• Computable problems can be solved by a computer

• Analyzing programs is often uncomputable–Halting Theorem: "Will this program

ever finish?" is sometimes uncomputable

–Security analysis: "Does any input crash this program?" is often uncomputable

Computability

• One-way functions are more time-consuming to undo than to do

• RSA encryption keys (used for HTTPS) are hard to crack because multiplying is faster than factoring

• Multiplying big prime numbers is a one-way function

Encryption and Time Complexity

• Linear time complexity: Twice as much input takes twice as much time

Time Complexity

• Quadratic time complexity: –Double the input 4x the time–Triple the input 9x the time

Time Complexity

• Factoring takes longer than quadratic time. So prime numbers can keep our secrets safe . . . maybe.

Encryption Keys Broken by Factoring

• Feel free to appreciate the beauty without getting into details

An Adventure to the Edge of Math

• One of seven six unsolved Millennium Problems ("Million Dollar Questions")

• If proved (or disproved), these problems would tell us a lot about what computers can or cannot do–We don't know "how hard" some

problems are–Are the hardest "NP problems"

(pictured below) solvable in polynomial time?

Does P=NP?

• Time to solve a P problem grows like

n some number

• Time to check an NP problem grows like some number n

Does P=NP? The Details

• If a problem can be checked in reasonable time, can it be solved in reasonable time?

• NP-Complete problems are the hard ones. They've been shown to be equivalent to each other.

• Three famous NP-Complete problems with many applications–Travelling Salesperson–Knapsack Problem–3-Color a Graph

Does P=NP? We Don't Know!

• What is the shortest route visiting all these cities?

NP-Complete Problems: Travelling Salesperson Problem

NP-Complete Problems: Knapsack Problem• Choice of items that have weight and

value• Maximize value without exceeding

maximum weight10 lbs

$150

15 lbs$220

3 lbs$40

3 lbs$40

5 lbs$70

3 lbs$40

3 lbs$405

lbs$70

5 lbs$70

5 lbs$70

• Color all nodes using 3 colors total• Adjacent nodes must not share color

NP-Complete: 3-Color a Graph

Another 3-Coloring Problem

Can You 3-Color It?