Vous êtes sur la page 1sur 38

Table of Contents

Table of Contents..........................................................................................................1 Assumptions................................................................................................................3 I2.0 Introduction............................................................................................................4 3.0 Problem Analysis....................................................................................................5 4.0 Problem Solving Method.........................................................................................7 5.0 Algorithm Analysis..................................................................................................8 5.1 Kruskal's Algorithm.................................................................................................9 5.1.1 Algorithm Execution........................................................................................9 5.1.2 Pseudo code .................................................................................................10 5.1.3 Time complexity.............................................................................................12 5.1.4 Derived solution.............................................................................................13 5.1.4.1 Mathematical evaluation..........................................................................13 5.1.4.2 Graph representation..............................................................................16 5.2 Prims Algorithm....................................................................................................17 5.2.1 Algorithm Execution.......................................................................................17 5.2.2 Pseudo code.................................................................................................18 5.2.3 Time complexity.............................................................................................20 5.2.4 Derived solution.............................................................................................21 5.2.4.1 Mathematical evaluation..........................................................................21 5.2.4.2 Graph representation.............................................................................22 5.3 Dijkstras Algorithm...............................................................................................23 1

5.3.1 Algorithm Execution.......................................................................................23 5.3.2 Pseudo code..................................................................................................24 5.3.3 Time complexity.............................................................................................26 5.3.4 Derived solution.............................................................................................27 5.3.4.1 Graph representation..............................................................................27 5.3.4.2 Mathematical evaluation..........................................................................29 5.4 Brute Force Algorithm...........................................................................................31 5.4.1 Algorithm Execution.......................................................................................31 5.4.2 Pseudo code..................................................................................................32 6.0 Complexity classes...............................................................................................34 6.1 Justification of chosen complexity class...........................................................35 6.2 Definition of complexity class P........................................................................35 6.3 Example for complexity class P........................................................................35 APPENDIX..................................................................................................................37 References..............................................................................................................37

Assumptions

Consider the time complexity of each algorithm according to worst case. Consider only about weighted, connected, undirected graphs. Every algorithm execution is begin with node A Consider only the binary heap representations in queues. Binary heap implementation is not explained in any situation.

I2.0 Introduction
As graph theory has many significant realistic applications, finding the optimize solution for road network has more priority. The objective of this assignment is to finding a more efficient solution for interconnecting cities and towns in order to ensure that passengers can travel easily. Graph theory will be considering overcoming with optimal solution for this problem.

The road map is represented by the following graph.

1.1 Road Map

The nodes represent the cities / towns and edges represent the roads. The weights of each edge represent the distance between cities. Following key points need to be considering when simplifying the road network. 1. Because of the economic importance of A and B cities, the direct road between those must be included. 2. All capital cities need to be interconnected with shortest distance.

3.0 Problem Analysis


(This chapter considers the same manner used in the book Algorithm (Johnsonbaugh R., et al.2004) to analyze the given problem and its solution).

According to (Skiena S.S. et al.2003) a graph (G) is a pair of sets of Vertices (V) and Edges (E) and can be denoted as G = (V,E). In here V is a set of objects and E is a set of vertex pairs. Considering those features of graphs, figure 1.1 (road map) also can be represented as graph. V refers to the towns / cities and E refer to the paths between those cities. V= {A,B,C,D,E,F,G,H,I,J,K,L,M,N,O} E={{A,B},{B,C},{A,D},{C,D},{C,E},{E,F},{B,F},{F,H},{H,J},{J,I},{I,G},{I,F},{B,G}, {K,G} {K,A},{L,A},{L,K},{K,O},{O,N},{N,M},{M,D},{M,A},{M,O}} G = (V, E)

Road networks between cities / towns are typically undirected (Skiena S.S. et al.2003). Because most of the roads are allow for travel both directions. In this scenario, figure 1.1 does not represent any direction between cities. Therefore it is an undirected graph. This means the pairs of edges not need to be ordered and it is just sets of two vertices (Goodrich M.T. et al. 2006). It can be representing as both E1 and E2. E1 = {A, B} or (A, B) E = E1 = E2 E2 = {B, A} or (B, A)

According to (Skiena S.S. et al.2003) in weighted graphs, each edge assigned with a numerical value. Figure 1.1 also applies this mechanism to represent the distance between cities. Therefore it can be considered as a weighted graph. Then if required can be represent this as G = (V, E, w). In here w = distance (Johnsonbaugh R., et al.2004).

Cyclic graph is a graph that has at least one edge, which begins and ends on the same vertex (Goodrich M.T. et al. 2006). Since having more than one cyclic path, this problem consists with a cyclic graph. Path = P, P1 = A, B, C, D, A P2 = O, M, N, O P3 = A, L, K, A

But this feature is not worthy for this scenario due to its undirected nature. Because unwanted cyclic paths create unwanted loops, since those wasted the time, cost and also reduces the efficiency. Therefore solution should require overcoming with an acyclic graph by eliminating those loops/ cycles.

One of the key requirements of this problem is to giving ability to get there from here. It means need to have at least one path from any city to any other city. It may either directly or by going through other cities (Johnsonbaugh R., et al.2004). Such a graph is defined as connected graph (Goodrich M.T. et al. 2006). Since not having any disjoint connected regions this can be represented as {A, B, C, D, E, F, G, H, I, J, K, L, M, N, O,} continuously. As well as given figure 1.1 road map satisfy this concept, the solution to this problem also need to be satisfy.

When summarizing all above analysis results indicates, given problem is a simple graph (Johnsonbaugh R., et al.2004) with the features of undirected, weighted, cyclic, connected and solution can be simplify as undirected, weighted, acyclic, connected graph. Then it becomes a Tree (Skiena S.S. et al.2003). According to (Johnsonbaugh R., et al.2004) a tree (T) is a simple graph that satisfying the features of states above.

After simplifying the features of given problem and proposed solution one more thing needs to be added. The optimal solution expect with the shortest distance from each capital city. Therefore required to consider about all possibilities of spanning trees and select the minimum among those (Goonetillake, J. 2011). Must require considering the main key points as mention in the introduction when finding the MST for this problem.

The given problem expects approximation solution by using brute-force, Prims, Kruskals and Dijkstras algorithms and require to compare the efficiency of those algorithms in terms of bigO notation. Also require to decide and justify the complexity class of this problem.

4.0 Problem Solving Method


1. Understand the problem. 7

2. Understand the key constrains of given problem. 3. Analyze the problem. 4. Then decide the solutions nature in terms of exact vs. approximate, nature of the inputs and also the data structures require using. 5. According to above results design the algorithm. 6. Then check for correctness and prove the existence of the algorithm. 7. Then analyze the algorithm in term of complexity time and complexity class. 8. Implement the algorithm (develop a code)

5.0 Algorithm Analysis


The given problem requires to concern about Prims, Kruskals, Dijkstras and Brute Force algorithms. Because of providing optimal solution for a given problem, (except Brute Force algorithm) above mentioned algorithms are laying under approximate algorithms category. 8

According to (Goonetillake, J. 2011) and (Cormen, T.H.et al.2001) among these algorithms Prims and Kruskals algorithms are used to find MST and both are use greedy approach. Greedy algorithms build up a solution piece by piece (Dasgupta, S., et al. 2006: P.139) and at each step provide the choice that looks best at the moment (Cormen, T.H.et al.2001: P.474). Therefore its not generally guaranteed to find globally optimal solutions and provide locally optimal solution in the hope that will lead to a global optimal solution (Goonetillake, J. 2011). Even those do not necessarily give the best solution, but for some problems they do. However the two approaches are differing in how they perform in the graph (Goonetillake, J. 2011).

Dijkstras algorithm applies for finding the shortest path problems and it also uses the greedy approach (Cormen, T.H.et al.2001). Brute Force uses different approach rather comparing to other algorithms. It is more straightforward and usually directed on the problem. Also it guaranteed to produce an optimum solution (Levitin, A. 2007).

Next chapter will discuss about the execution of above algorithms with their pseudo codes. Also explain each steps of the algorithm in briefly. Then will explain the time complexities of each algorithm in terms of Big-O notation. Finally provide the derived solutions according to each algorithm with graph representation and the calculations.

5.1 Kruskal's Algorithm


5.1.1 Algorithm Execution

This algorithm begins with modeling a collection of disjoint sets of nodes of a particular component. Initially each node is a component of itself (Dasgupta, S., et al. 2006: P.143). The set of all disjoint sets together define as forest. Because of the key constrains of the given problem require merging both A and B nodes. Therefore edge of (A, B) needs to be added to 9

the forest (A) at the beginning. To perform next process each set of vertices in a tree must require containing in the current forest (Cormen, T.H.et al.2001: P.482). Then algorithm will ordered each edge by ascending order according to their weight (Goodrich M.T. et al. 2006). Then it finds safe edges to add to the growing forest (Cormen, T.H.et al.2001: P.482). If some node is including some MST and also including given graph called safe edges (Cormen, T.H.et al.2001: P.477) (there are other several requirements also need to be satisfy for eligible to safe edges and those will be discuss in Appendix.. ). This requires beginning with the lightest edge and once an edge is chosen, the corresponding components require to be merged (Dasgupta, S., et al. 2006: P.143). Then will reach to next lightest edge and check for eligibility. While perform this process make sure not to have any cycles (Dasgupta, S., et al. 2006: P.143). After the algorithm has added enough edges, it terminates the process and return the MST as output (Goodrich M.T. et al. 2006: P.855).

5.1.2 Pseudo code

The following pseudo code provides form (Goonetillake, J. 2011). MST - Kruskal (G, w) 1. 2. 3. 4. A0 for each vertex v V[G] do Make - Set(v) A A U { (A, B) } (edited according to the given requirement) 10 A Tree w - weight

5. 6. 7. 8. 9. 10.

Sort the edges of E into non-decreasing order by weight w for each edge (u, v) E, taken in non-decreasing order by weight w do if Find - Set(u) Find - Set(v) then A A U { (u, v) } Union (u, v) return A

# 1

Explanation Kruskals algorithm is being performed on graph (G). Initially MST (T) (in here denoted as A) is empty set.

2 3

Loop through all vertices (v) in graph (G) (Within the loop)Consider each vertex as separate component and make a set to hold those nodes separately. (Create |V| trees (one tree contains each node)) (Goonetillake, J. 2011).

(Break the rules of Kruskals algorithm according to given problem). First of all add the A and B directly in to a MST.

5 6 7

Sort the edges in ascending order according to their weight. Again run a loop to take the edges in the sorted order (Dasgupta, S., et al. 2006). Check whether the two endpoint vertices of each edge that selected from previous state, belong to 11

the same tree. 8 9 10 If those two vertices are safe to connect (if they are not belong to same tree) added to A. Then merged those two vertices. (two different trees MST) Finally return the MST.

5.1.3 Time complexity

According to (Cormen, T.H.et al.2002: P.345). 1. Initialize empty set of A = O (1)

2. First for loop (making Sets of vertices ( |V| )) = O ((V+ E) (V)) 3. Add A and B in to MST 4. Sort E 5. Second for loop (find Sets and union operations on the disjoint-set forest) = O (1) = O (E lg E) = O (E) since (G) is connected, |E| >= |V| - 1 (in this case E= 15 , V= 14) O (E (V)) Running time of the algorithm = O (1) + O ((V+ E) (V)) + O (1) + O (E lg E) + O (E (V)) = (|V|) = O (lg V) = O (lg E) 12

= O (E lg E) = |E| <= |V|^2 lg |E| = O (2 lg V) = O (lg V) = O (E lg V)

5.1.4 Derived solution


5.1.4.1 Mathematical evaluation

According to (Goonetillake, J. 2011) Input: - An undirected connected graph (G) with n (=15) vertices and m (= 23) edges with Weight w (Dasgupta, S., et al. 2006: P.144). Output: - A minimum spanning tree (T) for (G) (Dasgupta, S., et al. 2006: P.144). Initially A = {} A = Forest

Looping - Sets {A} {B} {C} {D} {E} {F} {G} {H} {I} {J} {K} {L} {M} {N} {O} Because of the key constrains of this problem, A and B required to connect directly. Therefore add (A, B) to A A= {(A, B)} Combine Set (A) & Set (B) Sets {A, B} {C} {D} {E} {F} {G} {H} {I} {J} {K} {L} {M} {N} {O} Union

13

E Sorted in ascending order, E V 2 (N,M) 2 (L,K) 2 (D,C) 2 (C,E) 3 (K,G) 4 (L,A) 4 (E,F) 4 (M,D) 4 (B,G) 5 (C,B) 5 (F,H)

5 (N,O)

6 (M,A)

6 (G,I)

7 ( I, J )

7 (B,F)

8 (K,A)

10

11 (O,M)

12 (H,J)

13 (F,I)

20 (A,B)

(A,D) (O,K)

Step 1Take (N, M); Find- Set (N) Find-Set (M) add (N, M) to A A= {(A, B), (N, M)} Combine Set (N) & Set (M) Sets {A, B} {C} {D} {E} {F} {G} {H} {I} {J} {K} {L} {(M, N)} {O} Step 2 Take (L, K); Find- Set (L) Find-Set (K) add (L, K) to A A= {(A, B), (N, M), (L, K)} Combine Set (L) & Set (K) Sets {A, B} {C} {D} {E} {F} {G} {H} {I} {J} {K, L} {M, N} {O} Step 3 add (D, C) A= {(A,B),(N,M),(L,K),(D,C)} {A,B} {C,D} {E} {F} {G} {H} {I} {J} {K, L} {M, N} {O} Step 4 add (C, E) A= {(A,B),(N,M), (L,K), (D,C), (C, E)} {A, B} {C, D, E} {F} {G} {H} {I}{J} {K, L} {M, N}{O} 14

Step 5 add (K, G)

A= {(A,B),(N,M),(L,K),(D,C),(C,E),(K,G)} {A, B} {C, D, E} {F} {H} {I} {J} {G, K, L} {M, N} {O}

Step 6 add (L, A) A= {(A,B),(N,M),(L,K),(D,C),(C,E),(K,G),(L,A)} {A, B, G, K, L} {C, D, E} {F} {H} {I} {J} {M, N} {O} Step 7 add (E, F) A= {(A,B),(N,M),(L,K),(D,C),(C,E),(K,G),(L,A),(E,F)} {A,B,G, K,L} {C, D, E, F} {H} {I} {J} {M, N} {O} Step 8 add (M, D) A= {(A,B),(N,M), (L,K), (D,C), (C,E), (K,G), (L,A), (E,F), (M,D)} {A, B, G, K, L} {C, D, E, F, M, N} {H} {I} {J} {O} Step 9 (B, G) ignored. Because it creates a cycle and also it connects two vertices that already in the same component. {A, B, G, K, L} Step 10 add (C, B) A={(A,B),(N,M),(L,K),(D,C),(C,E),(K,G),(L,A),(E,F),(M,D),(C, B)} {A, B, G, K, L, C, D, E, F, M, N} {H} {I} {J} {O} Step 11 add (F, H) A={(N,M),(L,K),(D,C),(C,E),(K,G),(L,A),(E,F),(M,D),(C, B),(F,H)} {A, B, G, K, L, C, D, E, F, M, N, H} {I} {J} {O} Step12add(N,O) A={(N,M),(L,K),(D,C),(C,E),(K,G),(L,A),(E,F), (M,D),(C,B), (N, O) {A, B, G, K, L, C, D, E, F, M, N, H, O} {I} {J} Step13 (M, A) ignored. Connect two vertices that already in the same component. {A, B, G, K, L, C, D, E, F, M, N, H, O} Step14 add(G, I) A={(N,M),(L,K),(D,C),(C,E),(K,G),(L,A),(E,F), (M,D),(C,B), (G,I) {A, B, G, K, L, C, D, E, F, M, N, H, O, I} {J} Step15 add(I, J) A={(N,M),(L,K),(D,C),(C,E),(K,G),(L,A),(E,F), (M,D),(C,B), (I,J) {A, B, G, K, L, C, D, E, F, M, N, H, O, I, J} 15

Step16 - 23 ignore all. Already merge all the nodes to MST {A, B, G, K, L, C, D, E, F, M, N, H, O, I, J}

Therefore MST is = 2 + 2 + 2 + 2 + 3 + 4 + 4+ 4+ 5 + 5+ 5 +6 +7+ 20 = 71

5.1.4.2 Graph representation

4 2

D
2 2

N
5

C
5 2 0

O
4

L
2 7

16

5.2 Prims Algorithm


5.2.1 Algorithm Execution

A popular alternative to Kruskals algorithm is Prims (Dasgupta, S., et al. 2006). Unlike Kruskals algorithm, Prims algorithm considers the edges from a single tree for the Set A. Also require starting the tree from an arbitrary root vertex and grows until the tree spans all the vertices in graph (G) (Goonetillake, J. 2011). In this process must require considering about the edges that satisfy both features of less weight and safe when added to tree (A) to ensure the outputs edges in A form a MST at the algorithm termination (Cormen, T.H.et al.2001).

At the beginning require to input the connected graph (G) and the root (r) of the MST to perform on the graph (G). Then set the key of each vertex to except for the selected root (r) and it is set to zero (Goonetillake, J. 2011). Set the parent of each vertex to NIL. Initialize the min-priority queue (Q) to contain all vertices of the corresponding tree (Cormen, T.H.et al.2001). Minimum weight of any edge that connecting considered vertex (v) to a vertex in the tree is Key [v] and define key [v] in to if there is no such edge (Cormen, T.H.et al.2001: P.485). Then define parent of (v) in tree as [v]. In first step check the distance from root node to its adjacent vertices and update the both key and parent values. Due to key constrains of the given problem required to select node (A) as rot node and then need to connect with (B) directly. After putting root node to the (A), check for the next minimum key value. Then again 17

remove the selected vertex from the (Q), and update its adjacent vertices values and put it in to (A). Likewise remove the most eligible vertex at a time that satisfy all the conditions (minimum weight and in the same tree) from the min-priority queue (Q) and adds to A (Goonetillake, J. 2011). Then update the key and values to adjacent vertexs corresponding values (update only in vertex value, not in the tree). This will be doing until min-priority queue (Q) become empty (Cormen, T.H.et al.2001).

5.2.2 Pseudo code

According to (Goonetillake, J. 2011). MST-PRIM (G, w, r) 1 2 3 4 5 6 7 8 9 10 11 12 13 key [r] 0 Q V [G] Insert operation while Q do u EXTRACT-MIN (Q) extract min operation else for each v Adj [u] do if v Q and w (u, v) < key [v] decrease key 18 do if (u==A) (edited according to the given requirements) then [B] A then key [B] w (A, B) for each u V [G] do key [u] [u] NIL

14 15

then [v] u key [v] w (u, v)

Explanation Prims algorithm is being performed on graph (G). Input root node as (r).

1 2 3 4 5

Loop through all vertices (v) in graph (G). Set the key value of each vertex to . Set the parent of each vertex to NIL (Cormen, T.H.et al.2001: P.485). Set the key value of root node to zero. Assume that begin with node A. Initialize the min-priority queue (Q) to contain all vertices of the corresponding graph (G) (Cormen, T.H.et al.2001).

6 7 8

Run a loop until min-priority queue (Q) becomes empty (Cormen, T.H.et al.2001: P.485). Extract a minimum node from min-priority queue (Q). (In first step it must be the root node). (Break the rules of Prims algorithm according to given problem). Check whether the extracted node is equal to node A. (root node is the minimum value in first step).

9 10 11 12 13 13

If it is A then change the parent value of B as A due to connect both nodes directly. Then change the key value of B as weight of the edge of (A, B). (To get the B as next node). If the extract node is not equal to A, then perform below processes. Consider the all adjacency vertices of above selected node. Those are must not A or B (otherwise again change the key values and parent values). Check whether that their weight of the edge is less than for the key value. (V-Va < key [v]) : V-Va (edge value from the consider node to the tree (A).) V-Va = w (u, v)

14

If above condition is true, update the parent value to latest root node (above considered node) (Cormen, T.H.et al.2001: P.485).

15

Update the key value to weight of the edge (Cormen, T.H.et al.2001: P.485). 19

5.2.3 Time complexity

According to (Goonetillake, J. 2011) the performance of Prims algorithm depends on how we implement the min-priority queue. For this execution used binary heap implementation. According to (Cormen, T.H.et al.2002: P.347). Suppose (Q) is a binary heap. Initializing (Q) and first for loop Decrease key of r Line 8 11 While loop = O (V lg V) = O (lg V) = O (1) = |V| EXTRACT-MIN (Q) calls O (V lg V) |E| DECREASE-KEY calls O (E lg V) Total time = O (E lg V)

20

5.2.4 Derived solution


5.2.4.1 Mathematical evaluation

According to (Cormen, T.H.et al.2002: P.346). The edges of (A) will form a rooted tree with root (r). Node Vs parent in the tree denoted by [V], if V= r or has no parent [V] = NIL Each vertex in graph (G) require to include in tree (in here (A)) denoted as Va Key [V] = minimum weight of any edge (U, V), U Va In min-priority queue (Q), each vertex hold as = V Va Return V(as min) such that there exist U Va, (U, V) (both are in same tree) , V-Va = min Key [V] = if V is not adjacent to any vertices in Va During the algorithm Set of A can be denote as A= {(V, [V]) : V Va - {r} - Q} When the algorithm terminates, the min-priority queue (Q) become empty. Therefore above can be simplified as A= {(V, [V]) : V Va {r}}

21

Set S {} {} A AB , ABG , , ABGK , , , A B G K, L , , , A B G K, L C , , , , A B G K, L C, D , , , , A B G K, L C, D E , , , , , A B G K, L C, D E F , , , , , , A B G K, L C, D E F, M , , , , , , A B G K, L, C, D E F, M N , , , , , , A B G K, L C, D E F, M N, H , , , , , , , A B G K, L C, D E F, M N, H O , , , , , , , , A B G K, L C, D E F, M N, H O, I , , , , , , , , A B G K, L C, D E F, M N, H O, I, J , , , , , , , ,

A (NIL , ) (NIL 0) , -

B (NIL , ) (NIL , ) A(B 20) , -

C (NIL , ) (NIL , ) (NIL , ) (5, B ) (5, B ) (5, B ) (5, B ) -

D (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (2, C) -

E (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (2, C) (2, C) -

F (NIL , ) (NIL , ) (NIL , ) (7, B ) (7, B ) (7, B ) (7, B ) (7, B ) (7, B ) (4, E ) -

G (NIL , ) (NIL , ) (NIL , ) (4, B ) -

H (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (5, F) (5, F) (5, F) -

I (NIL , ) (NIL , ) (NIL , ) (NIL , ) (6, G ) (6, G ) (6, G ) (6, G ) (6, G ) (6, G ) (6, G ) (6, G ) (6, G ) (6, G ) (6, G ) -

J (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (12, H ) (12, H ) (7, I) -

K (NIL , ) (NIL , ) (NIL , ) (NIL , ) (3, G ) -

L (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (2, K) -

M (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (4, D ) (4, D ) (4, D ) -

N (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (2, M ) -

O (NIL , ) (NIL , ) (NIL , ) (NIL , ) (NIL , ) (10, K) (10, K) (10, K) (10, K) (10, K) (10, K) (10, K) (5, N) (5, N) -

(Dasgupta, S., et al. 2006: P. 152).

Therefore MST is = 2 + 2 + 2 + 2 + 3 + 4 + 4+ 4+ 5 + 5+ 5 +6 +7+ 20

= 71

5.2.4.2 Graph representation


4 2

D
2 2

N
5

C
5 2 0

O
4

L
2 7

22

5.3 Dijkstras Algorithm


5.3.1 Algorithm Execution

It is a more general adaptation of Breadth First Search algorithm. In BFS finds the shortest paths in any given graph (G) (Dasgupta, S., et al. 2006: P.119). Dijkstras algorithm solves the same problem for the cases of weighted graph (G) which all edge weights are nonnegative (Cormen, T.H.et al.2001: P.504). For given scenario will focus on single source shortest paths problem. It means find a shortest path from a given source vertex to each vertex of the graph (G) (Goonetillake, J. 2011). Especially this algorithm has two main shortest path properties (that directly involve with given requirements). According to (Goonetillake, J. 2011) those are, A sub path of a shortest path is a shortest path of itself (each and every city need to be originating from capital cities with the shortest distance). There is a tree of shortest paths from a start vertex to all other vertices.

Require to decide a node (V) as a starting point. Initialize the min-priority queue (Q) to contain all vertices of graph (G). Define a label D [u] for each vertex to store approximate distance from sources node (V). Therefore always the meaning of D [u] is that hold the length of the best path from source node that have found so far (Goodrich M.T. et al. 2006: P.843). At the beginning require to define source nodes D [v] as zero and for all other vertices D [u] as and predecessor value [v] as NIL. Then algorithm will select the vertex with the shortest path from the min-priority queue (Q) and put it in to a new separate set (S) that holds a shortest paths(Cormen, T.H.et al.2001: P.504). Due to key requirements of the given problem need to add node A and B directly. Therefore need to provide ability of begin with both A or B in the implementation of the algorithm. Then algorithm will use the technique call relaxation to relax the nodes (Goonetillake, J. 2011). Once a new vertex is adds to a (S) required to update key values (if need predecessor value also) of each vertex that adjacent to that vertex. This update operation is known as relaxation (Goodrich M.T. et al. 2006: P.843). This process will be doing until min-priority queue (Q) is become empty.

23

5.3.2 Pseudo code

According to (Goonetillake, J. 2011). Dijkstra(G, w, s) 1. Initialise-Single-Source(G, s) for each vertex v V[G] do D[v] [v] NIL D [s] 0 2. S {} 3. Q V[G] Insert operation (Cormen, T.H.et al.2001: P.504, P. 507). 4. while Q {} 5. 6. 7. 8. 9. 10. 11. 12. do u = extract-min(Q) extract min operation (Cormen, T.H.et al.2001:P.507). S S U {u} Do if (u == A) (edited according to requirements of the problem) D [B] D [A] +w (A, B) [B] A S S U {B}

for each vertex v Adj[u] do relax(u, v, w) Relax (u, v, w) decrease key operation if D[v] > D[u] + w(u, v) then D[v] D[u] + w(u, v) 24

[v] u

Explanation Dijkstras algorithm is being performed on graph (G). Input undirected graph (G), positive edge lengths and source node (s).

1.1 Initialize source node and loop through all vertices (v) in graph (G) 1.2 Set the key value of each vertex to . 1.3 Set the predecessor of each vertex to NIL. 1.4 Set the key value of source node to zero. 2 3 4 5 6 7 8 Initialize set (S) to empty set S; hold vertices whose final shortest path weights are estimated. Initialize the min-priority queue (Q) to contain all vertices of the corresponding graph (G). Run a loop until min-priority queue (Q) becomes empty. Extract a minimum node from min-priority queue (Q). (In first step it must be the source node). Add above selected node to set (S) by removing it from (Q). If added node is A Then change the key value of B as the total value of node As key value and the weight of the (A, B) edge value. 9 10 7 Then change the predecessor value of B as A Add the node B to set (S) by removing it from (Q). Consider the all adjacency vertices of above selected node.

8.1 Check each edge with its weight to perform relaxation (Goonetillake, J. 2011). 8.2 Check whether that their key value is less or greater than to the accumulate value of latest source nodes key value and the considered nodes edge value. D[v] D[u] + w(u, v) or D[v] > D[u] + w(u, v)

8.3 If D[v] > D[u] + w (u, v), update the key value as the accumulate value of latest source nodes key value and the considered 25

nodes edge value. D[v] D[u] + w(u, v) 8.4 If above is true, then update the predecessor value as latest source node [v] u

5.3.3 Time complexity

Running time of Dijkstras algorithm is depends on the type of the min-priority queue (Goonetillake, J. 2011). Therefore this algorithm will use the binary heap as the min-priority queue. In the algorithm it uses three main priority queue operations to maintain the queue. Those are, Insert Extract min Decrease key

According to (Goonetillake, J. 2011), Binary min heap Building min binary heap Line 7 10 Decrease key Extract min Total time = O (V) = O (1) = O (lg V) E = O (lg V) V = O ((V + E) lg V) = O (E lg V)

26

5.3.4 Derived solution


5.3.4.1 Graph representation

Considering about the requirements of the given problem require providing two graphs (requirement is to provide the shortest distance from each capital city to each and every city). Since the single source shortest paths mechanism provides the shortest path from one source node to every other vertex, require representing one graph with starting from capital city A and other for starting with capital city B.

Visited {} A B (A, 20) L (A, 4) K (L,6) M (A, 6) N (M,8) D (A, 9) G (K , 9) C (D, 11) O (N , 13) E (C , 13) I (G, 15) F (E, 17) H (F, 22) J (I, 22)

A B C D E F G H I J K L M N O (N IL, 0) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) B (A, 20)(N IL, ) D (A, 9) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) K (A, 8) L (A, 4) M (A, 6) (N IL, ) (N IL, ) (N IL, ) D (A, 9) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) K (A, 8) L (A, 4) M (A, 6) (N IL, ) (N IL, ) (N IL, ) D (A, 9) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) (N IL, ) K (L,6) M (A, 6) (N IL, ) (N IL, ) (N IL, ) D (A, 9) (N IL, ) (N IL, ) G (K , 9) (N IL, ) (N IL, ) (N IL, ) M (A, 6) (N IL, )O (K , 16) (N IL, ) D (A, 9) (N IL, ) (N IL, ) G (K , 9) (N IL, ) (N IL, ) (N IL, ) N (M,8)O (K , 16) (N IL, ) D (A, 9) (N IL, ) (N IL, ) G (K , 9) (N IL, ) (N IL, ) (N IL, ) O (N , 13) C (D, 11) (N IL, ) (N IL, ) G (K , 9) (N IL, ) (N IL, ) (N IL, ) O (N , 13) C (D, 11) (N IL, ) (N IL, ) (N IL, ) I (G, 15) (N IL, ) O (N , 13) E (C, 13)(N IL, ) (N IL, ) I (G, 15) (N IL, ) O (N , 13) E (C, 13)(N IL, ) (N IL, ) I (G, 15) (N IL, ) F (E, 17) (N IL, ) I (G, 15) (N IL, ) F (E, 17) (N IL, ) J (I, 22) H (F, 22) J (I, 22) J (I, 22) -

27

9 8 6

13

E
17 22

M C A
0 11

F B
20

J
22

O
13

L K
6

G
9

I
15

V isite d {} B A (B , 2 0 ) G (B ,4 ) C (B , 5 ) D (C , 7 ) E (C , 7 ) F (B , 7 ) K (G , 7 ) L (K , 9 ) I (G , 1 0 ) M (D , 1 1 ) H (F , 1 2 ) N (M , 1 3 ) J (I, 1 7 ) O (K , 1 7 )

B A C D E F G H I J K L M (N IL , 0()N IL , (N IL , (N IL , (N IL , (N IL , (N IL , (N IL , (N IL , (N IL , (N IL , (N IL , ( N IL , ) ) ) ) ) ) ) ) ) ) ) - A (B , 2 0C (B , 5 () N IL , (N IL , F (B , 7 )G (B ,4 ( N IL , (N IL , (N IL , (N IL , (N IL , ( N IL , ) ) ) ) ) ) ) ) ) C (B , 5 () N IL , (N IL , F (B , 7 )G (B ,4 ( N IL , (N IL , (N IL , (N IL , (N IL , ( N IL , ) ) ) ) ) ) ) ) C (B , 5 () N IL , (N IL , F (B , 7 ) ) ) (N IL , I) (G , 1 0(N IL , K (G , 7(N IL , ( N IL , ) ) ) ) D (C , 7 ) (C , 7 )F (B , 7 ) E (N IL , I) (G , 1 0(N IL , K (G , 7(N IL , ( N IL , ) ) ) ) E (C , 7 )F (B , 7 ) (N IL , I) (G , 1 0(N IL , K (G , 7(N IL , M (D , ) ) ) ) F (B , 7 ) (N IL , I) (G , 1 0(N IL , K (G , 7(N IL , M (D , ) ) ) ) - H (F , 1 2I )(G , 1 0(N IL , K (G , 7(N IL , M (D , ) ) ) ) - H (F , 1 2I )(G , 1 0(N IL , ) ) L (K , 9 M (D , ) - H (F , 1 2I )(G , 1 0(N IL , ) ) M (D , - H (F , 1 2 ) J (I, 1 7 ) M (D , - H (F , 1 2 ) J (I, 1 7 ) M (D , J (I, 1 7 ) M (D , J (I, 1 7 ) M (D , M (D , M (D ,

N O ()N IL , (N IL , ) ()N IL , (N IL , ) ()N IL , (N IL , ) ()N IL , (N IL , ) ()N IL , (N IL , ) 1 1 ) IL , (N IL , (N ) 1 1 ) IL , (N IL , (N ) 1 1 ) IL , (N IL , (N ) 1 1 ) IL , O) (K , (N 1 1 ) IL , O) (K , (N 1 1 ) IL , O) (K , (N 1 N) (M , 1O ) (K , 1 3 1 N) (M , 1O ) (K , 1 3 1 1 ) - O (K , 1 1 ) - O (K , 11) -

) ) ) ) ) ) ) ) 17) 17) 17) 17) 17) 17) 17)

28

7 13 11

E
7 12

M C A
20 5

F B
0

J
17

O
17

L K
7

G
4

I
10

5.3.4.2 Mathematical evaluation

This algorithm is consider the edges which weights are non-negative w (u, v) 0,(u, v) E Focus on single source shortest paths problem, means in given graph ((G) = (V, E)) find a shortest path prom a given source vertex (A), A V to each vertex (v), v V (Goonetillake, J. 2011). The weight of path P = <A, B,O> sum of the weights of its component edges (Goonetillake, J. 2011). V = {A, B, C, D, E, F, G, H, I, J, K, L, M, N, O}; Q V E ={ <N,O>,<N,M>,<O,M>,<M,D>, <M,A>,<A,D>,<O,K>,<L,K>, <A,B>,<D,C>,<C,B>, <K,A>,<E,F>,<B,G>,<K,G>,<G,I>,<I,F>,<C,E>,<B,F>,<I,J>,<H,J>,<F,H>} 29

Have two sets of vertices, Q = priority queue = V-S hold all the vertices of graph (G). S = hold vertices whose final shortest path weights are estimated (Cormen, T.H.et al.2002: P.362). To maintain S, algorithm provide two values, D[v] keys to hold shortest path weights (that found so far). [v] (predecessor) to hold adjacent shortest path key node.

Initially Q V [G], S = {}; if source is A D [A] = 0, [A] = NIL and for other vertices D[v] = , [v] = NIL Add A to S S = {A} remove A from (Q) Q A (first step) (From next step) check for current key value with latest key value + weight of its edge value check for D [B] > D [A] + w (A, B) or D [B] < D [A] + w (A, B) If, D [B] D [A] + w (A, B) D [B] is unchanged by relaxation or

D [B] > D [A] + w (A, B) D [B] is changed by relaxation D [B] D [A] + w (A, B) (this updating technique is called as relaxation) [B] A

Then select the minimum vertex among all the vertices and add to (S) by removing from (Q) If B is the minimum add B to S S = {A, B} remove B from (Q) Q {A, B} (this will be doing until Q= ) (Goonetillake, J. 2011). Therefore at the termination of the algorithm Q= , S = V

30

5.4 Brute Force Algorithm


5.4.1 Algorithm Execution

This is a straightforward approach and directly based on the problem nature. Unlike other algorithms this will be consider all the possibilities of solutions to the given problem and chose the best out of it (Goodrich M.T. et al. 2006: P.748). But according to (Goodrich M.T. et al. 2006) a complete graph can have V ^ (v-2) spanning trees. Among those one should be the minimum spanning tree. When this equation apply to given problem, can state that it will produce (15)^(15- 2) (15) ^ 13 number of spanning trees. Therefore cause to reduce the efficiency of the algorithm due to produce large number of outputs. (Therefore this is not a good approach to find MST for given problem).

31

The main goal of this algorithm is to finding all the possibilities and chose the best solution. Therefore this can be used to find all the spanning trees which starting from each and every node and visiting to other nodes in the given graph (G) and select the MST.

In this algorithm require to maintain two main sets to hold the spanning trees (SP) and hold the edges of MST (MST) so far created. Then require checking the number of possibilities to iterate the process. In generally if graph (G) with (n) nodes has n^(n-2) spanning trees. Then require initializing a tree (T) for each possibility. (Because of the key requirements of given problem, required to initialize edge of (A, B) to spanning tree (A) at the beginning). Then modeling a collection of disjoint sets of edges of a particular component. Initially each edge is a component of itself. Then extract a random set of edge and check with the tree set (A) to verify it is already in there or not. When considering the number of possibilities and must require to consider about the worst case scenario of those possibilities, it is hard to perform sorting processes. Therefore in this algorithm will be considering random selection of edges. After merging all the separate sets with tree (A) then it will check with the set of spanning tree (SP) to verify it is already there or not. Complex situation of repeating the same combination of spanning trees can be avoiding here. After checking all the possibilities of spanning trees check for the minimum value. Then move all the edges of that minimum spanning tree to set of (MST).

5.4.2 Pseudo code

MST Brute Force (G, w) SP 0 MST 0 W0 Sum 0 While SP n ^ (n-2)

A Tree

w weight

SP- spanning threes

32

A0 A A U { (A, B) } (edited according to the given requirement) for each vertex v V[G] do Make - Set(e) Q E [G] for each edges e Q do (v, u) EXTRACT-RANDOM EDGE do if Find - Set(u) Find - Set(v) then A A U { (u, v) } sum sum + w (u, v)

w sum if set (A) set (SP) SP SP U { A } for each A a SP sum 0 return MST

33

6.0 Complexity classes


Almost all the algorithms including the algorithms that have discussed so far are polynomial time algorithms. It means those are depending on the size (n) of the inputs (Cormen, T.H.et al.2001: P.840). Therefore in most of the time the worst case running time is O (n for some ); constant k. But in reality there are problems that can be solve with different running time and also cannot be solve by any computer at all (Cormen, T.H.et al.2001: P.840).

According to (Cormen, T.H.et al.2001: P.841) there are three fundamental classes of problems. They are P, NP and NPC (NP-Complete). The problems of class P can be solvable in polynomial time. Problems can be verifiable (can be given a certificate of a solution) in polynomial time in class NP. But when it comes to NPC it is difficult to say that it solves the problems in polynomial time due to their hardness (Dasgupta, S., et al. 2006: P.259, P.260). Therefore we can summarize the flow of the difficulty and efficiency of complexity classes. P NP and P NPC therefore

NP-Complete NP P (increase) direction of efficiency NP-Complete NP P (increase) direction of difficulty (Dasgupta, S., et al. 2006).

34

According to above flow can state that any problem in P is approximately in NP (Cormen, T.H.et al.2001: P.842).

6.1 Justification of chosen complexity class

According to (Cormen, T.H.et al.2001: P.841) class P problems can be solve in time O (n) with the input of size (n). For given problem that found the MST by providing undirected graph (G) and distance as the inputs and asked to find a tree with minimum value. In above chapters of this assignment solve this problem using different algorithms with providing the same complexity time in Big-O notation (E lg V). Therefore this problem will satisfy the both conditions of depending on the input (depending on the graph (G), number of vertices (V) and number of edges (E)) and providing the solution in polynomial time (Dasgupta, S., et al. 2006: P.259, P.251).

Among those algorithms Kruskals algorithm is more efficient due to its approach of how it solves the given problem. It not uses additional computation that used in other algorithms such as relaxation, queue processes. Therefore it is more efficient than other algorithms

6.2 Definition of complexity class P

According to (Dasgupta, S., et al. 2006) P is the set of problems (decision problems) that can be solved in polynomial time (in other words quickly).

6.3 Example for complexity class P

35

In reality there are many problems that lay under the class P. According to (Dasgupta, S., et al. 2006: P.257) some common problems such as creating an electronic circuit, creating a network, linear programming are under this class category. In here discuss about the sorting algorithms as an example to class P because of it is most fundamental problem in the study of algorithms (Cormen, T.H.et al.2001: P.110). There are few commonly used sorting algorithms. Those are merge sort, quick sort and insertion sort. According to (Cormen, T.H.et al.2001: P.111) following are the time complexities of those. Merge sort = O (n lg n) Quick sort = O (n^2)

Insertion sort = O (n^2) When considering above time complexities could say those sorting problems are belong to class P. because all above sorting methods are depend on the input size (n) and can solve in polynomial time. Since those are satisfied the class Ps requirements.

36

APPENDIX
References

Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C. (2001). Introduction to Algorithms. 2nd ed. USA: MIT press. Cormen, T.H., Lee, C.,Lin, E. (2002). Instructors Manual: Introduction to Algorithms. 2nd ed. USA: MIT press. Dasgupta, S., Papadimitriou, C.H., Vazirani, U.V. (2006). Algorithms. [Online] Available from http://www.cs.berkeley.edu/~vazirani/algorithms/all.pdf. [Accessed: 21/01/2012] Goodrich, M.T., Tamassia, R. (2006). Data Structures and Algorithms in Java. 4th ed. USA: John Wiley & Sons. Goonetillake, J. (2011). Minimum Spanning Trees, SCS 1006. [Lecture notes] Introduction to Data Structures & Algorithms. Colombo University Sri Lanka, UCSC, lec hall 3, 20 June. Goonetillake, J. (2011). Greedy Algorithms, SCS 1006. [Lecture notes] Advanced Data Structures and Algorithms (ADSA). Colombo University Sri Lanka, UCSC, lec hall 3, 23 June. Goonetillake, J. (2011). Graph Algorithms, SCS 1006. [Lecture notes] Advanced Data Structures and Algorithms (ADSA). Colombo University Sri Lanka, UCSC, lec hall 3, 25 June. 37

Johnsonbaugh, R., Schaefer, M. (2004). Algorithms. India: Pearson Education. Levitin, A. (2007). Introduction to the Design &Analysis of Algorithms.2nd ed.Pearson, Addison Wiesley. Skiena, S.S., Revilla, M.A. (2003). Programming Challenges: The Programming Contest Training Manual. New York: Springer

38

Vous aimerez peut-être aussi