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

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

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

Figure 2 Graph Theory Problem (MST)  The nodes in the above diagram represent the various oil stations scattered across a country. The weights of each edge represent the distance between each point. INDIVIDUAL ASSIGNMENT ALGO Page 6 . DFS & Dijkstra’s 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. while the edges represent the oil pipelines running between them.INDIVIDUAL ASSIGNMENT Figure 1Graph Theory Problem (for BFS.

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 Prim‟s and Kruskal‟s 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. to solve the real world problems. Traversal continues until the last node is INDIVIDUAL ASSIGNMENT ALGO Page 7 . and Dijkstra‟s algorithms using both adjacency list and adjacency matrix. I have to execute different algorithms to achieve goal. Depth first search (DFS). According to the requirement. The graphs can be of different types for example weighted or un-weighted. 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. Kruskal‟s algorithm etc. Different algorithms are applied on these graphs like Prim‟s algorithm. Directed or Undirected graph. Dijkstra‟s algorithm. for the both Graph Theory Problems. Determine the complexity class. I have to analyze and find optimal solution using Breadth first search (BFS). complete graph. 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. Provide the definition for their complexity class and also give justifications as to why the problems belong to respective classes. Kruskal‟s. Prim‟s.

where the total weight of all the edges in the tree is minimized. 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. INDIVIDUAL ASSIGNMENT ALGO Page 8 . Prism‟s Algorithms   Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a connected weighted graph. tree structure. 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. They are used for numerous different real world applications for example in road and rail construction and many more. Kruskal‟s Algorithms  Kruskal's algorithm is a greedy algorithm in graph theory that finds a minimum spanning tree for a connected weighted graph. or graph. This means it finds a subset of the edges that forms a tree that includes every vertex. There are various algorithms exists for graph searching and path finding. More about Prism‟s algorithm is described in the Prim‟s algorithm section given below. If the graph is not connected.INDIVIDUAL ASSIGNMENT visited while searching stops once the node is found. then it finds a minimum spanning tree for each connected component. 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.

1).G.7).4).J.5).(E.(C.5).J.(I.1).J}.E.2).and the edges are {(A.(E.F.3).(I.1).H.4).1).D.(D.(F. 2008).(G.(B.3).D.I.B. 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.2).F.I.(I.(E.(F. one for each node.C.C.E.C.3).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 \ C 7 F 2 J 3 H 6 \ Figure 3 Adjacency list solution for Graph at Figure1 INDIVIDUAL ASSIGNMENT ALGO Page 9 .  In this graph vertex are {A.G.4).H.(C.(A.(G.B.) (PEB.J.H.INDIVIDUAL ASSIGNMENT First Graph Problem Derived solutions (Adjacency List and Matrix) and their analysis Adjacency List: Adjacency lists are linked lists.6).F.H. containing the names of the nodes to which a node is connected.(I. 2).D.C.(A.F.

One simple way to facilitate this is to store a VxV matrix (where V is the number of vertices). where each cell in row r. This operation takes O (1) time. we can quickly see whether an edge exists between any two given vertices.INDIVIDUAL ASSIGNMENT Adjacency Matrix:- In adjacency matrix. or not. Table 1 Adjacency Matrix solution INDIVIDUAL ASSIGNMENT ALGO Page 10 . we represent a graph as a matrix of dimensions V*V. to vertex c.  For an adjacency matrix. represent whether there is an edge from vertex r.   We have 10 numbers of nodes from A-J now we can construct our 10 x 10 Matrix. 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. and column c.   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.

 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. meaning the graph is sparse. The look up time is O(1). this consumes lots of extra unnecessary space.INDIVIDUAL ASSIGNMENT Analysis of both solutions (Adjacency list & Adjacency Matrix)  For both directed graphs.  If total number of edges is much less then V*V. meaning the graph is sparse. Whether or not an edge is present between two vertices. we have to cell representing that.  If total number of edges is much less then V*V. this consumes very less amount of memory space. as we can directly access a cell to check whether an edge exists or not.   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. Table 2 Difference b/w Adjacency list and Adjacency Matrix adjacency matrix is O(|V|2). list and the  For both directed graphs.   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. So to check whether vertex A has an edge to vertex E. This makes the space complexity of adjacency matrix. INDIVIDUAL ASSIGNMENT ALGO Page 11 . O(V*V). and the matrix undirected adjacency undirected adjacency of O(|V|2). we have to traverse through the complete list of adjacency of vertex A.

e. Parent of source node is Nil.               Distance between each edge is 1 unit. Initially minimum priority queue is empty. Initially parent of all other nodes is Nil. until it finds the goal. breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all the neighboring nodes. INDIVIDUAL ASSIGNMENT ALGO Page 12 . All child nodes obtained by expanding a node are added to a FIFO (i. If all the adjacent nodes are explored then its colour will be black.INDIVIDUAL ASSIGNMENT Breadth First Search In graph theory. Assumption for BFS  Let the graph be un-weighted and un-directed. Then for each of those nearest nodes. First In.. Distance of source vertex is 0. Initially all nodes are white and distance are infinity (∞) Source vertex will initialize with gray colour. it explores their unexplored neighbor nodes. and so on. First Out) queue. 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.

BFS tree may vary. It computes the smallest distance from s to each reachable vertex INDIVIDUAL ASSIGNMENT ALGO Page 13 . but the distances computed by the algorithm will not.INDIVIDUAL ASSIGNMENT Algorithms/Pseudo-codes of BFS Result of BFS may vary depending on the order in which the vertices are explored .

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 .dist=∞ and so on Parent of each nodes are nil A.p=nil and so on For source vertex A :.INDIVIDUAL ASSIGNMENT Execution steps for Breadth First Search Step-1         Initially all nodes are white Initially each node is having distance infinity(assumption) A.

INDIVIDUAL ASSIGNMENT Step 2    Now parent of B.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 .E and H is A B.E.E and H are added to queue .C become gray and B becomes black Page 15 INDIVIDUAL ASSIGNMENT ALGO .B.

A).(H.pred=E I is search & added to queue .INDIVIDUAL ASSIGNMENT  Step 4 According to First In First Out algorithms E is de-queue and searching it‟s neighbours    Now parent I.(H.I become gray and E becomes black According to First In First Out algorithms H are de-queue and searching it‟s neighbours Step 5   {(H.I)} found and it is already traversed no need to add in queue Node H becomes black Page 16 INDIVIDUAL ASSIGNMENT ALGO .E).

F)} found and it is already traversed no need to add in queue INDIVIDUAL ASSIGNMENT ALGO Page 17 .D).E)} found and it is already traversed no need to add in queue {(C.INDIVIDUAL ASSIGNMENT  According to FIFO algorithms C is de-queue and searching it‟s neighbours will begin.C).(I.(I.(C.B).(C. Step 6     Step 7 {(C.H).F)} found and added to queue Node C becomes black According to FIFO algorithm I is de-queue and searching it‟s neighbours will begin  {(I.(I.(C.I).E).

INDIVIDUAL ASSIGNMENT
   {(I,J)} found and added to queue Node I becomes black According to FIFO algorithm D is de-queue and searching it‟s 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 it‟s 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 it‟s 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 it‟s 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

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

the total time spent in scanning adjacency list is O (E). As every vertex is added only one time in the queue thus occupies O (1) space and V vertex occupies O (V).       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.INDIVIDUAL ASSIGNMENT Algorithms/Pseudo-codes of Depth First Search (V). The overhead of initialization is O (V).  Time complexity: The sum of the lengths of all the adjacency lists is O (E). Space complexity: The space complexity of breadth first search is expressed as O|V| which is function of 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. (E).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 .

p=Nil G G.p=Nil Start traversing from source A to it‟s deeper and make vertex A as gray Step-1 B A.p=Nil F F.p=Nil H H.p=Nil A E E.p=Nil F F.p=Nil G G.INDIVIDUAL ASSIGNMENT Execution Steps for Depth First Search Source Graph B A.p=Nil J J.p=Nil D D.p=Nil J J.p=Nil I I.p=Nil I I.p=Nil INDIVIDUAL ASSIGNMENT ALGO Page 23 .p=Nil B.p=Nil B.p=Nil C C.p=Nil A 1 E E.p=Nil C C.p=Nil D D.p=Nil H H.

p=Nil I I.p=Nil INDIVIDUAL ASSIGNMENT ALGO Page 24 .p=Nil A 1 B. Now C.p=Nil J J.pred=A B 2 A.p=Nil B.pred=B D B 2 A.p=A C C.p=A C 3 C.p=Nil A 1 E E.p=B D.p=Ni l H H.p=Nil I I. B) so make vertex B as gray.p=Nil H H.p=Nil G G.p=Nil F F.p=Nil D D.p=Nil J J.C). make the vertex C as gray.p=Nil F F. Now B.p=Nil G G.p=Nil E E.INDIVIDUAL ASSIGNMENT Step-2 There is an edge (A.p=Nil Step-3 There is an edge (B.

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

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

p=Nil A 1 B.p=Nil C.p=Nil B 2 A.p=I INDIVIDUAL ASSIGNMENT ALGO Page 27 .p=Nil 5 H H.p=I Step-9 There is an edge (F.INDIVIDUAL ASSIGNMENT Step-8 There is an edge (J.p=C 4 5 H H.p=Nil A 1 E E.D ) make the vertex D as gray D.F) make the vertex F as gray F.p=A C 3 F 8 F.p=J G G.p=F E E.p=H 7 J J.p=E 6 I I.p=C 4 B.pred=J B 2 A.pred=F D 9 F 8 F.p=B D D.p=H 7 J J.p=J G G.p=B D.p=E 6 I I.p=A C 3 C.

p=J C.p=B D 9 G 10 G.(G.p=D D.p=I Step-11 There are an edge (G.F.Color=Black as there is no more node from which G can connect.p=Nil A 1 E E.p=Nil A 1 E E.p=E 6 I I.p=I INDIVIDUAL ASSIGNMENT ALGO Page 28 .pred=D B 2 A.F).p=H 7 J J.p=C 4 B.D) and J.J).p=A C 3 F 8 F.p=B D 9 G 10 G.p=A C 3 F 8 F.p=H 7 J J.INDIVIDUAL ASSIGNMENT Step-10 There is an edge (D.Mark G as black and backtrack to vertex D G. B 2 A.p=D 10/11 D.p=F 5 H H.(G.D are visited.p=E 6 I I.G) make the vertex G as gray G.p=C 4 B.p=J C.p=F 5 H H.

(F.Color=Black B 2 A.p=J 10 G.p=H 7 J J.p=D 10/11 8/13 F.J) and C.p=F 9/ 9 12 G E E.p=I Step-13 There are an edge (F.p=C 4 F 8 F.p=C 4 B.Color=Black D B 2 A.F which are already visited.C). Mark D as black and backtrack to vertex F D.p=H 7 J J.p=I INDIVIDUAL ASSIGNMENT ALGO Page 29 .p=Nil A 1 B.F.p=A C 3 F 8 C.(D.C.G.G).(F.INDIVIDUAL ASSIGNMENT Step-12 There are edges (D.p=Nil A 1 E E.p=D 10/11 5 H H.(F.(D.J are visited.p=E 6 I I.G.p=B D D.p=A C 3 C.p=B D.p=F 9/ 9 12 G 10 G.C).p=E 6 I I.D.p=J 5 H H.).) and G.Mark F as black and backtrack to vertex J F.D).

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

p=I 7/14 Step-17 There are an edge (E.(E.Color=Black B 2 A.A are visited.H).(H.I).H.A) and I.p=J D.p=B D 9 8/13 F.p=J D.p=D 10/11 5 H 5/16 H.p=Nil A 1 E E.p=F 9/12 G 10 G.C).(E.p=F 9/12 G 10 G.(H.p=H 7 J J.p=B D 9 8/13 F.p=A C 3 F 8 C.A).p=H 7 J J.E.E).(E.p=D 10/11 5 H 5/16 H.p=A C 3 F 8 C.Mark H as black and backtrack to vertex E H.p=E 6 I 6/15 I.C.Color=Black B 2 A.p=Nil A 1 4/17 E E.p=E 6 I 6/15 I. Mark E as black and backtrack to vertex C E.p=C 4 B.INDIVIDUAL ASSIGNMENT Step-16 There are edges (H.p=C 4 B.p=I 7/14 INDIVIDUAL ASSIGNMENT ALGO Page 31 .I are visited.I) and A.

(C.Color=Black D 9 F 8 8/13 F.Color=Black 2/19 B.p=Nil A 1 B.B).p=B 3/18 F 8 D 9 8/13 F. Now C.p=F 9/12 G 10 G.(C.A are visited.I.(B.I).C). Mark „E‟ as black and backtrack to vertex B.(C.D.p=J D.p=A C 3 C.p=I 7/14 Step-19 There are an edge (B.p=E 6 I 6/15 I.p=B 3/18 D.p=C 4 B C 3 C.p=D 10/11 E E.p=J B 2 A.p=I 7/14 INDIVIDUAL ASSIGNMENT ALGO Page 32 .p=A 2 A.p=Nil A 1 4/17 E E.p=H 7 J J. Now B.p=D 10/11 5 H 5/16 H.INDIVIDUAL ASSIGNMENT Step-18 There are an edge (C.p=C 4/17 4 5 H 5/16 H.E.D).(C.I)and B.A) and C.p=E 6 I 6/15 I. Mark B as black and backtrack to vertex A.p=F 9/12 G 10 G.(C.F are visited.E).F).p=H 7 J J.

p=B 3/18 F 8 D 9 8/13 F.p=B 3/18 F 8 D 9 8/13 F.p=Nil A 1 1/20 4/17 E E.p=E 6 I 6/15 I.p=C 4 2/19 B.(A.p=D 10/11 5 H 5/16 H.INDIVIDUAL ASSIGNMENT Step-20 There are an edge (A.E.H) and B. INDIVIDUAL ASSIGNMENT ALGO Page 33 .H are visited.p=E 6 I 6/15 I.p=A C 3 C.There is any edge associated with A so traversed are complete A.p=F 9/12 G 10 G.p=A C.p=Nil A 1 1/20 4/17 E E.p=J D.E).p=I J 7/14 Note: the solution may vary according to user.p=H 7 J.p=I 7/14 Step-21 Final structure of Depth first search from source vertex to destination G B 2 A.p=D 10/11 5 H 5/16 H.(A.p=C 4 2/19 B.p=H 7 J J.B). Mark A as black .p=J C 3 D.p=F 9/12 G 10 G.Color=Black B 2 A.

Moreover. DFS is typically used to traverse an entire 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. 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. linear in the size of the graph. Therefore. in this setting. INDIVIDUAL ASSIGNMENT ALGO Page 34 . 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 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 time and space analysis of DFS differs according to its application area. initialization takes Θ (|V|) time. the running time of DFS is Θ (V + E). and takes time O (|V| + |E|). In theoretical computer science. Thus.

hence this problem is sometimes called the single-source shortest paths problem. u.e. if we could improve the best estimate of the shortest path to v by including (u. 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. E) for the case in which all edges weights are non-negative. This problem is related to the spanning tree one.v) in the path to v.p=Nil 4 G 2 7  3  2  1 G.p=Nil D 1  4  F D.p=Nil 4 E. it must include all vertices. It turns out that one can find the shortest paths from a given source to all points in a graph in the same time.p=Nil 2 INDIVIDUAL ASSIGNMENT ALGO 5 6  I 3 I. connected to a vertex.INDIVIDUAL ASSIGNMENT Dijkstra’s Algorithm Dijkstra‟s algorithms solve the single source shortest paths problem on a weighted directed graph G= (V. For a graph.p=Nil 3 1 5 F. v. This is the source vertex B B.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.p=Nil Page 35 . G= (V. E) where V is a set of vertices and E is a set of edges Dijkstra‟s algorithm keeps two sets of vertices S .p=Nil A 0 C C.p=Nil E 1  A.p=Nil  H H. 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 .p=Nil  J J. The graph representing all the paths from one vertex to all the others must be a spanning tree i.

INDIVIDUAL ASSIGNMENT Algorithm/ Pseudo-codes of Dijkstra’s Algorithm The running time of Dijkstra‟s 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. Source vertex will be initialized with 0 distances. Assumption of Dijkstra’s     Initially all vertexes will have distance infinity (∞) and predecessor nil from the source vertex except source vertex. 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. INDIVIDUAL ASSIGNMENT ALGO Page 36 . The traversal of graph is shown by red lined edges.

e.p=Nil A  4  F 3 1 5 F. 5 6  I 3 I.p=Nil  J J.p=Nil Z= {A} Step-2: INDIVIDUAL ASSIGNMENT ALGO Page 37 .p=Nil 4 E. 0.p=Nil 2 Delete the first element of the Queue Q which having minimum distance i.INDIVIDUAL ASSIGNMENT Use of Dijkstra’s algorithm to solve the problem with description Step-1: Initially Z is an empty set Z= {ᴓ} B B.p=Nil E 0 1 C C.p=Nil D 1 D.p=Nil  A.p=Nil 4 G 2 7  3  2  1 G.p=Nil  H H. which is A. than update set of A.

INDIVIDUAL ASSIGNMENT B 3 A.p=A E 1 1 C 2 C.p=Nil 4 G 2 7  2  1 G.p=E 1 D  F D.p=Nil Page 38 .p=A E 1 1 C C.p=E 6 4 I I.p=Nil 4 G 2 7  2  1 G.p=A 4 E.p=Nil A 0 B.p=Nil A 0 B.p=Nil 3 1 4 5 F.p=Nil Step-3 B 3 A.p=Nil 2 3 5 5 H H.p=Nil D 1  4  F D.p=Nil 3 1 5 F.p=A 4 E.p=E 3  J J.p=Nil 2 3 INDIVIDUAL ASSIGNMENT ALGO 5 3 H H.p=Nil  J J.p=A 6  I 3 I.

B. H} INDIVIDUAL ASSIGNMENT ALGO Page 39 .p=E 3  J J.p=Nil Step-5 Z= {A.p=E 1 D 3 F 6 D.p=E 6 4 I I.p=C 4 G 2 7  1 G.p=Nil 2 2 3 5 3 H H. E. C.INDIVIDUAL ASSIGNMENT Step-4 B 3 A.p=Nil A 0 B.p=A 4 E.p=C 3 1 4 5 F.p=A E 1 1 C 2 C.

dist+w(D.p=A 4 E. C.p=E 1 D 3 F D. F. arranging in ascending order Relax the edge F.p=Nil 2 3 5 5 2 G  G.p=Nil 2 6 3 5 .p=E 1 D 3 F D.p=C 4 7 2 1 4 I I. 5) and set Z= {A.p=E 6  J J. E.p=Nil A 0 B.p=A E 1 1 6 C 2 C.p=E 6  J J.5) Remove the vertex D and draw the line between (D.p=C 3 1 4 F.p=A E 1 1 5 C 2 C.p=E 3 3 H H.dist>D.F) -> Change the distance of (D.p=E 3 3 H H.p=Nil A 0 B.INDIVIDUAL ASSIGNMENT B 3 A.p=Nil INDIVIDUAL ASSIGNMENT ALGO Page 40 5 G 2  G. D} B 3 A. H.F.p=C 3 1 4 F.p=C 4 7 2 1 4 I I.p=A 4 E.p=Nil Step-6   Remove the vertex D and compare weight of its associate vertex and relax the edge . B.

p=E 6 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 2  G.p=E 1 D 3 F 5 D.p=C 3 1 4 F.p=A 4 E.p=Nil A 0 B.INDIVIDUAL ASSIGNMENT Step-7 B 3 A.p=D 4 7 2 1 4 I I.p=Nil 2 6 3 5 Page 41 .p=Nil 3 H H.p=A E 1 1 C 2 C.p=E 3 7 J J.

I. arranging in ascending order Relax the edge J.p=E 1 D 3 F D.p=A E 1 1 1 C 2 C.p=Nil A 0 3 B.H.p=C 3 1 4 F.dist+w(F.p=A 4 E. F.p=Nil 2 6 3 5 Page 42 .p=E 6 4 I I.p=D 4 7 2 1 4 I I.dist+w(F.E.dist>F.p=I INDIVIDUAL ASSIGNMENT ALGO 5 5 G 2  G.p=A E 1 1 5 C 2 C.dist>F.p=E 6 Step-9    Remove the vertex F and compare weight of its associate vertex and relax the edge .p=A 4 E.p=C 2 G  G.G)-> Change the distance of (F.p=D 4 7 2 1 3 H H.p=Nil 2 6 3 5 D.C.9) Remove the vertex F and draw the line between (D.B.p=Nil A 0 B.G.p=E 3 7 J J.p=Nil 3 H H.D.J) -> (False) No need to Change the distance of (F.p=E 1 4 D 3 F 5 F. 5) and set Z= {A.J) G.F} B 3 A.p=E 3 7 J J.INDIVIDUAL ASSIGNMENT B 3 A.

H.p=E 6 Step-11 INDIVIDUAL ASSIGNMENT ALGO 5 G G.B.p=A E 1 1 5 C 2 C.C.p=E 1 D 3 F D.D.J} B 3 A.INDIVIDUAL ASSIGNMENT Step-10   Remove the vertex J and compare weight of its associate vertex and relax the edge .p=D 4 9 7 2 1 4 I I.p=F 2 2 6 3 5 Page 43 .F.p=A 4 E. 7) and set Z= {A. 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.E.p=C 3 1 4 F. I.p=Nil A 0 B.p=I 3 H H.p=E 3 7 J J.I.

p=D 4 9 7 2 1 4 I I.p=E 1 D 3 F D.p=Nil A 0 3 B.p=A E 1 1 5 C 2 C.p=E 3 7 J J.p=D 4 9 2 3 H H.p=F 2 2 6 3 5 G G.p=E 1 F 5 2 D 3 D.p=F 3 Page 44 .p=A 1 C.p=C 3 1 4 F.INDIVIDUAL ASSIGNMENT B 3 A.p=C E 1 1 F.p=A C 2 E.p=E 6 Now Q is empty so the final shortest path is B 3 A.p=E 4 I I.p=Nil A 0 B.p=I 3 H H.p=E 3 7 J J.p=I INDIVIDUAL ASSIGNMENT ALGO 5 G G.p=A 4 E.

while the edges represent the oil pipelines running between them. 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 Prim‟s and Kruskal‟s algorithmic approaches and to compare the efficiencies of both in terms of Big-Oh notation with their calculated execution times. Also. nodes in the above diagram represent the various oil stations scattered across a country. determine which complexity class. the above Graph Theory Problems are in. INDIVIDUAL ASSIGNMENT ALGO Page 45 . E) which is having 10 vertices and 18 edges. provide the definition for their complexity class and also we have to give justifications as to why the problems belong to respective classes. Our main concern is to find minimum spanning tree (MST) of above graph by using Prim‟s and Kruskal‟s algorithm. The weights of each edge represent the distance between each point. The above graph is directed and weighted graph represented as G= (V. And we are searching for the result such that it could be effective in terms of time and cost. Include algorithm/pseudo-code with our explanations.INDIVIDUAL ASSIGNMENT Second Graph Problem Analysis of Problem: Representation of Graph Second As per given in our assignment.

Let the graph be undirected. In this method: 1. by considering additional components of the data structure one by one. keep it that is add it to the set A. The steps are repeated until all vertices are added to the MST Assumption of Kruskal’s     Let the graph be denoted by G=(V. Weight of the graph will be denoted by w(u. to arrive at an optimal global solution.2 etc written on nodes is the name of the node. There are two Minimum Spanning Trees algorithm Prim’s Algorithm and Kruskal’s Algorithm Kruskal’s Algorithm The basic idea of the Kruskal‟s algorithm is as follow: Scan all edges in increasing weight order. So. 0. INDIVIDUAL ASSIGNMENT ALGO Page 46 . 3.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. I have chosen Greedy Algorithm for these solutions. The edge is added to the tree if no cycle arises after its inclusion 4. All edges are ordered by weight 2. a free or a graph) by starting from an optimal solution to some component or small part of the data structure and extending it. homogeneous data structure (such as an array.v) where u and v are two consecutive vertices. Each edge in this sequence is checked to see whether it can be considered part of the tree under construction.E) where V be the vertex and E be the edges. 1. A Greedy Algorithm is a method for finding an optimal solution to some problem involving a large. if an edge is safe.

INDIVIDUAL ASSIGNMENT Algorithm/ Pseudo-codes of Kruskal’s Algorithm The running time of Kruskal‟s algorithm: The complexity of the algorithm is determined by the complexity of the sorting method applied and the method used for cycle detection. |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 . 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.

{9}.{7}.{4}.{9}.{3}.{3}.{11} Step-2: Sort all the edges in ascending order and creating a set INDIVIDUAL ASSIGNMENT ALGO Page 48 .{8}.{1}.{2}.{11}  Arrange the weight of edge in ascending order Step-1: create the set for all the vertices of the graph {0}.{5}.{2}.{5}.{1}.{10}.{8}.{6}.{7}.{4}.{10}.{6}.INDIVIDUAL ASSIGNMENT Execution steps for Kurskal’s Algorithm  Create the set of vertex o {0}.

4) has minimum weight and don‟t lie in same set so add it in set A and highlight edge A= (3. 5) doesn‟t lie in the same set add it in set A and highlight the edge Set:{ 0}. 5) having weight 2 (4. Set : {0}.{10}.{8}.{8}.{6}.{9}. 2.{1}.{7}.4.{10}.{7}.{3.{3.{9}.{7}. 3) having weight 2 (2.{11} INDIVIDUAL ASSIGNMENT ALGO Page 49 . Edge (3. 1.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.5}.{10}.{2}.{8}. 4).{2.5}.{5}.{1}.4.{6}. 3) doesn‟t lie in the same set add it in set A and highlight the edge Set:{ 0}.4}.{9}.{6}.{1}. If lie in one set form cycle otherwise not.3.{11} Step-4: Next minimum edge is (4.{11} Step-5: Next minimum edge is (2.

{11} INDIVIDUAL ASSIGNMENT ALGO Page 50 .{6. 4) having weight 3 (1. 7) having weight 3 (6.INDIVIDUAL ASSIGNMENT Step-6: Next minimum edge is (0.3. 1) doesn‟t lie in the same set add it in set A and highlight the edge Set:{ 0.4.7}.1}.{7}.{11} Step-7: Next minimum edge is (1.1.{8}.1.{10}.5}.{8}.{9}.2.3.{7}.{9}.{9}.{8}.3.4.5}.{6}.{10}.4.{10}. 4) doesn‟t lie in the same set add it in set A and highlight the edge Set:{ 0.{6}. 1) having weight 3 (0.{2.2.5}.{11} Step-8: Next minimum edge is (6. 7) doesn‟t lie in the same set add it in set A and highlight the edge Set:{ 0.

5. 9) doesn‟t lie in the same set add it in set A and highlight the edge Set:{ 0. 3) having weight 4 (0.5.{11} Step-11: Next minimum edge is (4.5}.{6.9}.{8 .{6.1.3.7}. 9) having weight 3 (8.4.INDIVIDUAL ASSIGNMENT Step-9: Next minimum edge is (8.{6. 8) having weight 4 (4.{10}.{10}. 8) doesn‟t lie in the same set add it in set A and highlight the edge Set:{ 0.2.3.3.7}.5}.4.1. 3) having weight 4 (0.3.1.4.{11} INDIVIDUAL ASSIGNMENT ALGO Page 51 .{11} Step-12: Next minimum edge is (0.7}.{6.2. 3) lay in the same set no need to add and highlight Set:{ 0.{11} Step-10: Next minimum edge is (0.1.9}.{10}.8.2.7}.8.9}.4.2.9}.{8 .{10}. 3) lay in the same set no need to add it in set A Set:{ 0.

{11} Step-16: Next minimum edge is (8.7}.8. 2) having weight 5 (2.{10} INDIVIDUAL ASSIGNMENT ALGO Page 52 .6) lie in the same set no need to add and highlight Set:{ 0.{10}.4.9.7}.1.6.3.6.9.7) lie in the same set no need to add and highlight Set:{ 0.6.{11} Step-15: Next minimum edge is (3. 11) doesn‟t lie in the same set add it in set A and highlight the edge Set:{ 0.{11} Step-14: Next minimum edge is (0.{10}.{10}.1.3.8.1.7}.6.4.5.INDIVIDUAL ASSIGNMENT Step-13: Next minimum edge is (2.3.6) doesn‟t lie in the same set add it in set A and highlight the edge Set:{ 0.2.2.8.8.1.11}.2.7. 7) having weight 5 (3.5.5.9.4.4. 6) having weight 4 (2.9.5. 11) having weight 5 (8.3.2.

3.11.11}. 10) doesn‟t lie in the same set add it in set A and highlight the edge Set:{ 0.{10} Step-19: Next minimum edge is (6. 9) lay in the same set no need to add and highlight Set:{ 0.{10} Step-18: Next minimum edge is (1.1.6. 9) having weight 5 (5.4.7.9.7.8.5. 5) lay in the same set no need to add and highlight Set:{ 0.4.8.7.3.8.5.9.2.5.6.INDIVIDUAL ASSIGNMENT Step-17: Next minimum edge is (5.11}.4.9.10} INDIVIDUAL ASSIGNMENT ALGO Page 53 .1.2. 5) having weight 6 (1. 10) having weight 6 (6.1.3.2.6.

2.10} Step-21: Next minimum edge is (7.4. 11) having weight 8 (10.1.8.8.5.9.INDIVIDUAL ASSIGNMENT Step-20: Next minimum edge is (7.3.1.6.10} INDIVIDUAL ASSIGNMENT ALGO Page 54 .7.5.6.2.4.4.2.11. 11) lie in the same set no need to add and highlight Set:{ 0.11.3.8.5.6.1. 10) having weight 7 (7. 8) having weight 6 (7.10} Step-22: Next minimum edge is (10.9.11.8 ) lie in the same set no need to add and highlight Set:{ 0.7.9. 10) lay in the same set no need to add and highlight Set:{ 0.3.7.

11. 9) lay in the same set no need to add and highlight Set:{ 0.6.2.1. INDIVIDUAL ASSIGNMENT ALGO Page 55 .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.8.7. 9) having weight 9 (11.9.INDIVIDUAL ASSIGNMENT Step-22: Next minimum edge is (11.3.5.4.

This means it finds a subset of the edges that forms a tree that includes every vertex.  „Q‟ is assumed to be Queue. P or p is assumed to be Parent or Predecessor of that node.INDIVIDUAL ASSIGNMENT Prim’s Algorithm Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a connected weighted undirected. where the total weight of all the edges in the tree is minimized Prim’s Algorithm is used to find a Minimum Spanning Tree rooted at a given vertex A Grow a MST by adding vertexes. Algorithm/ Pseudo-codes of Prim’s Algorithm The running time of Prim‟s algorithm:  The inner loop of the algorithm can be O(E) in the worst case. a vertex that has least distance to the growing tree is added. INDIVIDUAL ASSIGNMENT ALGO Page 56 . the inner loop may iterate O(|V||E|) in total Assumption of Prim’s  Initialize except source vertex all other vertexes with infinity distance and parent as NIL. and since the outer loop iterates |V|-1 times. In each step. D or d is assumed to be distance between two nodes.

INDIVIDUAL ASSIGNMENT  Vertex with „0‟ value is assumed to be the source node.  0.  Explored edges are marked with red bold line.  The graph is assumed to be undirected. and 3. 2. 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 7 9 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. Check minimum distance from deleted node of a graph INDIVIDUAL ASSIGNMENT ALGO Page 57 .2 etc written on nodes is the name of the node.1. Step wise solution of Prim‟s Algorithm Step 1: Initialize distance of all vertexes.

This statement is true then update the distance and predecessor 2. d > wt (0. This statement is true then update the distance and predecessor 3. 2. 3. d > wt (1. 1. 5) =>  >6 . d= 4.pred=0 2. 4. d> wt(0.pred = 0 3.d = 3 . 4) =>  > 3. d = 3 .Pred=1 5. 1) =>  > 3. 2) =>  >5 . This statement is true then update the distance and predecessor 1. d =5.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 7 9 10 P=NIL D=∞ 8 11 P=NIL D=∞ Step 3: Find neighbor of deleted element which is 4. This statement is true then update the distance and predecessor INDIVIDUAL ASSIGNMENT ALGO Page 58 . d> wt(1.INDIVIDUAL ASSIGNMENT 1. 3) =>  > 4. d> wt(0. This statement is true then update the distance and predecessor 1. 5 Check minimum distance from deleted node of a graph 4.

d =6. This statement is true then update the distance and predecessor 3. 5) => 6 > 2. d > wt (4. 3. 3) => 4 > 1.d = 4. 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 7 9 10 P=NIL D=∞ 8 11 P=NIL D=∞ Step 4: Find neighbor of deleted element which is 3. d > wt (4.Pred=4 5. 5.INDIVIDUAL ASSIGNMENT 2. This statement is true then update the distance and predecessor INDIVIDUAL ASSIGNMENT ALGO Page 59 . 8 Check minimum distance from deleted node of a graph 3.

d > wt (3. This statement is true then update the distance and predecessor 8.d = 4.Pred=3 7. 7) => ∞ >5.d = 2. 8.INDIVIDUAL ASSIGNMENT 5. 2) => 5> 2. d > wt (3. d > wt (4. 2. 5.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 7 9 10 P=NIL D=∞ 8 11 P=NIL D=∞ Step 5: Find neighbor of deleted element which is 2.Pred=4 8. 8) => ∞> 4. This statement is true then update the distance and predecessor 2. 7 Check minimum distance from deleted node of a graph 2.d = 2. This statement is true then update the distance and predecessor INDIVIDUAL ASSIGNMENT ALGO Page 60 .

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 7 9 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. This statement is true then update the distance and predecessor 9.Pred=5 INDIVIDUAL ASSIGNMENT ALGO Page 61 .d = 5. 9. 9) => ∞> 5. d > wt (5.INDIVIDUAL ASSIGNMENT 7.

6. This statement is true then update the distance and predecessor 6.d = 4. d > wt (2.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 7 9 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 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 5 Step 7: Find neighbor of deleted element which is 6 Check minimum distance from deleted node of a graph 6. 6) => ∞> 4.

9) => 5> 3.INDIVIDUAL ASSIGNMENT 7. 11. d > wt (9.d = 5. 7) => 5> 6. 9. 11) => ∞> 5. 11) => 5> 9.Pred=8 INDIVIDUAL ASSIGNMENT ALGO Page 63 .Pred=8 9. This statement is true then update the distance and predecessor 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 7 9 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. This statement is False then no need to update 7. 7. d > wt (8.d = 5. This statement is true then update the distance and predecessor 11.d = 5.d = 3.Pred=3 11. d > wt (8. d > wt (8. 11. This statement is False then 11.

d = 3.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 7 9 10 P=6 D=6 8 11 P=8 D=5 INDIVIDUAL ASSIGNMENT ALGO Page 64 . d > wt (6. 10) => ∞ > 6. This statement is true then update the distance and predecessor 7. d > wt (6.Pred=6 10. 7) => 5> 3.d = 6. This statement is true then update the distance and predecessor 10. 10. 7. 10 Check minimum distance from deleted node of a graph 7.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 5 Step 10: Find neighbor of deleted element which is 7.

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 7 9 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.Pred=6 10. INDIVIDUAL ASSIGNMENT ALGO Page 65 . d > wt (11. This statement is False then no need to update 10.d = 6.d = 6. d > wt (7. 10) => 6 > 7. This statement is False then no need to update 10.INDIVIDUAL ASSIGNMENT Step 11: Find neighbor of deleted element which is 10 Check minimum distance from deleted node of a graph 10.Pred=6 10. 10) => 6 >8.

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 .So no further traversal are required.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 5 Finally all the edges are deleted from queue and the queue becomes empty .

Prim‟s etc they are solvable in a given span of time.g. including linear programming and calculating the greatest common divisor. from all the algorithms which I have discussed above e.INDIVIDUAL ASSIGNMENT Problem Classes Problems are divided in to the different classes like P(Polynomial). Justification:. NP(non Polynomial). DFS. P is also known to be at least as large as L. From the graph given below we can easily say that the entire problem comes under P class. P is known to contain many natural problems.complete and NP. 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. BFS. NP. INDIVIDUAL ASSIGNMENT ALGO Page 67 .According to my research.Hard and many more. Polynomial class. Description and Justification of chosen class Selected class for these graphs is P class i. the class of problems decidable in a logarithmic amount of memory space. More over the following table summarizes why all the given algorithms are in P Class. That means we can found solution to the graph problems using these algorithms.e.

2009) To form a tree at every stage. the inner loop may iterate O(|V||E|) It is determined by the complexity of the in total. Space-saving. a new vertex is always added to an old node Nodes can be joined in any way. Time-saving. we can perform an EXTRAXTION-MIN operation in O(lg V) amortized time and a decrease-key operation in O(1) amortized time. A time to time check is required. that may risk in the formation of cycle. (Cormen. and since the outer loop iterates O(|E|lg|V|) |V|-1 times. 2009) INDIVIDUAL ASSIGNMENT ALGO Page 68 . Efficiency of this algorithm can be improved by Efficiency of this algorithm can be improved using the Fibonacci heap. which is asymptotically the same as for our implementation of Kruskal‟s algorithm. Hence. sorting method applied and the method used Thus the total time for Prim‟s algorithm is O(V for cycle detection lg V+ E lg V)=(E lg V). running time of Prim‟s algorithm improves to O(E+V lg V). (Cormen. If |V| elements are by using the sorting algorithms organized into a Fibonacci heap. from old to old or new to new.INDIVIDUAL ASSIGNMENT Comparison of Time complexities of algorithms with their analysis Prim’s Algorithm The inner loop of the algorithm can be O(E) in The Kruskal’s Algorithm complexity for the algorithm is the worst case.

with each iteration taking O(1) time i. It goes as deep as possible. in a weighted graph. there are a total of |E| iterations of the for loop. Total running time is Since the total number of edges in all O(V+E). It find the shortest path.INDIVIDUAL ASSIGNMENT BFS DFS Dijkstra’s It is appropriate to find the shortest path between two vertices in a weighted graph. algorithms as all the steps expect this is almost common in other algorithms. We search all the vertices in the graph to determine the lowest cost route between each point. constant. INDIVIDUAL ASSIGNMENT ALGO Page 69 . Most critical drawback is huge space requirement.e. Most critical drawback is huge and unpredictable time complexity Efficiency of this algorithm can be improved by using the sorting It doesn‟t require extra knowledge. Therefore. the adjacency lists is |E|. but not Doesn‟t find the shortest suitable to find the shortest path path. In this algorithm if the sorting method is applied in best way. Total running time is O (V+ E). Total running time is O (|V|2+|E|) =O (|V|2) It goes as broad as possible at each node.

html FIGURE 1GRAPH THEORY PROBLEM (FOR BFS.... T................ Algorithm.........nist..... Retrieved 10 10....... 10 TABLE 2 DIFFERENCE B/W ADJACENCY LIST AND ADJACENCY MATRIX .........html Rouse.............................wolfram....... September)..... Sparse Graph............ ltd........ from WhatIs........... from NIST: http://xlinux...... Retrieved 10 10...... E.. 6 FIGURE 2 GRAPH THEORY PROBLEM (MST) ............... E......................................... 2012...... PEB........................ DFS & DIJKSTRA’S SSSP) . Retrieved October 18.. from MathWorld-A Wolfram Web Resource: http://mathworld........... 2012... (2005..........................techtarget.. Mimimum Spanning tree.. 2012. H.............. (2008....... August 14)...........................gov/dads/HTML/sparsegraph.......... Leiserson. PHI Learning Pvt.... 570........com: http://whatis............................. 6 FIGURE 4 ADJACENCY LIST SOLUTION FOR GRAPH AT FIGURE1 ... M.............. Graph theory.........INDIVIDUAL ASSIGNMENT REFRENCES Cormen..... W.............. In C.com/definition/graph-theory Weisstein.... Introduction to Algorithms (pp...com/Algorithm............ (2009)....................................573)......... September 6)...... 11 INDIVIDUAL ASSIGNMENT ALGO Page 70 . (2012.. 9 TABLE 1 ADJACENCY MATRIX SOLUTION ...........................

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer: Get 4 months of Scribd and The New York Times for just $1.87 per week!

Master Your Semester with a Special Offer from Scribd & The New York Times