Download Efficiency How Fast Is My Algo, Lecture Notes - Computer Science and more Study notes Introduction to Computers in PDF only on Docsity! Lesson: Efficiency
How Fast Is My Algorithm?
AKA: 15-121 in 40 minutes.
How do we decide whether one program is more efficient
(faster) than another?
if different processors?
if written in different languages?
if one uses a while loop and one uses recursion?
How do we determine the INHERENT efficiency of
an ALGORITHM, independent from the processor,
programming language, coding style, etc.?
1 of 18
Key Idea:
We determine how the running time of an algorithm
depends on the size of the problem it's solving.
public static int countValues(int[] a)
{
return a.length;
}
Does the running time for this method
depend on the length of the array?
No.
If we double the length of the array,
the method will take the same amount of time to run.
2 of 18
//swaps value at position i with value at position j
public static void swap(int[{] a, int i, int j)
{
int temp = a[i];
afi] = a[jl;
a(j] = temp;
Does the running time for this method
depend on the length of the array?
No.
The bubble algorithm:
Swap lst and 2nd if out of order.
Swap 2nd and 3rd if out of order.
Swap 3rd and 4th if out of order.
3 Fe 6 7 eos 8
2 3 4 A r a
1 5
5 of 18
public static void bubble(int[] a)
{
for (int i = 0; i < a-length - 1; i++)
{
de (eles Sis eis) _)
swap(a, io, Re TE),
}
}
Does the running time for this method
depend on the length of the array?
Yes.
The time is proportional to the length of the array.
public static void sort(int[] a)
{
for (int i = 0; i < a.length; i++)
bubble();
}
Does the running time for this method
depend on the length of the array?
How many times do we call bubble? a.length
time for each call to bubble is proportional to a.length
If we double the length of the array,
then we will call bubble twice as many times,
AND each call to bubble will take twice as long.
Therefore, sort will take 4 times as long.
The time for sort is proportional to (a.length)%*2
6 of 18
public boolean contains(int[] a, int value)
{
for (int i = 0; i < a.length; i++)
if (a{i] == value) return true
}
return false
This algorithm is called “sequential search".
Running time is proportional to the length of
the array.
Suppose we know that the array is already sorted.
Can we do better?
How long does it take to find a name in the phonebook?
10 - 20 seconds
7 of 18
How long will it take me to check
if a particular phone number
appears in the phone book?
a really long time,
since I'd have to use sequential search.
Binary search only works if the elements appear
in sorted order.
Otherwise, I need to use the "sequential search"
algorithm.
n refers to the size of the problem
(In our examples, n is the length of the array)
"Constant Time" = time does not depend on n
“Logarithmic Time" = time is proportional to log n
"Linear Time" = time is proportional to n
“Quadratic Time" = time is proportional to n“%*2
10 of 18
\
n Constant | Logarithmic Linear | Quadratic
fee | |
| \
1000 10 ns \ 100 ns 10 us \ 10 ms
|
10 ms {/ 3 hours
1 million { 10 ns \ 200 ns |
1 billion 10 ns | 300 ns 10 s 300 years
(binary \ (seauen= |
search) tial (sorting)
search)
The Subset Sum Problem
Given a set of integers, does the sum of some non-empty
subset equal exactly zero?
{-7, -3, -2, 5, 8}
Yes: -3 + -2+5=0
{-10, -3, -2, 7, 14, 15}
Yes: -10 + -3 + -2 +15 =0
Describe an algorithm for solving this problem.
11 of 18
The Brute Force Algorithm:
Find the sum of every possible subset.
If any sum is 0, output yes.
Otherwise, output no.
How many possible subsets are there of n elements?
2°n
What is the running time of this algorithm?
proportional to 2%n
The brute force algorithm for solving the
Subset Sum Problem runs in “exponential time"
How slow is that?
12 of 18
The Hamiltonian Cycle Problem
Is there a roundtrip that visits each city exactly once?
5 6
Denver Chicago
\ { o™
| N
~~
2 ™~ mae
LA ————++ |
‘ —, iw
Xe Seo / fl
Nes ee |
aa 4 “Se (Be gt
~ Phoenix —— Dallas
SPE EO
Subset Sum Problem “NG
f sy
/ Hamiltonian Cycle Problem \
ar Complete Traveling Salesman Problem
Problems 2
Graph Coloring Problem f
\ and thousands more! of
oe att
No one knows if these problems can be solved by
polynomial time algorithms.
If any one of these problems can be solved
in polynomial time,
then all of them can be solved in polynomial time.
15 of 18
Subset Sum Problem “Se
™,
! Hamiltonian Cycle Problem \
\
“NP-Complete"
Traveling Salesman Problem
Problems 2
Graph Coloring Problem ff
yf
\ and thousands more! gt
~~ get
If we could find a fast algorithm for solving
the Subset Sum Problem,
then we could use it somehow to solve the
Hamiltonian Cycle Problem quickly,
and vice versa!
That's very surprising,
considering these problems don't look anything alike!
oo a
Subset Sum Problem “NG
{ : ~
/ Hamiltonian Cycle Problem \
pt
“NP-Complete"
Traveling Salesman Problem
Problems
Graph Coloring Problem ff
/
\ and thousands more! gt
~~ get
To win $1,000,000 from the Clay Mathematics Institute:
* Find a polynomial time algorithm for solving any
one of these problems.
OR
* Prove any one of these problems cannot be solved
in polynomial time.
16 of 18
public static boolean isNice(arbitrarilylargeint n)
{
while (n > 1)
{
if (n % 2 == 0)
n=n/ 2;
else
n=3*#n+1;
}
return true;
}
Find the value of: isNice(7)
public static boolean isNice(arbitrarilylargeint n)
{
while (n > 1)
{
if (n % 2 == 0)
n=n/ 2;
else Will isNice always return
n=3*n+13 Z *
true, or will it run
} forever on some input
return true;
} values?
It's not always easy to
tell if a method will
finish, or if it will run
forever.
17 of 18