













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 21
This page cannot be seen from the preview
Don't miss anything!














Algorithm Design & Analysis Amortized Cost
Implement a queue with two stacks
Lecture 3 David Maier (^1)
Algorithm Design & Analysis enqueue(x, Q)
Queue Operations
dequeue(Q) return pop(SO)( )
Q (^) G
Lecture 3 David Maier (^2)
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)
Algorithm Design & Analysis Complexity
Lecture 3 David Maier (^4)
Algorithm Design & AnalysisImproving Efficiency More efficiency- find just the elements of the set
and SIZE[j] = Union(i,j) {assume i= , j= }
Lecture 3 David Maier (^7)
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
Lecture 3 David Maier (^9)
Algorithm Design & Analysis Tree-Structured Union-Find
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
i F(i) = 0 1 2 3 4 5 F(i)
Lecture 3 David Maier (^14)
Algorithm Design & Analysis Complexity 2
Lecture 3 David Maier (^15)
Algorithm Design & Analysis Complexity 3
Lecture 3 David Maier (^16)
Algorithm Design & Analysis Complexity 6 w
w is root
v i
Lecture 3 David Maier (^19)
v,w in different groups
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)
Lecture 3 David Maier (^21)
Algorithm Design & Analysis Complexity 9
N(g) ≤ F(g)Σ r=F(g-1)+
Lecture 3 David Maier (^22)
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
Lecture 3 David Maier (^34)
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
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