Post on 24-Jun-2015
description
Universidade Federal da ParaíbaCentro de Informática
Introduction to theAsymptotic Analysis I
Lecture 12
1107186 – Estrutura de Dados – Turma 02
Prof. Christian Azambuja PagotCI / UFPB
2Universidade Federal da ParaíbaCentro de Informática
Algorithm Efficiency
● Can be measured in terms of:– Space.
– Bandwidth.
– Time !!!
– Etc.
3Universidade Federal da ParaíbaCentro de Informática
Algorithm Efficiency
● Is influenced by – Input (Problem Instance):
● Sorting: number of elements in a list.● Multiplication: number of bits of each operand. ● Graph traversal: number of nodes and edges.● Etc.
– Number of instructions executed.● Depends on the computer organization.
– Thus, we must define some assumptions regarding “our machine”...
4Universidade Federal da ParaíbaCentro de Informática
Algorithm Efficiency
● Assumptions regarding the architecture of our virtual computer:– Sequential processor.
– Simple (regarding microinstructions) instructions.
– No concurrency.
– RAM memory.
– No memory hierarchy.
5Universidade Federal da ParaíbaCentro de Informática
Example: Insertion Sort
● The algorithm:
void InsertionSort(int* v, int size){int i, j, key;for (i=1; i<size; i++){
key = v[i];j = i1;while ((j>=0) && (v[j]>key)){
v[j+1] = v[j];j = j1;
}v[j+1] = key;
}}
C code excerpt:
v = [5, 2, 4, 6, 1, 3]
Input vector:
v = [1, 2, 3, 4, 5, 6]
Output vector:
6Universidade Federal da ParaíbaCentro de Informática
Example: Insertion Sort
● The cost of each line:
void InsertionSort(int* v, int size){int i, j, key;for (i=1; i<size; i++){
key = v[i];j = i1;while ((j>=0) && (v[j]>key)){
v[j+1] = v[j];j = j1;
}v[j+1] = key;
}}
C code excerpt:
Cost(μs) # steps
c1
c2
c3
c4
c5
c6
c7
nn−1n−1∑i=1
n−1ti
∑i=1
n−1(t i−1)
∑i=1
n−1(t i−1)
n−1
7Universidade Federal da ParaíbaCentro de Informática
Example: Insertion Sort
● Total cost T(n):Cost(μs) # steps
c1
c2
c3
c4
c5
c6
c7
nn−1n−1∑i=1
n−1ti
∑i=1
n−1(t i−1)
∑i=1
n−1(t i−1)
n−1
T (n)=c1n+c2(n−1)+c3(n−1)+c 4∑i=1
n−1t i+c5∑i=1
n−1(ti−1)+c6∑i=1
n−1(t i−1)+c7(n−1)
8Universidade Federal da ParaíbaCentro de Informática
Example: Insertion Sort
● Best T(n):
void InsertionSort(int* v, int size){int i, j, key;for (i=1; i<size; i++){
key = v[i];j = i1;while ((j>=0) && (v[j]>key)){
v[j+1] = v[j];j = j1;
}v[j+1] = key;
}}
C code excerpt:
Cost(μs) # steps
c1
c2
c3
c4
c5
c6
c7
nn−1n−1
n−1
n−100
ti = 1
9Universidade Federal da ParaíbaCentro de Informática
Example: Insertion Sort
● Best T(n) (ti=1):
Cost(μs)
c1
c2
c3
c4
c5
c6
c7
T (n)=c1n+c2(n−1)+c3(n−1)+c 4∑i=1
n−1t i+c5∑i=1
n−1(ti−1)+c6∑i=1
n−1(t i−1)+c7(n−1)
nn−1n−1
n−1
n−100
# steps
T (n)=c1n+c2(n−1)+c3(n−1)+c 4(n−1)+c7(n−1)Total Cost:(for comparision)
Best case:
10Universidade Federal da ParaíbaCentro de Informática
Example: Insertion Sort
● Best T(n) (ti=1):
a=c1+c2+c3+c4+c7
b=−(c2+c3+c 4+c7)
n
T(n)
T (n)=c1n+c2(n−1)+c3(n−1)+c 4(n−1)+c7(n−1)1
T (n)=(c1+c2+c3+c4+c7)n−(c2+c3+c4+c7)2
T (n)=an+b3
11Universidade Federal da ParaíbaCentro de Informática
Example: Insertion Sort
● Worst T(n):
void InsertionSort(int* v, int size){int i, j, key;for (i=1; i<size; i++){
key = v[i];j = i1;while ((j>=0) && (v[j]>key)){
v[j+1] = v[j];j = j1;
}v[j+1] = key;
}}
C code excerpt:
Cost(μs) # steps
c1
c2
c3
c4
c5
c6
c7
nn−1n−1
n−1
∑i=1
n−1(i+1)
∑i=1
n−1i
∑i=1
n−1i
ti = i+1
12Universidade Federal da ParaíbaCentro de Informática
Example: Insertion Sort
● Worst T(n) (ti=i+1):
Cost(μs)
c1
c2
c3
c4
c5
c6
c7
T (n)=c1n+c2(n−1)+c3(n−1)+c 4∑i=1
n−1t i+c5∑i=1
n−1(ti−1)+c6∑i=1
n−1(t i−1)+c7(n−1)
# steps
Total Cost (for comparision) :
Worst case:
nn−1n−1
n−1
∑i=1
n−1(i+1)
∑i=1
n−1i
∑i=1
n−1i
T (n)=c1n+c2(n−1)+c3(n−1)+c 4∑i=1
n−1( i+1)+c5∑i=1
n−1i+c6∑i=1
n−1i+c7(n−1)
13Universidade Federal da ParaíbaCentro de Informática
c4 ( n(n+1)−22 )+c5(n(n−1)
2 )+T (n)=c1n+c2(n−1)+c3(n−1)+
c6( n (n−1)
2 )+c7 (n−1)
2
∑i=1
n−1(i+1)=
n(n+1)−22
T (n)=c1n+c2(n−1)+c3(n−1)+
c4∑i=1
n−1(i+1)+c5∑i=1
n−1i+
c6∑i=1
n−1i+c7 (n−1)
1
Example: Insertion Sort
● Worst T(n) (ti=i+1):
a=c4
2+
c5
2+
c6
2
b=c1+c2+c3+c4
2−
c5
2−
c6
2+c7
c=−(c2+c3+c4+c7)
∑i=1
n−1i=
n(n−1)
2
n
T(n)
T (n)=( c4
2+
c5
2+
c6
2 )n2+(c1+c2+c3+
c4
2−
c5
2−
c6
2+c7)n−(c2+c3+c4+c7 )3
T (n)=an2+bn+c
4
14Universidade Federal da ParaíbaCentro de Informática
Example: Insertion Sort
● Comparison:– Best case:
– Worst case:
T (n)=an−b
T (n)=an2+bn+c
n
T(n)
n
T(n)Running time is dominated by n.
Running time is dominated by n2.
15Universidade Federal da ParaíbaCentro de Informática
Discussion
● Sometimes we can determine the exact running time of an algorithm. However:– Evaluation of long expressions are time consuming.
– Most of the terms contribute too little to the description of the algorithm behavior.
– Obtained running times (in terms of time units) may vary from machine to machine.
– Small inputs are efficiently processed. We are concerned about very large inputs.