Vous êtes sur la page 1sur 38

Assignment Title :

Algorithmics Assignment

Module Code and Title:
Algorithmics CE00333-3

Intake Code:
GF10B1SE

Submitted By :
Maheeka Jayasuriya (CB003346)

Submitted To :
Mr. Balachandran Gnanasekaraiyar

Date Assigned :
18
th
April 2011

Date Due :
6
th
June 2011


CE00333-3 Algorithmics CB003346
i

TABLE OF CONTENTS
TABLE OF CONTENTS .............................................................................................................. i
LIST OF GRAPHS ...................................................................................................................... ii
LIST OF PSEUDOCODES ........................................................................................................iii
LIST OF TABLES ...................................................................................................................... iv
1 ASSUMPTIONS ................................................................................................................. 1
2 INTRODUCTION ............................................................................................................... 2
3 PROBLEM ANALYSIS ...................................................................................................... 3
4 PROBLEM SOLVING METHOD ...................................................................................... 5
5 APPLICATION OF ALGORITHMS .................................................................................. 6
5.1 PRIM'S ALGORITHM ................................................................................................. 7
5.1.1 Algorithm Execution ............................................................................................. 7
5.1.2 Time Complexity ................................................................................................. 10
5.1.3 Derived Solution .................................................................................................. 11
5.2 KRUSKAL'S ALGORITHM ...................................................................................... 11
5.2.1 Algorithm Execution ........................................................................................... 11
5.2.2 Time Complexity ................................................................................................. 14
5.2.3 Derived Solution .................................................................................................. 14
5.3 DIJKSTRA'S ALGORITHM ...................................................................................... 14
5.3.1 Algorithm Execution ........................................................................................... 14
5.3.2 Time Complexity ................................................................................................. 17
5.3.3 Derived Solution .................................................................................................. 18
5.4 BRUTE-FORCE ALGORITHM ................................................................................ 18
5.4.1 Algorithm Execution ........................................................................................... 18
5.4.2 Time Complexity ................................................................................................. 22
5.4.3 Derived Solution .................................................................................................. 23
5.5 COMPARISON OF THE ALGORITHMS ................................................................. 23
6 COMPLEXITY CLASSES ............................................................................................... 25
6.1 JUSTIFICATION OF CHOSEN COMPLEXITY CLASS ......................................... 25
6.2 DEFINITION OF COMPLEXITY CLASS P ............................................................ 25
6.3 EXAMPLE FOR COMPLEXITY CLASS P .............................................................. 25
7 REFERENCE .................................................................................................................... 27
8 APPENDIX ....................................................................................................................... 28
8.1 Pseudocode Standard .................................................................................................. 28
8.2 Primitive Operations ................................................................................................... 28
8.3 Priority Queues ........................................................................................................... 28
8.4 Big-O Notation............................................................................................................ 29
8.5 Graphs ......................................................................................................................... 29
8.5.1 Complete Graph For The Problem ...................................................................... 29
8.5.2 Minimum Spanning Tree Without Constraints .................................................... 30
8.5.3 Minimum Spanning Tree With Constraints ......................................................... 31
8.5.4 Minimum Spanning Tree Using Dijkstra's Starting From Island 5 ..................... 32
8.6 Number Of Spanning Trees For A Complete Graph With Constraints ....................... 32
CE00333-3 Algorithmics CB003346
ii

LIST OF GRAPHS
Graph 8.1 Complete Graph for the Problem.............................................................................. 29
Graph 8.2Minimum Spanning Tree without constraints ........................................................... 30
Graph 8.3Minimum Spanning Tree with constraints................................................................. 31
Graph 8.4Minimum Spanning Tree using Dijkstra's starting from island 5 .............................. 32


CE00333-3 Algorithmics CB003346
iii

LIST OF PSEUDOCODES
Pseudocode 5.1 Prims Algorithm ................................................................................................ 9
Pseudocode 5.2 Kruskals Algorithm ........................................................................................ 12
Pseudocode 5.3 Dijkstras Algorithm ........................................................................................ 16
Pseudocode 5.4Brute Force Algorithm ...................................................................................... 20

CE00333-3 Algorithmics CB003346
iv

LIST OF TABLES
Table 2.1 Distance between islands ............................................................................................. 2
Table 5.1 Explanation for Prims Algorithm Pseudocode ........................................................... 9
Table 5.2 Explanation for Kruskals Algorithm Pseudocode .................................................... 13
Table 5.3Explanation for Dijjkstras Algorithm Pseudocode .................................................... 16
Table 5.4 Explanation for Brute Force Algorithm Pseudocode ................................................. 20


CE00333-3 Algorithmics CB003346
1

1 ASSUMPTIONS
For time complexity we will be considering the worst case.
Only the given algorithms are considered in deriving solution for the problem.
The solutions here are only valid for weighted, undirected complete graph.
Time complexities for operations of Queues, heaps and arrays are not explained but
directly used.

CE00333-3 Algorithmics CB003346
2

2 INTRODUCTION
The purpose for this assignment is to solve the problem of determining a network of
interconnected islands, optimized to minimize interconnection cost. The assignment specifies
application of graph theory to determine the solution.

There are eight islands on a lake that needs to be connected via seven bridges so that all of the
islands can be reached via any combination of bridges. The cost of construction is proportional
to the length of bridge. The distance between the islands is as below.
Table 2.1 Distance between islands
Island 8
Island 7 305
Island 6 175 205
Island 5 360 400 170
Island 4 160 330 295 230
Island 3 260 115 350 435 195
Island 2 265 175 215 180 185 155
Island 1 240 210 340 280 200 345 120

While keeping the construction cost minimum, the following two problems needs to be
addressed.
1. For minimum cost which of the seven bridges needs to be built
2. Islands 5,7 and 6,3 needs to be directly connected. The number of bridges is seven

CE00333-3 Algorithmics CB003346
3

3 PROBLEM ANALYSIS
A graph represents relationships between pairs of objects (Goodrich M.T. et al. 2006). For this
problem the relationship is the distance and the objects are the islands. Therefore, this problem
is able to be represented in a graph. A graph has a set of vertices(V) and edges(E) which
derives it's standard representation G(V,E). Here V refers to the 8 islands and E refers to the 28
connections or paths between the islands. The edges are weighted, which means that each edge
has a numeric label associated with it. In this case, the numeric value is the distance between
any considered pair of islands. Therefore, this becomes a weighted graph. Also, the edges are
not ordered. That is there is no direction to travel in any available edge. Hence, this graph
becomes an undirected graph. This concludes that the problem addresses a weighted
undirected graph denoted by G(V,E).
When the graph is drawn for above data of Table 1.1: Distance between islands it is clear that
any considered node is connected to all other nodes (Graph 8.1: Complete graph for the
problem) which makes this a complete graph. In a cyclic graph a path exists from a node to
itself. But the solution to this problem should not have cyclic nature. The reason is that, a path
to itself is unnecessary and a waste of resources, especially since this is an undirected graph.
Therefore we should consider the solution as an acyclic graph. An acyclic graph is a tree.

Now to find the optimum solution which is the least length of 7 bridges to connect the 8
islands. This means that all the islands have to be connected , but the connection might be via
any combination of bridges. This problem is solved with a spanning tree. A spanning tree is a
tree which contains every vertex of a connected graph. Since this problem also requires the
minimum aspect, the solution has to be a Minimum Spanning Tree(MST) where apart from all
vertices being connected, sum of the weights of the connected edges is minimum out of all
possible spanning trees.

The following chapters discusses and compares different algorithms that can be used for
solving the problem of finding the MST for this problem. Also, the solutions are not blind
applications of the algorithms, but they have to follow the 2 constraints mentioned before.
Which are,
CE00333-3 Algorithmics CB003346
4


1. Only 7 bridges exist
2. Islands 5,7 and 6,3 must be connected.

Therefore, in deriving the MST these constraints also has to be addressed. The assignment
mentions to use Prim's, Kruskal's, Dijkstra's and Brute-force algorithms to solve this problem.
A comparison of the differences in executions, time complexity and solutions of each of the
algorithms is one component of the assignment. The other component addresses the problem
of complexity classes of problems.

CE00333-3 Algorithmics CB003346
5

4 PROBLEM SOLVING METHOD
To solve the problem in hand the following process is used,
1. Understanding the problem
2. Deciding the type of solution required
3. Designing algorithms
4. Coding the algorithm
5. Deriving the solution, proving correctness
6. Analysing algorithm time efficiency
7. Justifying selection of algorithm
8. Finding the complexity class of algorithm

CE00333-3 Algorithmics CB003346
6

5 APPLICATION OF ALGORITHMS
Four different algorithms are concerned to solve the above problem. They are Prim's,
Kruskal's, Dijkstras and Brute Force. Out of these algorithms Prim's, Kruskal's and Dijkstras
algorithms use the greedy method. Greedy method is applied for optimization problems
(Goodrich M.T. , 2006). In here, the solution is obtained from a sequence of choices. The
choices are made in such a way that it is the most optimum at that particular time. All the
algorithms discussed below belongs to the greedy method of problem solving. Bruteforce uses
a different apprach. It verifies all the possible solutions and then after determine and produce
the optimum solution.
This section discusses the execution of these algorithms with pseudocodes. It discusses the
steps of the algorithm and explains the tricky and important sections of it. It also discusses
how we have approached to solve the problem of the assignment starting from the original
algorithms. The pseudocode standard used in this documentation is given in Appendix 8.1.
This section also discusses the time complexities in terms of Big-O notation. To calculate the
time complexities we consider the worst case of an algorithm. The process of calculation first
requires to count the number of primitive operations in the pseudocode. The primitive
operations considered here is given in Appendix 8.2. According to these operations we count
the number of primitive operations executed. The actual running time of an algorithm is
proportional to the number of primitive operations (Goodrich M.T. et al. 2006). The running
time of an algorithm depends on the input even of same size. Therefore, we need to define
three cases as average-case, best-case and worst-case. An algorithm's analysis must be for the
most challenging case, thus we consider the worst case of input. Asymptotic notation is the
notation used to mention time complexity of an algorithm. It is mathematical notation which
characterises running times based on functions of input size n. "Big-O" is the asymptotic
notation used for this discussion. It is discussed in Appendix 8.4
The next content of this chapter is the solutions derived from each of the algorithms. We have
mentioned the solution and the calculations here. The graphical representation of the answers
are available in Appendix 8.5.
CE00333-3 Algorithmics CB003346
7

5.1 PRIM'S ALGORITHM
5.1.1 Algorithm Execution
Since Prim's algorithm implements greedy method the growth of spanning tree starts from a
root vertex v. At each iteration of selection, minimum-weight edge e = (v,u) is selected to
connect vertex v to vertex u outside to the cloud of currently connected vertices. Thus e
becomes a part of the tree, T. This process is repeated until a spanning tree, T is formed (all
vertices connected). This becomes the MST to the problem (Goodrich M.T. et al. 2006).

In the prim's algorithm, all the vertices and their minimum weighted edge to connect to the
tree, T is saved in a priority queue, Q. The key for elements in Q is denoted by D[v] where v
refers to the vertex and its value is given by (v,(u,v)) where (u,v) is the minimum weighted
edge for a vertex to connect to T. D[v] holds w(u,v) which is the minimum weight. At the very
first when T is empty, we select a random vertex and assign its D[v] to zero. This makes sure
that during retrieving minimum D[v] element from Q, that vertex will be selected because all
other vertices(denoted by u) holds D[u] = . But this element will not hold an edge, therefore
only the vertex is added to T, since at the beginning all the edges are set to null. Then after all
the (u,v) of the other vertices in Q is updated to hold the minimum weighted edges it can have
to reach T (u refers to vertex already in T and v refers to vertices in Q). To perform the
updating, first denote the vertices adjacent to u (last added vertex) by z. Now the current
w(u,v) value is checked with new w(u,z) for each vertex in Q to see which is the minimum. If
w(u,z) is smaller than w(u,v), then the minimum weighted edge for vertex z is replaced by
(z,u) and D[z] is updated to hold the new w(u,z). Then after, at each iteration, vertex with
minimum D[v] is retrieved from Q and it's edge(u,v) is added to T. This process is continued
until Q becomes empty.

As far as the constraints are considered for this problem, edges (3,6) and (5,7) has to be
definitely connected in the MST. For a particular edge to be possible to be added to T, either of
its vertices has to join T. For example, if vertex 3 was added to T, all edges connected to 3
becomes possible to be added to T. The importance of this is that, when (3,6) is a compulsary
edge, either vertices 3 or 6 has to join T, for (3,6) to be added. Therefore, the suggested
CE00333-3 Algorithmics CB003346
8

solution to solve the problem of constraints is to make sure that when any of the vertices
{3,6,5,7} is added to T, adding its relevant compulsory edge before proceeding to other edges.
The technique used is same as the one used to add the very first vertex to T, by making its D[z]
to hold zero. As soon as either of vertices {3,6,5,7} is added, and when edges (3,6) or (5,7)
becomes possible to be added to T, D[z] key of the other vertex of Q, not yet connected is
updated to zero and the edge is also updated to now hold the compulsory edge. Then, the next
iteration adds that edge to T. For example, if vertex 3 was added, we update the key value of
vertex 6 to zero, D[6] = 0 and update its value to hold (6,(3,6)). then since D[6] is the
minimum in Q, it will be added to T. The rest of the procedure is same as the normal Prim's
algorithms as described in the previous paragraph. Following is the pseudocode to solve the
problem using Prim's algorithm.
1
2

3

4
5
6
7
8
9
10

11
12
13
14
15
16
17
18
19
20
Algorithm Prim(G) :
input: A weighted connected graph G with n(=8) vertices and m(=28) edges
{vertices are named as 1,2,..,8}
output: A minimum spanning tree T for G
{Limit set L compromises of the compulsary edges (5,7) and (6,3)}
Initialize L{(3,6),(5,7)}
Pick any vertex v of G
D[v] 0
for each vertex u v do
D[u]
Initialize T
Initialize a priority queue Q with an entry ((u,null),D[u]) for each vertex u, where (u,null) is
the element and D[u] is the key.
while Q is not empty do
(u,e)Q.removeMin()
Add vertex u and edge e to T.
for each vertex z adjacent to u such that z is in Q do
{perform the relaxation procedure on edge (u,v)}
if w((u,z)) <D[z] then
D[z]w((u,z))
Change to (z,(u,z)) the element of vertex z in Q.
Change to D[z] the key of vertex z in Q.
{check whether any of the possible edges are in the L set to verify whether
CE00333-3 Algorithmics CB003346
9


21
22
23
24
25
26
a compulsary edge is reachable}
if (u,z) is in L then
D[z]0
Change to (z,(u,z)) the element of vertex z in Q.
Change to D[z] the key of vertex z in Q.
Remove element (u,z) from L.
return the tree T
Pseudocode 5.1 Prims Algorithm
Table 5.1 Explanation for Prims Algorithm Pseudocode
# Explanation
1 The Prim's algorithm is being performed on graph G.
2 Input to the algorithm is the weighted connected graph G, with n vertices and m edges where n=8 and
m=28.The vertices are named as 1,2..8
3 Output of the algorithm is the Minimum Spanning Tree T for graph G.
4 The edges that form the compulsory bridges is saved in a set L for reference purpose.
5 Select any vertex, v of the n vertices of graph G. This means the vertex V is in the cloud C now. C is
the cloud of connected vertices graph G.
6 Since v is already in the cloud C, D[v] is assigned to zero.
7 D[u] is the weight of the best current edge for joining vertex u to the cloud C. For all other vertices of
the graph which are not in the cloud C(except v), denoted by u value of D[u] is assigned to infinity.
8
9 Since set T does not contain any vertices or edges yet, T is initialized to null set. T represents the MST
being created.
10 Create a priority queue, Q to hold value (u,null) and key D[u] for all vertices u.
11 Loop through lines 12-25 until the Q still has elements
12 Get the edge with the minimum D[u] from Q to (u,e).
13 Add the retrieved edge (u,e) to T. Since all elements in the queue for first iteration is null, this will add
only a single vertex v to T. In following iterations, it will add the minimum weighted edge In the
queue to T.
CE00333-3 Algorithmics CB003346
10

14 Loop through lines 14-25 for all the adjacent vertices to u denoted by z
15 Perform edge relaxation on all edges connected to z. (lines 16-19) Relaxation is performed to see
whether D[z] can take a better value using the edge (u,z)
16 w((u,z)) is the weight of edge connecting u and z, if this is smaller than D[z], do line 17-19
17 Assign value w((u,z)) to D[z]
18 Update value in vertex z of Q to (z,(u,z))
19 Update key value of z vertex element in Q to D[z]
20 Check whether there is a possibility to reach a compulsory edge. If there is make sure, it will be picked
by the tree in the next iteration by making the key value minimum for such a edge.
21 Check whether a reachable edge is in the set of compulsory edges L.
22 Assign the key value of vertex z of Q to zero
23 Update value in vertex z of Q to (z,(u,z)) which refers to a compulsory edge.
24 Update key value of z vertex element in Q to D[z] which is now zero
25 Remove element (u,z) which was a compulsory edge from T since it was taken care of.
26 Return the tree T which is the MST for graph G
5.1.2 Time Complexity
Inserting all vertices in Q with their initial key values = O(n log n)
At each iteration of while loop,
To remove edge (u,v) from Q = log n
To perform relaxation on edges adjacent to u = O(degree(u)log n) = O (log n)
Therefore, fore the whole while loop = (1 + Jcgrcc(u)) log n
u in 0

Therefore the total running time of the algorithm = O((n+m) log n)
( Since Jcgrcc :
: in 0
= 2m for a graph G with m edges )
= O (m log n)

CE00333-3 Algorithmics CB003346
11

Note : The steps not mentioned can be implemented in constant time. (i.e. Comparison of edge
weights, etc. )
5.1.3 Derived Solution
Suppose the starting vertex was 3. Then T will be adding edges to itself in the following order,
T = {(3,6) , (3,5) , (5,7) , (4,5) , (5,8) , (1,8) , (2,8)}
Total edge weight = 350+115+400+160+170+120+155 = 1470
The graphical representation of the MST is available in the Appendix 8.6.3.
5.2 KRUSKAL'S ALGORITHM
5.2.1 Algorithm Execution
Kruskal's algorithm builds the MST using clusters of vertices. Initially it considers each vertex
to be in a cluster of its own. Then after each edge is considered in increasing order of weight.
When an edge e connects two different clusters, then e is added to the set of edges of the MST
and the cluster is modified to also hold the vertices of the new cluster that was merged by e. If
e connects two vertices that are already in the same cluster, e is discarded. When enough
number of edges are added to the MST, the algorithm is terminated and MST is returned
(Goodrich M.T. et al. 2006).

In Kruskal's algorithm, the minimum weighted edges are added to T. The concept of clusters
ensures that no loops are created during this process. When the constraints are considered,
edges (3,6) and (5,7) must be included in the MST. In Kruskal's, unlike Prim's each of iteration
of adding edges to T, is independent, but within the limitation of clusters. Therefore, to make
use of this independency it is important to modify clusters accordingly. If the modification is
done correctly, there is no problem in adding an edge even at the very beginning of the
execution of the algorithm.This is the technique used to find the solution for the problem.
Therefore, edges (3,6) and (5,7) are first added to T. When they are added, their respective
clusters are also merged . That is C(3) and C(6) is merged to form {3,6} and C(5) and C(7) to
form {5,7}. After they have been added to T, T goes inside the while loop and loops until the
CE00333-3 Algorithmics CB003346
12

MST is complete.

Let's now discuss the condition for the while loop. It checks whether T has less than (n-1)
edges. For this problem this means 7 edges. It means that when it has 7 edges, the MST is
complete. It is important to understand how this condition works. We start the algorithm with 8
clusters each having a single vertex. In each edge addition to T we merge two of these clusters.
That is when one edge is added to T we reduce the number of clusters also by one. When MST
is formed, there should be only one cluster, which holds all the vertices. Therefore, when 7
edges are added to T, 8 clusters are reduced by 7 to result in 1 cluster with all vertices. This
means that when all vertices are connected, there is only one cluster remaining and T has 7
edges in it. This created T is then the MST. Following is the pseudocode for the Kruskal's
algorithm.

1
2

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Algorithm Kruskal(G) :
input: A weighted connected graph G with n(=8) vertices and m(=28) edges
{vertices are named as 1,2,..,8}
output: A minimum spanning tree T for G
for each vertex v in G do
Define an elementery cluster C(v){v}
Initialize a priority queue Q to contain all edges in G, using weights as keys.
T {T will ultimately contain the edges of the MST}
{Add compulsary edges to T}
Add edge (3,6) to T.
Add edge (5,7) to T.
Merge C(3) and C(6) to one cluster, that is union C(3) and C(6).
Merge C(5) and C(7) to one cluster, that is union C(5) and C(7).
while T has fewer elements than n-1 edges do
(u,v)Q.removeMin()
Let C(v) be the cluster containing v, and let C(u) be the cluster containing u.
if C(v) C(u) then
Add edge (v,u) to T.
Merge C(v) and C(u) to one cluster, that is union C(v) and C(u).
return the tree T
Pseudocode 5.2 Kruskals Algorithm
CE00333-3 Algorithmics CB003346
13

Table 5.2 Explanation for Kruskals Algorithm Pseudocode
# Explanation
1 The Kruskal's algorithm is being performed on graph G.
2 Input to the algorithm is the weighted connected graph G, with n vertices and m edges where n=8 and
m=28. The vertices are named as 1,2..8.
3 Output of the algorithm is the Minimum Spanning Tree T for graph G.
4 Loop through all vertices v in G and define clusters denoted by C[v] to hold {v}
5
6 Create a priority queue, Q to hold all edges in G. The key for the elements is the weight of the edge.
7 Since set T does not contain any vertexes or edges yet, T is initialized to null set.T refers to the MST
being created.
8 The compulsory edges are added to T to make sure that MST definitely includes those.
9 Adding edge (3,6) to T
10 Adding edge (5,7) to T
11 Merge clusters C(3) and C(6) to one cluster resulting {3,6}
12 Merge clusters C(5) and C(7) to one cluster resulting {5,7}
13 Loop through lines 14-18 until T has less than 7 elements
14 Get the edge (u ,v)with minimum weight(key) from Q
15 Denote clusters containing u and v vertexes as C(u) and C(v) respectively
16 If vertexes v and u both reside in the same cluster do not add edge(u,v) to T, since it would create a
loop. Otherwise do 17-18
17 Add edge (u,v) to T
18 Merge C(u) and C(v) to hold union of C(u) and C(v) clusters.
19 Return the tree T which is the MST for graph G

CE00333-3 Algorithmics CB003346
14

5.2.2 Time Complexity
Inserting all edges in Q with their initial key values = O(m log m)
At each iteration of while loop,
To remove edge (u,v) from Q = O(log n)
Using sequence-based union-find structure allows to perform a series of N union and
find operations in O(N log N) time
Therefore, (n-1) calls to union and at most m calls to find, total time for merging
clusters and determining clusters belonging to = O(m log n)
Therefore,
Therefore the total running time of the algorithm = O((n+m) log n)
Can be simplified as = O (m log n)
5.2.3 Derived Solution
T will be adding edges to itself in the following order,
T = {(3,6),(5,7),(3,5),(1,8),(2,8),(4,5),(5,8)}
Total edge weight = 350 + 400 + 115 + 120 + 155 + 160 + 170 = 1470
The graphical representation of the MST is available in the Appendix 8.6.3.
5.3 DIJKSTRA'S ALGORITHM
5.3.1 Algorithm Execution
Dijkstras algorithm's original purpose is to apply greedy method to a single source, shortest-
path problem. When a graph is given and a source vertex to initiate traversal is given, the
algorithm finds the shortest path to connect each of other vertexes to the source vertex. It
performs a weighted breadth first search starting from the source vertex.
This is not an algorithm to find MST. The purpose of Dijsktra's is to find the shortest path
from a source vertex to a destination vertex. Although this seems to be also resulting the path
which thus connects the vertexes, it does not. It only returns the shortest path weight. It can be
CE00333-3 Algorithmics CB003346
15

argued that if we can find the shortest path weight, the algorithm can be improved to save the
edges that create the shortest path. Although, this statement is true, we then cannot call that
algorithm Dijkstra's algorithm. Prim's is the algorithm which addresses this statement. It starts
from one vertex, which is equivalent to source vertex of Dijkstra's and performs same
minimum-keyed element retrieval from priority queue and relaxation process, where instead of
the path weight, it stores the edges. These edges in the end produce the MST for the graph.
Instead of MST in Prim's Dijkstras produce the shortest weight for each of the vertices,
starting from the source vertice. Therefore, for this problem too we need to be producing the
shortest paths from a source vertice(island) to other vertices. It is also important to mention
that when the source vertice changes, so does the shortest path.
To implement the constraints of bridges (3,6) and (5,7) in Dijkstras, similar to Prim's when
adjacent edges are checked, we need to check whether such an edge is a compulsory edge. If
so we add the other vertex of that particular edge and update the label Ds to proceed to other
iterations. The rest of the process is similar to Prim's algorithm. Following is the pseudocode
for the Dijkstra's algorithm.
1
2


3

4
5
6
7
8
9
10
11
12
13
14
15
Algorithm Dijkstra(G,v) :
input: A undirected weighted graph G with non-negative edge weights, and a
distinguishable vertex v of G
{v can take any vertex from 1,2..8}
output: A label D[u] , for each vertex u of G, such that D[u] is the length of the
shortest path from v to u in G
Initialize L{(3,6),(5,7)}{Limit set L contains of the compulsary edges (5,7) and (6,3)}
D[v] 0
for each vertex u v do
D[u]
Let a priority queue Q contain all the vertices of G using D labels as keys.
while Q is not empty do
{pull a new vertex u into the cloud}
uQ.removeMin()
for each vertex z adjacent to u such that z is in Q do
{perform the relaxation procedure on edge (u,z)}
if D[u] + w((u,z)) <D[z] then
D[z] D[u] + w((u,z))
CE00333-3 Algorithmics CB003346
16

16
17

18
19
20
21
22
23
24
25
26
27
28
Change to D[z] the key of vertex z in Q.
{check whether any of the possible edges are in the L set to verify whether
a compulsary edge is reachable}
if (u,z) is in L then
D[z]0
zQ.removeMin()
Remove element (u,z) from L.
D[z]D[u]+ w(u,v)
for each vertex x adjacent to z such that x is in Q do
{perform the relaxation procedure on edge (x,z)}
if D[z] + w((x,z)) <D[x] then
D[x] D[z] + w((x,z))
Change to D[x] the key of vertex x in Q.
return the label D[u] for each vertex u
Pseudocode 5.3 Dijkstras Algorithm
Table 5.3Explanation for Dijjkstras Algorithm Pseudocode
# Explanation
1 The Dijkstras algorithm is being performed on graph G with source vertex v.
2 Input to the algorithm is the non-negative weighted connected graph G and the source vertex v
3 Output of the algorithm is the shortest paths to all vertices from v
4 The edges that form the compulsory bridges is saved in a set L for reference purpose.
5 Vertex, v needs to be in cloud C at the beginning. Therefore D[v] is assigned zero.
6 For all other vertices of the graph which are not in the cloud C(except v), denoted by u value of D[u] is
assigned to infinity.
7
8 Create a priority queue, Q to hold values D[u] for all vertices u ) with D[u] as key.
9 Loop through lines 10-28 until the Q still has elements
10 During each iteration a new vertex is added to the cloud
11 Take the vertex u with minimum D[u]. In the first iteration it will take vertex v since D[v] is zero and
all other D labels are infinity.
CE00333-3 Algorithmics CB003346
17

12 Loop through 13-28 for all vertices z adjacent to u
13 Perform relaxation procedure on edge (u,z) in lines 14-16. Relaxation is performed to see whether D[z]
can take a better value using the edge (u,z)
14 w((u,z)) is the weight of edge connecting u and z, if D[u]+w((u,z)) is smaller than D[z], do line 17-19
15 Assign value D[u]+w((u,z)) to D[z]
16 Update key value of z vertex element in Q to D[z]
17 Check whether there is a possibility to reach a compulsory edge. If there is make sure, it will be picked
by the tree in the next iteration by making the key value minimum for such a edge.
18 Check whether a reachable edge is in the set of compulsory edges L. If it is, do line 19-27. If it is a
compulsory edge, that vertex should be pulled to cloud C and a new process of relaxation needs to be
performed.
19 Assign D[z] to zero, so that it now becomes the minimum-keyed element in Q
20 Vertex z now holds the minimum-keyed element from Q. Remove it from Q
21 Remove edge (u,v) from L
22 Assign the value of D[z] to hold D[z]+w((x,z))
23 Loop through 13-28 for all vertices x adjacent to z
24 Perform relaxation procedure on edge (x,z) in lines 25-27. Relaxation is performed to see whether D[x]
can take a better value using the edge (x,z)
25 w((x,z)) is the weight of edge connecting x and z, if D[z]+w((x,z)) is smaller than D[x], do line 26-27
26 Assign value D[z]+w((x,z)) to D[x]
27 Update key value of x vertex element in Q to D[x]
28 Return the labels D[u] for vertices u in G
5.3.2 Time Complexity
Inserting all vertices in Q with their initial key values = O(n log n)
At each iteration of while loop,
To remove vertex u from Q = log n
CE00333-3 Algorithmics CB003346
18

To perform relaxation on edges adjacent to u = O(degree(u)log n)
To perform relaxation on edges adjacent to z = O(degree(z)log n)
Therefore, fore the whole while loop
= (1 + Jcgrcc(u) + Jcgrcc(z) ) log n
z in 0

Therefore the total running time of the algorithm = O((n+m) log n)
Can be simplified as = O (n
2
log n)
5.3.3 Derived Solution
Suppose the source vertex was 5. The following D[u] values will be returned for vertices u.
D[1] = 280, D[2] = 215, D[3] = 115, D[4] = 160, D[5] = 0, D[6] = 465,D[7] = 400, D[8] = 170
The MST it follows contains edges, {(1,5), (2,5),(3,5),(4,5),(3,6),(5,7),(5,8)}
Total edge weight = 1690
The graphical representation of the MST is available in the Appendix 8.6.4.
This process can be repeated for any source vertex.
5.4 BRUTE-FORCE ALGORITHM
5.4.1 Algorithm Execution
Brute-force is another algorithmic design pattern just as greedy method. It is most used for
searching and optimization problems. In this technique, we enumerate all possible
configurations of the inputs involved and then pick the best out of all those enumerated
configurations. Brute-force is most commonly used in pattern-matching (Goodrich M.T. et al.
2006). Cayley's formula (1889) suggests that for a complete graph with v vertices, it produces
v
v-2
spanning trees (ST). This means 8
8-2
= 262144 STs for this problem. This is a huge number
and will definitely result in problems of efficiency. Therefore, using this technique to find the
MST of a graph is not a good method.
However as far as MST for this problem is considered, the way to approach the solution is to
define all possible STs for the problem and decide which is the MST by comparing total edge
CE00333-3 Algorithmics CB003346
19

weight's for all of them. The algorithms can either start from one vertex and move along edges,
similar to Prim's or can simply pick up edges as in Kruskal's. Although the approach of
selecting edges can be same as Prim's or Kruskal's, this algorithm does not always pick the
minimum possible edge as in either of them.
STs are formed and their total edge weight is calculated and checked with the reference value
which holds the so far minimum total edge weight. If the new weight is smaller than the
former minimum, the MST is assigned to the new T. This process is continued until all
possible STs are examined and finally at the end T will hold the MST.
The picking up of edges is done as in Kruskal's for the brute-force algorithm used here. That
means any edge can be picked at any time. The reason to select Kruskal's kind of approach
without Prim's approach of starting from one vertex is that Prim's requires additional
computations (i.e. edge relaxation). Since we anyway have to visit all possible STs, it is better
to use the simpler approach which is the approach in Kruskal's in this case.
As mentioned in Kruskal's the number of edges in a ST for a graph of n vertices is (n-1)
Therefore, for this problem we need to focus on 7 edges. With the constraints of the two
compulsary edges (3,6) and (5,7), each of the STs formed can be different only in 5 edges.
Therefore, the brute-force algorithm must consider the different ways of selecting those 5
edges. The tricky part in the algorithm is to make sure that the same combination of edges
does not occur again and again.
As explained in Appendix 8.7, to make sure all the STs are considered with the help of number
of STs when constraints are available is rather a difficult task. A relationship needs to be
defined for this, which is not a part of this assignment. Therefore, the best possible approach
to be used here is to define all the possible STs eventhough it results in a large number. Then
after the MST is found from those STs which satisfies the constraints.However, there is a small
drawback in this approach, because not only the looping happens for a large number, it also
might go to an infinite loop where no distinguishable T occur. This is the limitation of not
having a mechanism to make sure same T is not created while edges are being added.
Following is the pseudocode for the brute-force technique.
1 Algorithm BruteForce(G) :
CE00333-3 Algorithmics CB003346
20

2

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
input: A weighted connected graph G with n(=8) vertices and m(=28) edges
{vertices are named as 1,2,..,8}
output: A minimum spanning tree MST for G
{Limit set L compromises of the compulsary edges (5,7) and (6,3)}
Initialize L{(3,6),(5,7)}
SP {SP will contain the spanning trees, (in terms of edges) so far created}
MST {MST will ultimately contain the edges of the MST}
while SP has fewer elements than n
n-2
do
for each vertex v in G do
Define an elementery cluster C(v){v}
Initialize an queue Q to contain all edges in G.
T {T will contains edges of a considered ST}
while T has fewer elements than n-1 edges do
(u,v)Q.removeRandomElement()
Let C(v) be the cluster containing v, and let C(u) be the cluster containing u.
if C(v) C(u) then
Add edge (v,u) to T.
Merge C(v) and C(u) to one cluster, that is union C(v) and C(u).
if T is not in SP
Add tree T to SP.
W {W will ultimately hold the total weight of edges for MST}
for each tree T in SP do
if L is in T then
Sum0
for each edge(u,v) in T do
SumSum+w((u,v))
if Sum < W then
WSum
MSTT
return tree MST
Pseudocode 5.4Brute Force Algorithm
Table 5.4 Explanation for Brute Force Algorithm Pseudocode
# Explanation
1 The brute-force algorithm is being performed on graph G
CE00333-3 Algorithmics CB003346
21

2 Input to the algorithm is the weighted connected graph G, with n vertices and m edges where n=8 and
m=28. The vertices are named as 1,2..8.
3 Output of the algorithm is the Minimum Spanning Tree MST for graph G.
4 The edges that form the compulsory bridges is saved in a set L for reference purpose.
5 L holds the edges (3,6) and (5,7)
6 Initialize the set of spanning trees SPs to null set
7 Initialize the set of MST to null set
8 Loop through lines 8-19 until SP contains all possible spanning trees (262144) for G.
9 Loop through all vertices v in G and do line 10
10 Define clusters denoted by C[v] to hold {v}
11 Create a queue, Q to hold all edges in G.
12 Since set T does not contain any vertexes or edges yet, T is initialized to null set. T refers to the SP
being created.
13 Loop through lines 14-18 until T has less than 7 elements
14 Get the edge(u,v) a random edge from Q
15 Denote clusters containing u and v vertexes as C(u) and C(v) respectively
16 If vertexes v and u both reside in the same cluster do not add edge(u,v) to T, since it would create a
loop. Otherwise do 17-18
17 Add edge (u,v) to T
18 Merge C(u) and C(v) to hold union of C(u) and C(v) clusters.
19 If the created tree T is not already in SP do line 20
20 Add T to SP
21 Initialize W which will ultimately hold the minimum edge weight for each valid(with constraints) tree
T in SP
22 For each tree T in SP do lines 23-29
23 If constrained edges in L are in edges of T, then do line 24-29
CE00333-3 Algorithmics CB003346
22

24 Initialize Sum to zero which will hold the total weight of the edges of T
25 For each edge in T (u,v) do line 26
26 Add the weight of (u,v) edge to sum
27 Compare the calculated total edge weight with W to see whether it is lesser than former minimum total
edge weight. If it is do lines 28-29
28 Assign new total edge weight Sum to W
29 Assign the new MST to T
30 Return the MST



5.4.2 Time Complexity
At each iteration of the outer while loop,
Inserting all edges in Q with their initial key values = O(m log m)
At each iteration of while loop,
To remove edge (u,v) from Q = O(log n)
Using sequence-based union-find structure allows to perform a series of N union and
find operations in O(N log N) time
Therefore, (n-1) calls to union and at most m calls to find, total time for merging
clusters and determining clusters belonging to = O(m log n)
Therefore the total running time for two loops = O(n
n-2
(n+m) log n)
Can be simplified as = O(n
n
log n)
At each iteration of the for loop
To reset value of sum = O(n)
Total running time for loop = O(n
2
)

Therefore, total running time for the algorithm = O(n
2
+ n
n
log n)
Can be simplified as = O(n
n
log n)
CE00333-3 Algorithmics CB003346
23

5.4.3 Derived Solution
The solution for MST from brute force algorithm is same as the graph in Appendix 8.6.3. It
will initially consider 262144 STs and then calculate each of its weight to find the MST. MST
will be as follows.
MST = {(3,6),(5,7),(3,5),(1,8),(2,8),(4,5),(5,8)}
Total edge weight = 350 + 400 + 115 + 120 + 155 + 160 + 170 = 1470
5.5 COMPARISON OF THE ALGORITHMS
When comparing the algorithms, as discussed before Prims, Kruskals and Dijkstras uses
greedy method and Brute Force uses the complete opposite algorithm. To compare the
algorithms, we need to compare their time complexities. Following are the time complexities
for the four algorithms in terms of Big-O notation.
Prim's O(m log n)
Kruskal's O(m log n)
Dijkstra's O(n
2
log n)
Brute Force O(n
n
log n)
When refering to these time complexities, it is clear that Prims and Kruskals has same values
and they hold the minimum of all four. Next is Dijkstra and finally Brute Force. Brute Force
has a very large running time compared with other three. The reason is the high number of
computations done for deriving all spanning trees. It only works well for small graphs.
Therefore, when considering running times Prims and Kruskals are the most time efficient
algorithms that can be used for a this kind of problem.
When discussing about the solutions derived from each of the algorithms, Prims, Kruskals
and Brute Force will provide the same solution. But Dijsktras cannot be guaranteed to
produce the same solution, because it depends on the source vertex for the algorithm.
Therefore, it might not be the most optimized solution it is providing. This is clear when
referring to the graphs Graph 8.3 and Graph 8.4. The MST is different to the shortest path
created when source vertex is 5. The total edge weight is also a higher value than the MST.
CE00333-3 Algorithmics CB003346
24

Therefore, Dijkstras is not the best algorithm for this kind of a problem.
Hence, Dijkstras and Brute Force are both not good to be used for a problem as this. Best is to
use Kruskals or Prims because they provide the correct answer in the most efficient time.

CE00333-3 Algorithmics CB003346
25

6 COMPLEXITY CLASSES
The time complexities for graphs in terms of Big-O can be O(n), O(n
2
) , O(n
3
) or even
O(nlogn). All these functions can be given in form n
k
, in which n is the input size. O(n
k
) is
then referred to as polynomial time (Johnsonbaugh R., et al. 2004). Algorithms in with
polynomial running time is considered to be efficient, which means they can be performed on
large n values.
According to Cormen T.H., et. al. (2001), there are three fundamental complexity classes of
problems. They are P, NP and NPC (NP Complete). Class P refers to problems that are solvable in
polynomial time. Class NP refers to problems that are verifiable in polynomial time. Verifying a
problem means that when a solution is given for a problem, it should be able to confirm that the
solution is correct in time polynomial. If a problem is in P, it is also in NP. Class NPC refers to
problems that are intracable. That means, we cannnot conclude whether NPC problems are
solvable in polynomial time or not.
6.1 JUSTIFICATION OF CHOSEN COMPLEXITY CLASS
Out of the different time complexities achieved for different algorithms applied on same
problem, Prims and Kruskals yields the most efficiency. Therefore, time complexity of O(m
log n) is defined for this problem. This means that the problem has a polynomial time
complexity. Which means that this problem belongs to class P.
6.2 DEFINITION OF COMPLEXITY CLASS P
P is the complexity class of decision problems that can be solved using a polynomial amount
of computation time. (Stroppa N. 2006)
6.3 EXAMPLE FOR COMPLEXITY CLASS P
Class P contains many problems. Examples for some are, finding shortest path in networks,
sorting, linear programming. Problem of sorting an array will be discussed here as an example
for a class P problem. For a sorting problem there are a few algorithms that are often used.
They are merge-sort, quick-sort, insertion-sort and selection-sort. Following are the time
complexities for sorting an array of n elements in terms of Big-O as mentioned by Goodrich
CE00333-3 Algorithmics CB003346
26

M.T., et.al. (2006).
Merge-sort = O(n log n)
Quick-sort = O(n
2
)
Insertion-sort = O(n
2
)
Selection-sort = O(n
2
)
When considering the time complexities of these different algorithms, it is clear that all of
them have a polynomial time complexity. Also the solutions for these problems are verifiable
in polynomial time. If an array of elements and sorted elements are given, we can confirm
whether the solution is correct. Therefore, this fulfils both requirements of a class P problem.

CE00333-3 Algorithmics CB003346
27

7 REFERENCE

Brassard G., Bratley P. (2005), Fundamentals of Algorithmics, Prentice Hall of India, India:Delhi

Cormen T.H., Leiserson C.E., Rivest R.L., Stein C. (2001) , Introduction to Algorithms, 2nd ed.,
MIT Press, USA:Cambridge

Goodrich M.T., Tamassia R. (2006), Data Structures and Algorithms in Java, 4th ed., John Wiley
& Sons, USA

Johnsonbaugh R., Schaefer M. (2004), Algorithms, Dorling Kindersley, India:Delhi

Stroppa N. (2006),Algorithms and Complexity, [online], Available :
http://www.computing.dcu.ie/~nstroppa/teaching/ca313_complexity.pdf, [Accessed 5th June
2011]
CE00333-3 Algorithmics CB003346
28

8 APPENDIX
8.1 Pseudocode Standard
This documentation follows the same pseudocode standard used in the book "Data Structures
and Algorithms in Java" (Goodrich M.T., et al. 2006).
8.2 Primitive Operations
This documentation consideres the same primitive operations used in the book"Data Structures
and Algorithms in Java" (Goodrich M.T., et al. 2006).
Assigning a value to a variable
Calling a method
Performing an arithmetic operation (for example, adding two numbers)
Comparing two numbers
Indexing into an array
Following an object reference
Returning from a method.
8.3 Priority Queues
A priority queue is an abstract data type used for storing a collection of prioritized elements
that supports arbitraryelement insertion but supports removal of elements in order of priority
(Goodrich M.T. et al. 2006). An element in priority queue contains key and value. Key decides
the priority of the elements. Out of methods in priority queues, removeMin() is the most
used method in this documentation. This returns the elemts with the smallest key value.
For the purpose of this assignment, we consider that the priority queue is implemented in a
Heap. Heap allows insertion and removal of elements in logarithmic time. This is better than
implementing using a list because then running time is proportional to n, input size. A heap is a
binary tree which stores a collection of entries at its nodes. Heap-order priority happens in
such a way that the key stored in node v is always greater than its parent's key. The up-heap
bubbling and down-heap bubbling techiniques with running time of O(n log n) is used for
insertion and removal in this assignment.
CE00333-3 Algorithmics CB003346
29

8.4 Big-O Notation
As mentioned by Goodrich M.T. et al. (2006), Big-O notation is as follows.
Let f(n) and g(n) be functions mapping nonnegative integers to real numbers. We say that f(n)
is O(g(n)) if there is a real constant c > 0 and an integer constant n
0
1 such that,
f(n) cg(n), for n n
0
8.5 Graphs
8.5.1 Complete Graph For The Problem

Graph 8.1 Complete Graph for the Problem

CE00333-3 Algorithmics CB003346
30

8.5.2 Minimum Spanning Tree Without Constraints

Graph 8.2Minimum Spanning Tree without constraints

Total edge weight = 115+120+155+160+170+175+180 = 1075
CE00333-3 Algorithmics CB003346
31

8.5.3 Minimum Spanning Tree With Constraints

Graph 8.3Minimum Spanning Tree with constraints

Total edge weight = 400 + 350 + 115 + 160 + 170 + 120 + 155 = 1470
CE00333-3 Algorithmics CB003346
32

8.5.4 Minimum Spanning Tree Using Dijkstra's Starting From Island 5

Graph 8.4Minimum Spanning Tree using Dijkstra's starting from island 5
8.6 Number Of Spanning Trees For A Complete Graph With Constraints
For a graph with v vertices, the spanning tree contains (v-1) edges. Also the number of
spanning trees can be given by v
v-2
(Cayleys 1889). Using this formula, lets determine a
relationship between number of constraints and the number of spanning trees. Consider the
following complete graph with 4 vertices. The coloured edges will be the constraints
(compulsary edges) we will be considering

CE00333-3 Algorithmics CB003346
33

According to Cayleys formula there has to be 4
4-2
=16 spanning trees for the graph and each
spanning tree contains (4-1) = 3 edges. The second image shows all the spanning trees.
According to the spanning trees, lets consider the number of spanning trees when constraints
are present.
# constraints Constraint # spanning trees
0 No constraint 16
1
Blue 8
Red 8
Green 8
2
Blue and Red 3
Blue and Green 4
Red and Green 3
3 Red, Green and Blue 1

Conclusion:
According to the above numbers, althought there is a relationship in one constraint, there is no
such linear relationship when two or more constraints are present. If the two compulsary edges
are adjacent then the number of spanning tree is lesser. When the number of constraints equals
the number of edges in spanning tree, only one spanning tree exists.

Vous aimerez peut-être aussi