








































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
Material Type: Notes; Class: ANALYSIS OF ALGORITHMS; Subject: COMPUTER SCIENCE; University: Texas A&M University; Term: Unknown 1989;
Typology: Study notes
1 / 48
This page cannot be seen from the preview
Don't miss anything!









































(Chapters 21, 22-25, 15)
Acknowledgement: These notes are compiled by Nancy Amato at Texas A&M University. Parts of these course notes are based on notes from courses given by Jennifer Welch at Texas A&M University.
Graph Algorithms - Outline of Topics
Topic Outline
Representing a Graph – Adjacency List
Adjacency List: An array A[1, V ] of lists, one for each node v ∈ V. v’s list contains (ptrs to) all nodes adjacent to v in G
example:
3
6
1 2
4 5
7
Complexity Issues
Representing a Graph – Adjacency Matrix
Adjacency Matrix: An array A[V, V ] such that
A[i, j] =
1 if (i, j) ∈ E 0 otherwise
example:
3
6
1 2
4 5
7
1 2 3 4 5 6 7
1 2 3 4 5 6 7 1 1 1 1 1 1 1 1 1 1 1 1 1 1
o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
1
1
Complexity Issues
Exercise: Breadth-First Search and Adjacency Matrices
BFS(G, s) enqueue(Q, s) and visit(s) while (not-emptyQ) u := dequeue(Q) for each v adjacent to u if (v not visited yet) enqueue(Q, v) and visit(v) endfor endwhile
Exercise:
Depth-First Search
DFS(G, s) visit(s) for each v adjacent to s if (v not visited yet) DFS(G, v) endfor
Complexity (Adjacency List Representation)
Note: DFS divides edges into two categories:
Claim: if (u, v) is a back edge, then v is an ancestor of u
Data Structures for Disjoint Sets
Disjoint Set Abstract Data Type (ADT) (Chapt 22)
Note 1: The create operation is typically used during the initialization of a particular algorithm.
Note 2: We assume we have a pointer to each x ∈ U (so we never have to look for a particular element). Thus the the problems we’re trying to solve are how to manage the sets (unions) and how to find the id of the set containing a particular element (finds)
Graph Algorithm Application – Connected Components
Connected-Components(G) let U = V = {a, b, c, d, e, f, g}
Initially (after step 1), we have the V sets:
{a}, {b}, {c}, {d}, {e}, {f }, {g}
Finally (after step 2), we have the two sets:
{a, b, c, d}, {e, f, g}
which represent the two connected components of the graph.
Weighted Union Implementation
Idea: add weight field to each node holding the number of nodes in the subtree rooted at this node (we’ll only care about weight field of roots)
find(x):
union(x, y):
x (^) U y (^) = x
y
Theorem: Any k-node tree created by k − 1 weighted unions has height ≤ log k (assume we start with singleton sets). That is, trees stay ‘short’.
Proof: By induction on k. Basis: k = 1, height = 0 = log 1 Inductive Hypothesis: Assume true for all i < k. Induction Step: Show true for k. Suppose the last operation performed was union(x, y) and that m = wt(x) ≤ wt(y) = k − m, so that m ≤ k/2.
hx hy h
U y = k−m k−m nodes
y
nodes
x
nodes m
x
nodes m
h = max(hx + 1, hy) ≤ log k
Path Compression Implemenation
Idea: extend the idea of weighted union (i.e., unions still weighted), but on a find(x) operation, make every node on the path from x to the root (the id) a child of the root.
after find(x)
a
b
c
d
x
a
b c d
x
So... find(x) still has worst-case time of O(log n), but subsequent finds for nodes that used to be ancestors of x will now be very fast.
Theorem: Let S be any sequence of O(n) unions and finds. Then the worst-case time for performing S with weighted unions and path compres- sion is O(n log∗^ n)
log∗^ n is ‘almost’ constant log∗^ n is the number of times we have to take the log of a number to reach 1:
Minimum Spanning Trees (Chapt 24)
1
2
3
4
6 5 6
7
2
3
8 5
Definition: Given an undirected graph G = (V, E) with weights on the edges, a minimum spanning tree of G is a subset T ⊆ E such that T has
Kruskal’s MST Algorithm
Idea:
MST-Kruskal(G)
Running Time
Note: We only get this bound because of amortized analysis
Correctness of Kruskal’s Algorithm
Theorem: Kruskal’s MST algorithm produces a MST.
Proof: Clearly algorithm produces a spanning tree. We need to argue it is an MST.
Suppose in contradiction it is not an MST. Suppose that the algorithm adds edges to the tree in order e 1 , e 2 ,... , en− 1 and let i be the value such that e 1 , e 2 ,... , ei− 1 is a subset of some MST T , but e 1 , e 2 ,... , ei− 1 , ei is not a subset of any MST.
Consider T ∪ {ei}
Claim: T ′^ = T − {e∗} ∪ {ei} is a MST
This contradiction means our original assumption must be wrong (so the algo- rithm does find an MST). 2
Prim’s MST Algorithm
Idea:
MST-Prim(G)
Running Time – Adjacency List Rep
Note: we don’t find the minimum element of Eout very efficiently...