Complexity and Big-O Notation

Complexity and Big-O Notation - Complexity and Big-O...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
C OMPLEXITY AND B IG -O N OTATION Contents z Introduction { Test Yourself #1 { Test Yourself #2 z Big - O Notation z How to Determine Complexities { Test Yourself #3 { Test Yourself #4 z Best - case and Average - case Complexity z When do Constants Matter? Introduction An important question is: How efficient is an algorithm or piece of code? Efficiency covers lots of resources, including: z CPU (time) usage z memory usage z disk usage z network usage All are important but we will mostly talk about CPU time in 367. Other classes will discuss other resources (e.g., disk usage may be an important topic in a database class). Be careful to differentiate between: 1. Performance: how much time/memory/disk/. .. is actually used when a program is run. This depends on the machine, compiler, etc. as well as the code. 2. Complexity: how do the resource requirements of a program or algorithm scale, i.e., what happens as the size of the problem being solved gets larger. Complexity affects performance but not the other way around. The time required by a method is proportional to the number of "basic operations" that it performs. Here are some examples of basic operations: z one arithmetic operation (e.g., +, *). z one assignment z one test (e.g., x == 0) z one read z one write (of a primitive type) Page 1 of 10 Complexity and Big-O Notation 2008/3/27
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Some methods perform the same number of operations every time they are called. For example, the size method of the List class always performs just one operation: return numItems ; the number of operations is independent of the size of the list. We say that methods like this (that always perform a fixed number of basic operations) require constant time . Other methods may perform different numbers of operations, depending on the value of a parameter or a field. For example, for the array implementation of the List class, the remove method has to move over all of the items that were to the right of the item that was removed (to fill in the gap). The number of moves depends both on the position of the removed item and the number of items in the list. We call the important factors (the parameters and/or fields whose values affect the number of operations performed) the problem size or the input size . When we consider the complexity of a method, we don't really care about the exact number of operations that are performed; instead, we care about how the number of operations relates to the problem size. If the problem size doubles, does the number of operations stay the same? double? increase in some other way? For constant-time methods like the size method, doubling the problem size does not affect the number of operations (which stays the same). Furthermore, we are usually interested in the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 10

Complexity and Big-O Notation - Complexity and Big-O...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online