Docsity

Kruskal's Algorithm - Lecture Notes | CS 422, Study notes of Algorithms and Programming

Toggle sidebar
bg1
Algorithm Design and Analysis 4/1/2008
Lecture 30: Kruskal’s Algorithm
Instructor: Mike Kowalczyk
1 Some Terminology
1.1 Sets
A set is a collection of elements. You can think of it as an array, except the elements are not
ordered. If Ais a set with the element bin it, we can denote this by b∈A. If every element of A
is in the set B,Ais said to be a subset of B, denoted by AāŠ†Bor BāŠ‡A.
1.2 Graphs
A graph is a pair: (V, E), where Vis the set of vertices, and Eis the set of edges. Usually you’ll see
ā€œLet G= (V, E ) be a graph...ā€. When someone says ā€œgraphā€ they usually mean ā€œsimple graphā€,
meaning no self-loops, undirected edges, and no multiple edges between a single pair of vertices.
1.3 Paths and cycles
Apath on a simple graph G= (V, E) is a sequence of vertices (v0, v1, v2,· · · , vk) where each vi∈V
and each (vi, vi+1)∈E. A cycle is a path that starts and ends at the same vertex (v0=vk). A
simple path is a path where no vertex is repeated (vi6=vjif i6=j). A simple cycle is a cycle where
only the start and end vertices are identical, and k≄3 (this avoids an edge getting used twice).
1.4 Trees
Trees have many equivalent definitions. For example:
1. A tree is a graph with one unique path between each pair of vertices.
2. A tree is a connected graph (every vertex has a path to every other vertex), and nāˆ’1 edges,
where nis the number of verices.
3. A tree is an acyclic graph (no simple cycles) which has nāˆ’1 edges, where nis the number of
vertices.
2 Kruskal’s Algorithm
Suppose we are given a connected graph G= (V, E) and each edge e∈Ehas an associated weight
we∈R. We would like to find a tree T= (V, E0) where E0āŠ†E(called a spanning tree), of
minimum weight (meaning we are minimizing the sum of all the weights of the constituent edges
Pe∈E0we).
We will use a greedy approach. The algorithm will be to sort the edges increasing by weight,
and add the lowest cost remaining edge, if it doesn’t introduce a cycle.
1
pf2

Partial preview of the text

Download Kruskal's Algorithm - Lecture Notes | CS 422 and more Study notes Algorithms and Programming in PDF only on Docsity!

Algorithm Design and Analysis 4/1/

Lecture 30: Kruskal’s Algorithm

Instructor: Mike Kowalczyk

1 Some Terminology

1.1 Sets

A set is a collection of elements. You can think of it as an array, except the elements are not ordered. If A is a set with the element b in it, we can denote this by b ∈ A. If every element of A is in the set B, A is said to be a subset of B, denoted by A āŠ† B or B āŠ‡ A.

1.2 Graphs

A graph is a pair: (V, E), where V is the set of vertices, and E is the set of edges. Usually you’ll see ā€œLet G = (V, E) be a graph...ā€. When someone says ā€œgraphā€ they usually mean ā€œsimple graphā€, meaning no self-loops, undirected edges, and no multiple edges between a single pair of vertices.

1.3 Paths and cycles

A path on a simple graph G = (V, E) is a sequence of vertices (v 0 , v 1 , v 2 , Ā· Ā· Ā· , vk) where each vi ∈ V and each (vi, vi+1) ∈ E. A cycle is a path that starts and ends at the same vertex (v 0 = vk). A simple path is a path where no vertex is repeated (vi 6 = vj if i 6 = j). A simple cycle is a cycle where only the start and end vertices are identical, and k ≄ 3 (this avoids an edge getting used twice).

1.4 Trees

Trees have many equivalent definitions. For example:

  1. A tree is a graph with one unique path between each pair of vertices.
  2. A tree is a connected graph (every vertex has a path to every other vertex), and n āˆ’ 1 edges, where n is the number of verices.
  3. A tree is an acyclic graph (no simple cycles) which has n āˆ’ 1 edges, where n is the number of vertices.

2 Kruskal’s Algorithm

Suppose we are given a connected graph G = (V, E) and each edge e ∈ E has an associated weight we ∈ R. We would like to find a tree T = (V, E′) where E′^ āŠ† E (called a spanning tree), of minimum weight (meaning we are minimizing the sum of all the weights of the constituent edgesāˆ‘

e∈E′^ we). We will use a greedy approach. The algorithm will be to sort the edges increasing by weight, and add the lowest cost remaining edge, if it doesn’t introduce a cycle.

Let’s prove correctness. First, let’s convince ourselves we actually get a tree. By definition, the algorithm never introduces a cycle (it checks each time it adds an edge). Now how do we know that T is connected? If there’s a vertex v ∈ V with no edge is ā€œgoing to itā€, then introducing an edge ā€œgoing to itā€ will not create a cycle, hence the algorithm will add that edge (we know there’s an edge going to it because G is connected). Note: this part of the argument isn’t entirely correct or complete. Fix it as a homework exercise. Now, let’s see if it’s a minimum cost tree that we construct. Let T = (V, E′) be the tree constructed by the algorithm. Let OP T = (V, E′′) be an optimal spanning tree. Assume that OP T 6 = T and let e ∈ E′^ be the first edge added to T which is not in the edge set of OP T (i.e. e /∈ E′′). Since OP T is a tree, it has a path P from one vertex of e to the other, and e is not traversed by this path. This path P contains some edge e′^ ∈/ E′^ (otherwise T would have a simple cycle). Now, if we modify OP T by removing e′^ and adding e, and we still have a tree (any vertex still has a path to any other, and we have the same number of edges). Furthermore, we ≤ we′ because our algorithm would have choosen e′^ first otherwise. This modification to OP T maintains optimality, and therefore every step of the algorithm leads to an optimal spanning tree. The runtime is straightforward to analyze, and depends on the selection of data structures used to implement the algorithm.