Cub11k's BIU Notes
Cub11k's BIU Notes
Assignments
Discrete-math
Discrete-math 1
Discrete-math 10
Discrete-math 11
Discrete-math 12
Discrete-math 2
Discrete-math 3
Discrete-math 4
Discrete-math 5
Discrete-math 6
Discrete-math 7
Discrete-math 8
Discrete-math 9
Infi-1
Infi-1 10
Infi-1 11
Infi-1 2
Infi-1 3
Infi-1 4
Infi-1 5
Infi-1 6
Infi-1 7
Infi-1 8
Infi-1 9
Linear-1
Linear-1 1
Linear-1 10
Linear-1 11
Linear-1 12
Linear-1 2
Linear-1 3
Linear-1 4
Linear-1 5
Linear-1 6
Linear-1 7
Linear-1 8
Linear-1 9
Linear-2
Linear-2 1
Lectures
Data-structures
Data-structures 1
Data-structures 2
Data-structures 3
Discrete-math
Discrete-math 10
Discrete-math 11
Discrete-math 12
Discrete-math 13
Discrete-math 14
Discrete-math 15
Discrete-math 16
Discrete-math 18
Discrete-math 19
Discrete-math 20
Discrete-math 21
Discrete-math 22
Discrete-math 23
Discrete-math 24
Discrete-math 25
Discrete-math 26
Discrete-math 3
Discrete-math 4
Discrete-math 5
Discrete-math 6
Discrete-math 7
Discrete-math 8
Discrete-math 9
Exam 2023 (2A)
Exam 2023 (2B)
Exam 2023 (A)
Exam 2023 (B)
Exam 2023 (C)
Exam 2024 (A)
Exam 2024 (B)
Exam 2024 (C)
Midterm
Infi-1
Exam 2022B (A)
Exam 2022B (B)
Exam 2023B (A)
Exam 2023B (B)
Exam 2024 (A)
Exam 2024 (B)
Exam 2025 (A)
Infi-1 10
Infi-1 12
Infi-1 13
Infi-1 14
Infi-1 15
Infi-1 16
Infi-1 17
Infi-1 19
Infi-1 20
Infi-1 21
Infi-1 22
Infi-1 23
Infi-1 24
Infi-1 25
Infi-1 26
Infi-1 5
Infi-1 6
Infi-1 7
Infi-1 9
Midterm
Theorems and proofs
Infi-2
Infi-2 1
Infi-2 10
Infi-2 11
Infi-2 12
Infi-2 13
Infi-2 14
Infi-2 15
Infi-2 16
Infi-2 17
Infi-2 2-3
Infi-2 3-4
Infi-2 5
Infi-2 6
Infi-2 7
Infi-2 8
Infi-2 9
Linear-1
Exam 2023 (B)
Exam 2023 (C)
Exam 2024 (A)
Exam 2024 (B)
Exam 2024 (C)
Exam 2025 (A)
Linear-1 11
Linear-1 12
Linear-1 13
Linear-1 4
Linear-1 5
Linear-1 6
Linear-1 7
Linear-1 8
Linear-1 9
Midterm
Random exams
Theorems and proofs
Linear-2
Linear-2 1
Linear-2 2
Linear-2 3
Linear-2 4
Linear-2 5
Linear-2 6
Linear-2 7
Linear-2 8
Seminars
CSI
CSI 2
Data-structures
Data-structures 1
Data-structures 2
Data-structures 3
Discrete-math
Discrete-math 1
Discrete-math 10
Discrete-math 11
Discrete-math 12
Discrete-math 2
Discrete-math 3
Discrete-math 4
Discrete-math 5
Discrete-math 6
Discrete-math 7
Discrete-math 8
Discrete-math 9
Infi-1
Infi-1 10
Infi-1 11
Infi-1 12
Infi-1 13
Infi-1 3
Infi-1 4
Infi-1 5
Infi-1 6
Infi-1 8
Infi-2
Infi-2 1
Infi-2 2
Infi-2 3
Infi-2 4
Infi-2 6
Infi-2 7
Infi-2 8
Linear-1
Linear-1 10
Linear-1 11
Linear-1 12
Linear-1 3
Linear-1 5
Linear-1 6
Linear-1 7
Linear-1 8
Linear-1 9
Linear-2
Linear-2 1
Linear-2 2
Linear-2 3
Linear-2 4
Linear-2 5
Linear-2 6
Linear-2 7
Templates
Lecture Template
Seminar Template
Home
Data-structures 2
Amortized complexity
#definition
Method of assessing algorithms performance based on it over-all complexity,
and not just worst edge-cases
Aggregate analysis
Compute total cost over multiple operations and find the average
Useful when operations have predictable, recurring costs (!!)
Let’s examine a dynamic array that doubles in size when full
Each insertion is either
O
(
1
)
or
(
m
)
where
m
is the current number of elements in array
Let’s aggregate all insertions:
Assume array starts with size
1
First insertion is
1
itself
Secong insertion is
1
itself
+
2
for resizing up to
4
Third insertion is
1
itself
Fourth insertion is
1
itself
+
4
for resizing up to
8
Fifth to seventh insertion are
1
themselves
…
Let
n
=
2
k
⟹
k
=
log
n
Total number of operations for
n
insertions is:
T
(
n
)
=
(
1
+
2
+
4
+
8
+
16
+
⋯
+
n
)
+
n
=
=
2
log
n
−
1
2
−
1
+
n
=
2
n
−
1
∼
2
n
⟹
T
(
n
)
=
O
(
n
)
,
T
A
(
n
)
=
2
n
n
=
O
(
1
)
⟹
Amortized complexity of our insertions is
O
(
1
)
Although there are heavy operations like the last resizing, that costs
O
(
n
)
on itself,
these operations occur rarely and contribute less to the over-all complexity,
than myriads of simple
O
(
1
)
insertions
Accounting method
Assign pre-paid "credits" to balance cheap and expensive operations
Useful when future operations are clearly predictable and can be "pre-paid"
Let’s examine a stack with
p
u
s
h
,
p
o
p
and
m
u
l
t
i
P
o
p
methods
Each
p
u
s
h
operation costs
O
(
1
)
as it is an insertion to an array
Each
p
o
p
operation costs
O
(
1
)
as it is a simple extraction from the end of array
Each
m
u
l
t
i
P
o
p
operation costs
O
(
m
)
as it is
m
extractions
Let’s use a trick of overestimating
p
u
s
h
operation, assume it costs
2
operations
but store the unused "credit" for future use
Each
p
u
s
h
would then contribute to a
+
1
credit on the balance
Each
p
o
p
can then become "free" by using this stored "credit"
We can safely assume that when executing
p
o
p
there will be at least one "credit",
or the stack is empty and operation is simply
O
(
1
)
m
u
l
t
i
P
o
p
then also becomes "free" as it is "pre-paid" for by all the
p
u
s
h
operations
⟹
Over-all cost over
n
p
u
s
h
e
s
and
p
o
p
s
/
m
u
l
t
i
P
o
p
s
is
2
n
Which gives us an amortized complexity of
O
(
1
)
Potential method
Use a potential function to track stored work and balance costs instead of "credits"
Useful when state changes over time and can be modeled using a function
Let’s examine a binary counter
Each increment the counter has to flip some number bits
For
0000
→
0001
it’s one bit
For
0111
→
1000
it’s four bits
Let us define a work function:
w
(
s
)
=
#
of bit flips
Let us define a potential function:
Φ
(
s
)
=
#
of
1
′
s
in
s
Let us then define amortized cost of each increment as:
f
(
s
)
=
w
(
s
)
+
(
Φ
(
s
+
1
)
−
Φ
(
s
)
)
meaning that operations contributing to increase in
w
will become more expensive
while operations, that are expensive on its own will be compensated for reducing potential
f
(
0000
)
=
1
+
(
Φ
(
0001
)
−
Φ
(
0000
)
)
=
2
f
(
0001
)
=
2
+
(
Φ
(
0010
)
−
Φ
(
0001
)
)
=
2
f
(
0010
)
=
1
+
(
Φ
(
0011
)
−
Φ
(
0010
)
)
=
2
…
f
(
0111
)
=
4
+
(
Φ
(
1000
)
−
Φ
(
0111
)
)
=
2
f
(
1111
)
=
4
+
(
Φ
(
0000
)
−
Φ
(
1111
)
)
=
0
⟹
T
(
n
)
=
∑
i
=
1
n
f
(
s
i
)
≤
∑
i
=
1
n
2
=
2
n
⟹
T
(
n
)
=
O
(
n
)