You are on page 1of 36

Optimal Spanning Trees

Nahla Mohamed Ahmed Ibrahim (nahla@aims.ac.za)


African Institute for Mathematical Sciences (AIMS)
Supervised by: Prof Stephan Wagner
Stellenbosch University, South Africa
19 May 2011
Submitted in partial fulllment of a postgraduate diploma at AIMS
Abstract
In a given weighted graph (network), every edge has a certain weight (cost), and one wants to select a
certain set of edges whose total cost is a minimum such that these edges keep all vertices connected -
an optimal spanning tree. This essay shows that this can essentially be solved by a greedy approach. It
explains Prims algorithm and Kruskals algorithm, which are well-known greedy algorithms in nding the
solution of a Minimum Spanning Tree (M.S.T). It also contains the implementations of these algorithms,
which require O(mlog n) time for a graph with n vertices and m edges. If the vertices are points in the
plane and the weights are Euclidean distances, then one obtains the Euclidean Minimum Spanning Tree
problem (E.M.S.T). The property that an E.M.S.T is a subgraph of the Delaunay triangulation (D.T),
can be used to increase the eciency of nding an E.M.S.T to O(nlog n) by applying any of the greedy
algorithms to work on the D.T which has O(n) edges.
Declaration
I, the undersigned, hereby declare that the work contained in this essay is my original work, and that
any work done by others or by myself previously has been acknowledged and referenced accordingly.
Nahla Mohamed Ahmed Ibrahim, 19 May 2011
i
Contents
Abstract i
1 Introduction 1
2 Introducing Graphs and Spanning Trees 2
2.1 Introducing Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3 Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.4 Depth First Search and Breadth First Search . . . . . . . . . . . . . . . . . . . . . . . . 5
3 Optimum Spanning Trees 7
3.1 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 Kruskals Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3 Prims Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.4 The Comparison of Kruskals and Prims Algorithms . . . . . . . . . . . . . . . . . . . . 13
4 Matroids 14
4.1 Denition of a Matroid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.2 Optimization Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5 Euclidean Minimum Spanning Trees 18
5.1 Delaunay Triangulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.2 The Delaunay Triangulation and E.M.S.T . . . . . . . . . . . . . . . . . . . . . . . . . 20
6 Conclusion 23
A The Implementation Codes for Kruskals and Prims Algorithms 24
A.1 Kruskals Algorithm Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
A.2 Prims Algorithm Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
References 33
ii
1. Introduction
A Minimum Spanning Tree (M.S.T) problem is a problem of nding a spanning tree of a connected
weighted graph with minimum cost, which is one of the most well-known problems of combinatorial
optimization. The methods of its solution have generated important ideas in the design of computer
algorithms. The M.S.T problem has obvious applications in communication networks such as connecting
a set of computers in a network, or linking a set of cities with minimum cost. It sometimes occurs as a
sub-problem in nding a solution to a bigger problem such as the Travelling Salesman Problem or the
Minimum Weight Matching Problem.
The rst ecient solutions of the M.S.T problem were given by Kruskal [Kru56] and Prim [Pri57].
Loberman and Weinberger [LW57] also discovered the same algorithm that was proposed by Kruskal,
but their work was done a bit later after Kruskal published his paper. Kruskals construction can be
viewed as a special case of a more general construction that was given in [LW57]. Prim and Dijkstra
[Dij60] gave a similar approach in their construction, which was concerned with storage requirements
[GH85].
Suitable data structures for such algorithms, were developed by J. E. Hopcroft and J. D. Ullman
[HU73], and their application to the M.S.T problem was implemented by M. D. Mcllroy. The best
implementations of Kruskals and Prims algorithms in nding an M.S.T of a given graph with n vertices
and m edges require O(mlog n) time. Nonetheless, time complexity O(m (m, n)), where is the
inverse of Ackermanns function [Tar75], can be achieved if the edges of the graph are given in a sorted
order. Another way of achieving this time complexity is by using the radix-sort algorithm on edge weights
when the weights are relatively small integers [GH85].
This essay is grouped into four chapters. In Chapter 2, we introduce some of the basic concepts in
graph theory that we need to represent our problem. In Chapter 3, we discuss Kruskals and Prims
algorithms, and we refer to their implementation in Appendix A. Moving further on the theoretical side,
Chapter 4 introduces matroids, which allow us to generalise the greedy method that is used to provide
a solution to the M.S.T problem. Finally, in Chapter 5, we look for Euclidean Minimum Spanning Trees
(E.M.S.T), which is a special case of the M.S.T in which the vertices are points in the plane and the
weights are distances.
1
2. Introducing Graphs and Spanning Trees
In this chapter, we introduce some of the basic concepts of graph theory, which we will need in the
following chapters. In section 2.1 we dene some basic expressions of graph theory, followed by data
structures which we need for computational purposes in section 2.2. In section 2.3, we introduce an
important class of graphs, which is called Trees. Section 2.4 describes the Depth rst search and Breadth
rst search algorithms for generating what we call spanning trees of a connected graph.
2.1 Introducing Graphs
A graph G is a pair (V, E), where V is a set of elements called vertices, and E is a set of pairs (u, v)
called edges, such that u and v belong to V , we write e = (u, v). Figure 2.1 shows an example of a
graph G with 5 vertices and 7 edges.
e
1
e
2
e
3
e
4
e
5
e
6
e
7
v
1
v
2
v
3
v
4
v
5
Figure 2.1: Graph G = ({v
1
, v
2
, . . . , v
5
}, {e
1
, e
2
, . . . , e
7
})
The degree of a vertex v, denoted by d(v), is the number of edges having v as an endpoint. In Figure
2.1, we have d(v
2
) = d(v
5
) = 2, d(v
1
) = d(v
4
) = 3 and d(v
3
) = 4. A vertex v with d(v) = 0 is called
an isolated vertex. A regular graph is a graph where all vertices have the same degree. A k-regular
graph is a graph where the degree of every vertex is k.
If the number of edges in a graph G is close to the maximal number of edges, G is called a dense graph.
In the case where G has only few edges, it is called a sparse graph. When the two endpoints of an
edge are the same vertex, this is called a loop. Multiple edges occur when more that one edge share
the same two endpoints. For our purposes we will work with simple graphs, which are graphs having no
loops or multiple edges.
A complete graph is a graph which has an edge between every pair of distinct vertices. Notice that a
complete graph with n vertices is an (n 1)-regular graph. Figure 2.2 is an example of incomplete
graph, whereas the graph in Figure 2.3 is complete, which is 4-regular [Gib85].
A graph is said to be nite if the number of vertices |V |, and the number of edges |E|, are nite.
Throughout our discussions, we shall normally work with nite graphs. H is a subgraph of G if it is
obtained by removing a non-zero number of edges and/or vertices of G. In such a case, G is called a
supergraph of H and we write H G. Removing a vertex necessarily implies that we remove all the
2
Section 2.2. Data Structures Page 3
Figure 2.2: Incomplete graph
Figure 2.3: Complete graph
associated edges, whereas removing an edge does not imply removing any vertices.
A path in a graph G is a sequence of vertices such that from each of these vertices (except the last)
there is an edge to the next vertex in the sequence. A nite path always has a rst vertex called a start
vertex, and a last vertex, called an end vertex. A cycle is a path such that the start vertex and end
vertex are the same. Note that the choice of the start vertex in a cycle is arbitrary. A path with no
repeated vertices is called a simple path, and a cycle with no repeated vertices except the start and end
vertex is called a simple cycle. The length of a path or a cycle is the number of edges it contains. Two
paths are edge-disjoint if they do not have a common edge [Gib85].
Two vertices are connected if there is a path between them. If any two vertices in a graph G are
connected, then the graph is called a connected graph. The maximal connected subgraphs of G are
called its components.
A planar graph is a graph that can be drawn in the plane without edge crossings, i.e., edges intersect
only at their common vertices. A plane graph is a planar graph that has been drawn in the plane. A
dual graph G

of a plane graph G is a plane graph whose vertices correspond to the faces of G. The
edges of G

correspond to the edges of G as follows: If e is an edge of G with face X on one side and
a face Y on the other side, then the endpoints of the dual edge e

are the vertices x, y of G

that
represent the faces X, Y of G. This concept is only valid for plane graphs [Jai06, Wes01].
In some applications, it is natural to assign a number to each edge of a graph. The resulting graph is
called a weighted graph. The weight associated with an edge is usually a real number and it is denoted
by w(e). The sum of the weights of all edges of a graph is called its weight [Gib85].
2.2 Data Structures
We now introduce some of the common data structures that are used to represent graphs for computa-
tional purposes.
Section 2.3. Trees Page 4
2.2.1 Adjacency Matrices. An adjacency matrix is a way of representing the edges in a graph as a
matrix. For a given graph G = (V, E), its adjacency matrix is a |V | |V | matrix A such that
A[i, j] =
_
1 if (v
i
, v
j
) E
0 otherwise,
where V = {v
1
, v
2
, . . . , v
|V |
}. In the case of weighted graph G = (V, E), its adjacency matrix A is
given by
A[i, j] =
_
w((v
i
, v
j
)) if (v
i
, v
j
) E
0 otherwise.
Figure 2.4, shows a weighted graph G and its corresponding adjacency matrix A. Note that A is
symmetric [Wil96].
3
2
4
1
6
2
5
v
1
v
2
v
3
v
4
v
5
Graph G
A =
_

_
v
1
v
2
v
3
v
4
v
5
v
1
0 2 0 2 4
v
2
2 0 1 0 3
v
3
0 1 0 6 0
v
4
2 0 6 0 5
v
5
4 3 0 5 0
_

_
L =
v
1
v
2
v
3
v
4
v
5

v
2
, 2 v
4
, 2 v
5
, 4
v
1
, 2 v
3
, 1 v
5
, 3
v
2
, 1 v
4
, 6
v
1
, 2 v
3
, 6 v
5
, 5
v
1
, 4 v
2
, 3 v
4
, 5
Figure 2.4: The adjacency matrix A and the adjacency list L of graph G.
Clearly, the specication of A requires O(n
2
) steps, thus the use of an adjacency matrix for representing
graphs eliminates all algorithms of complexity O(|E|).
2.2.2 Adjacency List. An adjacency list is a representation of all edges or arcs in a graph as a list.
Notice that with an undirected graph, an adjacency list representation duplicates the edge information.
Adjacency lists can also be used to represent weighted graphs, where each node in the adjacency list
represents an edge with its weight as shown in Figure 2.4. An adjacency list is an ecient representation
of a graph, because each node has a list which only contains the nodes that are its neighbours. Thus
the specication of the adjacency list requires O(|E|) steps.
2.3 Trees
In this section, we introduce the notion of trees, which is an important class of graphs. A tree T is a
connected graph with no cycles. The edges of trees are called branches. A forest is a disjoint union of
Section 2.4. Depth First Search and Breadth First Search Page 5
trees.
Let T be a graph with n vertices, then the following statements are equivalent [Wil96]:
T contains no cycles, and has n 1 edges.
T is connected, and has n 1 edges.
There is a unique path between every pair of vertices in T.
T contains no cycles, and adding a new edge to T creates a unique cycle.
T is connected, and removing any edge from T makes it disconnected.
A spanning tree of a connected graph G is a subgraph of G that includes all the vertices of G and has
the properties of a tree.
2.3.1 Theorem. A graph is connected if, and only if, it has a spanning tree.
Proof. Let G be a connected graph. While G has a cycle, remove one edge from the cycle until we
get a connected acyclic subgraph H. Then H is a spanning tree of G. On the other hand, if G has a
spanning tree, then there is a path between each pair of vertices in G; thus G is connected.
2.4 Depth First Search and Breadth First Search
In this section, we introduce some commonly-used algorithms for generating spanning trees of a con-
nected graph.
2.4.1 Depth First Search Algorithm. To generate a spanning tree of a given connected graph G,
we need to visit all the vertices of G only once. Suppose we are currently at vertex v in Depth First
Search (D.F.S) algorithm, the general requirement is that the next vertex to be visited is adjacent to v,
and has not yet been visited. If no such vertex exists, then the search returns to the vertex visited just
before v. This process is repeated until every vertex has been visited. The algorithm of D.F.S is shown
in Algorithm 1.
Algorithm 1 D.F.S algorithm
Require: Connected graph G = (V, E).
Ensure: Spanning tree T.
T
for all v V do
v.visit False
end for
for all v V do
if v.visit = False then
DFS(v) {Dened in Algorithm 2}
end if
end for
Section 2.4. Depth First Search and Breadth First Search Page 6
Algorithm 2 DFS(u):
Require: vertex u.
Ensure: DFS(u).
u.visit True
for all u

adjacent to u do
if u

.visit = False then


T T {(u, u

)}
DFS(u

)
end if
end for
2.4.2 Breadth First Search Algorithm. The general requirement in Breadth First Search (B.F.S) is
that all vertices adjacent to v which have not yet been visited, are visited in some order right afterwards.
This process is repeated for each of those adjacent vertices, until each vertex has been visited. The
algorithm of B.F.S is shown in Algorithm 3.
The running time of D.F.S and B.F.S is O(|E|), therefore a spanning tree can be found in linear time
using any of these algorithms [Gib85].
Algorithm 3 B.F.S algorithm
Require: Connected graph G = (V, E).
Ensure: Spanning tree T.
i 1
L []
T
for all v V do
v.order 0
end for
choose u from V
L.append(u)
while L non-empty do
pick rst element u in L
for all u

adjacent to u do
if u

.order = 0 then
u

.order i
i i + 1
L.append(u

)
T T {(u, u

)}
end if
end for
end while
3. Optimum Spanning Trees
In this chapter, we introduce the problem of nding Minimum Spanning Trees (M.S.T) of a connected
weighted graph. We also discuss Prims and Kruskals algorithms, which are the best-known classic
greedy algorithms in solving M.S.T problem of a given graph G. In order to simplify the description of
the algorithms, we assume G is simple and connected.
3.1 Problem Statement
Consider the problem of constructing a railway system linking a set of towns, or connecting a set of
routers in a computer network by giving the cost of all possible direct connections. How can we make
all the communications at a minimum cost? We can model this kind of problem using graph theory,
by generating a weighted graph G whose vertices represent the objects we need to connect (towns or
routers), and edges with their weights representing the direct paths with their costs.
Greedy algorithms are algorithms that use the strategy of constructing a solution piece by piece, and
always choosing the next piece that makes the solution locally optimal. Although this approach can fail
to give the optimal solution for some computational tasks, our problem is one of the applications where
the greedy algorithms succeed to compute its optimum solution [DPV06].
Problem statement: Given a weighted graph G, we are interested in nding a spanning tree T, with
the smallest total weight.
3.2 Kruskals Algorithm
Kruskals algorithm is an iterative method for solving the M.S.T problem of a connected weighted graph
G with n vertices. The algorithm starts by initializing a graph T with all the vertices in G and no edges.
After that, it repeats the operation of adding an edge with a minimum weight to T such that no cycle
is created, otherwise it ignores the edge. This repetition terminates when T has (n 1) edges.
Algorithm 4 Kruskals Algorithm for Solving the MST Problem
Require: A simple connected weighted graph G with n vertices.
Ensure: A minimum spanning tree T of G.
Sort the edges with respect to their weights as e
1
, e
2
, . . . , e
m
T
i = 0
while T has less than (n 1) edges do
if (T {e
i
}) does not contain a cycle then
add e
i
to T
i = i + 1
end if
end while
Figure 3.1 illustrates the execution of this algorithm.
7
Section 3.2. Kruskals Algorithm Page 8
2
3
1
5
3
4
4
v
1
v
2
v
3
v
4
v
5
Connected weighted graph G
v
1
v
2
v
3
v
4
v
5
1
v
1
v
2
v
3
v
4
v
5
Step 0 Step 1
2
1
v
1
v
2
v
3
v
4
v
5
2
1
3
v
1
v
2
v
3
v
4
v
5
Step 2 Step 3
2
1
3
4
v
1
v
2
v
3
v
4
v
5
Step 4: Minimum Spanning Tree of G
Clusters Step 0 Step 1 Step 2 Step 3 Step 4
C(v
1
) {v
1
} {v
1
, v
4
} {v
1
, v
2
, v
4
} {v
1
, v
2
, v
4
} {v
1
, v
2
, v
3
, v
4
, v
5
}
C(v
2
) {v
2
} {v
2
} {v
1
, v
2
, v
4
} {v
1
, v
2
, v
4
} {v
1
, v
2
, v
3
, v
4
, v
5
}
C(v
3
) {v
3
} {v
3
} {v
3
} {v
3
, v
5
} {v
1
, v
2
, v
3
, v
4
, v
5
}
C(v
4
) {v
4
} {v
1
, v
4
} {v
1
, v
2
, v
4
} {v
1
, v
2
, v
4
} {v
1
, v
2
, v
3
, v
4
, v
5
}
C(v
5
) {v
5
} {v
5
} {v
5
} {v
3
, v
5
} {v
1
, v
2
, v
3
, v
4
, v
5
}
Figure 3.1: The steps of constructing an M.S.T of a connected weighted graph G by using Kruskals
algorithm, and the table shows the changing of the clusters of vertices at each step.
Section 3.2. Kruskals Algorithm Page 9
3.2.1 Theorem (Correctness). Given a connected weighted graph G with n vertices, Kruskals algorithm
generates an M.S.T of G.
Proof. Clearly, Kruskals algorithm generates a spanning tree T of G, that refers to the process of
ignoring the edges which create cycles. Since G is a connected graph, the iteration terminates when T
gets exactly (n 1) edges, hence, T is a spanning tree.
We want to show that T has minimum total weight. By contradiction, assume T is not an M.S.T of
G, also, assume that the (n 1) edges of T are added in the order e
1
, e
2
, . . . , e
n1
. Dene an M.S.T
T
k
= T for G with the largest index k such that e
1
, e
2
, . . . , e
k1
T
k
and e
k
is not in T
k
. Adding
e
k
to T
k
creates a unique cycle, and since T has no cycles, this implies that there exists an edge e

k
in the cycle which is dierent from e
1
, e
2
, . . . , e
k
. Therefore, T

= T
k
e

k
+ e
k
is a spanning tree
and e
1
, e
2
, ..., e
k
T

. From the denition of Kruskals algorithm, we must have w(e


k
) w(e

k
), thus
w(T

) w(T
k
). Since T
k
is an M.S.T of G, w(T

) w(T
k
), hence, T

is a M.S.T of G. But
this contradicts that T
k
is M.S.T which has a maximum number of (k 1) rst edges shared with T.
Therefore, T is an M.S.T of G.
3.2.2 The Implementation of Kruskals Algorithm. The progress of Kruskals algorithm is based on
two main operations. It starts by sorting the edges by their weights. This operation can be implemented
by using a priority queue Q, which contains all edges of G by the order of their weights.
The second operation tests if the addition of an edge e
i
to T, in step i, creates a cycle. To simplify
this test, we can dene a cluster C(v) for each vertex v in G, such that at each step, C(v) shows the
list of all vertices connected with v (as shown in Figure 3.1). Initially, the cluster C(v) contains only
v. To test if the addition of e
i
= (u, v) to T generates a cycle, we can easily check if u and v belong
to the same cluster or not The python implementation code of this algorithm is shown in Appendix A
section A.1.
.
Algorithm 5 The Implementation Algorithm
Require: A simple connected weighted graph G with n vertices.
Ensure: A minimum spanning tree T of G.
for each vertex in G do
initialize a cluster of v v.cluster {v}
end for
Dene a priority queue Q that contains all the edges in G ordered with respect to their weights.
T
while T has less than (n 1) edges do
remove the rst edge (u, v) from Q
if u.cluster = v.cluster then
add (u, v) to T
u.cluster merge(u.cluster, v.cluster)
v.cluster u.cluster
end if
end while
Section 3.3. Prims Algorithm Page 10
3.2.3 The Complexity of the Algorithm. The initialization of a priority queue Q, which contains
sorted edges, needs an application of a sorting algorithm with complexity O(mlog m) (Merge-sort,
etc). We can dene C(v) as a pointer to the position of the cluster list of v, hence we only need one
comparison to test C(u) = C(v).
The merging of two clusters can be executed by appending the cluster with a smaller number of elements
to the larger one, and make both pointers C(u) and C(v) point to the same merged cluster. Thus, the
time complexity of merging the two clusters C(u) and C(v) is O(min(|C(u)|, |C(v)|)).
For a vertex v, the cluster C(v) of v, starts with one element v. At each step of merging clusters C(u)
and C(v), the element v is appended to C(u) while |C(v)| |C(u)| and |C(v)| n. However, at each
time of appending v in the merging process, the size of C(v) is at least duplicated; hence, v moves at
most (log n) times. Since this occurs for every vertex v, thus the time complexity of moving all vertices
during the merge operation is O(nlog n).
3.3 Prims Algorithm
Prims algorithm is another greedy algorithm which is used to solve the M.S.T problem of a connected
weighted graph G with n vertices. The algorithm starts by initializing a tree T = (V, E) with one vertex
V = {v}. Dene a set E
T
as the set of edges that have one endpoint in V and the other endpoint
in V . The algorithm repeats the operation of adding the edge in E
T
with minimum weight to E, and
the endpoint in V to V . This repetition terminates when T has n vertices, or equivalently, has (n 1)
edges.
Algorithm 6 Prims Algorithm for Solving the M.S.T Problem
Require: A simple connected weighted graph G with n vertices.
Ensure: A minimum spanning tree T = (V, E) of G.
V {v}
E
while T has less than (n 1) edges do
Choose e = (v, v) with the minimum weight such that v V, v V .
V V {v}
E E {e}.
end while
We illustrate the execution of this algorithm in the example shown in Figure 3.2.
3.3.1 Theorem (Correctness). Given a connected weighted graph G with n vertices, Prims algorithm
constructs T, an M.S.T of G.
Proof. Prims algorithm constructs a spanning tree T of G: at any step, the current set of edges only
connects the vertices of V . We only insert edges between V and V so no cycles are created. Further,
the iteration stops when T has (n 1) edges, which implies T is a spanning tree.
Now, we want to show that T has minimum total weight. By contradiction, assume T is not an M.S.T
of G, and also, assume that (n1) edges of T are added in the order e
1
, e
2
, . . . , e
n1
. Dene an M.S.T
T
k
= T for G with the largest index k such that e
1
, e
2
, . . . , e
k1
T
k
and e
k
not in T
k
. Adding e
k
to
Section 3.3. Prims Algorithm Page 11
2
3
1
5
3
4
4
v
1
v
2
v
3
v
4
v
5
Connected weighted graph G
v
1
1
v
1
v
4
Step 0 Step 1
2
1
v
1
v
2
v
4
2
1
4
v
1
v
2
v
3
v
4
Step 2 Step 3
2
1
3
4
v
1
v
2
v
3
v
4
v
5
Step 4: Minimum Spanning Tree of G
Set of vertices Step 0 Step 1 Step 2 Step 3 Step 4
V {v
1
} {v
1
, v
4
} {v
1
, v
2
, v
4
} {v
1
, v
2
, v
3
, v
4
} {v
1
, v
2
, v
3
, v
4
, v
5
}
V {v
2
, v
3
, v
4
, v
5
} {v
2
, v
3
, v
5
} {v
3
, v
5
} {v
5
}
Figure 3.2: The steps of constructing an M.S.T of a connected weighted graph G by using Prims
algorithm.
T
k
creates a unique cycle C, since T has no cycles, this implies that there exists an edge e

k
in C that
is dierent from e
1
, e
2
, . . . , e
k
, and one of its endpoint is in V
k
and the other in V
k
, where V
k
is the set
of vertices in T at step k.
Section 3.3. Prims Algorithm Page 12
Hence, T

= T
k
e

k
+ e
k
is a spanning tree and e
1
, e
2
, . . . , e
k
T

. From the denition of the


algorithm, we must have w(e
k
) w(e

k
), thus w(T

) w(T
k
). Since T
k
is an M.S.T of G, that is,
w(T

) w(T
k
), T

is an M.S.T of G. There is a contradiction that T


k
has a maximum number of
(k 1) rst edges shared with T. Therefore, T is an M.S.T of G.
3.3.2 The Implementation of Prims Algorithm. First, the algorithm starts by choosing an arbitrary
vertex u, and initializing V = {u}. To dene a set E
T
of all edges that have one endpoint in V and
the other in V , we can dene a weight and a parent for all vertices. For each vertex v that connects
with u, we set the parent of v to be u, and its weight to be the weight of the edge (u, v). For all other
vertices, we set their parent as null, and their weights as innity. At each step of adding a vertex z to
V , if the weight of any vertex v that connects with z is greater than the weight of the edge (z, v), we
update the parent of v to z, and its weight to the weight of the edge (z, v).
Now, the operation of selecting the edge in E
T
with the minimum weight can be done by constructing
a priority queue Q that contains all the vertices in V by using their weights as the key. Hence, the rst
vertex v in Q and its parent u, are the two endpoints of the edge e, that has the minimum weight among
all edges incident with u. The python implementation code of this algorithm is shown in Appendix A
section A.2.
Algorithm 7 The Implementation Algorithm
Require: A simple connected weighted graph G with n vertices and m edges.
Ensure: A minimum spanning tree T of G.
for each vertex v in G do
initialize a weight of v v.weight +
initialize a parent of v v.parent null
end for
Pick any vertex u in G
Set u.weight 0
for each vertex v adjacent to u do
v.weight w((u, v))
v.parent u
end for
Initialize a priority queue Q that contains (v, v.weight), for each vertex v,
where v.weight is the key
T
while Q is not empty do
remove the rst element (z, z.weight) from Q
add (z.parent, z) to T
for each vertex v adjacent to z and v in Q do
if w((z, v)) < v.weight then
v.weight w((z, v))
update the key v.weight in Q
end if
end for
end while
Section 3.4. The Comparison of Kruskals and Prims Algorithms Page 13
3.3.3 The Complexity of Prims Algorithm. Let G be a graph with n vertices and m edges. The time
complexity of the implementation of Prims algorithm is based on the process of updating the weights
of the vertices in Q. That is, updating the values of the keys of Q, and its corresponding change of the
order of the vertices in Q.
For any edge e = (v, v) in G, the weight of v is updated at most once. Hence, the operation of updating
the weights needs to be performed in O(m) time. Since the weight of v is the key value of the v in Q,
then this update needs a changing of the position of v, which requires O(log n) time.
Note that, any other operations in the implementation of Prims algorithm are done in linear time.
Therefore, the total running time of Prims algorithm is O(mlog n).
3.4 The Comparison of Kruskals and Prims Algorithms
Although Kruskals and Prims algorithms can be implemented by using dierent data structures and
strategies, they have the same worst-case running time in constructing the minimum spanning tree.
In our previous implementation of Kruskals algorithm, we used a priority queue data structure to store
the edges of the graph, and a list data structure to store the clusters. In Prims algorithm, we only used a
priority queue. Hence, Prims algorithm is better from the point of view of data structures construction.
In addition, the edges in the priority queue vary for each step of building the M.S.T when using Prims
algorithm, whereas the edges in the priority queue in Kruskals algorithm are the same during all the
process of constructing the M.S.T. In Kruskals algorithm, there is an additional operation of merging
clusters, which is not needed in Prims algorithm.
Considering the case of a dense graph, the way of constructing the priority queue in Kruskals algorithm
requires the operation of sorting all edges in the graph. In Prims algorithm, this situation is relatively
better, since we do not need to make all possible comparison to build our M.S.T. Thus, Prims algorithm
performs better in the case of dense graphs, whereas Kruskals algorithm performs better in sparse graphs.
4. Matroids
A matroid is a structure consisting of subsets of a xed set satisfying certain properties. A graphic
matroid is a type of matroid that works on graphs. Independent subsets are dened on this matroid
as subsets of edges of a given graph which have no cycles. The optimum spanning tree problem is
equivalent to the problem of nding a maximal independent subset in a graphic matroid with optimum
total weight. One of the amazing properties of matroids is that the greedy algorithm always works on
matroids, this property gives us a guarantee that the greedy algorithm can always solve our optimization
problem.
In this chapter, we will dene matroids, give some examples of matroids, introduce some properties of
matroids, and discuss the optimization problem.
4.1 Denition of a Matroid
Let E be a nite set and I a collection of subsets of E. A pair M = (E, I), is called a matroid if it
satises the following properties:
(I
1
) Inclusion property: If X Y and Y I, then X I.
(I
2
) Exchange property: If X and Y are in I and |X| < |Y |, then there is an element e Y \X
such that X {e} I.
If the pair M = (E, I) satises the inclusion property, then it is called a subset system. The subsets of
E which are in I are called independent sets, while the subsets outside I are called dependent sets. A
maximal independent set X in a matroid M = (E, I) is a subset of E, in which for every e E\X,
X {e} is a dependent set. A maximal independent set is called a base of M [Goe09].
4.1.1 Examples of Matroid.
Uniform matroid: For a given k, a matroid M = (E, I) is a uniform matroid, if I = {X E :
|X| k}. M is denoted by U
k,n
where n = |E|. Note that a base of U
k,n
is any subset X such
that |X| = k.
Free matroid: This is a special case of a uniform matroid U
k,n
, in which any subset of E is
independent, i.e., k = n.
Linear matroid: Let E be the set of columns of a matrix A, and let I be the set of all subsets
of columns of A that are linearly independent. Then the pair (E, I) is a matroid.
Proof. (I
1
) is obviously satised, since for any linearly independent set Y , X Y is also linearly
independent. Let X, Y I and |X| < |Y |. Dene S = span{X Y } to be the vector space
spanned by the columns of X Y . Since Y is linearly independent, then the dimension of S,
dim(S), is at least |Y |. To prove the exchange property by contradiction, assume that for all
e Y \X, X{e} is a linearly dependent set. Then S span{X}, thus |Y | dim(S) |X| <
|Y |. This is a contradiction, hence, (I
2
) holds.
14
Section 4.1. Denition of a Matroid Page 15
Notice that a base of a linear matroid corresponding to a matrix A is a linearly-independent set
B I such that |B| = rank(A).
Graphic matroid: For a given graph G = (V, E), let I be the set of all subsets of E that do not
contain cycles (forests), then M = (E, I) is a matroid.
Proof. It is trivial that each subset of a forest is also a forest, so (I
1
) is satised. For a given forest
F, notice that the graph G
F
= (V, F) has |V | |F| connected components. Suppose X and Y
are two forests and |X| < |Y |, then the graph G
X
= (V, X) has more connected components
than G
Y
= (V, Y ), this means that there exists an edge e Y \X which connects two dierent
connected components in X, thus, X {e} is a forest, so it is in I. Therefore, (I
2
) holds.
If G is a connected graph, then each spanning tree T of G is a base of the graphic matroid.
If G is disconnected, a base will be the union of spanning trees of all connected components
[Goe09, Oxl92].
To show an example does not satisfy matroid properties, consider a graph G = (V, E), and I = {F
E : F is a matching
1
}. M = (E, I) is not a matroid since I
2
is not necessarily satised. Consider a
counter example, V = {a, b, c, d} and the two matchings X = {(a, b), (c, d)} Y = {(b, c)}, but there is
no an edge e X\Y such that X {e} still matching.
4.1.2 Theorem (Cardinality property). A subset system M = (E, I) is a matroid if, and only if, for
any set A E, all maximal independent sets in A have the same number of elements.
Proof. First, assume M is a matroid. By contradiction, assume A E has two maximal independent
sets X, Y such that |X| < |Y |. From the exchange property, there exists e Y \X such that
X {e} I. But X {e} is also a subset of A, thus X is not maximal in A.
Secondly, assume that all maximal independent sets in A have the same size. Assume also that X and
Y are two independent sets such that |X| < |Y |, let A = X Y . We can extend Y to be a maximal
independent set Y

in A, hence, |X| < |Y | |Y

|. From the assumption that all maximum independent


sets in A have the same size, X is not a maximal independent set in A, therefore, there exists e A\X
i.e. e Y , such that X {e} I. Therefore, M is a matroid.
In a graphic matroid, if G = (V, E) is connected, the maximal independent sets in I are the spanning
trees of G. We can see that all spanning trees have the same size of edges, which is n 1, where
n = |V |. If G is disconnected, the maximal independent sets in I are the forests of size n c, where c
is the number of connected components in G.
4.1.3 Circuits. A minimal dependent set in an arbitrary matroid M = (E, I) is a dependent subset C
of E in which for every e C, C\{e} is an independent set. A minimal dependent set of M is called a
circuit. In a graphic matroid, cycles are dependent sets, and by removing any edge from a cycle we get
an independent set. Thus, cycles represent circuits in a graphic matroid.
4.1.4 Theorem (Unique circuit property). Suppose that X is an independent set in a matroid M =
(E, I), and e E is such that X {e} is a dependent set. Then M has a unique circuit contained in
X {e}, and this circuit contains e.
1
is a set of edges without common vertices.
Section 4.2. Optimization Problem Page 16
Proof. Since X {e} is a dependent set, we can generate a circuit C from X {e} by removing all
elements in X {e} such that C is still dependent. Thus, X {e} contains a circuit C. From the
inclusion property, all subsets of X are independent, hence C should contain e. Now we want to prove
the uniqueness of the circuit by contradiction. Assume that C
1
, C
2
are circuits contained in X {e}
and C
1
= C
2
. By minimality of C
1
and C
2
, C
1
C
2
and C
2
C
1
, this implies that there exists
t C
1
\C
2
. By minimality of C
1
, C
1
\{t} is an independent set, and we can extend it to a maximal
independent set S of X {e}, which satises |S| < |X {e}|. Since X is another independent set in
X {e}, we must have |S| = |X|. Furthermore, S = X\{t} {e} I. However, C
2
S and from
the inclusion property C
2
I, it contradicts our assumption that C
2
is a circuit.
Notice that if C is the unique circuit of X{e}, then for each t C, X\{t}{e} is an independent set.
The graphic matroid, which is a special case of matroids, satises the unique circuit property. However,
if we add an edge to a forest and the resulting graph has a cycle, then it is unique.
4.2 Optimization Problem
Consider a matroid M = (E, I, w), where w is a weight function w : E R. Dene a weight w(X)
of any non-empty subset X of E by
w(X) =

eX
w(e),
and w() = 0. The optimization problem for M is the problem of nding an independent set B of a
maximum weight [Oxl92]. If w(e) 0 for all e E, the solution B will be a base of the matroid, and if
w(e) < 0 for some e, then any optimum solution does not contain e, so we can eliminate e from E. In
the graphic matroid of a connected graph G, the optimization problem is equivalent to the problem of
nding maximum spanning trees, which can be solved by using greedy algorithms as shown in Chapter
2. This happens to be the case for all matroids.
The Greedy Algorithm
Let M = (E, I, w) be a matroid with a weight function w. The following algorithm returns the maximal
independent set S of a maximum weight over all independent sets.
Algorithm 8 The greedy algorithm
Require: A matroid M = (E, I, w) with a weight function w.
Ensure: An independent set S of a maximum weight.
Sort the elements in E such that w(e
1
) w(e
2
) w(e
|E|
)
S
for i = 1 : |E| do
if S +e
i
I then
S S +e
i
end if
end for
In the graphic matroid, this algorithm is reduced to Kruskals algorithm, which we have discussed in the
previous chapter for solving the maximum weight spanning tree problem.
Section 4.2. Optimization Problem Page 17
4.2.1 Theorem. Let M = (E, I, w) be a matroid with a weight function w. The above greedy algorithm
returns the maximal independent set S of the maximum weight among all maximal independent sets.
Proof. By contradiction, let F be a maximal independent set with a maximum weight, and w(F) >
w(S). Suppose that F = {f
1
, f
2
, . . . , f
k
}, such that the elements in F are ordered as w(f
1
) w(f
2
)
w(f). Since w(F) > w(S), we can choose the rst index p such that w(f
p
) > w(s
p
). Consider
the two sets A = {f
1
, f
2
, . . . , f
p
} and B = {s
1
, s
2
, . . . , s
p1
}. Since A F and B S, from the
inclusion property, they are independent sets. Since |A| > |B| from the exchange property, there exists
f
i
A\B such that B {f
i
} I. But w(f
i
) w(f
p
) > w(s
p
), which contradicts the progress of the
greedy algorithm in adding s
p
to S instead of f
i
.
4.2.2 Theorem. Consider a subset systemM = (E, I, w). The greedy algorithm solves the optimization
problem for M if, and only if, M is a matroid.
Proof. Let M be a matroid. Theorem 4.2.1 shows that the greedy algorithm nds the maximal inde-
pendent set S which has a maximal weight, which is a solution of the optimization problem.
Now, assume M is not a matroid. We want to show how the greedy algorithm fails to nd the set of
maximal weight for some weight functions. Suppose that there are two sets X, Y I and |X| < |Y |
such that for all e Y \X, the set X {e} is dependent. Dene a weight function w(e) as
w(e) =
_

_
|X| + 2 e X
|X| + 1 e Y \X
0 Otherwise.
The greedy algorithm adds the highest weights rst. That is, the elements in X, then jumps over all the
elements in Y \X since they will generate a dependent set with X, after this, it checks the remaining
elements. The total weight becomes |X|(|X| + 2). But the optimal solution contains at least all the
elements in Y with total weight |Y |(|X| +1) which is at least (|X| +1)(|X| +1). Therefore, the greedy
algorithm fails to nd the solution of the optimization problem.
We have observed this powerful property, which determines the behaviour of a greedy algorithm without
doing something obvious with the algorithm.
5. Euclidean Minimum Spanning Trees
A Euclidean Minimum Spanning Tree (E.M.S.T) of a set P of n points in the plane is a minimum
spanning tree of P, where the weight of an edge between a pair of points is the Euclidean distance
between those points. The simplest way to nd an E.M.S.T of a set of n points is to construct a
complete weighted graph G = (V, E, w) of n vertices, which has n(n1)/2 edges, such that the weight
function w of G is the length of any edge e in the graph.
After this construction, we can apply any of the Minimum Spanning Trees (M.S.T) algorithm (such
as Kruskals algorithm which we have discussed in Chapter 2) to nd an E.M.S.T. Since the graph G
has (n
2
) edges, M.S.T algorithms require O(n
2
log n) time to get an E.M.S.T. This running time can
be decreased to O(nlog n) by using what is called Delaunay triangulation which we will discuss in the
following sections.
5.1 Delaunay Triangulation
5.1.1 Triangulation. A triangulation T of R
n
is a subdivision of R
n
into a set of n-dimensional
simplices
1
, such that:
Any simplex face is shared by either one adjacent simplex or none at all.
Any bounded set in R
n
intersects only nitely many simplices in T.
In a sense, a triangulation generates a mesh of simplices from a given set of points in R
n
.
5.1.2 Delaunay Triangulation. A Delaunay Triangulation (D.T) of a set P of points in the plane is
dened as a triangulation where the circumcircle of every triangle
2
does not contain any other points
from P. Figure 5.1 shows an example of a Delaunay and a non-Delaunay triangle. For an edge e that
connects two points from a set of points P in the plane, e is called Delaunay edge if there exists a circle
passing through the two endpoints of e that does not contain any interior point of P (see Figure 5.2).
t
p
t
p
A B
Figure 5.1: A: Triangle t is non-Delaunay since the point p lies within the circumcircle; B: t is a Delaunay
triangle.
1
n-dimensional simplices are a generalization of the notion of triangles or tetrahedrons to n dimensions.
2
a circle that passes through the endpoints of a triangle
18
Section 5.1. Delaunay Triangulation Page 19
e
e

A B
Figure 5.2: A: e is a non-Delaunay edge; B: e

is a Delaunay edge since it has an empty circumcircle.


5.1.3 Properties of The Delaunay Triangulation.
Empty-circle property: A circle that passes through the endpoints of any D.T triangle is empty.
Convex hull: The exterior face of D.T is a convex polygon.
The Delaunay triangulation is equivalent to Delaunay edges: A triangulation T is a Delaunay
triangulation if, and only if, all the edges in T are Delaunay edges.
This property is used in some algorithms for constructing a Delaunay triangulation. Therefore,
the condition that needs to be tested in these algorithms, is if all edges in the graph are Delaunay
edges or not. The proof of this property is discussed in Theorem 5.1.4.
The ipped diagonal is always Delaunay: If two non-Delaunay triangles share an edge e, then
by removing e and adding a diagonal edge, the new triangles are Delaunay triangles. Thus, any
non-Delaunay edge can always be ipped to form a Delaunay edge (see Figure 5.3).
This property aects a single part of the mesh, however, some algorithms of constructing the
Delaunay triangulation are based on this property, and they start by an arbitrary triangulation and
continue ipping non-Delaunay edges until none are left. This type of algorithms works in two
dimensions and does not extend to higher dimensions. It takes O(n
2
) in edge ips.
Maxmin-angle property: The Delaunay triangulation maximizes the minimum angle among all
triangulations of a given set of points. This property is one of the advantages of the Delaunay
triangulation, which improves the mesh quality [Zim05, Ple].
e
e

A B
Figure 5.3: A is not a Delaunay triangulation. Flipping of a non-Delaunay edge e in A forms a Delaunay
triangulation in B.
5.1.4 Theorem. For a set of points P, two points p
1
and p
2
are connected by an edge in the Delaunay
triangulation if, and only if, there is an empty circle passing through p
1
and p
2
.
Section 5.2. The Delaunay Triangulation and E.M.S.T Page 20
Proof. Let p
1
, p
2
and p
3
be the endpoints of a Delaunay triangle, then there exists a circle C that
passes through p
1
, p
2
, p
3
and does not contain any interior point. Thus, C is an empty circle passing
through p
1
and p
2
.
Now we prove the other direction. Let p
1
, p
2
and p
3
be the endpoints of a triangle t. By contradiction,
assume that there are empty circles passing through each edge of t, and t is a non-Delaunay triangle,
this implies that the circumcircle of t contains an interior point p
i
. Without any loss of generality, the
edge that separates p
i
from inside t is (p
1
, p
2
) (see Figure 5.4). Hence, any circle that passes through
p
1
and p
2
must contains p
3
or p
i
as an interior point, but this contradicts our assumption.
p
1
p
3
p
2
p
i
t
Figure 5.4: p
i
lies inside the circumcircle of triangle t, the edge (p
1
, p
2
) is not a Delaunay edge.
5.1.5 The Relation Between D.T and Voronoi Diagram. A Voronoi diagram of a set of points in
the plane is a division of the plane into regions for each point in the set, where the region of a point
p contains the part of the plane which is closer to p than any other point. Each region is known as a
Voronoi cell and denoted by vo(p). Boundaries between the cells are the perpendicular bisectors of the
lines joining the points. Voronoi vertices are the points which are created in the intersections of these
boundaries. Voronoi edges are the boundaries between two Voronoi cells.
The Delaunay triangulation is a dual graph of a Voronoi diagram which is a planar graph. The Delaunay
triangulation has a vertex for each Voronoi cell, and an edge for each Voronoi edge. Figure 5.5 shows
an example of a Delaunay triangulation and a Voronoi diagram of a set of points and the dual relation
between them.
Generally the construction of the Voronoi diagram requires O(nlog n) running time with certain algo-
rithms such as Fortunes algorithm [Zim05]. Then, the Delaunay triangulation can be generated from
the Voronoi diagram by using the dual relation. The Delaunay triangulation can be constructed directly
in O(nlog n), by using more technical algorithms such as divide and conquer algorithms. These are
based on drawing a line recursively to divide the set of points into two sets, computing the Delaunay
triangulation for each set and merging these two sets. The merge operation can be done in time O(n),
so the total running time is O(nlog n) [Zim05].
5.2 The Delaunay Triangulation and E.M.S.T
The Delaunay triangulation has an interesting property: The E.M.S.T of a set of n points is a subgraph of
the Delaunay triangulation. This property can increase the eciency of nding an E.M.S.T, by applying
minimum spanning tree algorithms on the Delaunay triangulation which has O(n) edges instead of
using the complete graph which has O(n
2
) edges. The proof of this property is shown in the following
theorem.
Section 5.2. The Delaunay Triangulation and E.M.S.T Page 21
A B
Figure 5.5: A shows a Delaunay triangulation of a set of 6 vertices, and B shows the corresponding
Voronoi diagram.
5.2.1 Theorem. A Euclidean minimum spanning tree of a set of points P is a subgraph of any Delaunay
triangulation of P.
Proof. Let T be an E.M.S.T of P with total weight w(T). Assume that p
1
and p
2
are two points in P,
and that there is an edge e in T that connects these two points. By contradiction, we assume that e is
not a Delaunay edge, thus every circle passing through p
1
and p
2
contains an interior point. Choose a
circle C with diameter e, and let p
i
be an interior point of C (as shown in Figure 5.6). Removing the edge
e from T divides T into two connected components. One of them contains p
1
and the other contains p
2
.
Assume that p
i
lies with p
1
in the same connected component, then by adding an edge e

that connects
p
2
with p
i
to T, it generates a spanning tree T

with total weight w(T

) = w(T) w(e) +w(e

). Since
the length of e is greater than the length of e

, i.e. w(e) > w(e

), this implies that T

has smaller total


weight than T which contradicts the assumption that T is an E.M.S.T.
e
p
1
p
i
p
2
e

p
1
p
i
p
2
Graph T Graph T

Figure 5.6: The Delaunay triangulation and E.M.S.T


Therefore, using this property, the generation of an E.M.S.T as a spanning tree of the Delaunay tri-
angulation needs O(nlog n) running time. Since the construction of the Delaunay triangulation runs
in O(nlog n) time, hence, the total running time needed to nd an E.M.S.T by using the Delaunay
triangulation is O(nlog n). Figure 5.7 is an example showing that the E.M.S.T of a set of 5 points is a
subgraph of the Delaunay triangulation.
The E.M.S.T problem can be generalized to m-dimensional space R
m
. In higher dimensions, the property
Section 5.2. The Delaunay Triangulation and E.M.S.T Page 22
A: Delaunay triangulation B: The E.M.S.T
Figure 5.7: The E.M.S.T is a subgraph of the Delaunay triangulation
that E.M.S.T is a subgraph of the Delaunay triangulation also holds, but the number of edges in the
triangulation increases with the dimension [AESW91].
6. Conclusion
Going through the discussions in this essay, it is clear that the well-known problem of nding a minimum
spanning tree (M.S.T) can be analysed and solved using greedy algorithms such as Kruskals and Prims
algorithms. In this essay, we gave an overview of some of the graph theoretic denitions and concepts,
which enabled us to discuss Kruskals and Prims algorithm. Also, we discussed the theory of matroids
that provides a general form of the greedy method, which is an important concept that can be applied
to various other problems. Finally, we discussed Euclidean minimum spanning trees, which have the
property of being subgraphs of the Delaunay triangulation, this property was used to decrease the
running time of computing M.S.T.
In this essay, we achieve time complexity O(mlog n) for nding the M.S.T of a graph with n vertices
and m edges. This also provided us with a method of nding a Euclidean minimum spanning tree in
time O(nlog n). We conclude this essay with implementations and illustrative examples.
23
Appendix A. The Implementation Codes for
Kruskals and Prims Algorithms
We use python to implement both Kruskals algorithm and Prims algorithm. Note that the implemen-
tation codes of Kruskals and Prims algorithms are available in the link: http://users.aims.ac.za/
~
nahla/essay.html
A.1 Kruskals Algorithm Code
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
#===============================================================
# Each vertex has a name and a cluster contains the vertices
# that connect with v.
class vertex:
def __init__ (self, v):
self.label = v
self.cluster = [v]
def merge (self, cluster1):
for v in cluster1:
self.cluster.append (v)
#===============================================================
def readf ():
edges = []
G = []
File = open("datakruskal.dat", "r")
for line in File:
L = line.split ()
G.append((L[1],L[2],float(L[0])))
L[0] = float (L[0])
L[1] = vertex (L[1])
L[2] = vertex (L[2])
edges.append (L)
File.close ()
return edges,G
#===============================================================
24
Section A.1. Kruskals Algorithm Code Page 25
def plotG (edges):
G = nx.Graph()
for e in edges:
G.add_edge (e[0], e[1], weight = e[2])
pos = nx.spring_layout(G)
nx.draw (G, pos)
edge_labels = dict ([((u,v), d[weight])
for u, v, d in G.edges (data = True)])
nx.draw_networkx_edge_labels (G, pos, edge_labels = edge_labels)
#===============================================================
if __name__ == "__main__":
n = input("Enter the number of vertices : ")
edges,G = readf ()
print "G = ", G
plt.figure (1)
plotG (G)
Q = sorted (edges)
T = []
i = 0
T_weight = 0
while len (T) < n - 1:
w, u, v = Q[i]
if u.cluster != v.cluster:
T.append ((u.label,v.label,w))
u.merge (v.cluster)
v.cluster = u.cluster
T_weight += w
i = i + 1
plt.figure (2)
print "T = ", T
plotG (T)
plt.show ()
print "The total weight of T = ", T_weight
A.1.1 datakruskal.dat File Format.
7 A B
5 A K
1 A D
2 B C
1 C E
2 E F
Section A.2. Prims Algorithm Code Page 26
9 F M
2 M L
1 L N
2 K N
1 K O
2 J O
1 J I
2 H I
1 H G
5 D G
1 C I
4 E L
8 F J
3 D J
4 D E
6 G L
A.2 Prims Algorithm Code
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
#===============================================================
# Each vertex has a label, weight, parent, a list of nighbour
# vertices and a list of these edges weights.
class vertex:
def __init__ (self, v):
self.label = v
self.weight = np.inf
self.vlist = []
self.wlist = []
self.parent = 0
def add (self, v, w):
self.vlist.append (v)
self.wlist.append (w)
#===============================================================
def readf ():
edges = []
File = open ("dataprim.dat","r")
for line in File:
Section A.2. Prims Algorithm Code Page 27
L = line.split ()
vertexList = [L[0]]
for i in range (1, len(L) - 1, 2):
vertexList.append ( (L[i], float(L[i+1])) )
edges.append (vertexList)
File.close ()
return edges
#===============================================================
def plotG (T):
G=nx.Graph ()
for e in T:
G.add_edge (e[0], e[1], weight = e[2])
pos = nx.spring_layout (G)
nx.draw (G, pos)
edge_labels = dict([((u, v), d[weight])
for u, v, d in G.edges(data = True)])
nx.draw_networkx_edge_labels (G, pos, edge_labels = edge_labels)
#===============================================================
if __name__ == "__main__":
L = readf ()
V = dict()
G = []
for l in L:
v = vertex (l[0])
V.update({l[0] : v})
for i in range (1, len(l)):
v.add (l[i][0], l[i][1])
G.append ((l[0], l[i][0], l[i][1]))
plt.figure (1)
plotG (G)
u = v
u.weight = 0
Q = []
i = 0
for label in u.vlist:
v = V[label]
v.parent = u.label
v.weight = u.wlist[i]
i = i + 1
for v in V.values():
Section A.2. Prims Algorithm Code Page 28
Q.append ([v.weight,v])
Q = sorted(Q)
print " "
T = []
i = 1
T_weight = 0
n = len (Q)
Q.remove (Q[0])
while len(Q) > 0:
w, u = Q[0]
Q.remove(Q[0])
T_weight += w
T.append ((u.parent,u.label,w))
j = 0
for label in u.vlist:
v = V[label]
if u.wlist[j] < v.weight:
v.weight = u.wlist[j]
v.parent = u.label
j = j + 1
for q in Q:
q[0] = q[1].weight
Q = sorted(Q)
plt.figure (2)
print "T = ", T
plotG (T)
plt.show ()
print "The total weight of T = ", T_weight
A.2.1 dataprim.dat File Format.
A B 7 D 1 K 5
B A 7 C 2
C B 2 E 1 I 1
D A 1 G 5 J 3 E 4
F E 2 M 9 J 8
E C 1 F 2 D 4 L 4
G D 5 H 1 L 6
H G 1 I 2
I H 2 J 1 C 1
J I 1 D 3 F 8 O 2
K A 5 O 1 N 2
L M 2 N 1 G 6 E 4
M F 9 L 2
Section A.2. Prims Algorithm Code Page 29
N L 1 K 2
O J 2 K 1
The Output
The output of the previous Kruskals code, in the case of a graph with 15 vertices and 22 edges, as
shown below,
G = [(A, B, 7.0), (A, K, 5.0), (A, D, 1.0), (B, C, 2.0),
(C, E, 1.0), (E, F, 2.0), (F, M, 9.0), (M, L, 2.0),
(L, N, 1.0), (K, N, 2.0), (K, O, 1.0), (J, O, 2.0),
(J, I, 1.0), (H, I, 2.0), (H, G, 1.0), (D, G, 5.0),
(C, I, 1.0), (E, L, 4.0), (F, J, 8.0), (D, J, 3.0),
(D, E, 4.0), (G, L, 6.0)]
T = [(A, D, 1.0), (C, E, 1.0), (L, N, 1.0), (K, O, 1.0),
(J, I, 1.0), (H, G, 1.0), (C, I, 1.0), (B, C, 2.0),
(E, F, 2.0), (M, L, 2.0), (K, N, 2.0), (J, O, 2.0),
(H, I, 2.0), (D, J, 3.0)]
The total weight of T = 22.0
The output in the case of implementing Prims code for the same graph G,
T = [(O, K, 1.0), (O, J, 2.0), (J, I, 1.0), (I, C, 1.0),
(C, E, 1.0), (C, B, 2.0), (E, F, 2.0), (I, H, 2.0),
(H, G, 1.0), (K, N, 2.0), (N, L, 1.0), (L, M, 2.0),
(J, D, 3.0), (D, A, 1.0)]
The total weight of T = 22.0
Section A.2. Prims Algorithm Code Page 30
Figure A.1: Graph G
Section A.2. Prims Algorithm Code Page 31
Figure A.2: The minimum spanning tree of G
Acknowledgements
All appreciation begins and ends with Allah. I thank Allah for helping me to do this work. I would like
to thank my supervisor Prof. Wagner for all his explanations and comments on my work. I would like
to mention that I am glad for being with this lovely AIMS family and I thank them also. Starting with
our fathers Prof. B. Green and Igsaan including all my AIMS brothers and sisters, I wish them the best.
In particular, I would like to thank my brothers Ahmed and Obeng for their guidance throughout the
AIMS period and on this work. Last but not least, I would like to thank my mother Zainab Yassin and
also dedicate this piece of work to her. I ask Allah to take care of her.
32
References
[AESW91] P. K. Agarwal, H. Edelsbrunner, O. Schwarzkopf, and E. Welzl, Euclidean minimum spanning
trees and bichromatic closest pairs, Discrete and Computational Geometry 6 (1991), 407
422.
[CT76] David R. Cheriton and Robert Endre Tarjan, Finding minimum spanning trees, Siam Journal
on Computing 5 (1976), 724742.
[Dij60] E. W. Dijkstra, Some theorem on spanning subtrees of a graph, Indag. math. 28 (1960),
196199.
[DPV06] S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani., Algorithms, http://highered.
mcgraw-hill.com/sites/0073523402/, 2006.
[GH85] R. L. Graham and Pavol Hell, On the history of the minimum spanning tree problem, IEEE
Annals of The History of Computing 7 (1985), 4357.
[Gib85] Alan Gibbons, Algorithmic graph theory, Press Syndicate of University of Cambridge, 1985.
[Goe09] Michel X. Goemans, Combinatorial optimization, 2009.
[GT02] M. Goodrich and R. Tamassia, Algorithm design: Foundations, analysis and internet exam-
ples, John Wiley and Sons Inc., 2002.
[HU73] J. E. Hopcroft and J. D. Ullman, Set merging algorithms, SIAM J. Comput. 2 (1973),
294303.
[Jai06] A. Jain, Planar graphs, dual graphs, connectivity and separability, Tech. Report 01CS3016,
2006.
[Kru56] J. B. Kruskal, On the shortest spanning subtree of a graph and the traveling salesman
problem, Proceeding of the American Mathematical Society 7 (1956), 4850.
[LW57] H. Loberman and A. Weinberger, Formal procedures for connecting terminals with a mini-
mum total wire length, Journal of The ACM 4 (1957), 428437.
[Oxl92] James G. Oxley, Matroid theory, Oxford University Press, 1992.
[Ple] Robert Pless, Voronoi diagrams and delauney triangulations, http://research.
engineering.wustl.edu/
~
pless/506/l17.html.
[Pri57] R. C. Prim, Shortest connection networks and some generalizations., Bell Systems Technical
Journal 36 (1957), 13891401.
[Tar75] Robert Endre Tarjan, Eciency of a good but not linear set union algorithm, Journal of The
ACM 22 (1975), 215225.
[Wes01] Douglas B. West, Introduction to graph theory, second edition, arrangement with Pearson
Eduaction, Inc, 2001.
[Wil96] Robin J. Wilson, Introduction to graph theory, fourth edition, Longman Malaysia, PP, 1996.
[Zim05] Henrik Zimmer, Voronoi and delaunay techniques, July 2005, http://www.henrikzimmer.
com/VoronoiDelaunay.pdf.
33

You might also like