Sei sulla pagina 1di 70

INDIVIDUAL ASSIGNMENT

INDIVIDUAL ASSIGNMENT ALGORITHMICS (CE00333-3)

HAND OUT DATE: 22nd August 2012 HAND IN DATE: 23rd October 2012

STUDENT NAME
DURGESH KUMAR (PT0981128)

MODULE LECTURER
Ms. Kriti Arora

INDIVIDUAL ASSIGNMENT ALGO

Page 1

INDIVIDUAL ASSIGNMENT

CERTIFICATE

This is to certify that Durgesh Kumar (pt0981128) has successfully prepared his assignment and he had putted his utmost effort for the successful completion of this assignment within the given time. Also he has covered all the areas of the project specification in an appropriate manner.

I must congratulate him for his sincere efforts and hard work. I must appreciate him for his dedication towards this assignment.

SUBMITTED BY Durgesh Kumar (pt0981128)

SUBMITTED TO Ms. Kriti Arora (Module Lecturer)

INDIVIDUAL ASSIGNMENT ALGO

Page 2

INDIVIDUAL ASSIGNMENT

ACKNOWLEDGEMENT

On completion of this project, I would like to take this opportunity to very earnestly thank my ALGORITHMICS professor; Ms. Kirti Arora without her constant guidance and support, this assignment would have not been possible. I am highly indebted to her for sharing me her valuable time and ideas. Next, I would like to thank my college for providing me with the very best of facilities in the form of library, computer labs, books, etc which formed the back-bone of this assignment. Lastly, I would like to thank all my peers and friends who were a constant help during the entire research work and helped me improve this assignment.

PREPARED BY Durgesh Kumar

INDIVIDUAL ASSIGNMENT ALGO

Page 3

INDIVIDUAL ASSIGNMENT
Table of Contents
Introduction ...................................................................................................................................... 5 First Graph theory Problem: .......................................................................................................... 5 Second Graph theory Problems: .................................................................................................... 6 Description of the approaches for Problem Solving............................................................................ 7 First Graph Problem .......................................................................................................................... 9 Derived solutions (Adjacency List and Matrix) and their analysis........................................................ 9 Adjacency List:- ............................................................................................................................. 9 Adjacency Matrix:- ...................................................................................................................... 10 Analysis of both solutions (Adjacency list & Adjacency Matrix) ................................................... 11 Breadth First Search ...................................................................................................................... 12 Assumption for BFS ................................................................................................................ 12 Algorithms/Pseudo-codes of BFS ................................................................................................. 13 Execution steps for Breadth First Search ............................................................................. 14 Analysis of breadth first search (BFS) ...................................................................................... 20 Depth First Search ......................................................................................................................... 21 Assumption of DFS .................................................................................................................. 21 Algorithms/Pseudo-codes of Depth First Search .......................................................................... 22 Analysis of Depth First Search .................................................................................................. 34 Dijkstras Algorithm....................................................................................................................... 35 Algorithm/ Pseudo-codes of Dijkstras Algorithm ......................................................................... 36 Assumption of Dijkstras ......................................................................................................... 36 Use of Dijkstras algorithm to solve the problem with description ................................................ 37 Second Graph Problem .................................................................................................................... 45 Assumption of Kruskals ......................................................................................................... 46 Algorithm/ Pseudo-codes of Kruskals Algorithm ......................................................................... 47 Execution steps for Kurskals Algorithm .............................................................................. 48 Algorithm/ Pseudo-codes of Prims Algorithm ............................................................................. 56 Assumption of Prims .............................................................................................................. 56 Step wise solution of Prims Algorithm .................................................................................... 57 Problem Classes .............................................................................................................................. 67 Description and Justification of chosen class.................................................................................... 67 Comparison of Time complexities of algorithms with their analysis ................................................. 68 REFRENCES .................................................................................................................................. 70 INDIVIDUAL ASSIGNMENT ALGO Page 4

INDIVIDUAL ASSIGNMENT

Introduction
An algorithm is a specific set of instructions for carrying out a procedure or solving a problem, usually with the requirement that the procedure terminate at some point. Specific algorithms sometimes also go by the name method, procedure, or technique. (Weisstein, 2012) The process of applying an algorithm to an input to obtain an output is called a computation. Graph theory is the study of points and lines. Using graph theory, engineers develop chips with maximum component density and minimum total interconnecting conductor length. This is important for optimizing processing speed and electrical efficiency. (Rouse, 2005) A graph is can be represented by G = (V, E) where V is a set of vertices and E is a binary relation on V. E contains a pair (u, v) if there is an edge between the u and v vertices. Vertices are simple objects which can have names and other properties; an edge is a, connection between two vertices. The graphs can be of two types depending upon direction i. Directed Graph: If the graph is directed, this pair is ordered: (u, v) is not the same as (v, u). Directed means those graph in which direction matters a lot. Direction is shown in graph generally using arrows. ii. Undirected Graph: If the graph is undirected, this pair is ordered: (u, v) is same as (v, u). Undirected means those graphs in which direction doesnt matters.

First Graph theory Problem:


Below is the listing of some of the problems for the first graph which we had to find out the solutions. There is a network of interconnected undirected graph and directed graph connecting 10 cities via 18 roads as shown in fig below. Find whether there is a path between source to destination node Searching for the result such that it could be effective in terms of time complexity and space complexity Find the shortest path between source to destination How the traversed node are stored in computer memory

INDIVIDUAL ASSIGNMENT ALGO

Page 5

INDIVIDUAL ASSIGNMENT

Figure 1Graph Theory Problem (for BFS, DFS & Dijkstras SSSP)

Second Graph theory Problems:


Below is the listing of some of the problems for the first graph which we had to find out the solutions.

Figure 2 Graph Theory Problem (MST)

The nodes in the above diagram represent the various oil stations scattered across a country, while the edges represent the oil pipelines running between them. The weights of each edge represent the distance between each point.

INDIVIDUAL ASSIGNMENT ALGO

Page 6

INDIVIDUAL ASSIGNMENT
We have to provide a solution for the GRAPH THEORY problem mentioned above by solving it step-wise to determine a Minimum Spanning Tree (MST) with Prims and Kruskals algorithmic approaches We have to compare the efficiencies of both in terms of Big-Oh notation with their calculated execution times. Include algorithm/pseudo-code with our self explanations. Determine the complexity class, for the both Graph Theory Problems. Provide the definition for their complexity class and also give justifications as to why the problems belong to respective classes.

Description of the approaches for Problem Solving


Various data structures are used to represent graphs in computer memory such as adjacency list and adjacency matrix. Different algorithms are applied on these graphs like Prims algorithm, Dijkstras algorithm, Kruskals algorithm etc. to solve the real world problems. The graphs can be of different types for example weighted or un-weighted, complete graph, Directed or Undirected graph. I have to execute different algorithms to achieve goal. According to the requirement, I have to analyze and find optimal solution using Breadth first search (BFS), Depth first search (DFS), Prims, Kruskals, and Dijkstras algorithms using both adjacency list and adjacency matrix. Adjacency list: Adjacency list are used to store the graph in a computer memory Adjacency list are used to store Sparse graph Sparse graph is a graph in which the number of edges is less

Adjacency Matrix Adjacency matrix are also used to store the graph in a computer memory Adjacency matrix are used to store dense graph Dense graph is a graph in which the number of edges is close to the maximal number of edges Graph Traversal Graph traversal refers to the problem of visiting nodes in a particular order in order to find a path or a particular node. Traversal continues until the last node is
INDIVIDUAL ASSIGNMENT ALGO Page 7

INDIVIDUAL ASSIGNMENT
visited while searching stops once the node is found. There are various algorithms exists for graph searching and path finding. They are used for numerous different real world applications for example in road and rail construction and many more. Breadth First Search (BFS) With the help of BFS we find out whether there is a path between two nodes or not. Depth First Search (DFS) Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtrack. Kruskals Algorithms Kruskal's algorithm is a greedy algorithm in graph theory that finds a minimum spanning tree for a connected weighted graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. If the graph is not connected, then it finds a minimum spanning tree for each connected component. Prisms Algorithms Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a connected weighted graph. More about Prisms algorithm is described in the Prims algorithm section given below. Dijkstra Algorithms Shortest path between two nodes means from source to destination which path is most efficient Always takes the shortest edge connecting a known node to an unknown node.

INDIVIDUAL ASSIGNMENT ALGO

Page 8

INDIVIDUAL ASSIGNMENT
First Graph Problem

Derived solutions (Adjacency List and Matrix) and their analysis


Adjacency List: Adjacency lists are linked lists, one for each node, containing the names of the nodes to which a node is connected. It is preferred when the graph is sparse (A graph in which the number of edges is much less than the possible number of edges.) (PEB, 2008). In this graph vertex are {A,B,C,D,E,F,G,H,I,J},and the edges are {(A,B,3),(B,C,4),(C,D,1),(D,F,2),(F,J,6),(F,G,4),(C,F,4),(A,E,1),(E,C,1),(E,I,3),(E,H, 2),(I,F,2),(I,J,3),(I,C,7),(G,D,5),(G,J,1),(A,H,5),(I,H,6)}
A B 3 H 5 E 1 /

B C D E F G H I J \ \

C v D F C G D

4 1 2 1 4 5

\ F \ I J J 3 6 1 \ \ H 2 \ 2 \

Figure 3 Adjacency list solution for Graph at Figure1

INDIVIDUAL ASSIGNMENT ALGO

Page 9

INDIVIDUAL ASSIGNMENT
Adjacency Matrix:-

In adjacency matrix, we represent a graph as a matrix of dimensions V*V, where each cell in row r, and column c, represent whether there is an edge from vertex r, to vertex c, or not. We have 10 numbers of nodes from A-J now we can construct our 10 x 10 Matrix. One simple way to facilitate this is to store a VxV matrix (where V is the number of vertices). In this scheme the entry at [A][B] stores either a 1 or 0 to indicated that the vertices a and b are either connected or disconnected. Value 1 represents a connection between the edges and 0 represents no connection exists when the graph has no edge weight on it. For weighted graph this entry can store the edge weight between the two vertices. It should be noted that in a graph with bi-directional edges the matrix will be symmetric about the diagonal. For an adjacency matrix, we can quickly see whether an edge exists between any two given vertices. This operation takes O (1) time.

Table 1 Adjacency Matrix solution

INDIVIDUAL ASSIGNMENT ALGO

Page 10

INDIVIDUAL ASSIGNMENT
Analysis of both solutions (Adjacency list & Adjacency Matrix)

For

both

directed graphs, list

and the

For

both

directed graphs,

and the matrix

undirected adjacency

undirected adjacency of O(|V|2).

representation

has the desirable property that the amount of memory it

representation requires memory

requires is O(V+E) The benefit of The plus side is that adjacency matrices are very easy to construct and traverse. The look up time is O(1), as we can directly access a cell to check whether an edge exists or not.

using adjacency list to represent a graph is that it takes much less space when the number of edges is lower compared to the total number of vertices present in the graph. The space complexity

The

space

complexity

of

of adjacency list is O(V+E). The downside of adjacency list is that an edge looks up takes linear time. So to check whether vertex A has an edge to vertex E, we have to traverse through the complete list of adjacency of vertex A. If total number of edges is much less then V*V, meaning the graph is sparse, this consumes very less amount of memory space.
Table 2 Difference b/w Adjacency list and Adjacency Matrix

adjacency matrix is O(|V|2). Whether or not an edge is present between two vertices, we have to cell representing that. This makes the space complexity of adjacency matrix, O(V*V).

If total number of edges is much less then V*V, meaning the graph is sparse, this consumes lots of extra unnecessary space.

INDIVIDUAL ASSIGNMENT ALGO

Page 11

INDIVIDUAL ASSIGNMENT
Breadth First Search

In graph theory, breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all the neighboring nodes. Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal. All child nodes obtained by expanding a node are added to a FIFO (i.e., First In, First Out) queue. Assumption for BFS Let the graph be un-weighted and un-directed. Distance between each edge is 1 unit. Initially all nodes are white and distance are infinity () Source vertex will initialize with gray colour. Distance of source vertex is 0. Parent of source node is Nil. Initially parent of all other nodes is Nil. Initially minimum priority queue is empty. If all the adjacent nodes are explored then its colour will be black. If node colour is white than it is assumed to be undiscovered. Let A be the source vertex of graph and start searching from A Nodes will be explored and added to queue (FIFO queue) Breadth first search is also known as gray black & white theorem Explores the edges of a graph to reach every vertex from a vertex A The black box in the table shows that the node is deleted.

INDIVIDUAL ASSIGNMENT ALGO

Page 12

INDIVIDUAL ASSIGNMENT
Algorithms/Pseudo-codes of BFS

Result of BFS may vary depending on the order in which the vertices are explored .BFS tree may vary, but the distances computed by the algorithm will not. It computes the smallest distance from s to each reachable vertex

INDIVIDUAL ASSIGNMENT ALGO

Page 13

INDIVIDUAL ASSIGNMENT
Execution steps for Breadth First Search
Step-1

Initially all nodes are white Initially each node is having distance infinity(assumption) A.dist= and so on Parent of each nodes are nil A.p=nil and so on For source vertex A :- Having three neighbours these neighbours are not yet visited When the neighbours are visited it becomes gray and source become black A is in the queue right now which is to be explored.

INDIVIDUAL ASSIGNMENT ALGO

Page 14

INDIVIDUAL ASSIGNMENT
Step 2

Now parent of B,E and H is A B,E and H are added to queue ,B,E,H become gray and A becomes black According to First In First Out algorithms B will be de-queued and searching its neighbours will begin in step 3

Step 3

Now parent of C is B C is searched & added to queue ,C become gray and B becomes black
Page 15

INDIVIDUAL ASSIGNMENT ALGO

INDIVIDUAL ASSIGNMENT
Step 4 According to First In First Out algorithms E is de-queue and searching its neighbours

Now parent I.pred=E I is search & added to queue ,I become gray and E becomes black According to First In First Out algorithms H are de-queue and searching its neighbours

Step 5

{(H,A),(H,E),(H,I)} found and it is already traversed no need to add in queue Node H becomes black
Page 16

INDIVIDUAL ASSIGNMENT ALGO

INDIVIDUAL ASSIGNMENT
According to FIFO algorithms C is de-queue and searching its neighbours will begin.

Step 6


Step 7

{(C,B),(C,I),(C,E)} found and it is already traversed no need to add in queue {(C,D),(C,F)} found and added to queue Node C becomes black According to FIFO algorithm I is de-queue and searching its neighbours will begin

{(I,H),(I,E),(I,C),(I,F)} found and it is already traversed no need to add in queue

INDIVIDUAL ASSIGNMENT ALGO

Page 17

INDIVIDUAL ASSIGNMENT
{(I,J)} found and added to queue Node I becomes black According to FIFO algorithm D is de-queue and searching its neighbours

Step 8

{(D,C),(D,F)} found and it is already traversed no need to add in queue {(D,G)} found and added to queue Node D becomes black According to First In First Out algorithms F are de-queue and searching its neighbours

Step 9

INDIVIDUAL ASSIGNMENT ALGO

Page 18

INDIVIDUAL ASSIGNMENT
{(F,C),(F,D),(F,I),(F,J)} found and it is already traversed no need to add in queue Node F becomes black According to FIFO algorithms J is de-queue and searching its neighbours

Step 10

{(J,I),(J,F),(J,G)} found and it is already traversed no need to add in queue Node J becomes black According to FIFO algorithm G is de-queue and searching its neighbours will took place

Step 11

INDIVIDUAL ASSIGNMENT ALGO

Page 19

INDIVIDUAL ASSIGNMENT
{(G,D),(G,F),(G,J)} found and it is already traversed no need to add in queue Node G becomes black All node are searched

Analysis of breadth first search (BFS)


Result of BFS may vary depending on the order in which the vertices are explored. BFS tree may vary, but the distances computed by the algorithm will not. It computes the smallest distance from s to each reachable vertex. This algorithm discovers all vertices at distance k from s before discovering any vertices at distance k+1. Gray vertices may have some adjacent white vertices. All vertices adjacent to black vertex will be either gray or black Searching and traversing a graph takes O(1) time Each vertex is labelled twice o once as UNEXPLORED o once as DISCOVERY or CROSS

Each vertex is inserted once into a queue Method of occurrence Edges is called once for each vertex BFS on a graph with v vertices and e edges takes O(v + e ) time
Standard complexity of given graph problem O (|v| + |e|) = O (v+e).

Find the information of a path with the minimum number of edges between two given vertices

INDIVIDUAL ASSIGNMENT ALGO

Page 20

INDIVIDUAL ASSIGNMENT
Depth First Search
Depth First Search as its name implies search Deeper in the graph whenever possible. In depth first search, edges are explored out of the most recently discovered vertex that still has unexplored edges leaving it. When all of s edges have been explor ed, the search backtracks to explore edges leaving the vertex from which was discovered. This process cotinues until we have discovered all the vertices that are reachable from the original source vertex. If any undiscovered vertices remain, then one of them is selected as a new source and the search is repeated from that source. This entire process is repeated until all the vertices are discovered. The principle of depth first search is quite simple to go forward(In depth) while there is such possibility otherwise go backtrack. Assumption of DFS The Depth-First-Search algorithm assumes that the input Graph G = (V, E) is represented using adjacency lists. Consider a directed graph G = (V, E). In Depth first search, each vertex will have three possible colors representing its state. Initially all the vertex are white in color. Initially all the nodes have Nil predecessor or parent. Counter will start from 0. Graph is undirected and distance between each edge is same. Source node is A and end node is G. A back edge connects a vertex to an ancestor in a DFS-tree. Note that a self-loop is a back edge. A forward edge is a non tree edge that connects a vertex to a descendent in a DFStree. A cross edge is any other edge in graph G. It connects vertices in two different DFStree or two vertices in the same DFS-tree neither of which is the ancestor of the other.

INDIVIDUAL ASSIGNMENT ALGO

Page 21

INDIVIDUAL ASSIGNMENT
Algorithms/Pseudo-codes of Depth First Search

(V).

(E).

Space complexity: The space complexity of breadth first search is expressed as O|V| which is function of V. As every vertex is added only one time in the queue thus occupies O (1) space and V vertex occupies O (V). Time complexity: The sum of the lengths of all the adjacency lists is O (E), the total time spent in scanning adjacency list is O (E). The overhead of initialization is O (V), and thus the total running time of BFS is O (V+E) which is function of V and E it means running time of BFS depends on V and E. Thus breadth-first search runs in time linear in the size of the adjacency-list representation of G. Space complexity of BFS for given graph 1 having 10 vertices = O (|V|) Time complexity having 10 vertices and 18 edges =O (|V|+|E|) DFS starts in arbitrary vertex and runs as follow Mark each vertex u as gray(Visited) For each edge (u,v) where u is white run DFS search for u recursively Mark vertex u as black and backtrack to the parent
Page 22

INDIVIDUAL ASSIGNMENT ALGO

INDIVIDUAL ASSIGNMENT
Execution Steps for Depth First Search
Source Graph
B A.p=Nil A E E.p=Nil F F.p=Nil G G.p=Nil B.p=Nil C C.p=Nil D D.p=Nil

H.p=Nil

I.p=Nil

J.p=Nil

Start traversing from source A to its deeper and make vertex A as gray

Step-1
B A.p=Nil A 1 E E.p=Nil F F.p=Nil G G.p=Nil B.p=Nil C C.p=Nil D D.p=Nil

H.p=Nil

I.p=Nil

J.p=Nil

INDIVIDUAL ASSIGNMENT ALGO

Page 23

INDIVIDUAL ASSIGNMENT
Step-2 There is an edge (A, B) so make vertex B as gray. Now B.pred=A
B 2 A.p=Nil A 1 E E.p=Nil F F.p=Nil G G.p=Nil B.p=A C C.p=Nil D D.p=Nil

H H.p=Nil

I.p=Nil

J.p=Nil

Step-3 There is an edge (B,C), make the vertex C as gray. Now C.pred=B
D

B 2 A.p=Nil A 1

B.p=A

C 3

C.p=B

D.p=Nil

E E.p=Nil

F.p=Nil

G G.p=Ni l

H.p=Nil

I.p=Nil

J.p=Nil

INDIVIDUAL ASSIGNMENT ALGO

Page 24

INDIVIDUAL ASSIGNMENT
Step-4 There are edges (C, E), (C, F), (C, I), (C,F) and (C,D). We can take any of the edge. I have choosen (C, E) so I made the vertex C as gray. E.pred=C
B 2 A.p=Nil A 1 E E.p=C 4 F F.p=Nil G G.p=Nil B.p=A C 3 C.p=B D D.p=Nil

H.p=Nil

I.p=Nil

J.p=Nil

Step-5 There is an edge (C, H) make the vertex C as gray. H.pred=E


B 2 A.p=Nil A 1 E E.p=C 4 F F.p=Nil G G.p=Nil B.p=A C 3 C.p=B D D.p=Nil

5 H H.p=E I I.p=Nil J J.p=Nil

INDIVIDUAL ASSIGNMENT ALGO

Page 25

INDIVIDUAL ASSIGNMENT
Step-6 There is an edge (H,I) make the vertex C as gray I.pred=H
B 2 A.p=Nil A 1 E E.p=C 4 B.p=A C 3 F F.p=Nil G G.p=Nil C.p=B D D.p=Nil

5 H H.p=E

6 I I.p=H J J.p=Nil

Step-7 There is an edge (I,J) make the vertex J as gray J.pred=I


B 2 A.p=Nil A 1 E E.p=C 4 B.p=A C 3 F F.p=Nil G G.p=Nil C.p=B D D.p=Nil

5 H H.p=E

6 I I.p=H

7 J J.p=I

INDIVIDUAL ASSIGNMENT ALGO

Page 26

INDIVIDUAL ASSIGNMENT
Step-8 There is an edge (J,F) make the vertex F as gray F.pred=J
B 2 A.p=Nil A 1 E E.p=C 4 B.p=A C 3 F 8 F.p=J G G.p=Nil C.p=B D D.p=Nil

5 H H.p=E

6 I I.p=H

7 J J.p=I

Step-9 There is an edge (F,D ) make the vertex D as gray D.pred=F


D 9 F 8 F.p=J G G.p=Nil

B 2 A.p=Nil A 1

B.p=A

C 3

C.p=B

D.p=F

E E.p=C 4

5 H H.p=E

6 I I.p=H

7 J J.p=I

INDIVIDUAL ASSIGNMENT ALGO

Page 27

INDIVIDUAL ASSIGNMENT
Step-10 There is an edge (D,G) make the vertex G as gray G.pred=D
B 2 A.p=Nil A 1 E E.p=C 4 B.p=A C 3 F 8 F.p=J C.p=B D 9 G 10 G.p=D D.p=F

5 H H.p=E

6 I I.p=H

7 J J.p=I

Step-11 There are an edge (G,J),(G,F),(G,D) and J,F,D are visited.Mark G as black and backtrack to vertex D G.Color=Black as there is no more node from which G can connect.
B 2 A.p=Nil A 1 E E.p=C 4 B.p=A C 3 F 8 F.p=J C.p=B D 9 G 10 G.p=D 10/11 D.p=F

5 H H.p=E

6 I I.p=H

7 J J.p=I

INDIVIDUAL ASSIGNMENT ALGO

Page 28

INDIVIDUAL ASSIGNMENT
Step-12 There are edges (D,G),(D,C),(D,F,) and G,C,F which are already visited. Mark D as black and backtrack to vertex F D.Color=Black
D

B 2 A.p=Nil A 1

B.p=A

C 3

C.p=B

D.p=F 9/ 9 12 G

E E.p=C 4

F 8 F.p=J

10 G.p=D 10/11

5 H H.p=E

6 I I.p=H

7 J J.p=I

Step-13 There are an edge (F,C),(F,D),(F,G,),(F,J) and C,D,G,J are visited.Mark F as black and backtrack to vertex J F.Color=Black
B 2 A.p=Nil A 1 E E.p=C 4 B.p=A C 3 F 8 C.p=B D D.p=F 9/ 9 12 G 10 G.p=D 10/11

8/13 F.p=J

5 H H.p=E

6 I I.p=H

7 J J.p=I

INDIVIDUAL ASSIGNMENT ALGO

Page 29

INDIVIDUAL ASSIGNMENT
Step-14 There are an edge (J,F),(J,G),(J,I) and F,G,I are visited.Mark J as black and backtrack to vertex I J.Color=Black
B 2 A.p=Nil A 1 E E.p=C 4 B.p=A C 3 F 8 C.p=B D 9 8/13 F.p=J D.p=F 9/12 G 10 G.p=D 10/11

5 H H.p=E

6 I I.p=H

7 J J.p=I

7/14

Step-15 There are edges (I,J),(I,F),(I,C),(I,E),(I,H) and J,F,C,E,H are visited already. Mark I as black and backtrack to vertex H I.Color=Black
B 2 A.p=Nil A 1 E E.p=C 4 B.p=A C 3 F 8 C.p=B D 9 8/13 F.p=J D.p=F 9/12 G 10 G.p=D 10/11

5 H H.p=E

6 I 6/15 I.p=H

7 J.p=I J 7/14

INDIVIDUAL ASSIGNMENT ALGO

Page 30

INDIVIDUAL ASSIGNMENT
Step-16 There are edges (H,I),(H,E),(H,A) and I,E,A are visited.Mark H as black and backtrack to vertex E H.Color=Black
B 2 A.p=Nil A 1 E E.p=C 4 B.p=A C 3 F 8 C.p=B D 9 8/13 F.p=J D.p=F 9/12 G 10 G.p=D 10/11

5 H 5/16 H.p=E

6 I 6/15 I.p=H

7 J J.p=I 7/14

Step-17 There are an edge (E,A),(E,C),(E,H),(E,I) and A,C,H,I are visited. Mark E as black and backtrack to vertex C E.Color=Black
B 2 A.p=Nil A 1 4/17 E E.p=C 4 B.p=A C 3 F 8 C.p=B D 9 8/13 F.p=J D.p=F 9/12 G 10 G.p=D 10/11

5 H 5/16 H.p=E

6 I 6/15 I.p=H

7 J J.p=I 7/14

INDIVIDUAL ASSIGNMENT ALGO

Page 31

INDIVIDUAL ASSIGNMENT
Step-18 There are an edge (C,B),(C,E),(C,I),(C,D),(C,F),(C,I)and B,E,I,D,F are visited. Mark E as black and backtrack to vertex B. Now C.Color=Black
D 9 F 8 8/13 F.p=J

B 2 A.p=Nil A 1

B.p=A

C 3

C.p=B 3/18

D.p=F 9/12 G 10 G.p=D 10/11

E E.p=C 4/17 4

5 H 5/16 H.p=E

6 I 6/15 I.p=H

7 J J.p=I 7/14

Step-19 There are an edge (B,C),(B,A) and C,A are visited. Mark B as black and backtrack to vertex A. Now B.Color=Black
2/19 B.p=A 2 A.p=Nil A 1 4/17 E E.p=C 4

C 3

C.p=B 3/18 F 8

D 9 8/13 F.p=J

D.p=F 9/12 G 10 G.p=D 10/11

5 H 5/16 H.p=E

6 I 6/15 I.p=H

7 J J.p=I 7/14

INDIVIDUAL ASSIGNMENT ALGO

Page 32

INDIVIDUAL ASSIGNMENT
Step-20 There are an edge (A,B),(A,E),(A,H) and B,E,H are visited. Mark A as black .There is any edge associated with A so traversed are complete A.Color=Black
B 2 A.p=Nil A 1 1/20 4/17 E E.p=C 4 2/19 B.p=A C 3 C.p=B 3/18 F 8 D 9 8/13 F.p=J D.p=F 9/12 G 10 G.p=D 10/11

5 H 5/16 H.p=E

6 I 6/15 I.p=H

7 J J.p=I 7/14

Step-21 Final structure of Depth first search from source vertex to destination G
B 2 A.p=Nil A 1 1/20 4/17 E E.p=C 4 2/19 B.p=A C.p=B 3/18 F 8 D 9 8/13 F.p=J

C 3

D.p=F 9/12 G 10 G.p=D 10/11

5 H 5/16 H.p=E

6 I 6/15 I.p=H

7 J.p=I J 7/14

Note: the solution may vary according to user.

INDIVIDUAL ASSIGNMENT ALGO

Page 33

INDIVIDUAL ASSIGNMENT
Analysis of Depth First Search
The DFS-Visit is called (from DFS or from itself) once for each vertex in V [G] since each vertex is changed from white to gray once. The for-loop in DFS-Visit is executed a total of |E| times for a directed graph or 2|E| times for an undirected graph since each edge is explored once. Moreover, initialization takes (|V|) time. Therefore, the running time of DFS is (V + E). The time and space analysis of DFS differs according to its application area. In theoretical computer science, DFS is typically used to traverse an entire graph, and takes time O (|V| + |E|), linear in the size of the graph. In these applications it also uses space O (|V|) in the worst case to store the stack of vertices on the current search path as well as the set of already-visited vertices. Thus, in this setting, the time and space bounds are the same as for breadth-first search and the choice of which of these two algorithms to use depends less on their complexity and more on the different properties of the vertex orderings the two algorithms produce.

INDIVIDUAL ASSIGNMENT ALGO

Page 34

INDIVIDUAL ASSIGNMENT
Dijkstras Algorithm
Dijkstras algorithms solve the single source shortest paths problem on a weighted directed graph G= (V, E) for the case in which all edges weights are non-negative. Shortest path problem is the problem of finding a path between two vertices in a graph such that the sum of the weight of its constituent edges is minimized . It turns out that one can find the shortest paths from a given source to all points in a graph in the same time; hence this problem is sometimes called the single-source shortest paths problem. This problem is related to the spanning tree one. The graph representing all the paths from one vertex to all the others must be a spanning tree i.e. it must include all vertices. There will also be no cycles as a cycle would define more than one path from the selected vertex to at least one other vertex. For a graph, G= (V, E) where V is a set of vertices and E is a set of edges Dijkstras algorithm keeps two sets of vertices S - The set of vertices whose shortest paths from the source have already been determined and V-S The remaining vertices Relaxation: The relaxation process updates the weight of all the vertices, v, connected to a vertex, u, if we could improve the best estimate of the shortest path to v by including (u,v) in the path to v. This is the source vertex

B.p=Nil 4 E.p=Nil E 1

A.p=Nil A 0

C.p=Nil

D 1

D.p=Nil

5
F.p=Nil 4 G

G.p=Nil

H H.p=Nil

INDIVIDUAL ASSIGNMENT ALGO

5
6

3 I.p=Nil

J J.p=Nil

Page 35

INDIVIDUAL ASSIGNMENT
Algorithm/ Pseudo-codes of Dijkstras Algorithm

The running time of Dijkstras algorithm: The complexity of the algorithm is O(|V|2) The complexity for the algorithm can be improved to O ((|E|+ |V|) lg |V|) by using a heap to store and order vertices and adjacency lists. Assumption of Dijkstras Initially all vertexes will have distance infinity () and predecessor nil from the source vertex except source vertex. Source vertex will be initialized with 0 distances. Create an empty set Z and initialize it with {} Graph will be directed as well as the weight of each edge will remain same as given. The traversal of graph is shown by red lined edges.

INDIVIDUAL ASSIGNMENT ALGO

Page 36

INDIVIDUAL ASSIGNMENT
Use of Dijkstras algorithm to solve the problem with description
Step-1: Initially Z is an empty set Z= {}
B B.p=Nil 4 E.p=Nil E 0 1 C C.p=Nil D 1 D.p=Nil

A.p=Nil A

5
F.p=Nil 4 G

G.p=Nil

H H.p=Nil

Delete the first element of the Queue Q which having minimum distance i.e. 0, which is A. than update set of A.

5
6

3 I.p=Nil

J J.p=Nil

Z= {A} Step-2:

INDIVIDUAL ASSIGNMENT ALGO

Page 37

INDIVIDUAL ASSIGNMENT
B 3 A.p=Nil A 0 B.p=A 4 E.p=A E 1 1 C C.p=Nil D 1

D.p=Nil

5
F.p=Nil 4 G

G.p=Nil

5
5 H H.p=A 6

3 I.p=Nil

J J.p=Nil

Step-3

B 3 A.p=Nil A 0

B.p=A 4 E.p=A E 1 1

C 2

C.p=E 1

D.p=Nil

5
F.p=Nil 4 G

G.p=Nil

INDIVIDUAL ASSIGNMENT ALGO

5
3 H H.p=E 6 4 I I.p=E 3

J J.p=Nil

Page 38

INDIVIDUAL ASSIGNMENT
Step-4

B 3 A.p=Nil A 0

B.p=A 4 E.p=A E 1 1

C 2

C.p=E 1

D 3 F 6

D.p=C

5
F.p=C 4 G

G.p=Nil

5
3 H H.p=E 6 4 I I.p=E 3

J J.p=Nil

Step-5

Z= {A, E, C, B, H}

INDIVIDUAL ASSIGNMENT ALGO

Page 39

INDIVIDUAL ASSIGNMENT
B 3 A.p=Nil A 0 B.p=A 4 E.p=A E 1 1 6 C 2 C.p=E 1 D 3 F D.p=C

F.p=C 4

1
4 I I.p=E 3

3 H H.p=E

J J.p=Nil

Step-6 Remove the vertex D and compare weight of its associate vertex and relax the edge , arranging in ascending order Relax the edge F.dist>D.dist+w(D,F) -> Change the distance of (D,F.5)

Remove the vertex D and draw the line between (D, F, 5) and set Z= {A, E, C, B, H, D}
B 3 A.p=Nil A 0 B.p=A 4 E.p=A E 1 1 5 C 2 C.p=E 1 D 3 F D.p=C

F.p=C 4

1
4 I I.p=E 3

3 H H.p=E

J J.p=Nil

INDIVIDUAL ASSIGNMENT ALGO

Page 40

5
G

G.p=Nil

5
2

G.p=Nil

INDIVIDUAL ASSIGNMENT
Step-7

B 3 A.p=Nil A 0

B.p=A 4 E.p=A E 1 1

C 2

C.p=E 1

D 3 F 5

D.p=C

F.p=D 4

1
4 I I.p=E 3 7 J J.p=Nil

3 H H.p=E

Step-8

There is no any path from node J vertex so no need to add in queue Queue Q

INDIVIDUAL ASSIGNMENT ALGO

5
G

G.p=Nil

Page 41

INDIVIDUAL ASSIGNMENT
B 3 A.p=Nil A 0 B.p=A 4 E.p=A E 1 1 5 C 2 C.p=E 1 D 3 F D.p=C

F.p=D 4

1
4 I I.p=E 3 7 J J.p=Nil

3 H H.p=E

Step-9 Remove the vertex F and compare weight of its associate vertex and relax the edge , arranging in ascending order Relax the edge J.dist>F.dist+w(F,J) -> (False) No need to Change the distance of (F,J) G.dist>F.dist+w(F,G)-> Change the distance of (F,G,9)

Remove the vertex F and draw the line between (D, F, 5) and set Z= {A,E,C,B,H,D,I,F}

B 3 A.p=Nil A 0
3

B.p=A 4 E.p=A E 1 1
1

C 2

C.p=E 1
4

D 3 F 5

F.p=D 4
7

3 H H.p=E

4 I I.p=E

7 J J.p=I

INDIVIDUAL ASSIGNMENT ALGO

5
G

G.p=Nil

D.p=C

G.p=Nil

Page 42

INDIVIDUAL ASSIGNMENT
Step-10

Remove the vertex J and compare weight of its associate vertex and relax the edge , arranging in ascending order No any edge associate to J Queue G

Predecessor F Distance 9 Remove the vertex J and draw the line between (J, I, 7) and set Z= {A,E,C,B,H,D,I,F,J}
B 3 A.p=Nil A 0 B.p=A 4 E.p=A E 1 1 5 C 2 C.p=E 1 D 3 F D.p=C

F.p=D 4 9

1
4 I I.p=E 3 7 J J.p=I

3 H H.p=E

Step-11

INDIVIDUAL ASSIGNMENT ALGO

5
G G.p=F

Page 43

INDIVIDUAL ASSIGNMENT
B 3 A.p=Nil A 0 B.p=A 4 E.p=A E 1 1 5 C 2 C.p=E 1 D 3 F D.p=C

F.p=D 4 9

1
4 I I.p=E 3 7 J J.p=I

3 H H.p=E

Now Q is empty so the final shortest path is


B 3 A.p=Nil A 0
3

B.p=A

C 2 E.p=A
1

C.p=E 1 F 5
2

D 3

D.p=C

E 1 1

F.p=D 4 9

3 H H.p=E

4 I I.p=E

7 J J.p=I

INDIVIDUAL ASSIGNMENT ALGO

5
G G.p=F

G G.p=F

Page 44

INDIVIDUAL ASSIGNMENT Second Graph Problem


Analysis of Problem:
Representation of Graph Second

As per given in our assignment, nodes in the above diagram represent the various oil stations scattered across a country, while the edges represent the oil pipelines running between them. The weights of each edge represent the distance between each point. The above graph is directed and weighted graph represented as G= (V, E) which is having 10 vertices and 18 edges. Our main concern is to find minimum spanning tree (MST) of above graph by using Prims and Kruskals algorithm. And we are searching for the result such that it could be effective in terms of time and cost. We have to deliver a solution for the GRAPH THEORY problem mentioned above by solving it step-wise to determine a Minimum Spanning Tree (MST) with Prims and Kruskals algorithmic approaches and to compare the efficiencies of both in terms of Big-Oh notation with their calculated execution times. Include algorithm/pseudo-code with our explanations. Also, determine which complexity class, the above Graph Theory Problems are in, provide the definition for their complexity class and also we have to give justifications as to why the problems belong to respective classes.

INDIVIDUAL ASSIGNMENT ALGO

Page 45

INDIVIDUAL ASSIGNMENT
Analysis of Solution with respective graph theory
According to the question the main concern is to find the shortest path from source to destination if more than one path exists. So, I have chosen Greedy Algorithm for these solutions. A Greedy Algorithm is a method for finding an optimal solution to some problem involving a large, homogeneous data structure (such as an array, a free or a graph) by starting from an optimal solution to some component or small part of the data structure and extending it, by considering additional components of the data structure one by one, to arrive at an optimal global solution. There are two Minimum Spanning Trees algorithm Prims Algorithm and Kruskals Algorithm

Kruskals Algorithm
The basic idea of the Kruskals algorithm is as follow: Scan all edges in increasing weight order; if an edge is safe, keep it that is add it to the set A. In this method: 1. All edges are ordered by weight 2. Each edge in this sequence is checked to see whether it can be considered part of the tree under construction. 3. The edge is added to the tree if no cycle arises after its inclusion 4. The steps are repeated until all vertices are added to the MST Assumption of Kruskals Let the graph be denoted by G=(V,E) where V be the vertex and E be the edges. Weight of the graph will be denoted by w(u,v) where u and v are two consecutive vertices. Let the graph be undirected. 0, 1,2 etc written on nodes is the name of the node.

INDIVIDUAL ASSIGNMENT ALGO

Page 46

INDIVIDUAL ASSIGNMENT
Algorithm/ Pseudo-codes of Kruskals Algorithm

The running time of Kruskals algorithm: The complexity of the algorithm is determined by the complexity of the sorting method applied and the method used for cycle detection. The complexity for the algorithm is O(|E|lg|V|) Running time of MIT-KRUSKAL is: O(1) + O(V) + O(E lg E) + O(E lg V) Since |V| |E|+1, we prefer a tight upper bound with V terms instead of E terms. |E| < |V|2 lg |E| < lg |V|2 lg |E| < 2 lg |V| Running time of MIT-KRUSKAL is: O(E lg V)

INDIVIDUAL ASSIGNMENT ALGO

Page 47

INDIVIDUAL ASSIGNMENT
Execution steps for Kurskals Algorithm
Create the set of vertex o {0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11} Arrange the weight of edge in ascending order

Step-1: create the set for all the vertices of the graph {0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}

Step-2: Sort all the edges in ascending order and creating a set

INDIVIDUAL ASSIGNMENT ALGO

Page 48

INDIVIDUAL ASSIGNMENT
Step-3: First choose the edge which has minimum weight and then check either this edge form cycle or not if the edge does not form cycle then add the edge in the set otherwise leave the edge To check whether edge form cycle or not for this check the end and start point of edges lie in one set or not. If lie in one set form cycle otherwise not. 1. Edge (3, 4) has minimum weight and dont lie in same set so add it in set A and highlight edge A= (3, 4). 2. Set : {0},{1},{2},{3,4},{5},{6},{7},{8},{9},{10},{11}

Step-4: Next minimum edge is (4, 5) having weight 2 (4, 5) doesnt lie in the same set add it in set A and highlight the edge Set:{ 0},{1},{3,4,5},{6},{7},{8},{9},{10},{11}

Step-5: Next minimum edge is (2, 3) having weight 2 (2, 3) doesnt lie in the same set add it in set A and highlight the edge Set:{ 0},{1},{2,3,4,5},{6},{7},{8},{9},{10},{11}

INDIVIDUAL ASSIGNMENT ALGO

Page 49

INDIVIDUAL ASSIGNMENT
Step-6: Next minimum edge is (0, 1) having weight 3 (0, 1) doesnt lie in the same set add it in set A and highlight the edge Set:{ 0,1},{2,3,4,5},{6},{7},{8},{9},{10},{11}

Step-7: Next minimum edge is (1, 4) having weight 3 (1, 4) doesnt lie in the same set add it in set A and highlight the edge Set:{ 0,1,2,3,4,5},{6},{7},{8},{9},{10},{11}

Step-8: Next minimum edge is (6, 7) having weight 3 (6, 7) doesnt lie in the same set add it in set A and highlight the edge Set:{ 0,1,2,3,4,5},{6,7},{8},{9},{10},{11}

INDIVIDUAL ASSIGNMENT ALGO

Page 50

INDIVIDUAL ASSIGNMENT
Step-9: Next minimum edge is (8, 9) having weight 3 (8, 9) doesnt lie in the same set add it in set A and highlight the edge Set:{ 0,1,2,3,4,5},{6,7},{8 ,9},{10},{11}

Step-10: Next minimum edge is (0, 3) having weight 4 (0, 3) lay in the same set no need to add it in set A Set:{ 0,1,2,3,4,5},{6,7},{8 ,9},{10},{11}

Step-11: Next minimum edge is (4, 8) having weight 4 (4, 8) doesnt lie in the same set add it in set A and highlight the edge Set:{ 0,1,2,3,4,5,8,9},{6,7},{10},{11}

Step-12: Next minimum edge is (0, 3) having weight 4 (0, 3) lay in the same set no need to add and highlight Set:{ 0,1,2,3,4,5,8,9},{6,7},{10},{11}

INDIVIDUAL ASSIGNMENT ALGO

Page 51

INDIVIDUAL ASSIGNMENT
Step-13: Next minimum edge is (2, 6) having weight 4 (2,6) doesnt lie in the same set add it in set A and highlight the edge Set:{ 0,1,2,3,4,5,8,9,6,7},{10},{11}

Step-14: Next minimum edge is (0, 2) having weight 5 (2,6) lie in the same set no need to add and highlight Set:{ 0,1,2,3,4,5,8,9,6,7},{10},{11}

Step-15: Next minimum edge is (3, 7) having weight 5 (3,7) lie in the same set no need to add and highlight Set:{ 0,1,2,3,4,5,8,9,6,7},{10},{11}

Step-16: Next minimum edge is (8, 11) having weight 5 (8, 11) doesnt lie in the same set add it in set A and highlight the edge Set:{ 0,1,2,3,4,5,8,9,6,7,11},{10}

INDIVIDUAL ASSIGNMENT ALGO

Page 52

INDIVIDUAL ASSIGNMENT
Step-17: Next minimum edge is (5, 9) having weight 5 (5, 9) lay in the same set no need to add and highlight Set:{ 0,1,2,3,4,5,8,9,6,7,11},{10}

Step-18: Next minimum edge is (1, 5) having weight 6 (1, 5) lay in the same set no need to add and highlight Set:{ 0,1,2,3,4,5,8,9,6,7,11},{10}

Step-19: Next minimum edge is (6, 10) having weight 6 (6, 10) doesnt lie in the same set add it in set A and highlight the edge Set:{ 0,1,2,3,4,5,8,9,6,7,11,10}

INDIVIDUAL ASSIGNMENT ALGO

Page 53

INDIVIDUAL ASSIGNMENT
Step-20: Next minimum edge is (7, 8) having weight 6 (7,8 ) lie in the same set no need to add and highlight Set:{ 0,1,2,3,4,5,8,9,6,7,11,10}

Step-21: Next minimum edge is (7, 10) having weight 7 (7, 10) lay in the same set no need to add and highlight Set:{ 0,1,2,3,4,5,8,9,6,7,11,10}

Step-22: Next minimum edge is (10, 11) having weight 8 (10, 11) lie in the same set no need to add and highlight Set:{ 0,1,2,3,4,5,8,9,6,7,11,10}

INDIVIDUAL ASSIGNMENT ALGO

Page 54

INDIVIDUAL ASSIGNMENT
Step-22: Next minimum edge is (11, 9) having weight 9 (11, 9) lay in the same set no need to add and highlight Set:{ 0,1,2,3,4,5,8,9,6,7,11,10} Minimum spanning tree =3+3+1+2+2+4+4+3+3+5+6=36

Final graph after applying MST is as below. From this graph we can reach at every point with the help of edges provided.

INDIVIDUAL ASSIGNMENT ALGO

Page 55

INDIVIDUAL ASSIGNMENT
Prims Algorithm
Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for

a connected weighted undirected. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized Prims Algorithm is used to find a Minimum Spanning Tree rooted at a given vertex A Grow a MST by adding vertexes. In each step, a vertex that has least distance to the growing tree is added.

Algorithm/ Pseudo-codes of Prims Algorithm

The running time of Prims algorithm: The inner loop of the algorithm can be O(E) in the worst case, and since the outer loop iterates |V|-1 times, the inner loop may iterate O(|V||E|) in total Assumption of Prims Initialize except source vertex all other vertexes with infinity distance and parent as NIL. Q is assumed to be Queue, D or d is assumed to be distance between two nodes, P or p is assumed to be Parent or Predecessor of that node.
INDIVIDUAL ASSIGNMENT ALGO Page 56

INDIVIDUAL ASSIGNMENT
Vertex with 0 value is assumed to be the source node. 0,1,2 etc written on nodes is the name of the node. The graph is assumed to be undirected. Explored edges are marked with red bold line. Step wise solution of Prims Algorithm Step 1: Initialize distance of all vertexes. Except source vertex all other vertexes with infinity distance and parent as NIL. Arrange all vertexes in Queue Q in Ascending Order for maintaining minimum priority Queue

P=NIL D= 3 0 5

P=NIL D= 1 6

4 P=NIL D= 4 2 2 5 3

P=NIL D= 1

3 P=NIL D= 4 2 5 P=NIL D=

4 P=NIL D= P=NIL D= 8 6 3 5 9

5 P=NIL D=

P=NIL D=

6 3 7 6

10 P=NIL D= 8

11 P=NIL D=

Delete the first element which has minimum weight

Step 2: Find neighbor of deleted element which is 1, 2, and 3. Check minimum distance from deleted node of a graph
INDIVIDUAL ASSIGNMENT ALGO Page 57

INDIVIDUAL ASSIGNMENT
1. d > wt (0, 1) => > 3, This statement is true then update the distance and predecessor 1. d = 3 , 1.pred=0 2. d> wt(0, 2) => >5 , This statement is true then update the distance and predecessor 2. d =5, 2.pred = 0 3. d> wt(0, 3) => > 4, This statement is true then update the distance and predecessor 3. d= 4, 3.pred =0

P=NIL D=0 3 0 5

P=0 D=3 1 6

4 P=0 D=5 4 2 2 5 3

P=0 D=4 1

3 4

P=NIL D= 2 5 P=NIL D=

4 P=NIL D= P=NIL D= 8 6 3 5 9

5 P=NIL D=

P=NIL D=

6 3 7 6

10 P=NIL D= 8

11 P=NIL D=

Step 3: Find neighbor of deleted element which is 4, 5 Check minimum distance from deleted node of a graph 4. d > wt (1, 4) => > 3, This statement is true then update the distance and predecessor 1.d = 3 , 4.Pred=1 5. d> wt(1, 5) => >6 , This statement is true then update the distance and predecessor
INDIVIDUAL ASSIGNMENT ALGO Page 58

INDIVIDUAL ASSIGNMENT
2.d =6, 5.pred = 1

P=NIL D=0 3 0 5

P=0 D=3 1 6

4 P=0 D=5 4 2 2 5 3

P=0 D=4 1

3 4

P=1 D=3 2 5 P=1 D=6

4 P=NIL D= P=NIL D= 8 6 3 5 9

5 P=NIL D=

P=NIL D=

6 3 7 6

10 P=NIL D= 8

11 P=NIL D=

Step 4: Find neighbor of deleted element which is 3, 5, 8 Check minimum distance from deleted node of a graph 3. d > wt (4, 3) => 4 > 1, This statement is true then update the distance and predecessor 3.d = 4, 3.Pred=4 5. d > wt (4, 5) => 6 > 2, This statement is true then update the distance and predecessor
INDIVIDUAL ASSIGNMENT ALGO Page 59

INDIVIDUAL ASSIGNMENT
5.d = 2, 5.Pred=4 8. d > wt (4, 8) => > 4, This statement is true then update the distance and predecessor 8.d = 4, 8.Pred=4

P=NIL D=0 3 0 5

P=0 D=3 1 6

4 P=0 D=5 4 2 2 5 3

P=4 D=1 1

3 4

P=1 D=3 2 5 P=4 D=2

4 P=NIL D= P=4 D=4 8 6 3 5 9

5 P=NIL D=

P=NIL D=

6 3 7 6

10 P=NIL D= 8

11 P=NIL D=

Step 5: Find neighbor of deleted element which is 2, 7 Check minimum distance from deleted node of a graph 2. d > wt (3, 2) => 5> 2, This statement is true then update the distance and predecessor 2.d = 2, 2.Pred=3 7. d > wt (3, 7) => >5, This statement is true then update the distance and predecessor

INDIVIDUAL ASSIGNMENT ALGO

Page 60

INDIVIDUAL ASSIGNMENT
7.d = 5, 7.Pred=3

P=NIL D=0 3 0 5

P=0 D=3 1 6

4 P=3 D=2 4 2 2 5 3

P=4 D=1 1

3 4

P=1 D=3 2 5 P=4 D=2

4 P=3 D=5 P=4 D=4 8 6 3 5 9

5 P=NIL D=

P=NIL D=

6 3 7 6

10 P=NIL D= 8

11 P=NIL D=

Step 6: Find neighbor of deleted element which is 9 Check minimum distance from deleted node of the graph 9. d > wt (5, 9) => > 5, This statement is true then update the distance and predecessor 9.d = 5, 9.Pred=5

INDIVIDUAL ASSIGNMENT ALGO

Page 61

INDIVIDUAL ASSIGNMENT
P=NIL D=0 3 0 5 6 4 P=3 D=2 4 2 2 5 P=3 D=5 P=NIL D= 6 3 7 6 10 P=NIL D= 8 11 P=NIL D= 7 6 5 9 8 3 3 1 4 P=4 D=4 9 P=5 D=5 P=4 D=1 3 4 P=1 D=3 2 5 P=4 D=2 1 P=0 D=3

Step 7: Find neighbor of deleted element which is 6 Check minimum distance from deleted node of a graph 6. d > wt (2, 6) => > 4, This statement is true then update the distance and predecessor 6.d = 4, 6.Pred=2

P=NIL D=0 3 0 5

P=0 D=3 1 6

4 P=3 D=2 4 2 2 5 3

P=4 D=1 1

3 4

P=1 D=3 2 5 P=4 D=2

4 P=3 D=5 P=4 D=4 8 6 3 5 9

5 P=5 D=5

P=2 D=4

6 3 7 6

10 P=NIL D= 8

11 P=NIL D=

Step 8: Find neighbor of deleted element which is 7, 11, 9 Check minimum distance as

INDIVIDUAL ASSIGNMENT ALGO

Page 62

INDIVIDUAL ASSIGNMENT
7. d > wt (8, 7) => 5> 6, This statement is False then no need to update 7.d = 5, 7.Pred=3 11. d > wt (8, 11) => > 5, This statement is true then update the distance and predecessor 11.d = 5, 11.Pred=8 9. d > wt (8, 9) => 5> 3, This statement is true then update the distance and predecessor 9.d = 3, 9.Pred=8

P=NIL D=0 3 0 5

P=0 D=3 1 6

4 P=3 D=2 4 2 2 5 3

P=4 D=1 1

3 4

P=1 D=3 2 5 P=4 D=2

4 P=3 D=5 P=4 D=4 8 6 3 5 9

5 P=8 D=3

P=2 D=4

6 3 7 6

10 P=NIL D= 8

11 P=8 D=5

Step 9: Find neighbor of deleted element which is 11 Check minimum distance from deleted node of a graph 11. d > wt (9, 11) => 5> 9, This statement is False then 11.d = 5, 11.Pred=8

INDIVIDUAL ASSIGNMENT ALGO

Page 63

INDIVIDUAL ASSIGNMENT
P=NIL D=0 3 0 5 6 4 P=3 D=2 4 2 2 5 P=3 D=5 P=2 D=4 6 3 7 6 10 P=NIL D= 8 11 P=8 D=5 7 6 5 9 8 3 3 1 4 P=4 D=4 9 P=8 D=3 P=4 D=1 3 4 P=1 D=3 2 5 P=4 D=2 1 P=0 D=3

Step 10: Find neighbor of deleted element which is 7, 10 Check minimum distance from deleted node of a graph 7. d > wt (6, 7) => 5> 3, This statement is true then update the distance and predecessor 7.d = 3, 7.Pred=6 10. d > wt (6, 10) => > 6, This statement is true then update the distance and predecessor 10.d = 6, 10.Pred=6

P=NIL D=0 3 0 5

P=0 D=3 1 6

4 P=3 D=2 4 2 2 5 3

P=4 D=1 1

3 4

P=1 D=3 2 5 P=4 D=2

4 P=6 D=3 P=4 D=4 8 6 3 5 9

5 P=8 D=3

P=2 D=4

6 3 7 6

10 P=6 D=6 8

11 P=8 D=5

INDIVIDUAL ASSIGNMENT ALGO

Page 64

INDIVIDUAL ASSIGNMENT
Step 11: Find neighbor of deleted element which is 10 Check minimum distance from deleted node of a graph 10. d > wt (7, 10) => 6 > 7, This statement is False then no need to update 10.Pred=6 10.d = 6,

P=NIL D=0 3 0 5

P=0 D=3 1 6

4 P=3 D=2 4 2 2 5 3

P=4 D=1 1

3 4

P=1 D=3 2 5 P=4 D=2

4 P=6 D=3 P=4 D=4 8 6 3 5 9

5 P=8 D=3

P=2 D=4

6 3 7 6

10 P=6 D=6 8

11 P=8 D=5

Step 12: Find neighbor of deleted element which is 10 Check minimum distance from deleted node of a graph 10. d > wt (11, 10) => 6 >8, This statement is False then no need to update 10.Pred=6 10.d = 6,

INDIVIDUAL ASSIGNMENT ALGO

Page 65

INDIVIDUAL ASSIGNMENT
P=NIL D=0 3 0 5 6 4 P=3 D=2 4 2 2 5 P=6 D=3 P=2 D=4 6 3 7 6 10 P=6 D=6 8 11 P=8 D=5 7 6 5 9 8 3 3 1 4 P=4 D=4 9 P=8 D=3 P=4 D=1 3 4 P=1 D=3 2 5 P=4 D=2 1 P=0 D=3

Finally all the edges are deleted from queue and the queue becomes empty .So no further traversal are required. Final Minimum Spanning Tree is: Total Minimum Weight= 3+3+1+2+4+3+6+4+3+5+2= 36
P=NIL D=0 3 0 1 P=0 D=3

P=4 D=1 P=3 D=2 4 P=6 D=3 P=2 D=4 6 3 7 2 2 3 1

3 4

P=1 D=3 2 5 P=4 D=2

4 P=4 D=4 8 3 5 6 10 P=6 D=6 11 P=8 D=5 9 P=8 D=3

INDIVIDUAL ASSIGNMENT ALGO

Page 66

INDIVIDUAL ASSIGNMENT

Problem Classes
Problems are divided in to the different classes like P(Polynomial), NP(non Polynomial), NP- complete and NP- Hard and many more.

Description and Justification of chosen class


Selected class for these graphs is P class i.e. Polynomial class. The class P consists of all those decision problems that can be solved on a deterministic sequential machine in an amount of time that is polynomial in the size of the input. P is known to contain many natural problems, including linear programming and calculating the greatest common divisor. P is also known to be at least as large as L, the class of problems decidable in a logarithmic amount of memory space. Justification:- According to my research, from all the algorithms which I have discussed above e.g. BFS, DFS, Prims etc they are solvable in a given span of time. That means we can found solution to the graph problems using these algorithms. More over the following table summarizes why all the given algorithms are in P Class. From the graph given below we can easily say that the entire problem comes under P class.

INDIVIDUAL ASSIGNMENT ALGO

Page 67

INDIVIDUAL ASSIGNMENT Comparison of Time complexities of algorithms with their analysis


Prims Algorithm The inner loop of the algorithm can be O(E) in The Kruskals Algorithm complexity for the algorithm is

the worst case, and since the outer loop iterates O(|E|lg|V|) |V|-1 times, the inner loop may iterate O(|V||E|) It is determined by the complexity of the in total. sorting method applied and the method used Thus the total time for Prims algorithm is O(V for cycle detection lg V+ E lg V)=(E lg V), which is asymptotically the same as for our implementation of Kruskals algorithm. (Cormen, 2009) To form a tree at every stage, a new vertex is always added to an old node Nodes can be joined in any way, from old to old or new to new, that may risk in the formation of cycle. A time to time check is required. Space-saving. Time-saving.

Efficiency of this algorithm can be improved by Efficiency of this algorithm can be improved using the Fibonacci heap. If |V| elements are by using the sorting algorithms organized into a Fibonacci heap, we can perform an EXTRAXTION-MIN operation in O(lg V) amortized time and a decrease-key operation in O(1) amortized time. Hence, running time of Prims algorithm improves to O(E+V lg V). (Cormen, 2009)

INDIVIDUAL ASSIGNMENT ALGO

Page 68

INDIVIDUAL ASSIGNMENT
BFS DFS Dijkstras It is appropriate to find the shortest path between two vertices in a weighted graph. Total running time is Since the total number of edges in all O(V+E). the adjacency lists is |E|, there are a total of |E| iterations of the for loop, with each iteration taking O(1) time i.e. constant. Therefore, Total running time is O (|V|2+|E|) =O (|V|2) It goes as broad as possible at each node. It goes as deep as possible. We search all the vertices in the graph to determine the lowest cost route between each point. Most critical drawback is huge space requirement. Most critical drawback is huge and unpredictable time complexity Efficiency of this algorithm can be improved by using the sorting It doesnt require extra knowledge.

It find the shortest path, but not Doesnt find the shortest suitable to find the shortest path path. in a weighted graph. Total running time is O (V+ E).

algorithms as all the steps expect this is almost common in other

algorithms. In this algorithm if the sorting method is applied in best way.

INDIVIDUAL ASSIGNMENT ALGO

Page 69

INDIVIDUAL ASSIGNMENT
REFRENCES

Cormen, T. H. (2009). Mimimum Spanning tree. In C. E. Leiserson, Introduction to Algorithms (pp. 570,573). PHI Learning Pvt. ltd. PEB. (2008, August 14). Sparse Graph. Retrieved October 18, 2012, from NIST: http://xlinux.nist.gov/dads/HTML/sparsegraph.html Rouse, M. (2005, September). Graph theory. Retrieved 10 10, 2012, from WhatIs.com: http://whatis.techtarget.com/definition/graph-theory Weisstein, E. W. (2012, September 6). Algorithm. Retrieved 10 10, 2012, from MathWorld-A Wolfram Web Resource: http://mathworld.wolfram.com/Algorithm.html

FIGURE 1GRAPH THEORY PROBLEM (FOR BFS, DFS & DIJKSTRAS SSSP) ........................................................................... 6 FIGURE 2 GRAPH THEORY PROBLEM (MST) ................................................................................................................ 6 FIGURE 4 ADJACENCY LIST SOLUTION FOR GRAPH AT FIGURE1 .......................................................................................... 9

TABLE 1 ADJACENCY MATRIX SOLUTION ................................................................................................................... 10 TABLE 2 DIFFERENCE B/W ADJACENCY LIST AND ADJACENCY MATRIX ............................................................................... 11

INDIVIDUAL ASSIGNMENT ALGO

Page 70

Potrebbero piacerti anche