lecture13
lecture13
http://www.cs.stonybrook.edu/˜skiena
Topic: Problem of the Day
Problem of the Day
Your job is to arrange n rambunctious children in a straight
line, facing front. You are given a list of m statements of
the form “i hates j”. If i hates j, then you do not want put
i somewhere behind j, because then i is capable of throwing
something at j.
1. Give an algorithm that orders the line, (or says that it is
not possible) in O(m + n) time.
2. Suppose instead you want to arrange the children in
rows, such that if i hates j then i must be in a lower
numbered row than j. Give an efficient algorithm to find
the minimum number of rows needed, if it is possible.
Questions?
Topic: Minimum Spanning Trees
Weighted Graph Algorithms
Beyond DFS/BFS exists an alternate universe of algorithms
for edge-weighted graphs.
Our adjacency list representation quietly supported these
graphs:
typedef struct edgenode {
int y; /* adjacency info */
int weight; /* edge weight, if any */
struct edgenode *next; /* next edge in list */
} edgenode;
typedef struct {
edgenode *edges[MAXV+1]; /* adjacency info */
int degree[MAXV+1]; /* outdegree of each vertex */
int nvertices; /* number of vertices in the graph */
int nedges; /* number of edges in the graph */
int directed; /* is the graph directed? */
} graph;
Minimum Spanning Trees
A tree is a connected graph with no cycles. A spanning tree is
a subgraph of G which has the same set of vertices of G and
is a tree.
A minimum spanning tree of a weighted graph G is the
spanning tree of G whose edges sum to minimum weight.
There can be more than one minimum spanning tree in a
graph → consider a graph with identical weight edges.
Find the Minimum Spanning Tree
(b) (c)
(a)
Why Minimum Spanning Trees?
The minimum spanning tree problem has a long history – the
first algorithm dates back to 1926!
MST is taught in algorithm courses because:
• It arises in many graph applications.
• It is problem where the greedy algorithm always gives the
optimal answer.
• Clever data structures are necessary to make it work.
Greedy algorithms make the decision of what next to do by
selecting the best local option from all available choices.
Applications of Minimum Spanning Trees
Minimum spanning trees are useful in constructing networks,
by describing the way to connect a set of sites using the
smallest total amount of wire.
Minimum spanning trees provide a reasonable way for
clustering points in space into natural groups.
What are natural clusters in the friendship graph?
Minimum Spanning Trees and Net Partitioning
One of the war stories in the text describes how to partition
a graph into compact subgraphs by deleting large edges from
the minimum spanning tree.
2 4
3
7
5
6
10 11
8 9
5
7 6
2 5 1
4 2 4 2
9 3 3 3
5 7 4 7 1 2 6 5 4
A 12 A A
G Prim(G,A) Kruskal(G)
https://upload.wikimedia.org/wikipedia/
en/3/33/Prim-algorithm-animation-2.gif
Prim’s Algorithm (Pseudocode)
During execution each vertex v is either in the tree, fringe
(meaning there exists an edge from a tree vertex to v) or
unseen (meaning v is more than one edge away).
Prim-MST(G)
Select an arbitrary vertex s to start the tree from.
While (there are still non-tree vertices)
Pick min cost edge between tree/non-tree vertices
Add the selected edge and vertex to the tree Tprim.
This creates a spanning tree, since no cycle can be introduced.
But is it minimum?
Why is Prim Correct? (Proof by Contradiction)
(a) (b)
The Contradiction
distance[start] = 0;
v = start;
while (!intree[v]) {
intree[v] = true;
if (v != start) {
printf("edge (%d,%d) in tree \n",parent[v],v);
weight = weight + dist;
}
p = g->edges[v];
while (p != NULL) {
w = p->y;
if ((distance[w] > p->weight) && (!intree[w])) {
distance[w] = p->weight;
parent[w] = v;
}
p = p->next;
}
dist = MAXINT;
for (i = 1; i <= g->nvertices; i++) {
if ((!intree[i]) && (dist > distance[i])) {
dist = distance[i];
v = i;
}
}
}
return(weight);
}
Prim’s Analysis
Finding the minimum weight fringe-edge takes O(n) time,
because we iterate through the distance array to find the
minimum
After adding a vertex v to the tree, by running through its
adjacency list in O(n) time we check whether it provides a
cheaper way to connect its neighbors to the tree. If so, update
the distance value.
The total time is n × O(n) = O(n2).
Questions?
Topic: Kruskal’s Algorithm
Kruskal’s Algorithm
Since an easy lower bound argument shows that every edge
must be looked at to find the minimum spanning tree, and the
number of edges m = O(n2), Prim’s algorithm is optimal on
dense graphs.
The complexity of Prim’s algorithm is independent of the
number of edges. Kruskal’s algorithm is faster on sparse
graphs
Kruskal’s algorithm is also greedy. It repeatedly adds the
smallest edge to the spanning tree that does not create a cycle.
Kruskal’s Algorithm in Action
5
7 6
2 5 1
4 2 4 2
9 3 3 3
5 7 4 7 1 2 6 5 4
A 12 A A
G Prim(G,A) Kruskal(G)
Kruskal is Correct (Proof by Contradiction)
• If Kruskal’s algorithm is not correct, these must be
some graph G where it does not give the minimum cost
spanning tree.
• If so, there must be a first edge (x, y) Kruskal adds such
that the set of edges cannot be extended into a minimum
spanning tree.
• When we added (x, y) there no path between x and y, or
it would have created a cycle. Thus adding (x, y) to the
optimal tree it must create a cycle.
• But at least one edge in this cycle must have been added
after (x, y), so it must have heavier.
The Contradiction
Deleting this heavy edge leaves a better MST than the optimal
tree, yielding a contradiction!
x y x y
v1
v2
(a) (b)
3 1 2 3 4 5 6 7
1 6 2 1 4 3 4 3 4 2
5
7
(l) (r)
Union-Find Data Structure
typedef struct {
int p[SET_SIZE+1]; /* parent element */
int size[SET_SIZE+1]; /* number of elements in subtree i */
int n; /* number of elements in set */
} union_find;
s S
d1
T1 T2 d2
k2 nodes
k1 nodes
k = k1+ k2 nodes
d is the height
... ...
N-1 leaves
3 13 FIND(4) 14
7 11 2 4 3 7 10 13
12
12
4 5 6 8 9 5 6 8 9 11
r1 = find(s, s1);
r2 = find(s, s2);
if (r1 == r2) {
return; /* already in same set */
}