Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

30
Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut

Transcript of Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Page 1: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Complexity, Origami, etc.

Big Oh

Traditional Origami

Fold and cut

Page 2: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Analyses of Algorithms

• What resources does an algorithm take?– runtime– space

• Calculate as function of size of input– size of set– magnitude of input values

• Typical problems– sort, search, paths/circuits of graph, Fibonacci, GCD– origami, 'off-line Tetris', piano-movers problem

Page 3: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Time complexity• Can write program, run program and

record time…• Want machine independent analysis in

terms of inputs – magnitude of inputs, size of set

• Count basic operations– For sorting, typically count compares

• Some judgment required• Want bounds, not necessarily details

Page 4: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Example• How many steps required to find average of N numbers

in an array?• Pseudo-code (array is aa, size n

sum = aa[0]; set sum to the first numberfor (i=1;i<n;i++) go through the array…sum = sum + aa[i]; adding in the numbers

How many steps?initialize sum 1initialize i 1compare i to n loop done completely n-1 times add aa[i] to sum 1 compare operation increment i 3*(n-1) + 3

Page 5: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Big Oh

• Formal definition of upper bounds

A function g(n) is said to be O(f(n)) if and only if the following is true:There exists a number n0 and a number c such that is n>n0

g(n) <= c*f(n)English: beyond a certain value of n, f(n) is an

upper bound for g(n). You can use a coefficient c.

Page 6: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Jargon

• You say– g(n) is O(f(n))– g(n) = O(f(n))– g(n) є O(f(n)) (contained in, belongs to, is in)

• Generally, the f(n) functions are standard– O(log n), log NOTE: base doesn't matter– O(n), linear– O(n2), n-squared, quadratic– O(nk) for some k, polynomial, also called P– O(2n), exponentialThese may be 'NP' or NP-Complete

Page 7: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Exercise

• What is the relationship between

log2(n) and

ln(n) by definition: loge(n)

Page 8: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

log n

• Why does base not matter?

• Becauselogb(n) = logb(a)*loga(n)

this is the coefficient

Page 9: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

NP, NP-complete• Formal definition: an algorithm is NP if it can be

performed by a non-deterministic Turing machine.• Less formal: an algorithm is NP if it can be done by a

process in which there are a finite number of choices, and assuming the correct choice is made, the process is polynomial. The correctness can be checked in polynomial time. If all choices must be tried, the algorithm is O(2n)

• NP-complete refers to a set of problems for which there are NP algorithms that have been shown to be equally hard – (can convert one to the other on polynomial time). – These include: traveling salesman, satisfying logical formulas,

subgraph isomorphism. – BIG question in computer science proving either P = NP or P

proper subset of NP.

Page 10: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Examples

• g(n) = 5n+10 is O(n)

Let n0=10, c = 6, then if n>105*n+10 <= 5*n+n = 6*n

• g(n) = 3n2+5n+100 is O(n2)Let n0 = 100, c=6, then if n>100

3n2+5n+100 <= 5n2+5n+100<=5n2+6n<= 5n2+n*n<= 6n2

Page 11: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

log n

• log n is O(n)—that is, log n is bounded by O(n)

Assume base 2. Let n0=2, c=1

log n <= n Why? Raise 2 to eachn <=2n

2<22, 3<23, etc.

Page 12: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Big Oh

• This is an asymptotic, upper bound

eventually, this is the n>n0 condition May not be a tight

bound.

Page 13: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Bounds

• If g(n) is O(n), it is also O(n2), O(nk), O(2n)

• But, n2 is not O(n)? Why?

• Suppose there was an n0 and a c,n2<=c*nDivide both sides by n,n<c FALSE, can choose n>c and n>n0

Page 14: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Exercises

• g(n) = 4n3+n+10. Show g(n) is O(n3)

• g(n) = log(n) * n. This is called log linear. Show it is O(n).

Page 15: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Growth rates illustrated

n=1 n=2 n=4 n=8 n=16 n=32

O(1) 1 1 1 1 1 1

O(logn) 0 1 2 3 4 5

O(n) 1 2 4 8 16 32

O(nlogn) 0 2 8 24 64 160

O(n2) 1 4 16 64 256 1024

O(n3), 1 8 64 512 4096 32768

O(2n) 2 4 16 235 65536 4294967296

Page 16: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Methods

• For loops, multiply number of steps in loop by upper bound on number of times loop is run

• For conditionals, add step for doing condition plus maximum of each of the branches

Page 17: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Lower bounds (omega)

• To give better performance estimates, we may also want to give lower bounds on growth rates

• Definition (omega): g(n) = Ω(f(n))

if there exist some constants c and n0 such that g(n) ≥ cf(n) for all n ≥ n0

• This says, g(n) grows at least this much

Page 18: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

“Exact” bounds• Definition (Theta): g(n) = Θ(f(n)) if and only

if g(n) =O(f(n)) and g(n) = Ω(f(n)).

• An algorithm is Θ(f(n)) means that f(n) is a tight bound (as good as possible) on its running time.– On all inputs of size n, time is ≤ f(n)– On all inputs of size n, time is ≥ f(n)

Page 19: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Complexity

• Bounds (such as Big Oh) are for specific algorithms– Tight[er] bounds are better than looser bounds– Sometimes there are reports giving average bounds

• Problems may have different algorithms with different bounds

• Sometimes possible to prove result about a problem as opposed to an algorithm– There is no method that is better than …

Page 20: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Good sourceAlgorithm (Run Time) Analysis

www.cs.uic.edu/~liub/teach/ cs201-2004/cs201-algo-analysis.ppt

Page 21: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Computing Fibonacci numbers

• Recursive program

1 long int fib(n) function returns integer

2 if (n <= 1) which case?

3 return 1; 0,1 case return 1

4 else return fib(n-1) + fib(n-2) else calculate

• This is based on definition. It takes a long time [for big numbers]

Page 22: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

fib(n) runs in exponential time

• Let T denote the running time.

T(0) = T(1) = c

T(n) = T(n-1) + T(n-2) + 2

where 2 accounts for line 2 plus the addition at line 3.

• It can be shown that the running time is Ω((3/2)n). – at most and at least exponential

Page 23: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Efficient Fibonacci numbers• Avoid recomputation

– Work iteratively: working up and saving past results• Solution with linear running time

int fib(int n) int fibn=0, fibn1=0, fibn2=1; set up 3 numbers if (n < 2) check case

return n low(er) cases return n else

for( int i = 2; i <= n; i++ ) loop fibn = fibn1 + fibn2; calculate fibn fibn1 = fibn2; reset fibn1 fibn2 = fibn; reset fibn2 ends the looping

return fibn; ends the else clause ends the function

Page 24: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Origami: paper folding

• Associated most with Japan• Many oldest models come from China• Independent development in Spain,

elsewhere

• Worldwide• Conventions all over: NYC in June• http://www.origami-usa.org/

Page 25: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Water bomb

• Traditional model

• Can ask questions:– what is surface area?– what areas are on outside?– from folding pattern, what is foldable

assignment of mountain and valley? What is folding order?

Page 26: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Modular origami

• 6 preliminary folds unit

• systems of models

• Rona Gurkewitz, others

Page 27: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Complexity and origami

• Many complexity questions have been addressed for origami, including: can a model be folded? Can a folding pattern be folded? Can a folding pattern with mountain and valley assignments be folded flat?

• http://www.msri.org/publications/ln/msri/2003/introdcgeom/demaine/2/index.html

Page 28: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Fold and cut

• Examples by Betsy Ross, Houdini, Martin Gardner (Scientific American), others.

• 5 pointed star

Page 29: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

General problem

• Take a square• Draw a polygon• Is there a way to fold a flat model and

make one cut to produce the polygon?• Answer: Yes.

– two ways. One way: generate skeleton, add perpendicular folds, then determine how to fold this pattern.

– NP-complete

Page 30: Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.

Conclusion

• Very big topic

• Active research

• Questions and comments?