Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Lecture 06: COMPLEXITY Sections 2.2 and 2.3 Jarek Rossignac CS1050:

Similar presentations


Presentation on theme: "1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Lecture 06: COMPLEXITY Sections 2.2 and 2.3 Jarek Rossignac CS1050:"— Presentation transcript:

1 1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Lecture 06: COMPLEXITY Sections 2.2 and 2.3 Jarek Rossignac CS1050: Understanding and Constructing Proofs Spring 2006

2 2 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Lecture Objectives Example of greedy algorithm Explore concepts of space and time complexity

3 3 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Growing a region of a triangle mesh From a (user-selected) tip vertex Grow in all directions uniformly (graph distance) –adding concentric layers one at a time Representation? Algorithm? Complexity?

4 4 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Representation of triangle meshes Vertices –Stored as table of structures –Referenced by their index: v=0,1,2… –3 coordinates: v.x, v.y, v.z –A Boolean flag v.m (true if processed) Triangles –Stored as table of structures –Referenced by their index: t=0,1,2… –3 vertex references: t.a, t.b, t.c –A Boolean flag t.m (true if processed) Vertices x y z m vertex 0 vertex 1 vertex 2 x y z m Triangles 3 2 4 m 6 5 8 m Triangle 3 7 5 6 m Triangle 4 1 2 3 m Triangle 0 a b c m Triangle 1 Triangle2 Triangle 5 8 5 1 m t2t2 vava vcvc vbvb

5 5 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac How to fix the region-growing algorithm? Assume initially a single vertex v is marked (v.m==true) We want to mark the triangles in L concentric layers Repeat L times { For each triangle t { if (t.a.m || t.b.m || t.c.m) { t.m=true; t.a.m=true; t.b.m=true; t.c.m=true; } Why does this algorithm not work? How would you fix it? What is its complexity?

6 6 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac How to smooth the region? We want to minimize the number of edges in the boundary between the grown region (marked triangles) and the rest We can perform one move at a time alter the status t.m of a single triangle Suggest a greedy approach to select the next move? Explain/show how the greedy approach can get stuck in a local minimum! How would you fix this? What is the total complexity?

7 7 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac What is asymptotic complexity O()? A function providing the upper bound of a (space or time) cost as a function of one or more variables that measure the size of the input data, as these variables grow very large. When we say that f(n) is O(g(n)), we use g(n) to provide a limit as to how fast f(n) can grow when n goes to infinity

8 8 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac What are other measures of complexity? O() provides an upper bound (on how bad f could get) –big O  () provides a lower bound –Omega –No algorithm is guaranteed to consistently do better –f(n) is O(g(n))  g(n) is  (f(n))  () if O() =  () –Theta –“f is order …”

9 9 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac How to simplify O()? Similar to horizontal asymptotes for functions Keep only highest order terms in polynomials Simplify the remaining fractions Replace constants with 1 f(n)=5n 3 +5n2+6n+9 is O(n 3 )

10 10 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac How to prove the f(n) is O(g(n))? Show the  (k,c) : (n>k)  f(n) < c g(n) –(k,c) are the witnesses

11 11 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Complexity of 1+2+…+n ? Prove that f(n)=1+2+… +n is O(n 2 )

12 12 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac * Proof * Prove that f(n)=1+2+… +n is O(n 2 ) 1+2+…+n < n+n+…+n=n 2 Hence for witnesses k=1 and c=1, (n>k)  f(n) < c n 2 Another way: f(n) = n(n+1)/2 = n 2 /2+n/2, which is O(n 2 )

13 13 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac What are the common reference functions? To express O(), we often use the following functions: constantO(1) logarithmicO(logn) linearO(n) nlognO(nlogn) quadraticO(n 2 ) polynomialO(n b ) exponentialO(b n ) factorialO(n!)

14 14 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac When is a problem solvable? When an algorithm exists for solving it –Otherwise it is unsolvable

15 15 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac When is a problem tractable? When is it solvable in polynomial time Otherwise it is intractable –can try to provide an approximate solution with a guarantee on its accuracy

16 16 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac When is a problem NP? When we believe that the problem is not tractable but can check a solution in polynomial time

17 17 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac When is a problem NP complete? When it falls in a class of NP problems such that if a polynomial solution exists for one, then there exist polynomial solutions for the others

18 18 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Give an example of an NP complete problem Find whether a Boolean function in n variables can be satisfied –Does there exist truth-value assignments to the variables for which the function evaluates to true? Note that we can check whether an assignment satisfies the function in polynomial time.

19 19 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Prove that log 2 n is O(n) let n be a posiive integer n < 2 n taking the log 2 on both sides log 2 n < n log 2 2 = n hence log 2 n is O(n)

20 20 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Does the base of log matter? Why? logb log b n = logn logb is a constant, so log b n and logn have the same complexity

21 21 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Space complexity of a fast train-sort? You want to sort a deck of n cards. Assume that you have a large table What would be the fastest approach? What is its time complexity? What is its space complexity?

22 22 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac How to support a constant-time query? You want to be able to say whether a student S has taken course C in a time that does not grow as a function of the total number |S| of students or of the total number |C| of courses. What data structure will you use? What is the space complexity of your approach? What is the time complexity of a query?

23 23 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Complexity bound for a sum, product? Let f(n) be O(g(n)) and Let f’(n) be O(g’(n)) f(n)+f’(n) is O(max(g(n),g’(n))) –max, not + f(n)f’(n) is O(g(n)g’(n)) –product

24 24 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Example of a 2D array Let T[n][n] be a two dimensional array of integers What is the time complexity of visiting each element of T as a function of n? What is the time complexity of checking whether T has duplicates?

25 25 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Assigned Reading Sections 2.2 and 2.3

26 26 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Assigned Exercises and quiz material Complexity notations, descriptors, meaning f(n)=(2x 3 +5x 2 ) / (3x 2 –x) is O(?) Example of a greedy algorithm stuck in local minima Complexity of testing whether T[][] has duplicates Definition and example of an NP complete problem Problems 7 and 8 page 151 (polynomial evaluation)

27 27 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Cost of evaluating a polynomial We count the number of multiplications (no powers) –Number of additions is the same Powers method f(n)=a n x n +a n–1 x n–1 +…a 1 x+a 0 n + (n–1) +…. 1 = n(n+1)/2 O(n 2 ) Horner’s method f(n)=(…((a n x+a n–1 )x+a n–2 )x+…a 1 )x+a 0 1 + 1 +… + 1 = n O(n)

28 28 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Assigned Project P2 (sorting) was due today. Who did a non-standard method? Anyone needs an extension? –Penalty (5% of your project’s grade per day) –Unless special circumstances (pre-approved) You do not have to use Processing –Can use Java, as long as the TA and I can run your applet in a web browser –You will not be able to build upon my examples…


Download ppt "1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Lecture 06: COMPLEXITY Sections 2.2 and 2.3 Jarek Rossignac CS1050:"

Similar presentations


Ads by Google