Transcript PPT

CS 332: Algorithms
Dijkstra’s Algorithm Continued
Disjoint-Set Union
Return to MST (Kruskal)
Amortized Analysis
David Luebke
1
7/27/2016
Review: Minimum Spanning Tree

Problem: given a connected, undirected,
weighted graph, find a spanning tree using
edges that minimize the total weight
6
4
5
9
14
2
10
15
3
David Luebke
8
2
7/27/2016
Review: Minimum Spanning Tree


MSTs satisfy the optimal substructure property: an
optimal tree is composed of optimal subtrees
If T is MST of G, and A  T is a subtree of T, and
(u,v) is the min-weight edge connecting A to V-A,
then (u,v)  T
David Luebke
3
7/27/2016
Review: Prim’s Algorithm
4
MST-Prim(G, w, r)
6
4
Q = V[G];
5
5
2
for each u  Q
14
2
key[u] = ;
10
key[r] = 0;
0
8
p[r] = NULL;
3
8
3
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
David Luebke
4
9
9
u
15
15
7/27/2016
Review: Prim’s Algorithm
MST-Prim(G, w, r)
Q = V[G];
What will be the running time?
for each u  Q
key[u] = ;
A: Depends on queue
key[r] = 0;
binary heap: O(E lg V)
p[r] = NULL;
Fibonacci heap: O(V lg V + E)
while (Q not empty)
u = ExtractMin(Q);
for each v  Adj[u]
if (v  Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
David Luebke
5
7/27/2016
Review:
Single-Source Shortest Path

Problem: given a weighted directed graph G,
find the minimum-weight path from a given
source vertex s to another vertex v



“Shortest-path” = minimum weight
Weight of path is sum of edges
E.g., a road map: what is the shortest path from
Chapel Hill to Charlottesville?
David Luebke
6
7/27/2016
Review: Shortest Path Properties
Optimal substructure: the shortest path
consists of shortest subpaths
 Let (u,v) be the weight of the shortest path
from u to v. Shortest paths satisfy the triangle
inequality: (u,v)  (u,x) + (x,v)
 In graphs with negative weight cycles, some
shortest paths will not exist

David Luebke
7
7/27/2016
Review: Relaxation

Key technique: relaxation

Maintain upper bound d[v] on (s,v):
Relax(u,v,w) {
if (d[v] > d[u]+w) then d[v]=d[u]+w;
}
5
2
9
5
2
Relax
5
David Luebke
2
6
Relax
7
5
8
2
6
7/27/2016
Review: Bellman-Ford Algorithm
BellmanFord()
for each v  V
d[v] = ;
d[s] = 0;
for i=1 to |V|-1
for each edge (u,v)  E
Relax(u,v, w(u,v));
for each edge (u,v)  E
if (d[v] > d[u] + w(u,v))
return “no solution”;
Initialize d[], which
will converge to
shortest-path value 
Relaxation:
Make |V|-1 passes,
relaxing each edge
Test for solution:
have we converged yet?
Ie,  negative cycle?
Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w
David Luebke
9
7/27/2016
Review: Bellman-Ford Algorithm
BellmanFord()
for each v  V
d[v] = ;
d[s] = 0;
for i=1 to |V|-1
for each edge (u,v)  E
Relax(u,v, w(u,v));
for each edge (u,v)  E
if (d[v] > d[u] + w(u,v))
return “no solution”;
What will be the
running time?
Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w
David Luebke
10
7/27/2016
Review: Bellman-Ford

Running time: O(VE)



Not so good for large dense graphs
But a very practical algorithm in many ways
Note that order in which edges are processed affects
how quickly it converges
David Luebke
11
7/27/2016
Review: DAG Shortest Paths

Problem: finding shortest paths in DAG


Bellman-Ford takes O(VE) time.
Do better by using topological sort
 If
were lucky and processes vertices on each shortest
path from left to right, would be done in one pass
 Every path in a dag is subsequence of topologically
sorted vertex order, so processing verts in that order, we
will do each path in forward order (will never relax
edges out of vert before doing all edges into vert).
 Thus: just one pass. Running time: O(V+E)
David Luebke
12
7/27/2016
Review: Dijkstra’s Algorithm
Dijkstra(G)
for each v  V
d[v] = ;
d[s] = 0; S = ; Q = V;
while (Q  )
u = ExtractMin(Q);
S = S  {u};
for each v  u->Adj[]
if (d[v] > d[u]+w(u,v))
Note: this
d[v] = d[u]+w(u,v);
is really a
call to Q->DecreaseKey()
David Luebke
13
Relaxation
Step
7/27/2016
Review: Dijkstra’s Algorithm
Dijkstra(G)
for each v  V
d[v] = ;
d[s] = 0; S = ; Q = V;
while (Q  )
u = ExtractMin(Q);
S = S  {u};
for each v  u->Adj[]
if (d[v] > d[u]+w(u,v))
d[v] = d[u]+w(u,v);
Running time: O(E lg V) using binary heap for Q
Can acheive O(V lg V + E) with Fibonacci heaps
David Luebke
14
7/27/2016
Dijkstra’s Algorithm
Dijkstra(G)
for each v  V
d[v] = ;
d[s] = 0; S = ; Q = V;
while (Q  )
u = ExtractMin(Q);
S = S  {u};
for each v  u->Adj[]
if (d[v] > d[u]+w(u,v))
d[v] = d[u]+w(u,v);
Correctness: we must show that when u is
removed from Q, it has already converged
David Luebke
15
7/27/2016
Correctness Of Dijkstra's Algorithm
p2
u
s
p2



x
y
Note that d[v]  (s,v) v
Let u be first vertex picked s.t.  shorter path than d[u]
Let y be first vertex V-S on actual shortest path from su


d[u] > (s,u)
 d[y] = (s,y)
Because d[x] is set correctly for y's predecessor x  S on the shortest path, and
When we put x into S, we relaxed (x,y), giving d[y] the correct value
David Luebke
16
7/27/2016
Correctness Of Dijkstra's Algorithm
p2
u
s
p2




x
y
Note that d[v]  (s,v) v
Let u be first vertex picked s.t.  shorter path than d[u]
d[u] > (s,u)
Let y be first vertex V-S on actual shortest path from su
 d[y] = (s,y)
d[u] > (s,u)
= (s,y) + (y,u) (Why?)
= d[y] + (y,u)
 d[y]
But if d[u] > d[y], wouldn't have chosen u. Contradiction.
David Luebke
17
7/27/2016
Disjoint-Set Union Problem

Want a data structure to support disjoint sets


Need to support following operations:




Collection of disjoint sets S = {Si}, Si  Sj = 
MakeSet(x): S = S  {{x}}
Union(Si, Sj): S = S - {Si, Sj}  {Si  Sj}
FindSet(X): return Si  S such that x  Si
Before discussing implementation details, we
look at example application: MSTs
David Luebke
18
7/27/2016
Kruskal’s Algorithm
Kruskal()
{
T = ;
for each v  V
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
19
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
20
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
21
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
22
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
9
14
17
T = ;
8
25
5
for each v  V
21
13
1?
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
23
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
24
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2?
19
{
14
17
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
25
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
26
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
9
14
17
T = ;
8
25
5?
for each v  V
21
13
1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
27
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
28
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8?
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
29
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
30
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
9?
14
17
T = ;
8
25
5
for each v  V
21
13
1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
31
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
32
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v  V
21
13?
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
33
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
34
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14?
17
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
35
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
36
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17?
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
37
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19?
{
14
17
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
38
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v  V
21?
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
39
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25?
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
40
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
41
9
1
7/27/2016
Kruskal’s Algorithm
Run the algorithm:
Kruskal()
2
19
{
14
17
T = ;
8
25
5
for each v  V
21
13
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
42
9
1
7/27/2016
Correctness Of Kruskal’s Algorithm

Sketch of a proof that this algorithm produces
an MST for T:





Assume algorithm is wrong: result is not an MST
Then algorithm adds a wrong edge at some point
If it adds a wrong edge, there must be a lower
weight edge (cut and paste argument)
But algorithm chooses lowest weight edge at each
step. Contradiction
Again, important to be comfortable with cut
and paste arguments
David Luebke
43
7/27/2016
Kruskal’s Algorithm
What will affect the running time?
Kruskal()
{
T = ;
for each v  V
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
44
7/27/2016
Kruskal’s Algorithm
What will affect the running time?
Kruskal()
1 Sort
{
O(V) MakeSet() calls
T = ;
O(E) FindSet() calls
for each v  V
O(V) Union() calls
(Exactly how many Union()s?)
MakeSet(v);
sort E by increasing edge weight w
for each (u,v)  E (in sorted order)
if FindSet(u)  FindSet(v)
T = T  {{u,v}};
Union(FindSet(u), FindSet(v));
}
David Luebke
45
7/27/2016
Kruskal’s Algorithm: Running Time

To summarize:





Sort edges: O(E lg E)
O(V) MakeSet()’s
O(E) FindSet()’s
O(V) Union()’s
Upshot:


Best disjoint-set union algorithm makes above 3
operations take O(E(E,V)),  almost constant
Overall thus O(E lg E), almost linear w/o sorting
David Luebke
46
7/27/2016
Disjoint Set Union

So how do we implement disjoint-set union?

Naïve implementation: use a linked list to
represent each set:
 MakeSet():
??? time
 FindSet(): ??? time
 Union(A,B): “copy” elements of A into B: ??? time
David Luebke
47
7/27/2016
Disjoint Set Union

So how do we implement disjoint-set union?

Naïve implementation: use a linked list to
represent each set:
 MakeSet():
O(1) time
 FindSet(): O(1) time
 Union(A,B): “copy” elements of A into B: O(A) time


How long can a single Union() take?
How long will n Union()’s take?
David Luebke
48
7/27/2016
Disjoint Set Union: Analysis

Worst-case analysis: O(n2) time for n Union’s
Union(S1, S2)
Union(S2, S3)
…
Union(Sn-1, Sn)

1 element
2 elements
“copy”
n-1 elements
O(n2)
Improvement: always copy smaller into larger



“copy”
“copy”
Why will this make things better?
What is the worst-case time of Union()?
But now n Union’s take only O(n lg n) time!
David Luebke
49
7/27/2016
Amortized Analysis of Disjoint Sets
Amortized analysis computes average times
without using probability
 With our new Union(), any individual element is
copied at most lg n times when forming the
complete set from 1-element sets


Worst case: Each time copied, element in smaller set
1st time
2nd time
…
(lg n)th time
David Luebke
resulting set size
2
4
n
50
7/27/2016
Amortized Analysis of Disjoint Sets
Since we have n elements each copied at most
lg n times, n Union()’s takes O(n lg n) time
 We say that each Union() takes O(lg n)
amortized time





Financial term: imagine paying $(lg n) per Union
At first we are overpaying; initial Union $O(1)
But we accumulate enough $ in bank to pay for
later expensive O(n) operation.
Important: amount in bank never goes negative
David Luebke
51
7/27/2016
Amortized Analysis

Book describes 3 views of amortized analysis
in Chapter 18:



Aggregate
Accounting
Potential
David Luebke
52
7/27/2016
Amortized Analysis:
Aggregate Method

Aggregate method




This is what we just did for Union()
n operations take time T(n)
Average cost of an operation = T(n)/n
Not very precise
David Luebke
53
7/27/2016
Amortized Analysis:
Accounting Method

Accounting method


We have done this with graph algorithms
Charge each operation an amortized cost
 Usually



just guess/invent this cost
Amount not used stored in “bank”
Later operations can used stored work
Balance must not go negative
David Luebke
54
7/27/2016
Amortized Analysis:
Potential Method

Potential method



“Stored work” of accounting method is viewed as
“potential energy”
Most flexible and powerful approach
See book if interested; we won’t go into (or test)
David Luebke
55
7/27/2016
Amortized Analysis Example:
Dynamic Tables
Implementing a table (e.g., hash table) for
dynamic data, want to make it small as possible
 Problem: if too many items inserted, table may
be too small
 Idea: allocate more memory as needed

David Luebke
56
7/27/2016
Dynamic Tables
1. Init table size m = 1
2. Insert elements until number n > m
3. Generate new table of size 2m
4. Reinsert old elements into new table
5. (back to step 2)
 What is the worst-case cost of an insert?
 One insert can be costly, but the total?
David Luebke
57
7/27/2016
Analysis Of Dynamic Tables
Let ci = cost of ith insert
 ci = i if i-1 is exact power of 2, 1 otherwise
 Example:


Operation
Table Size
Insert(1)
1
David Luebke
Cost
1
58
1
7/27/2016
Analysis Of Dynamic Tables
Let ci = cost of ith insert
 ci = i if i-1 is exact power of 2, 1 otherwise
 Example:


Operation
Table Size
Insert(1)
Insert(2)
1
2
David Luebke
Cost
1
1 + 1
59
1
2
7/27/2016
Analysis Of Dynamic Tables
Let ci = cost of ith insert
 ci = i if i-1 is exact power of 2, 1 otherwise
 Example:


Operation
Table Size
Insert(1)
Insert(2)
Insert(3)
1
2
4
David Luebke
Cost
1
1 + 1
1 + 2
60
1
2
3
7/27/2016
Analysis Of Dynamic Tables
Let ci = cost of ith insert
 ci = i if i-1 is exact power of 2, 1 otherwise
 Example:


Operation
Table Size
Insert(1)
Insert(2)
Insert(3)
Insert(4)
1
2
4
4
David Luebke
Cost
1
1 + 1
1 + 2
1
61
1
2
3
4
7/27/2016
Analysis Of Dynamic Tables
Let ci = cost of ith insert
 ci = i if i-1 is exact power of 2, 1 otherwise
 Example:


Operation
Table Size
Insert(1)
Insert(2)
Insert(3)
Insert(4)
Insert(5)
1
2
4
4
8
David Luebke
Cost
1
1 + 1
1 + 2
1
1 + 4
62
1
2
3
4
5
7/27/2016
Analysis Of Dynamic Tables
Let ci = cost of ith insert
 ci = i if i-1 is exact power of 2, 1 otherwise
 Example:


Operation
Table Size
Insert(1)
Insert(2)
Insert(3)
Insert(4)
Insert(5)
Insert(6)
1
2
4
4
8
8
David Luebke
Cost
1
1 + 1
1 + 2
1
1 + 4
1
63
1
2
3
4
5
6
7/27/2016
Analysis Of Dynamic Tables
Let ci = cost of ith insert
 ci = i if i-1 is exact power of 2, 1 otherwise
 Example:


Operation
Table Size
Insert(1)
Insert(2)
Insert(3)
Insert(4)
Insert(5)
Insert(6)
Insert(7)
1
2
4
4
8
8
8
David Luebke
Cost
1
1 + 1
1 + 2
1
1 + 4
1
1
64
1
2
3
4
5
6
7
7/27/2016
Analysis Of Dynamic Tables
Let ci = cost of ith insert
 ci = i if i-1 is exact power of 2, 1 otherwise
 Example:


Operation
Table Size
Insert(1)
Insert(2)
Insert(3)
Insert(4)
Insert(5)
Insert(6)
Insert(7)
Insert(8)
1
2
4
4
8
8
8
8
David Luebke
Cost
1
1 + 1
1 + 2
1
1 + 4
1
1
1
65
1
2
3
4
5
6
7
8
7/27/2016
Analysis Of Dynamic Tables
Let ci = cost of ith insert
 ci = i if i-1 is exact power of 2, 1 otherwise
 Example:


Operation
Table Size
Insert(1)
Insert(2)
Insert(3)
Insert(4)
Insert(5)
Insert(6)
Insert(7)
Insert(8)
Insert(9)
1
2
4
4
8
8
8
8
16
David Luebke
Cost
1
1
1
1
1
1
1
1
1
66
+ 1
+ 2
1
2
3
4
5
6
7
1
8
2
9
+ 4
+ 8
7/27/2016
Aggregate Analysis

n Insert() operations cost
n
c
i 1
i
lg n
 n   2  n  (2n  1)  3n
j
j 0
Average cost of operation
= (total cost)/(# operations) < 3
 Asymptotically, then, a dynamic table costs the
same as a fixed-size table


Both O(1) per Insert operation
David Luebke
67
7/27/2016
Accounting Analysis

Charge each operation $3 amortized cost



Use $1 to perform immediate Insert()
Store $2
When table doubles



$1 reinserts old item, $1 reinserts another old item
Point is, we’ve already paid these costs
Upshot: constant (amortized) cost per operation
David Luebke
68
7/27/2016
Accounting Analysis

Suppose must support insert & delete, table
should contract as well as expand





Table overflows  double it (as before)
Table < 1/2 full  halve it: BAD IDEA (Why?)
Better: Table < 1/4 full  halve it
Charge $3 for Insert (as before)
Charge $2 for Delete
 Store
extra $1 in emptied slot
 Use later to pay to copy remaining items to new table
when shrinking table
David Luebke
69
7/27/2016
The End
David Luebke
70
7/27/2016
Exercise 1 Feedback

First, my apologies…



Harder than I thought
Too late to help with midterm
Proof by substitution:


T(n) = T(n/2 + n) + n
Most people assumed it was O(n lg n)…why?
 Resembled
proof from class: T(n) = 2T(n/2 + 17) + n
 The correct intuition: n/2 dominates n term, so it
resembles T(n) = T(n/2) + n, which is O(n) by m.t.

Still, if it’s O(n) it’s O(n lg n), right?
David Luebke
71
7/27/2016
Exercise 1: Feedback
So, prove by substitution that
T(n) = T(n/2 + n) + n = O(n lg n)
 Assume T(n)  cn lg n
 Then T(n)  c(n/2 + n) lg (n/2 + n)
 c(n/2 + n) lg (n/2 + n)  c(n/2 + n) lg (3n/2)
…

David Luebke
72
7/27/2016