Docsity

Introduction to Data Structures: Big O Notation and Algorithmic Efficiency, Study notes of Advanced Algorithms

Toggle sidebar
bg1
1
15-121 Introduction to Data Structures, Carnegie Mellon University - CORTINA 1
Algorithms
A Look At Efficiency
Big O Notation
1B
15-121 Introduction to Data Structures, Carnegie Mellon University - CORTINA 2
Big O
 Instead of using the exact number of
operations to express the complexity of a
computation, we use a more general notation
called "Big O".
 Big O expresses the type of complexity
function:
  Linear O(n)
  Quadratic O(n2)
  Logarithmic O(log n)
  Log-Linear O(n log n)
  Exponential O(2n)
  Constant O(1)
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Introduction to Data Structures: Big O Notation and Algorithmic Efficiency and more Study notes Advanced Algorithms in PDF only on Docsity!

15-121 Introduction to Data Structures, Carnegie Mellon University - CORTINA 1

Algorithms

A Look At Efficiency

Big O Notation

1B

15-121 Introduction to Data Structures, Carnegie Mellon University - CORTINA 2

Big O

 Instead of using the exact number of

operations to express the complexity of a

computation, we use a more general notation

called "Big O".

 Big O expresses the type of complexity

function:

 Linear O(n)

 Quadratic O(n^2 )

 Logarithmic O(log n)

 Log-Linear O(n log n)

 Exponential O(2n)

 Constant O(1)

15-121 Introduction to Data Structures, Carnegie Mellon University - CORTINA 3 Big O

 Let C represent a function for the number of

comparisons needed for an algorithm as a

function of the size of the input array(s).

 Search C(n) = n = O(n)

 Unique I C(n) = n^2 = O(n^2 )

 Diff C(m,n) = mn + n = O(mn)

 If arrays are the same size: O(n^2 )

15-121 Introduction to Data Structures, Carnegie Mellon University - CORTINA 4 More about Big O  Consider a computation that performs 5n^2 + 3n + 9 operations on n data elements.  The graph comparing the number of data elements to the number of computations will be quadratic. 5n^2 + 3n + 9 = O(n^2 )

 Unique II Algorithm C(n) = n(n-1)/2 = O(n^2 )

15-121 Introduction to Data Structures, Carnegie Mellon University - CORTINA 7 Binary Search: Worst Case  How many iterations are needed before we end up with no elements left to examine? Array length Iterations

n

15-121 Introduction to Data Structures, Carnegie Mellon University - CORTINA 8 Big O: Formal Definition  Let T(n) = the number of operations performed in an algorithm as a function of n.  T(n) = O(f(n)) if and only if there exists two constants, n 0 > 0 and c > 0, and a function f(n) such that for all n > n 0 , cf(n) ≥ T(n). n 0 T(n) n cf(n)

15-121 Introduction to Data Structures, Carnegie Mellon University - CORTINA 9 Example Again

 Let T(n) = 5n^2 + 3n + 9. Show that T(n) = O(n^2 ).

 Find c and n 0 such that for all n > n 0 ,
cn^2 > 5n^2 + 3n + 9.

 Find intersection point such that cn^2 = 5n^2 + 3n + 9.

 Let n = n 0 and solve for c: c = 5 + 3/n 0 + 9/n 02.

 If n 0 = 3, then c = 7.

 Thus, 7n^2 > 5n^2 + 3n + 9 for all n > 3.

 So 5n^2 + 3n + 9 = O(n^2 )

15-121 Introduction to Data Structures, Carnegie Mellon University - CORTINA 10 More about Big O  Big O gives us an upper-bound approximation on the complexity of a computation.  We can say that the following computation is O(n^3 ):

for (int i = 0; i < n; i++)

for (int j = 0; j < n; j++)

System.out.println(i + " " + j);

 A tighter bound would be O(n^2 ), but both are technically correct.

15-121 Introduction to Data Structures, Carnegie Mellon University - CORTINA 13 Traveling Salesperson

 Given: a network of nodes representing

cities and edges representing flight paths

(weights represent cost)

 Is there a route that takes the salesperson

through every city and back to the starting

city with cost no more than K?

 The salesperson can visit a city only once

(except for the start and end of the trip).

15-121 Introduction to Data Structures, Carnegie Mellon University - CORTINA 14 Traveling Salesperson A B D C G E F

15-121 Introduction to Data Structures, Carnegie Mellon University - CORTINA 15 Traveling Salesperson

 If there are N cities, what is the maximum number of

routes that we might need to compute?

 Worst-case: There is a flight available between every

pair of cities.

 Compute cost of every possible route.

 Pick a starting city
 Pick the next city (N-1 choices remaining)
 Pick the next city (N-2 choices remaining)

 Maximum number of routes: ______ = O(______)

how to build a route 15-121 Introduction to Data Structures, Carnegie Mellon University - CORTINA 16 Comparing Big O Functions N (amount of data) Number of Operations

O(N!)
O(1)

O(N log N) O(log N)

O(N^2 )
O(N)