INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell...
Transcript of INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell...
![Page 1: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/1.jpg)
INF421, Lecture 1Lists and Complexity
Leo Liberti
LIX, Ecole Polytechnique, France
INF421, Lecture 1 – p. 1
![Page 2: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/2.jpg)
Course
Objective: to teach you some data structures and associated
algorithms
Evaluation: TP noté en salle info le 16 septembre, Contrôle à la fin.
Note: max(CC, 34CC + 1
4TP )
Organization: fri 26/8, 2/9, 9/9, 16/9, 23/9, 30/9, 7/10, 14/10, 21/10,
amphi 1030-12 (Arago), TD 1330-1530, 1545-1745 (SI31,32,33,34)
Books:
1. Ph. Baptiste & L. Maranget, Programmation et Algorithmique, Ecole Polytechnique
(Polycopié), 2006
2. G. Dowek, Les principes des langages de programmation, Editions de l’X, 2008
3. D. Knuth, The Art of Computer Programming, Addison-Wesley, 1997
4. K. Mehlhorn & P. Sanders, Algorithms and Data Structures, Springer, 2008
Website: www.enseignement.polytechnique.fr/informatique/INF421
Contact: [email protected] (e-mail subject: INF421)
INF421, Lecture 1 – p. 2
![Page 3: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/3.jpg)
Lecture summary
Reminders
Complexity
Lists
INF421, Lecture 1 – p. 3
![Page 4: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/4.jpg)
Reminders
INF421, Lecture 1 – p. 4
![Page 5: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/5.jpg)
Memory
address
Memory cell
has an address
stores a datum d
INF421, Lecture 1 – p. 5
![Page 6: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/6.jpg)
Memory
address
Memory cell
has an address
stores a datum d
Two operations
Move datum from cell to CPU
(read)
0x1FFF241 CP
Ud
d
Move datum from CPU to cell
(write)
0x1FFF241 CP
Ud
INF421, Lecture 1 – p. 5
![Page 7: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/7.jpg)
Memory
address
Memory cell
has an address
stores a datum d
Two operations
Move datum from cell to CPU
(read)
0x1FFF241 CP
Ud
d
Move datum from CPU to cell
(write)
0x1FFF241 CP
Ud
Representation of memory: a sequence of cells
0x0
d0
0x1
d1
0x2
d2
0x3
d3
0x4
d4
0x5
d5
INF421, Lecture 1 – p. 5
![Page 8: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/8.jpg)
Memory
address
Memory cell
has an address
stores a datum d
Two operations
Move datum from cell to CPU
(read)
0x1FFF241 CP
Ud
d
Move datum from CPU to cell
(write)
0x1FFF241 CP
Ud
Representation of memory: a sequence of cells
0x0
d0
0x1
d1
0x2
d2
0x3
d3
0x4
d4
0x5
d5A function D : A → D
A: set of addresses
D: set of data elements
INF421, Lecture 1 – p. 5
![Page 9: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/9.jpg)
Assumptions
For theoretical purposes, assume memory is infinite
→ In practice it is finite
Each datum can be stored in a single cell
→ Different data elements might have different sizes
INF421, Lecture 1 – p. 6
![Page 10: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/10.jpg)
Naming memory
A program variable is just a namefor a chunk of memory
x denotes:0x4 0x5 0x6 0x7
INF421, Lecture 1 – p. 7
![Page 11: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/11.jpg)
Naming memory
A program variable is just a namefor a chunk of memory
x denotes:0x4 0x5 0x6 0x7
We simply associate a name to the starting address
The size of the chunk is given by the name’s type
INF421, Lecture 1 – p. 7
![Page 12: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/12.jpg)
Naming memory
A program variable is just a namefor a chunk of memory
x denotes:0x4 0x5 0x6 0x7
We simply associate a name to the starting address
The size of the chunk is given by the name’s type
Basic types: int, long, char, float, double
Composite types: Cartesian products of basic types
if y.a ∈ int and y.b ∈ float then y ∈ int × float
INF421, Lecture 1 – p. 7
![Page 13: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/13.jpg)
Basic operations
Assignment: write value in memory cell(s) named byvariable (i.e. “variable=value”)
Arithmetic: +,−,×,÷ for integer and floating pointnumbers
Test: evaluate a logical condition: if true, changeaddress of next instruction to be executed
Loop: instead of performing next instruction in memory,jump to an instruction at a given address (more like a “go to”)
INF421, Lecture 1 – p. 8
![Page 14: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/14.jpg)
Basic operations
Assignment: write value in memory cell(s) named byvariable (i.e. “variable=value”)
Arithmetic: +,−,×,÷ for integer and floating pointnumbers
Test: evaluate a logical condition: if true, changeaddress of next instruction to be executed
Loop: instead of performing next instruction in memory,jump to an instruction at a given address (more like a “go to”)
WARNING! In these slides, I use “=” to mean two different things:
1. in assignments, “variable = value” means “put value in the cell whose address is
named by variable”
2. in tests, “variable = value” is TRUE if the cell whose address is named by variable
contains value, and FALSE otherwise
in C/C++/Java “=” is used for assignments, and “==” for tests
INF421, Lecture 1 – p. 8
![Page 15: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/15.jpg)
Composite operations: programs
Programs are built recursively from basic operations
If A, B are ops, then concatenation “A;B” is an op
Semantics: execute A, then execute B
INF421, Lecture 1 – p. 9
![Page 16: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/16.jpg)
Composite operations: programs
Programs are built recursively from basic operations
If A, B are ops, then concatenation “A;B” is an op
Semantics: execute A, then execute B
If A, B are ops and T is a test, “if (T) A else B” isan op
Semantics: if T is true execute A, else B
INF421, Lecture 1 – p. 9
![Page 17: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/17.jpg)
Composite operations: programs
Programs are built recursively from basic operations
If A, B are ops, then concatenation “A;B” is an op
Semantics: execute A, then execute B
If A, B are ops and T is a test, “if (T) A else B” isan op
Semantics: if T is true execute A, else B
If A is an op and T is a test, “while (T) A” is an op
Semantics: 1:(if (T) A else (go to 2)) (go to 1) 2:
INF421, Lecture 1 – p. 9
![Page 18: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/18.jpg)
An example
1: input n;2: int s = 0;3: int i = 1;4: while (i ≤ n) do5: s = s+ i;6: i = i+ 1;7: end while8: output s;
n ? s ? i ?
INF421, Lecture 1 – p. 10
![Page 19: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/19.jpg)
An example
1: input n ;
2: int s = 0;3: int i = 1;4: while (i ≤ n) do5: s = s+ i;6: i = i+ 1;7: end while8: output s;
n 2 s ? i ?
INF421, Lecture 1 – p. 10
![Page 20: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/20.jpg)
An example
1: input n;
2: int s = 0 ;3: int i = 1;4: while (i ≤ n) do5: s = s+ i;6: i = i+ 1;7: end while8: output s;
n 2 s 0 i ?
INF421, Lecture 1 – p. 10
![Page 21: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/21.jpg)
An example
1: input n;2: int s = 0;
3: int i = 1 ;
4: while (i ≤ n) do5: s = s+ i;6: i = i+ 1;7: end while8: output s;
n 2 s 0 i 1
INF421, Lecture 1 – p. 10
![Page 22: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/22.jpg)
An example
1: input n;2: int s = 0;3: int i = 1;
4: while (i ≤ n) do
5: s = s+ i;6: i = i+ 1;7: end while8: output s;
n 2 s 0 i 1
i ≤ n ≡ 1 ≤ 2: true
INF421, Lecture 1 – p. 10
![Page 23: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/23.jpg)
An example
1: input n;2: int s = 0;3: int i = 1;4: while (i ≤ n) do
5: s = s+ i ;6: i = i+ 1;7: end while8: output s;
n 2 s 1 i 1
INF421, Lecture 1 – p. 10
![Page 24: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/24.jpg)
An example
1: input n;2: int s = 0;3: int i = 1;4: while (i ≤ n) do5: s = s+ i;
6: i = i+ 1 ;
7: end while8: output s;
n 2 s 1 i 2
INF421, Lecture 1 – p. 10
![Page 25: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/25.jpg)
An example
1: input n;2: int s = 0;3: int i = 1;
4: while (i ≤ n) do
5: s = s+ i;6: i = i+ 1;7: end while8: output s;
n 2 s 1 i 2
i ≤ n ≡ 2 ≤ 2: true
INF421, Lecture 1 – p. 10
![Page 26: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/26.jpg)
An example
1: input n;2: int s = 0;3: int i = 1;4: while (i ≤ n) do
5: s = s+ i ;6: i = i+ 1;7: end while8: output s;
n 2 s 3 i 2
INF421, Lecture 1 – p. 10
![Page 27: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/27.jpg)
An example
1: input n;2: int s = 0;3: int i = 1;4: while (i ≤ n) do5: s = s+ i;
6: i = i+ 1 ;
7: end while8: output s;
n 2 s 3 i 3
INF421, Lecture 1 – p. 10
![Page 28: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/28.jpg)
An example
1: input n;2: int s = 0;3: int i = 1;
4: while (i ≤ n) do
5: s = s+ i;6: i = i+ 1;7: end while8: output s;
n 2 s 3 i 3
i ≤ n ≡ 3 ≤ 2: false
INF421, Lecture 1 – p. 10
![Page 29: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/29.jpg)
An example
1: input n;2: int s = 0;3: int i = 1;4: while (i ≤ n) do5: s = s+ i;6: i = i+ 1;7: end while8: output s ;
n 2 s 3 i 3
output s = 3
INF421, Lecture 1 – p. 10
![Page 30: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/30.jpg)
Complexity
INF421, Lecture 1 – p. 11
![Page 31: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/31.jpg)
Complexity
Several different programs can yield the same result:which is best?
Evaluate their time (and/or space) complexity
time complexity: how many “basic operations”
space complexity: how much memory
used by the program during execution
Worst case: max values during execution
Best case: min values during execution
Average case: average values during execution
P : a programtP : number of basic operations performed by P
INF421, Lecture 1 – p. 12
![Page 32: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/32.jpg)
Time complexity (worst case)
∀P ∈ {assignment,arithmetic,test}:
tP = 1
INF421, Lecture 1 – p. 13
![Page 33: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/33.jpg)
Time complexity (worst case)
∀P ∈ {assignment,arithmetic,test}:
tP = 1
Concatenation: for P,Q programs:
tP ;Q = tP + tQ
INF421, Lecture 1 – p. 13
![Page 34: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/34.jpg)
Time complexity (worst case)
∀P ∈ {assignment,arithmetic,test}:
tP = 1
Concatenation: for P,Q programs:
tP ;Q = tP + tQ
Test: for P,Q programs and R a test:
tif (T ) P else Q = tT +max(tP , tQ)
max: worst-case policy
INF421, Lecture 1 – p. 13
![Page 35: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/35.jpg)
Time complexity (worst case)
∀P ∈ {assignment,arithmetic,test}:
tP = 1
Concatenation: for P,Q programs:
tP ;Q = tP + tQ
Test: for P,Q programs and R a test:
tif (T ) P else Q = tT +max(tP , tQ)
max: worst-case policy
Loop: it’s complicated
(depends on how and when loop terminates)
INF421, Lecture 1 – p. 13
![Page 36: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/36.jpg)
Loop complexity example
The complete loopLet P be the following program:
1: i = 0 ;2: while (i < n) do3: A;4: i = i+ 1;5: end while
Assume A does not change the value of i
Body of loop executed n times
tP (n) = 1 + n(tA + 3)
Why the ‘3’? Well, t(i<n) = 1, t(i+1) = 1, t(i=·) = 1
INF421, Lecture 1 – p. 14
![Page 37: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/37.jpg)
Orders of complexity
In the above program, suppose tA = 12n
INF421, Lecture 1 – p. 15
![Page 38: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/38.jpg)
Orders of complexity
In the above program, suppose tA = 12n
Then tP = 12n
2 + 3n+ 1
INF421, Lecture 1 – p. 15
![Page 39: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/39.jpg)
Orders of complexity
In the above program, suppose tA = 12n
Then tP = 12n
2 + 3n+ 1
No one really cares about the constants 2, 3: all that
matters is that tP “behaves no worse than” the fn. n2
12n
2 + 3 is O(n2)
INF421, Lecture 1 – p. 15
![Page 40: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/40.jpg)
Orders of complexity
In the above program, suppose tA = 12n
Then tP = 12n
2 + 3n+ 1
No one really cares about the constants 2, 3: all that
matters is that tP “behaves no worse than” the fn. n2
12n
2 + 3 is O(n2)
A function f(n) is order of g(n) (notation: O(g(n))) if:
∃c > 0 ∃n0 ∈ N ∀n > n0 (f(n) ≤ cg(n)) (4)
INF421, Lecture 1 – p. 15
![Page 41: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/41.jpg)
Orders of complexity
In the above program, suppose tA = 12n
Then tP = 12n
2 + 3n+ 1
No one really cares about the constants 2, 3: all that
matters is that tP “behaves no worse than” the fn. n2
12n
2 + 3 is O(n2)
A function f(n) is order of g(n) (notation: O(g(n))) if:
∃c > 0 ∃n0 ∈ N ∀n > n0 (f(n) ≤ cg(n)) (5)
For 12n
2 + 3, c = 1 and n0 = 2
INF421, Lecture 1 – p. 15
![Page 42: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/42.jpg)
Some examples
Functions Order
an+ b with a, b constants O(n)
polynomial of degree d′ in n O(nd) with d ≥ d′
n+ log n O(n)
n+√n O(n)
log n+√n O(
√n)
n log n3 O(n log n)an+bcn+d , a, b, c, d constants O(1)
Make an effort to find the best (most slowly increasing)function g(n) when saying “f(n) is O(g(n))”
E.g. no one would say that 2n+ 1 is O(n4) (although it’stechnically true) — rather say 2n+ 1 is O(n)
INF421, Lecture 1 – p. 16
![Page 43: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/43.jpg)
Remark
The complexity order is an asymptotic description oftP (n)
If tP (n) does not depend on n, its order of complexity isO(1) (i.e. constant)
Example: looping 101000 times over an O(1) code stillyields an O(1) program
In other words, n must appear as a parameter of theprogram for the complexity order to be anything otherthan constant
INF421, Lecture 1 – p. 17
![Page 44: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/44.jpg)
Complexity of easy loops
1: input n;
2: int s = 0;
3: int i = 1;
4: while (i ≤ n) do
5: s = s+ i;
6: i = i+ 1;
7: end while
8: output s;
t(n) = 3 + 5n+ 1 = 4n+ 4
⇒ t(n) is O(n)
INF421, Lecture 1 – p. 18
![Page 45: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/45.jpg)
Complexity of easy loops
1: input n;
2: int s = 0;
3: int i = 1;
4: while (i ≤ n) do
5: s = s+ i;
6: i = i+ 1;
7: end while
8: output s;
t(n) = 3 + 5n+ 1 = 4n+ 4
⇒ t(n) is O(n)
1: for i = 0; i < n− 1; i = i+ 1 do
2: for j = i+1; j < n; j = j +1 do
3: print i, j;
4: end for
5: end for
t(n) = 1 +
(5(n− 1) + 6) + . . .+ (5 + 6)︸ ︷︷ ︸
n−1
= 1 + 5((n− 1) + . . .+ 1) +
6(n−1) = 5
2n(n−1)+6n−5
= 5
2n2 + 7
2n− 5
t(n) is O(n2)
INF421, Lecture 1 – p. 18
![Page 46: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/46.jpg)
Arrays
INF421, Lecture 1 – p. 19
![Page 47: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/47.jpg)
Like a vector in maths
A vector x ∈ Qn is an n-tuple (x1, . . . , xn) for some n ∈ N
In computers: x is the name for a memory address withn successive cells
Indexing starts from 0 (last cell is called xn−1)
x : x0 x1 x2 x3 x4
INF421, Lecture 1 – p. 20
![Page 48: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/48.jpg)
Like a vector in maths
A vector x ∈ Qn is an n-tuple (x1, . . . , xn) for some n ∈ N
In computers: x is the name for a memory address withn successive cells
Indexing starts from 0 (last cell is called xn−1)
x : x0 x1 x2 x3 x4
An array is allocated when the memory is reserved
The size of the array, n, is decided at allocation time
Usually, the size of the array does not change
When the array is no longer useful, the reserved memory can be
deallocated or freed
INF421, Lecture 1 – p. 20
![Page 49: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/49.jpg)
Array operations
For an array of size n:
Operations Complexity
Read value of i-th component O(1)
Write value in i-th component O(1)
Size O(1)
Remove element (cell) forget it∗
Insert element (cell) forget it∗
Move subsequence to position i O(n)
Moving subsequence L to position i:extract (contiguous) subsequence L from the array, and re-insert it
after position i and before position i+ 1
i i iL1 L1 L1 L2L2 L2
∗: can actually simulate these operations using pointers
INF421, Lecture 1 – p. 21
![Page 50: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/50.jpg)
Norm of a vector in R5
1: input x ∈ Q5;2: int i = 0;3: float a = 0;4: while (i < 5) do5: a = a+ xi × xi;6: end while7: a = sqrt(a);
Computes
√∑4
i=0 x2i
Complexity: O(1) (why?)
INF421, Lecture 1 – p. 22
![Page 51: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/51.jpg)
Incomplete loop
1: input x ∈ {0, 1}n;2: int i = 0;3: while (i < n ∧ xi = 1) do4: xi = 0;5: i = i+ 1;6: end while7: if (i < n) then8: xi = 1;9: end if
10: output x;
Input Output
(0,0,0,0) (1,0,0,0)
(1,1,0,0) (0,0,1,0)
(0,1,1,0) (1,1,1,0)
(1,1,1,1) (0,0,0,0)
Components of x can only be 0 or 1
Loop continues over all components as long as their value is 1; at
the first 0 component, it stops
Complexity?
INF421, Lecture 1 – p. 23
![Page 52: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/52.jpg)
Worst case complexity of incomplete loop
Among all inputs of the algorithm, find those yieldingthe worst complexity
In the case above, x = (1, 1, . . . , 1) always makes theloop continue to the end, i.e. for n iterationsThm.
(1, 1 . . . , 1) is the input yielding worst complexity
Proof
Suppose false, then there is a vector x 6= (1, . . . , 1) yielding a complexity t(n) >
n. Since x 6= (1, . . . , 1), x contains at least one 0 component. Let j < n be the
smallest index such that xj = 0: at iteration j the loop breaks, and the complexity
is t(n) = j, which is smaller than n: contradiction.
Since the other operations are O(1), get O(n)
INF421, Lecture 1 – p. 24
![Page 53: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/53.jpg)
Worst case complexity of incomplete loop
Among all inputs of the algorithm, find those yieldingthe worst complexity
In the case above, x = (1, 1, . . . , 1) always makes theloop continue to the end, i.e. for n iterationsThm.
(1, 1 . . . , 1) is the input yielding worst complexity
Proof
Suppose false, then there is a vector x 6= (1, . . . , 1) yielding a complexity t(n) >
n. Since x 6= (1, . . . , 1), x contains at least one 0 component. Let j < n be the
smallest index such that xj = 0: at iteration j the loop breaks, and the complexity
is t(n) = j, which is smaller than n: contradiction.
Since the other operations are O(1), get O(n)
Potential difficulty of this approach: identifying the worst-case inputs and proving no other input is worse
INF421, Lecture 1 – p. 24
![Page 54: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/54.jpg)
Average case complexity of incomplete loop (1/2)
Average case analysis needs a probability space:
assume the event xi = b is independent of the events
xj = b for all i 6= j
assume each cell xi of the array contains 0 or 1 with equal
probability 12
INF421, Lecture 1 – p. 25
![Page 55: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/55.jpg)
Average case complexity of incomplete loop (1/2)
Average case analysis needs a probability space:
assume the event xi = b is independent of the events
xj = b for all i 6= j
assume each cell xi of the array contains 0 or 1 with equal
probability 12
For any vector having first k+1 components (1, . . . , 1︸ ︷︷ ︸
k
, 0),
the loop is executed k times (for all 0 ≤ k < n)
Event of a binary (k + 1)-vector having given components has probability(1
2
)k+1
INF421, Lecture 1 – p. 25
![Page 56: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/56.jpg)
Average case complexity of incomplete loop (1/2)
Average case analysis needs a probability space:
assume the event xi = b is independent of the events
xj = b for all i 6= j
assume each cell xi of the array contains 0 or 1 with equal
probability 12
For any vector having first k+1 components (1, . . . , 1︸ ︷︷ ︸
k
, 0),
the loop is executed k times (for all 0 ≤ k < n)
Event of a binary (k + 1)-vector having given components has probability(1
2
)k+1
If the vector is (1, . . . , 1︸ ︷︷ ︸
n
) the loop is executed n times
Event of a binary n-vector having given components has probability(1
2
)n
INF421, Lecture 1 – p. 25
![Page 57: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/57.jpg)
Average case complexity of incomplete loop (2/2)
The loop is executed k times with probability(12
)k+1, for k < n
INF421, Lecture 1 – p. 26
![Page 58: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/58.jpg)
Average case complexity of incomplete loop (2/2)
The loop is executed k times with probability(12
)k+1, for k < n
The loop is executed n times with probability(12
)n
INF421, Lecture 1 – p. 26
![Page 59: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/59.jpg)
Average case complexity of incomplete loop (2/2)
The loop is executed k times with probability(12
)k+1, for k < n
The loop is executed n times with probability(12
)n
Average number of executions:
n−1∑
k=0
k2−(k+1) + n2−n ≤n−1∑
k=0
k2−k + n2−n =
n∑
k=0
k2−k
INF421, Lecture 1 – p. 26
![Page 60: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/60.jpg)
Average case complexity of incomplete loop (2/2)
The loop is executed k times with probability(12
)k+1, for k < n
The loop is executed n times with probability(12
)n
Average number of executions:
n−1∑
k=0
k2−(k+1) + n2−n ≤n−1∑
k=0
k2−k + n2−n =
n∑
k=0
k2−k
Thm.
limn→∞
∑n
k=0 k2−k = 2
Proof
Geometric series∑
k≥0 qk = 1
1−qfor q ∈ [0, 1). Differentiate w.r.t. q, get
∑
k≥0 kqk−1 = 1
(1−q)2 ; multiply by q, get∑
k≥0 kqk = q
(1−q)2 . For q = 12 ,
get∑
k≥0 k2−k = (1/2)/(1/4) = 2.
INF421, Lecture 1 – p. 26
![Page 61: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/61.jpg)
Average case complexity of incomplete loop (2/2)
The loop is executed k times with probability(12
)k+1, for k < n
The loop is executed n times with probability(12
)n
Average number of executions:
n−1∑
k=0
k2−(k+1) + n2−n ≤n−1∑
k=0
k2−k + n2−n =
n∑
k=0
k2−k
Thm.
limn→∞
∑n
k=0 k2−k = 2
Proof
Geometric series∑
k≥0 qk = 1
1−qfor q ∈ [0, 1). Differentiate w.r.t. q, get
∑
k≥0 kqk−1 = 1
(1−q)2 ; multiply by q, get∑
k≥0 kqk = q
(1−q)2 . For q = 12 ,
get∑
k≥0 k2−k = (1/2)/(1/4) = 2.
Hence, the average complexity is constant O(1)
INF421, Lecture 1 – p. 26
![Page 62: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/62.jpg)
Jagged arrays
Jagged array: a vector whose components are vectors ofpossibly different sizes
E.g. x = ((x00, x01), (x10, x11, x12))
x :
x0 : x00 x01
x1 : x10 x11 x12
INF421, Lecture 1 – p. 27
![Page 63: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/63.jpg)
Jagged arrays
Jagged array: a vector whose components are vectors ofpossibly different sizes
E.g. x = ((x00, x01), (x10, x11, x12))
x :
x0 : x00 x01
x1 : x10 x11 x12
Special case: when all subvector sizes are the same, geta matrix: int x[][] = new int [2][3];
x =
(
x00 x01 x02
x10 x11 x12
)
INF421, Lecture 1 – p. 27
![Page 64: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/64.jpg)
Representing relations
Jagged arrays can be used to represent a relation on afinite set
INF421, Lecture 1 – p. 28
![Page 65: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/65.jpg)
Representing relations
Jagged arrays can be used to represent a relation on afinite set
Let V = {v1 . . . , vn} and E a relation on V
E is a set of ordered pairs (u, v)
INF421, Lecture 1 – p. 28
![Page 66: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/66.jpg)
Representing relations
Jagged arrays can be used to represent a relation on afinite set
Let V = {v1 . . . , vn} and E a relation on V
E is a set of ordered pairs (u, v)
Representation:
array of n components
the i-th component is the array of vj related to vi
INF421, Lecture 1 – p. 28
![Page 67: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/67.jpg)
Representing relations
Jagged arrays can be used to represent a relation on afinite set
Let V = {v1 . . . , vn} and E a relation on V
E is a set of ordered pairs (u, v)
Representation:
array of n components
the i-th component is the array of vj related to vi
Example: V = {1, 2, 3},E = {(1, 1), (1, 2), (2, 3), (3, 1), (3, 2), (3, 3)}
E :
1 1 2
2 3
3 1 2 3
INF421, Lecture 1 – p. 28
![Page 68: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/68.jpg)
Application: Networks
INF421, Lecture 1 – p. 29
![Page 69: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/69.jpg)
Array shortcomings
Essentially fixed size
Size must be known in advance
Changing relative positions of elements is inefficient
INF421, Lecture 1 – p. 30
![Page 70: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/70.jpg)
Lists
INF421, Lecture 1 – p. 31
![Page 71: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/71.jpg)
Doubly linked list
A nodepointers
⊥ x1 x2 x3
Node N : a list element
N.prev = address of previous node in list
N.next = address of next node in list
N.datum = the data element stored in the node
Placeholder node ⊥: before the first element, after the lastelement, no stored data
Every node has two pointers, and is pointed to by two nodes
INF421, Lecture 1 – p. 32
![Page 72: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/72.jpg)
Remove a node
Remove current node (this)
⊥ x1 x2 x3
In the example, this= x2
1: this.prev.next = this.next ;2: this.next.prev = this.prev ;
Worst case complexity: O(1)
INF421, Lecture 1 – p. 33
![Page 73: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/73.jpg)
Insert a node
Insert current node (this) after node x1
⊥
x1 x2N
In the example, this= N
1: this.prev = x1 ;2: this.next = x1.next ;3: x1.next = this ;4: this.next.prev = this ;
Worst case complexity: O(1)
INF421, Lecture 1 – p. 34
![Page 74: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/74.jpg)
Find next
Given a list L and a node x, find next occurrence ofelement b
If b ∈ L return node where b is stored, else return ⊥
1: while (x.datum 6= b ∧ x 6= ⊥) do2: x = x.next
3: end while4: return x
Warning: every test costs 2 basic operations, inefficient
INF421, Lecture 1 – p. 35
![Page 75: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/75.jpg)
Find next
Given a list L and a node x, find next occurrence ofelement b
If b ∈ L return node where b is stored, else return ⊥
1: while (x.datum 6= b ∧ x 6= ⊥) do2: x = x.next
3: end while4: return x
Warning: every test costs 2 basic operations, inefficient
1: ⊥.datum = b
2: while (x.datum 6= b) do3: x = x.next
4: end while5: return x
Now ttest = 1
INF421, Lecture 1 – p. 35
![Page 76: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/76.jpg)
List operations
For a doubly-linked list of size n:
Operations Complexity
Read/write value of i-th node O(n)
Find next O(n)
Sizea O(n)
Is it empty? O(1)
Read/write value of first/last node O(1)
Remove element O(1)
Insert element O(1)
Move subsequence to position i O(1)
Pop from front/back O(1)
Push to front/back O(1)
Concatenate O(1)
aSome implementations are O(1) by storing and updating size INF421, Lecture 1 – p. 36
![Page 77: INF421,Lecture1 ListsandComplexity€¦ · INF421, Lecture 1 – p. 5. Memory address Memorycell has an address stores a datum d Twooperations Move datum from cell to CPU (read) 0x1FFF241](https://reader034.fdocuments.in/reader034/viewer/2022050506/5f97f7107761d7494566d765/html5/thumbnails/77.jpg)
End of Lecture 1
INF421, Lecture 1 – p. 37