Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Minimum Spanning Trees: Algorithms and Analysis (CS231 Algorithms at Wellesley College), Exams of Algorithms and Programming

An overview of minimum spanning trees (mst), including definitions, algorithms (prim's and kruskal's), and analysis. The greedy approach to finding msts and discusses the complexity of various data structures used in the algorithms.

Typology: Exams

Pre 2010

Uploaded on 08/16/2009

koofers-user-3eb
koofers-user-3eb 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Wellesley College CS231 Algorithms November 15, 1996
Handout #30
Graphs 1
Reading: CLR Sections 5.4 -- 5.5; Chapter 24, Sections 25.1 -- 25.2
---------------------------------------------------------------------------------------------
------------------------
Minimum Spanning Trees
Definitions:
A weighted graph is a graph (V, E) together with a weighting function w: E --> Reals.
The weight of a graph is
e E
w(e) .
A (free) tree is a connected acyclic undirected graph.
A spanning tree of a graph (V, E) is a free tree (V, E') where E' E.
A sub-spanning tree of a graph (V, E) is a free tree (V, E') where V' V and E' E.
(My terminology)
A minimum (weight) spanning tree (MST) of a connected, undirected graph G is a
spanning tree of G with minimal weight. (It may not be unique.)
---------------------------------------------------------------------------------------------
------------------------
Skeleton of Greedy MST Algorithm
The following is the skeleton of a greedy MST algorithm. The skeleton can be instantiated to
both Prim's algorithm and Kruskal's algorithm.
Idea: Grow a set of edges ST that is a subset of a spanning tree of G. At each step, extend ST by
the "best" safe edge -- i.e., the "best" edge that maintains the invariant that ST is a subset of a
minimum spanning tree of G.
MST(G, w)
ST {}
D Init-Data(G) {Initialize auxiliary data structure}
while not Is-Spanning-Tree?(ST, D) do
{Invariant: ST is the subset of a spanning tree.}
(a,b) Find-Safe-Edge(ST, w, D)
ST ST {(a, b)}
return ST
Note: The spanning tree is represented by the edge set ST, from which the vertices can be
unambiguously derived.
pf3
pf4
pf5

Partial preview of the text

Download Minimum Spanning Trees: Algorithms and Analysis (CS231 Algorithms at Wellesley College) and more Exams Algorithms and Programming in PDF only on Docsity!

Wellesley CollegeCS231 AlgorithmsNovember 15, 1996 Handout #

Graphs 1

Reading: CLR Sections 5.4 -- 5.5; Chapter 24, Sections 25.1 -- 25.

**---------------------------------------------------------------------------------------------

Minimum Spanning Trees**

Definitions:

A weighted graph is a graph (V, E) together with a weighting function w: E --> Reals.

The weight of a graph is (^) ∑

e ∈ E

w(e).

A (free) tree is a connected acyclic undirected graph.

A spanning tree of a graph (V, E) is a free tree (V, E') where E' ⊆ E.

A sub-spanning tree of a graph (V, E) is a free tree (V, E') where V' ⊆ V and E' ⊆ E. (My terminology)

A minimum (weight) spanning tree (MST) of a connected, undirected graph G is a spanning tree of G with minimal weight. (It may not be unique.)

**---------------------------------------------------------------------------------------------

Skeleton of Greedy MST Algorithm**

The following is the skeleton of a greedy MST algorithm. The skeleton can be instantiated to both Prim's algorithm and Kruskal's algorithm.

Idea: Grow a set of edges ST that is a subset of a spanning tree of G. At each step, extend ST by the "best" safe edge -- i.e., the "best" edge that maintains the invariant that ST is a subset of a minimum spanning tree of G.

MST(G, w) ST ← {} D ← Init-Data(G) {Initialize auxiliary data structure} while not Is-Spanning-Tree?(ST, D) do {Invariant: ST is the subset of a spanning tree.} (a,b) ← Find-Safe-Edge(ST, w, D) ST ← ST ∪ {(a, b)} return ST

Note: The spanning tree is represented by the edge set ST, from which the vertices can be unambiguously derived.

Prim's Algorithm

Idea: Grow a single sub-spanning tree. At each step, add the least weight edge connecting a vertex not in the tree with a vertex in the tree.

Init-Data(G) root ← Choose-Root(vertices(G)) for v in vertices(G) do min-weight[v] ← ∞ min-parent[v] ← nil in-tree?[v] ← false min-weight[root] ← 0 Q ← Build-PQ(vertices(G)) {Priority queue ordered by min-weight.} Find-Safe-Vertex({}, w, Q) return Q

Is-Spanning-Tree?(ST, Q) return PQ-Empty?(Q)

Find-Safe-Edge(ST, w, Q) v ← Find-Safe-Vertex(Q) return (min-parent(v), v)

Find-Safe-Vertex(ST, w, Q) a ← PQ-Extract-Min(Q) in-tree?[a] ← true for b in Adj[a] do if not in-tree?[b] and w(a,b) < min-weight[b] then PQ-Decrease-Key(Q, b, w(a,b)) min-parent(b) ← a return a

Analysis:

  • Build-PQ called on |V| vertices
  • PQ-Extract-Min called once for each of |V| vertices
  • PQ-Decrease-Key called at most once for each of |E| edges

Priority Queue implementation

Build-PQ |V|·PQ-Extract-Min |E|·PQ-Decrease-Key Total

unsorted array/list O(V) (^) O(V (^2) ) O(E) (^) O(V (^2) )

binary heap O(V) O(V·lg(V)) O(E·lg(V)) O(E·lg(V)) Fibonacci heap O(V) O(V·lg(V)) O(E) O(V·lg(V) + E)

Note: A Fibonacci heap is a heap-like data structure in which Extract-Min takes O(lg(n)) amortized cost for n nodes, and Decrease-Key takes O(1) amortized cost for n nodes.

Single-Source Shortest Paths

Definitions:

In a weighted, directed graph (G, w), the weight of a path p = (v 0 , v 1 ,, ..., vk ,) is

w(p) = ∑

i=

k w(vi-1, vi).

The shortest-path weight from a to b is δ(a,b) = min{w(p) | p ∈ paths(a,b)}.

Note: min{} = ∞.

A shortest path from a to b is any path p such that w(p) = δ(a,b). (May not be unique.)

The single-source shortest path problem : given a weighted directed graph ((V, E), w) and a source vertex s in V, find a shortest path from s to every vertex of V.

Note: If a path has negative weight edges in a cycle, then shortest path is not defined. Some algorithms (like the Dijkstra algorithm we will study) assume non-negative weights. Other algorithms (such as Bellman-Ford, which we will not study) can handled negative weight edges as long as they don't appear in cycles.

Breadth First Search

In the simple case where all weights = 1, can expand a "frontier" outward from the source, level by level. We can color the nodes according to the following scheme:

  • white = undiscovered
  • gray = frontier node = discovered node whose edges have not been processed
  • black = fully processed = discovered node directly connected only to other discovered nodes

BFS(G,s) {Initialization} for v in vertices(G) do color[v] ← white {All nodes originally undiscovered} d[v] ← ∞ parent[v] ← nil color[s] ← gray d[v] ← 0 Q ← Enq(s,Empty-Queue) {Invariant: Q contains only gray nodes.} while not Empty-Queue?(Q) do f ← Deq(Q) {Next frontier node to process.} for g in Adj[f] do if color[g] = white then color[g] = gray d[g] = d[f] + 1 parent[g] = f Enq(g, Q) color[f] = black {Color node black when completely processed.}

Analysis:

  • Each vertex enqueued and dequeued once at O(1) time per operation: O(V)
  • Each edge scanned once: O(E)
  • Total = O(V + E) (= O(E) for a connected graph)

Dijkstra's Algorithm

Idea: Grow a shortest path tree from the source. Every node maintains a shortest path estimate and parent that indicates the final edge of the estimate shortest path. At each step, add the node with the smallest estimate to the tree via the edge to its parent.

{Note: PQ stands for Priority Queue} Dijkstra(G, w, s) Initialize-Single-Source(G,s) shortest ← {} {vertices at which ds [v]= δ(s,v)} Q ← Build-PQ (vertices(G)) {Invariant: Q contains (V - shortest) ordered by ds [v]) while not PQ-Empty?(Q) do a ← PQ-Extract-Min(Q) shortest ← shortest ∪ {a} for b ∈ Adj[a] do Relax((a,b), w) PQ-Decrease-Key(Q, b, ds [b])

Analysis:

  • Build-PQ called once on |V| elements
  • PQ-Extract-Min called |V| times
  • Relax and Decrease-Key called |E| times.

Priority Queue implementation

Build-PQ |V|·PQ-Extract-Min |E|·PQ-Decrease-Key Total

unsorted array/list O(V) (^) O(V (^2) ) O(E) (^) O(V (^2) )

binary heap O(V) O(V·lg(V)) O(E·lg(V)) O(E·lg(V)) Fibonacci heap O(V) O(V·lg(V)) O(E) O(V·lg(V) + E)