0% found this document useful (0 votes)
47 views36 pages

Complexity: Erin Keith

This document discusses complexity analysis and different data structures. It begins with an introduction to complexity analysis, explaining that it measures the time and space requirements of algorithms as a function of input size. It then discusses big O notation and different orders of complexity like constant, logarithmic, linear, quadratic, and more. Examples are provided to demonstrate how to calculate complexity. The document also covers array-based and list-based implementations of queues, as well as priority queues which are dequeued in priority order.

Uploaded by

maya fisher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views36 pages

Complexity: Erin Keith

This document discusses complexity analysis and different data structures. It begins with an introduction to complexity analysis, explaining that it measures the time and space requirements of algorithms as a function of input size. It then discusses big O notation and different orders of complexity like constant, logarithmic, linear, quadratic, and more. Examples are provided to demonstrate how to calculate complexity. The document also covers array-based and list-based implementations of queues, as well as priority queues which are dequeued in priority order.

Uploaded by

maya fisher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Complexity

ERIN KEITH

12_COMPLEXITY 1
Topics
1. Complexity
2. Array Based Queue
3. Priority Queue

12_COMPLEXITY 2
Complexity
Time
◦ The amount of time taken by an algorithm to run as a function
of the length of the input
Space
◦ The amount of space or memory taken by an algorithm to run
as a function of the length of the input

3
Complexity
Don’t use actual runtimes
◦ Those fluctuate on the machine based on what other processes
may be running
◦ They aren’t consistent from machine to machine, due to
different hardware specs
Use basic operations
◦ Comparisons
◦ Swaps
◦ Arithmetic operations

4
Big O Notation
‘O’ stands for Order of function
It is an (upper) bound, which assumes the worstcase scenario
There are also the
◦ Ω (Omega)
◦ Lower bound
◦ Best Case
◦ Θ (Theta)
◦ Average Case

5
 
Big O Notation
where
◦ is the actual function
◦ is the family of functions
◦ is the number of inputs (size)
◦ means “in the family of” or “on the order of”

6
Counting operations
This means we must count operations
cout << "Hello World" << endl;
1
for(int i = 0; i < n; i++)
cout << "Hello World" << endl;
n
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
cout << "Hello World" << endl;
n2
7
Counting operations
If f(x) is a sum of several terms, if there is one with largest growth
rate, it can be kept, and all others omitted.

If f(x) is a product of several factors, any constants (terms in the


product that do not depend on x) can be omitted.

Example:
2n2 + 3000n + 100000000000000 is Big O(n2)

8
Orders of Functions
Notation Name Example Approach

O(1) constant convert decimal to hexadecimal lookup table


values

O(logn) logarithmic binary search splitting things in 2s

O(n) linear searching an array single loop


O(nlogn) loglinear Merge sort splitting things in 2 and looping

O(n2) quadratic Bubble sort two loops


O(nc) polynomial c loops

O(cn) exponential dynamic programming traveling


salesman

O(n!) factorial brute force traveling salesman

9
Examples
int count = 0;
for(int i = 0; i < N; i++)
for(int j = 0; j < i; j++)
count++;

10
Examples
int count = 0;
for(int i = 0; i < N; i++)
for(int j = 0; j < i; j++)
count++;

This is the action we


must count

11
Examples
How many times will count++; run ?
When i = 0, 0
When i = 1, 1
When i = 2, 2
0 + 1 + 2 + … + (N – 1) = N * (N – 1) / 2
O(n2)

12
Examples
while(low <= high){
mid = (low + high)/2;
if(target < list[mid])
high = mid – 1;
else if(target > list[mid])
low = mid + 1;
else break;
}

13
Examples
How many times will the loop run ?
https://hackernoon.com/what-does-the-time-complexity-o-log-n-ac
tually-mean-45f94bb5bfbf

O(logn)

14
Examples
int count = 0;
for(int i = N; i > 0; i /= 2)
for(int j = 0; j < i; j++)
count++;

15
Examples
How many times will count++; run ?
When i = N, N
When i = N/2, N/2
When i = N/4, N/4
N + N/2 + N/4 + … + 1 = 2 * N
O(n)

16
Array Implementations
◦ Keep track of front and back
◦ Add?

◦ Remove?

12_COMPLEXITY 17
Array Implementations
◦ Keep track of front and back
◦ Add?
◦ Save item and increment back
◦ Remove?

12_COMPLEXITY 18
Array Implementations
◦ Keep track of front and back
◦ Add?
◦ Save item and increment back
◦ Remove?
◦ Increment front

12_COMPLEXITY 19
Array Implementations
◦ Keep track of front and back
◦ Add?
◦ Save item and increment back
◦ Remove?
◦ Increment front
◦ Problem: the queue is full when back equals
DEFAULT_CAPACITY-1 BUT this may happen without
the array being “full”.
Index 0 1 2 3
Item Apples Cheese

12_COMPLEXITY 20
Array Implementation

21
Array Implementations
◦ Keep track of front and back
◦ Add?
◦ Save item and increment back
◦ Remove?
◦ Increment front
◦ Problem: the queue is full when back equals
DEFAULT_CAPACITY-1 BUT this may happen without
the array being “full”.
◦ Treat array as circular!

12_COMPLEXITY 22
Array Implementations
◦ Treat array as circular!
◦ Add?
◦ Save item and increment back
◦ Remove?
◦ Increment front

◦ When either front or back advances past


DEFAULT_CAPACITY-1, then wrap around to 0.

12_COMPLEXITY 23
Array Implementations
◦ Treat array as circular!
◦ A visualization:

12_COMPLEXITY 24
Array Implementations
◦ Treat array as circular!
◦ Dequeue-ing and Enqueue-ing:

12_COMPLEXITY 25
Array Implementations
◦ Treat array as circular!
◦ Empty Queue:

12_COMPLEXITY 26
Array Implementations
◦ Treat array as circular!
◦ Full Queue:

12_COMPLEXITY 27
Priority Queue ADT
Includes qualities of a Queue ADT:
• Items are dequeued following a priority order
• Data includes an additional value: priority
• Same interface!
• same functions, different behavior

12_COMPLEXITY 28
Priority Queue ADT
Operations
• isEmpty(): boolean
• enqueue(newEntry: ItemType): boolean
• inserts item based on priority
• dequeue(): boolean
• removes highest priority item
• peekFront(): ItemType
• returns copy of highest priority item

12_COMPLEXITY 29
Priority Queue ADT
• enqueue(newEntry: ItemType): boolean
• inserts item based on priority
• How can we insert items in such a way that will make it easier
the dequeue and peekFront?

12_COMPLEXITY 30
Priority Queue ADT
• enqueue(newEntry: ItemType): boolean
• inserts item based on priority
• How can we insert items in such a way that will make it easier
the dequeue and peekFront?

• SORT!
• based on priority

12_COMPLEXITY 31
Priority Queue ADT
• enqueue(newEntry: ItemType): boolean
• inserts item based on priority
• How can we insert items in such a way that will make it easier
the dequeue and peekFront?

• SORT!
• based on priority
• How do we sort an item of a templated type?

12_COMPLEXITY 32
Priority Queue ADT
• enqueue(newEntry: ItemType): boolean
• inserts item based on priority
• How can we insert items in such a way that will make it easier
the dequeue and peekFront?

• SORT!
• based on priority
• How do we sort an item of a templated type?
• You can still use relational operators (>, <, etc.)
BUT you have to overload them in the item class you build.

12_COMPLEXITY 33
List Implementation
https://www.autonomousrobotslab.com/uploads/5/8/4/4/58449511/cs302
-82-implementation-of-queues.pdf

12_COMPLEXITY 34
Next Class
Reading Day
Complexity Activity
"Show and Tell"
HW 3

12_COMPLEXITY 35
Next Next Class
Midterm Review
Textbook:
• Chapters 1, 6, 8, 10, & 13
Internet:
• See Module Pages

12_COMPLEXITY 36

You might also like