Lecture Notes 3: Algorithm Design & Analysis - Amortized Cost and Union-Find - Prof. David, Study notes of Computer Science

A portion of lecture notes from a computer science course (cs 410/584) on algorithm design & analysis. The notes cover topics such as amortized cost, queue implementation with two stacks, union-find algorithm, improving efficiency, and tree-structured union-find. The notes also discuss the time complexity of union operations and path compression.

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-jlt
koofers-user-jlt 🇺🇸

10 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 410/584, Algorithm Design & Analysis: Lecture Notes 3
1© 1994, 2003, 2004, 2006,
2008, 2009 David Maier
Algorithm Design & Analysis
Amortized Cost
Not an algorithm design mechanism per se
For analyzing cost of algorithms
-
-
Example: Exercise 17.3-6
Implement a queue with two stacks
Why?
David Maier 1
Lecture 3
Algorithm Design & Analysis
enqueue(x, Q)
Queue Operations
dequeue(Q)
return( )
pop(SO)
G
QSI
S
O
David Maier 2
Lecture 3
G
F
E
D
A
B
C
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Lecture Notes 3: Algorithm Design & Analysis - Amortized Cost and Union-Find - Prof. David and more Study notes Computer Science in PDF only on Docsity!

Algorithm Design & Analysis Amortized Cost

  • Not an algorithm design mechanism per se
  • For analyzing cost of algorithms-

Example: Exercise 17.3-6-

Implement a queue with two stacks

Why?

Lecture 3 David Maier (^1)

Algorithm Design & Analysis enqueue(x, Q)

Queue Operations

dequeue(Q) return pop(SO)( )

Q (^) G

SI SO

Lecture 3 David Maier (^2)

G

F

E

D

A

B

C

Algorithm Design & Analysis A Complication dequeue(Q) if (^) while not empty(SO) hil t (^) e then mpty(SI)t (SI) ddo

return pop(SO)(top(SO)) SI SO

Lecture 3 David Maier (^3)

R

Q

P

SI SO

Algorithm Design & Analysis Complexity

  • operation enqueue is always
  • in a sequence of could be as bad ascould be as bad as n queue ops, one dequeue
  • but total cost of sequence isn’t so bad as

Consider: each element has $4- pay $1 for

  • pay $2 for

Lecture 3 David Maier (^4)

  • pay $1 for

Algorithm Design & AnalysisImproving Efficiency More efficiency- find just the elements of the set

  • always choose R[i] as before If j is a “name”, then LIST[j] points to

and SIZE[j] = Union(i,j) {assume i= , j= }

  1. Assume SIZE[i] SIZE[j]

Lecture 3 David Maier (^7)

  1. Assume SIZE[i] SIZE[j]
  2. Traverse LIST[i] and
  3. Add LIST[i] to
  4. Adjust SIZE[j]

Algorithm Design & Analysis Example i 1 2 3 4 5 6 7 R[i] 4 2 4 4 2 6 7 SIZE 0 2 0 3 0 1 1 LIST 5 1 6 7

Union(2,4) Lecture 3 David Maier (^8)

Algorithm Design & Analysis Time Complexity

Theorem: Can executein O(n·log n) time. n-1 Union operations

  • Time forf Union is proportional tol
  • Charge the cost to the elements movedwhen they go
  • How many times may a single element be

Lecture 3 David Maier (^9)

How many times may a single element be moved?

Algorithm Design & Analysis Tree-Structured Union-Find

Data structure: forest of trees, one per

Almost linear time for n ops

Lecture 3 David Maier (^10)

Find(i) - Union(i,j) -

Algorithm Design & Analysis Path Compression Further improvement on 1 1 Find time 5 3 2

a b

c g

h

a b^5 3 2 h c g d^ e^ f

Lecture 3 David Maier (^13)

d e f

Algorithm Design & Analysis Complexity

Need funny functions to prove complexity F(0) =

i F(i) = 0 1 2 3 4 5 F(i)

G is kind of an inverse

G(n) = the least k such that

Lecture 3 David Maier (^14)

In this universe,practical n G(n) ≤ for all

Algorithm Design & Analysis Complexity 2

Show that a sequence ofbe done in c·n Union/Find’s can

Assume: Find Union takes “units” proportional to takes

Definition: Theelement relative to u-height (union height) of an

  1. Let σ’ be σ with

Lecture 3 David Maier (^15)

  1. Construct a forest using
  2. u-height(v) is height of v in

Algorithm Design & Analysis Complexity 3

Lemma: There are at most

Proof: Node ofdescendents in forest u-height = r F. has at least

Ifdescendents are u-height(v) = u-height(w), sets of

There are at most elements, so distinct sets of 2 r

Lecture 3 David Maier (^16)

elements, so

Corollary:

Algorithm Design & Analysis Complexity 6 w

Consider each

node v on path

1. If v is root

w is root

v i

Lecture 3 David Maier (^19)

v,w in different groups

2. If v,w in same group and w not the root,

Algorithm Design & Analysis Complexity 7 Along any upward path,increase u-heights

At mostso case 1 applies to at most different groups So a Find operation gets charged at

Lecture 3 David Maier (^20)

most

Algorithm Design & Analysis Complexity 8 What happens when case 2 applies? v gets a new parent x and d u-height(x) u-height(w)

  • How many times canparent before its parent is in a v get a new higher group, and case 1 applies? At most

Lecture 3 David Maier (^21)

Algorithm Design & Analysis Complexity 9

Charges to nodes:

Σ (max nodes in )(max moves for )

N(g) ≤ F(g)Σ r=F(g-1)+

Lecture 3 David Maier (^22)

max moves for a node in a group ≤

Algorithm Design & Analysis Directed Graphs In a directed graph (digraph) E is Example E = {(a,b),(b,c),(a,c),(b,d), (c,d),(d,c)} a c

Lecture 3 David Maier (^25)

b d

Algorithm Design & Analysis Representation Time and space complexity depends on howwe capture the graph.

Edge listEdge list {a,b}, {b,c}, {a,c}, {b,d}, {c,d}

Adjacency List a → b c →→

Lecture 3 David Maier (^26)

c d →→

Algorithm Design & Analysis Representation 2 Adjacency matrix a b c d aa b ⎛⎛⎜ 111 ⎞⎞⎟ c d ⎜⎝ 10 ⎟⎠

Lecture 3 David Maier (^27)

Algorithm Design & Analysis Depth-First Search–Undirected Graphs A means to visit all nodes of anundirected graph

at node xpick edge {x,y} if edge y visited, pick another else

Lecture 3 David Maier (^28)

if no more edges, we’re done

Algorithm Design & Analysis Time Complexity Theorem: steps on a graph with DFS requires O(max(n,e))n nodes and e edges (given as an adjacency list). Time spent looking for unmarked nodes DFS(v)time spent, apart from recursive calls, is is called once for each node.

Σ (#nodes adjacent to v)

Lecture 3 David Maier (^31)

v∈N^ (^ j^ )

Algorithm Design & Analysis Back Edges If w {v,w}in the spanning forest (or vice ∈ B, then v is an ancestor of versa)versa).

If v an ancestor of DFNum[v] DFNum[w] w in DFS tree, then.

Lecture 3 David Maier (^32)

Algorithm Design & Analysis Biconnectivity (Problem 22-2) Let G = (N,E) be a connected A nodethere are a ∈ Nv is an articulation point ifand w different from a

1

there are such that every path from v and w different from a

Lecture 3 David Maier (^33)

Algorithm Design & Analysis Biconnected Component

Maximal subgraph that is still connectedafter

Express in terms of edges

Lecture 3 David Maier (^34)

Articulation point must lie in two components

Algorithm Design & Analysis Low Numbers For graph G = (N,E), let T =BB =

For simplicity, assume DFNum[v] = How high (lowesta node on a back edge? DFNum) can we hop from LOW[v] =

Lecture 3 David Maier (^37)

min({v} {w|there is ∪ {x,w} in B where

Algorithm Design & Analysis Calculating Low

If {x,w} in B and w < v then

AlsoAlso, child v is an articulation point if for someis an articulation point if for somec of v,

Calculate^ LOW[c] LOW during DFS

LOW[v]1. is minimum of

Lecture 3 David Maier (^38)

Algorithm Design & Analysis Biconnected Components Algorithm Count — Parent[v] — BI(v)mark v DFNum[v]Low[v] ← ←DFNum[v] Count, increment Count

Lecture 3 David Maier (^39)

Algorithm Design & Analysis for each edge {v,w} Algorithm Cont. do if w not marked then

if and LOW[w] v not root ≥ DFNum[v] then else^ LOW[v] if PARENT[v]^ ← ≠ w then

Lecture 3 David Maier (^40)

else LOW[v] if PARENT[v] ← ≠ w then

Need a check at end if root is an articulationpoint.