18 Complexity
18 Complexity
2
The Complexity of Algorithms
Given an algorithm, how efficient is this
algorithm for solving a problem given
3
The Complexity of Algorithms
When we analyze the time the algorithm uses to
solve the problem given input of a particular size,
4
Space Complexity
5
Space Complexity
To compute the how much space is required
for an algorithms to run, we have to study two
Sp(n) = c + f(n)
c is a constant, and f(n) is the function for the variable part
6
Time Complexity
7
Time Complexity
To analyze the time complexity of algorithms, we determine
the number of operations, such as
comparisons,
arithmetic operations (addition, multiplication, etc.),
basic operations
8
Time Complexity
Three types of time complexities:
The worst-case time complexity of an algorithm provides
an upper bound on the number of operations an algorithm
Basically, it’s the scenario (input) for which the algorithms uses
the least number of operations to terminate
9
The Complexity of Algorithms
In this course, we will “mainly” focus on time complexity
size
10
Time Complexity
Often more important than space complexity
space (memory) available tends to be larger and larger
time is still a problem for all of us
12
Sample Algorithms
13
Complexity Analysis of Max Alg.
Describe the time & space complexities of the algorithm for
finding the maximum element in a finite sequence of integers
15
Therefore the total number of data items S(n) = n + 2
Complexity Analysis of Max Alg.
Describe the time & space complexities of the algorithm for finding
the maximum element in a finite sequence of integers
complexity?
In best case, the max is the first element, and therefore the if condition is
always false we save n-1 operations T(n) = 3n – 1, which is O(n)
Therefore, T(n) is Θ(n)
We can also conclude that the average-case time complexity is also O(n)
16
Complexity of Linear Search
Determine the complexity of the linear search algorithm
procedure linear search(x:integer, a1, a2, …,an: distinct
integers)
1. i := 1
2. while (i ≤ n and x ≠ ai)
3. i=i +1
4. if i ≤ n then
5. location := i
6. else location := 0
7. return location
line 2 executes n+1 times at max, and costs 2 comparisons each time
2(n+1) comparisons
17
Complexity of Linear Search
Determine the complexity of the linear search
algorithm
procedure linear search(x:integer, a , a , …,a : distinct
1 2 n
integers)
1. i := 1
2. while (i ≤ n and x ≠ ai)
3. i=i +1
4. if i ≤ n then
5. location := i
6. else location := 0
7. return location
18
Complexity of Linear Search
Determine the complexity of the linear search algorithm
procedure linear search(x:integer, a1, a2, …,an: distinct
integers)
1. i := 1
2. while (i ≤ n and x ≠ ai)
3. i=i +1
4. if i ≤ n then
5. location := i
6. else location := 0
7. return location
at position I
One more comparison at line 4 so T(i) = 2i + 1
19
Complexity of Linear Search
Determine the complexity of the linear search algorithm
procedure linear search(x:integer, a1, a2, …,an: distinct Linear Search:
integers)
1. i := 1
• T(n) is O(n)
3. i=i +1
4. if i ≤ n then
5. location := i
6. else location := 0
7. return location
T(i) = 2i+1 and we have n+1 cases (n positions, and when its not in the array)
20
Complexity of Binary Search
Describe the complexity of binary
procedure binary search(x: integer, a1,a2,…, an: increasing integers)
1. i := 1 {i is the left endpoint of interval}
2. j := n {j is right endpoint of interval}
3. while i < j
4. m := ⌊(i + j)/2⌋
5. if x > am then i := m + 1
6. else j := m
7. if x = ai then location := i
8. else location := 0
9. return location {location is the subscript i of the term ai equal to x, or 0 if x is not found}
At the first iteration the size of the list is 2k and after the first iteration it is 2k-1,
then 2k-2 and so on until the size of the list is 21 = 2
At the last step, a comparison tells us that the size of the list is 20 = 1 and the
element is compared with the single remaining element so two more comparisons
algorithm, since no matter where the element x is, we have to do all the steps, and
the while loop will always iterate k+1 times, and therefore T(n) = Θ(lgn)
21
Complexity of Bubble Sort
What is the complexity of bubble sort?
procedure bubblesort(a1,…,an: real numbers )
1. for i := 1 to n− 1
2. for j := 1 to n − i
3. if aj >aj+1 then interchange aj and aj+1
22
Complexity of Insertion Sort
What is the complexity
procedure insertion_sort (a1, a2,
of insertion sort?
…, a )
1. for j := 2 to n
n
2. temp := aj
Solution: In the worst
case, the list is sorted 3. i := j-1
23
Complexity of Insertion Sort
What is the
procedure insertion_sort (a1, a2,
complexity of
…, an)
insertion sort? 1. for j := 2 to n
2. temp := aj
temp
already sorted 5. ai+1 := ai
6. i := i -1
The number of
comparisons in the 7. ai := temp
best case are 1
Therefore, Insertion Sort
{a1, a2, …, Bubble
outperforms an are sorted }
sort on sorted
comparison n-1 times: input. However, Bubble sort is easier
n -1 = O(n) to implement
24
Algorithmic Paradigms
25
Algorithmic Paradigms
An algorithmic paradigm is a general approach
based on a particular concept for constructing
8)
algorithms (Chapter 7)
There are many other paradigms that you may see in
later courses
26
Brute-Force Algorithms
A brute-force algorithm is solved in the most
straightforward manner, without taking
insertion sort
27
Computing the Closest Pair of Points
Construct a brute-force algorithm for finding
the closest pair of points in a set of n points
28 Continued →
Closest Pair of Points by Brute-Force
Algorithm for finding the closest pair in a set of n points
procedure closest pair((x1, y1), (x2, y2), … ,(xn, yn): xi, yi real numbers)
min = ∞
for i := 1 to n-1
for j := i+1 to n
if (xj − xi)2 + (yj − yi)2 < min
then min := (xj − xi)2 + (yj − yi)2
closest pair := (xi, yi), (xj, yj)
return closest pair
29
Understanding the Complexity of
Algorithms
30
Understanding the Complexity of
Algorithms
Intractable Problem:
There does not exist a polynomial time algorithm to solve this
problem
Unsolvable Problem:
No algorithm exists to solve this problem, e.g., halting problem
Class NP:
Solution can be checked in polynomial time
But no polynomial time algorithm has been found for finding a
NP Complete Class:
If you find a polynomial time algorithm for one member of the
class, it can be used to solve all the problems in the class
The P versus NP problem asks whether the class P = NP? Are there
problems whose solutions can be checked in polynomial time, but
time algorithm
It is generally believed that P≠NP since no one has been able to find a
polynomial time algorithm for any of the problems in the NP complete
class
33
Any Questions?
34