data structures - 09. introduction to the asymptotic analysis, i
DESCRIPTION
Slide da cadeira de Estrutura de Dados, ministrado pelo Prof. Dr. Christian Pagot, na Universidade Federal da Paraíba.TRANSCRIPT
![Page 1: Data Structures - 09. Introduction to the Asymptotic Analysis, I](https://reader033.vdocuments.net/reader033/viewer/2022052412/55899f68d8b42aab2b8b4711/html5/thumbnails/1.jpg)
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
![Page 2: Data Structures - 09. Introduction to the Asymptotic Analysis, I](https://reader033.vdocuments.net/reader033/viewer/2022052412/55899f68d8b42aab2b8b4711/html5/thumbnails/2.jpg)
2Universidade Federal da ParaíbaCentro de Informática
Algorithm Efficiency
● Can be measured in terms of:– Space.
– Bandwidth.
– Time !!!
– Etc.
![Page 3: Data Structures - 09. Introduction to the Asymptotic Analysis, I](https://reader033.vdocuments.net/reader033/viewer/2022052412/55899f68d8b42aab2b8b4711/html5/thumbnails/3.jpg)
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”...
![Page 4: Data Structures - 09. Introduction to the Asymptotic Analysis, I](https://reader033.vdocuments.net/reader033/viewer/2022052412/55899f68d8b42aab2b8b4711/html5/thumbnails/4.jpg)
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.
![Page 5: Data Structures - 09. Introduction to the Asymptotic Analysis, I](https://reader033.vdocuments.net/reader033/viewer/2022052412/55899f68d8b42aab2b8b4711/html5/thumbnails/5.jpg)
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:
![Page 6: Data Structures - 09. Introduction to the Asymptotic Analysis, I](https://reader033.vdocuments.net/reader033/viewer/2022052412/55899f68d8b42aab2b8b4711/html5/thumbnails/6.jpg)
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
![Page 7: Data Structures - 09. Introduction to the Asymptotic Analysis, I](https://reader033.vdocuments.net/reader033/viewer/2022052412/55899f68d8b42aab2b8b4711/html5/thumbnails/7.jpg)
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)
![Page 8: Data Structures - 09. Introduction to the Asymptotic Analysis, I](https://reader033.vdocuments.net/reader033/viewer/2022052412/55899f68d8b42aab2b8b4711/html5/thumbnails/8.jpg)
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
![Page 9: Data Structures - 09. Introduction to the Asymptotic Analysis, I](https://reader033.vdocuments.net/reader033/viewer/2022052412/55899f68d8b42aab2b8b4711/html5/thumbnails/9.jpg)
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:
![Page 10: Data Structures - 09. Introduction to the Asymptotic Analysis, I](https://reader033.vdocuments.net/reader033/viewer/2022052412/55899f68d8b42aab2b8b4711/html5/thumbnails/10.jpg)
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
![Page 11: Data Structures - 09. Introduction to the Asymptotic Analysis, I](https://reader033.vdocuments.net/reader033/viewer/2022052412/55899f68d8b42aab2b8b4711/html5/thumbnails/11.jpg)
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
![Page 12: Data Structures - 09. Introduction to the Asymptotic Analysis, I](https://reader033.vdocuments.net/reader033/viewer/2022052412/55899f68d8b42aab2b8b4711/html5/thumbnails/12.jpg)
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)
![Page 13: Data Structures - 09. Introduction to the Asymptotic Analysis, I](https://reader033.vdocuments.net/reader033/viewer/2022052412/55899f68d8b42aab2b8b4711/html5/thumbnails/13.jpg)
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
![Page 14: Data Structures - 09. Introduction to the Asymptotic Analysis, I](https://reader033.vdocuments.net/reader033/viewer/2022052412/55899f68d8b42aab2b8b4711/html5/thumbnails/14.jpg)
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.
![Page 15: Data Structures - 09. Introduction to the Asymptotic Analysis, I](https://reader033.vdocuments.net/reader033/viewer/2022052412/55899f68d8b42aab2b8b4711/html5/thumbnails/15.jpg)
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.