algo1 final durgesh print.pdf

Upload: karan-babbar

Post on 14-Apr-2018

243 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    1/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 1

    INDIVIDUAL ASSIGNMENT

    ALGORITHMICS (CE00333-3)

    HAND OUT DATE: 22nd August 2012

    HAND IN DATE: 23rd

    October 2012

    STUDENT NAME MODULE LECTURER

    DURGESH KUMAR (PT0981128) Ms. Kriti Arora

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    2/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 2

    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 SUBMITTED TO

    Durgesh Kumar (pt0981128) Ms. Kriti Arora

    (Module Lecturer)

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    3/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 3

    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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    4/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 4

    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 ......................................................................... 47Execution 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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    5/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 5

    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 sameas (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 graphconnecting 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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    6/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 6

    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 acrossa country, while the edges represent the oil pipelines running between them. Theweights of each edge represent the distance between each point.

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    7/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 7

    We have to provide a solution for the GRAPH THEORY problem mentionedabove 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 withtheir 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 haveto 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 lessAdjacency 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 inorder to find a path or a particular node. Traversal continues until the last node is

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    8/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 8

    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, treestructure, 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 minimumspanning 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 fora connected weighted graph.

    More about Prisms algorithm is described in the Prims algorithm section givenbelow.

    Dijkstra Algorithms

    Shortest path between two nodes means from source to destination which path ismost efficient

    Always takes the shortest edge connecting a known node to an unknown node.

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    9/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 9

    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 nodesto which a node is connected. It is preferred when the graph is sparse (Agraph in

    which the number ofedges 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

    C

    B

    J

    G

    I

    H

    F

    E

    D

    \

    \

    v

    3B

    D

    C

    D

    C

    G

    C

    F

    4

    1

    5

    4

    7

    2

    1

    E

    F

    J

    F

    J

    I

    1

    3

    1

    6

    2

    2

    H

    J

    H

    5

    2

    3 H 6

    \

    \

    /

    \

    \

    \

    \

    \

    Figure 3 Adjacency list solution for Graph at Figure1

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    10/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 10

    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 10Matrix.

    One simple way to facilitate this is to store a VxV matrix (where V is thenumber 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 noconnection exists when the graph has no edge weight on it.

    For weighted graph this entry can store the edge weight between the twovertices. 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 betweenany two given vertices. This operation takes O (1) time.

    Table 1 Adjacency Matrix solution

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    11/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 11

    Analysis of both solutions (Adjacency list & Adjacency Matrix)

    For both directed andundirected graphs, the

    adjacency list representation

    has the desirable property that

    the amount of memory it

    requires is O(V+E)

    For both directed andundirected graphs, the

    adjacency matrix

    representation requires memory

    of O(|V|2).

    The benefit ofusing 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 plus side is that adjacencymatrices 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.

    The space complexityof adjacency list is O(V+E).

    The space complexity ofadjacency matrix is O(|V|2).

    The downside of adjacency listis 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.

    Whether or not an edge is presentbetween 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 muchless then V*V, meaning the

    graph is sparse, this consumes

    very less amount of memory

    space.

    If total number of edges is muchless then V*V, meaning the graph

    is sparse, this consumes lots of

    extra unnecessary space.

    Table 2 Difference b/w Adjacency list and Adjacency Matrix

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    12/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 12

    Breadth First Search

    Ingraph theory,breadth-first search(BFS) is agraph search algorithmthat begins at the

    rootnodeand 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.

    http://en.wikipedia.org/wiki/FIFOhttp://en.wikipedia.org/wiki/Queue_(data_structure)http://en.wikipedia.org/wiki/Queue_(data_structure)http://en.wikipedia.org/wiki/FIFO
  • 7/27/2019 ALGO1 final Durgesh print.pdf

    13/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 13

    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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    14/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 14

    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.

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    15/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 15

    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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    16/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 16

    According to First In First Out algorithms E is de-queue and searching its neighboursStep 4

    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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    17/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 17

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

    {(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

    Step 7

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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    18/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 18

    {(I,J)} found and added to queue Node I becomes black According to FIFO algorithm D is de-queue and searching its neighboursStep 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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    19/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 19

    {(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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    20/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 20

    {(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 UNEXPLOREDo 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 eedges takes O(v+ e) time Standard complexity of given graph problemO (|v| + |e|) = O (v+e). Find the information of a path with the minimum number of edges between two

    given vertices

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    21/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 21

    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 explored, the search

    backtracksto 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 itsstate. 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 DFS-tree.

    A cross edge is any other edge in graph G. It connects vertices in two different DFS-tree or two vertices in the same DFS-tree neither of which is the ancestor of the other.

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    22/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 22

    Algorithms/Pseudo-codes of Depth First Search

    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 timespent 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

    (V).

    (E).

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    23/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 23

    Execution Steps for Depth First Search

    Source Graph

    A

    B C D

    E F G

    H IJ

    B.p=Nil C.p=Nil D.p=Nil

    G.p=Nil

    J.p=NilI.p=NilH.p=Nil

    A.p=Nil

    E.p=NilF.p=Nil

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

    Step-1

    A

    B CD

    E F G

    H IJ

    1

    B.p=Nil C.p=Nil D.p=Nil

    G.p=Nil

    J.p=NilI.p=NilH.p=Nil

    A.p=Nil

    E.p=NilF.p=Nil

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    24/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 24

    Step-2

    There is an edge (A, B) so make vertex B as gray. Now B.pred=A

    2

    A

    B CD

    E F G

    H IJ

    1

    B.p=A C.p=Nil D.p=Nil

    G.p=Nil

    J.p=NilI.p=NilH.p=Nil

    A.p=Nil

    E.p=NilF.p=Nil

    Step-3

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

    32

    A

    B CD

    E F G

    H IJ

    1

    B.p=A C.p=B D.p=Nil

    G.p=Ni

    l

    J.p=NilI.p=NilH.p=Nil

    A.p=Nil

    E.p=NilF.p=Nil

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    25/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 25

    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

    32

    4

    A

    B CD

    E F G

    H IJ

    1

    B.p=A C.p=B D.p=Nil

    G.p=Nil

    J.p=NilI.p=NilH.p=Nil

    A.p=NilE.p=C

    F.p=Nil

    Step-5

    There is an edge (C, H) make the vertex C as gray.

    H.pred=E

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    C.p=B D.p=Nil

    G.p=Nil

    J.p=NilI.p=NilH.p=E

    A.p=Nil

    E.p=CF.p=Nil

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    B.p=A

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    26/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 26

    Step-6

    There is an edge (H,I) make the vertex C as gray

    I.pred=H

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    B.p=A

    G.p=Nil

    J.p=NilI.p=HH.p=E

    A.p=Nil

    F.p=Nil

    32

    4

    65

    A

    B CD

    E F G

    H IJ

    1

    C.p=B D.p=Nil

    E.p=C

    Step-7

    There is an edge (I,J) make the vertex J as grayJ.pred=I

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    B.p=A D.p=Nil

    G.p=Nil

    I.p=HH.p=E

    A.p=Nil

    F.p=Nil

    32

    4

    65 7

    A

    B CD

    E F G

    H IJ

    1

    C.p=B

    J.p=I

    E.p=C

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    27/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 27

    Step-8

    There is an edge (J,F) make the vertex F as gray

    F.pred=J

    32

    4

    5

    A

    B C D

    E F G

    H IJ

    1

    B.p=A D.p=Nil

    G.p=Nil

    I.p=HH.p=E

    A.p=Nil

    32

    84

    65 7

    A

    B C D

    E F G

    H IJ

    1

    C.p=B

    J.p=I

    E.p=C

    F.p=J

    Step-9

    There is an edge (F,D ) make the vertex D as gray

    D.pred=F

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    B.p=A

    G.p=Nil

    I.p=HH.p=E

    A.p=Nil

    32 9

    84

    65 7

    A

    B CD

    E F G

    H IJ

    1

    C.p=B D.p=F

    J.p=I

    E.p=C

    F.p=J

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    28/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 28

    Step-10

    There is an edge (D,G) make the vertex G as gray

    G.pred=D

    32

    4

    5

    A

    B C D

    E F G

    H IJ

    1

    B.p=A

    I.p=HH.p=E

    A.p=Nil

    32 9

    84 10

    65 7

    A

    B C D

    E F G

    H IJ

    1

    C.p=B D.p=F

    G.p=D

    J.p=I

    E.p=C

    F.p=J

    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.

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    B.p=A

    I.p=HH.p=E

    A.p=Nil

    32 9

    84 10

    65 7

    A

    B CD

    E F G

    H IJ

    1

    C.p=B D.p=F

    G.p=D

    J.p=I

    E.p=C

    F.p=J

    10/11

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    29/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 29

    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

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    B.p=A

    I.p=HH.p=E

    A.p=Nil

    32 9

    84 10

    65 7

    A

    B CD

    E F G

    H IJ

    1

    C.p=B D.p=F

    G.p=D

    J.p=I

    E.p=C

    F.p=J

    10/11

    9/

    12

    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

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    B.p=A

    I.p=HH.p=E

    A.p=Nil

    32 9

    84 10

    65 7

    A

    B CD

    E F G

    H IJ

    1

    C.p=B D.p=F

    G.p=D

    J.p=I

    E.p=C

    F.p=J

    10/11

    9/

    12

    8/13

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    30/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 30

    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

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    B.p=A

    I.p=HH.p=E

    A.p=Nil

    32 9

    84 10

    65 7

    A

    B CD

    E F G

    H IJ

    1

    C.p=B D.p=F

    G.p=D

    J.p=I

    E.p=C

    F.p=J

    10/11

    9/12

    8/13

    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

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    B.p=A

    I.p=HH.p=E

    A.p=Nil

    32 9

    84 10

    65 7

    A

    B CD

    E F G

    H IJ

    1

    C.p=B D.p=F

    G.p=D

    J.p=I

    E.p=C

    F.p=J

    10/11

    9/12

    8/13

    7/146/15

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    31/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 31

    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

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    B.p=A

    I.p=HH.p=E

    A.p=Nil

    32 9

    84 10

    65 7

    A

    B CD

    E F G

    H IJ

    1

    C.p=B D.p=F

    G.p=D

    J.p=I

    E.p=C

    F.p=J

    10/11

    9/12

    8/13

    7/146/155/16

    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

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    B.p=A

    I.p=HH.p=E

    A.p=Nil

    32 9

    84 10

    65 7

    A

    B CD

    E F G

    H IJ

    1

    C.p=B D.p=F

    G.p=D

    J.p=I

    E.p=C

    F.p=J

    10/11

    9/12

    8/13

    7/146/155/16

    4/17

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    32/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 32

    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

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    B.p=A

    I.p=HH.p=E

    A.p=Nil

    32 9

    84 10

    65 7

    A

    B CD

    E F G

    H IJ

    1

    C.p=B D.p=F

    G.p=D

    J.p=I

    E.p=C

    F.p=J

    10/11

    9/12

    8/13

    7/146/155/16

    4/17

    3/18

    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

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    B.p=A

    I.p=HH.p=E

    A.p=Nil

    32 9

    84 10

    65 7

    A

    B CD

    E F G

    H IJ

    1

    C.p=B D.p=F

    G.p=D

    J.p=I

    E.p=C

    F.p=J

    10/11

    9/12

    8/13

    7/146/155/16

    4/17

    3/18

    2/19

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    33/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 33

    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

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    B.p=A

    I.p=HH.p=E

    A.p=Nil

    32 9

    84 10

    65 7

    A

    B CD

    E F G

    H IJ

    1

    C.p=B D.p=F

    G.p=D

    J.p=I

    E.p=C

    F.p=J

    10/11

    9/12

    8/13

    7/146/155/16

    4/17

    3/18

    2/19

    1/20

    Step-21

    Final structure of Depth first search from source vertex to destination G

    32

    4

    5

    A

    B CD

    E F G

    H IJ

    1

    B.p=A

    I.p=HH.p=E

    A.p=Nil

    32 9

    84 10

    65 7

    A

    B CD

    E F G

    H IJ

    1

    C.p=B D.p=F

    G.p=D

    J.p=I

    E.p=C

    F.p=J

    10/11

    9/12

    8/13

    7/146/155/16

    4/17

    3/18

    2/19

    1/20

    Note: the solution may vary according to user.

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    34/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 34

    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

    forbreadth-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.

    http://en.wikipedia.org/wiki/Breadth-first_searchhttp://en.wikipedia.org/wiki/Breadth-first_search
  • 7/27/2019 ALGO1 final Durgesh print.pdf

    35/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 35

    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

    0

    4 1

    1

    6 3

    4

    3

    52

    1

    7

    32

    42 5

    1

    A

    B CD

    E

    FG

    H I J

    B.p=Nil C.p=Nil D.p=Nil

    G.p=Nil

    J.p=NilI.p=NilH.p=Nil

    A.p=Nil

    E.p=NilF.p=Nil

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    36/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 36

    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 thesource 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.

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    37/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 37

    Use ofDijkstras algorithm to solve the problem with description

    Step-1:

    Initially Z is an empty set Z= {}

    0

    4 1

    1

    6 3

    4

    3

    52

    1

    7

    32

    42 5

    1

    A

    B CD

    E

    FG

    H I J

    B.p=Nil C.p=Nil D.p=Nil

    G.p=Nil

    J.p=NilI.p=NilH.p=Nil

    A.p=Nil

    E.p=NilF.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.

    Z= {A}

    Step-2:

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    38/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 38

    0

    3

    1

    5

    4 1

    1

    6 3

    4

    3

    52

    1

    7

    32

    42 5

    1

    A

    B CD

    E

    F G

    H I J

    B.p=A C.p=Nil D.p=Nil

    G.p=Nil

    J.p=NilI.p=NilH.p=A

    A.p=Nil

    E.p=AF.p=Nil

    Step-3

    0

    23

    1

    43

    4 1

    1

    6 3

    4

    3

    52

    1

    7

    32

    42 5

    1

    A

    B CD

    E

    FG

    H I J

    B.p=A C.p=E D.p=Nil

    G.p=Nil

    J.p=NilI.p=EH.p=E

    A.p=NilE.p=A

    F.p=Nil

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    39/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 39

    Step-4

    0

    23 3

    61

    43

    4 1

    1

    6 3

    4

    3

    52

    1

    7

    32

    4 2 5

    1

    A

    B CD

    E

    F G

    H I J

    B.p=A C.p=E D.p=C

    G.p=Nil

    J.p=NilI.p=EH.p=E

    A.p=Nil

    E.p=AF.p=C

    Step-5

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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    40/70

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    41/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 41

    Step-7

    0

    23 3

    51

    43 7

    4 1

    1

    6

    4

    3

    52

    1

    7

    32

    42

    1

    6

    A

    B CD

    E

    F G

    H I J

    B.p=A C.p=E D.p=C

    G.p=Nil

    J.p=NilI.p=EH.p=E

    A.p=Nil

    E.p=AF.p=D

    5

    3

    Step-8

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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    42/70

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    43/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 43

    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

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

    0

    23 3

    51 9

    43 7

    4 1

    1

    6 3

    4

    3

    52

    1

    7

    32

    4 2

    1

    6

    A

    B CD

    EF G

    H I J

    B.p=A C.p=E D.p=C

    G.p=F

    J.p=II.p=EH.p=E

    A.p=Nil

    E.p=A F.p=D

    5

    Step-11

    Queue G

    Predecessor F

    Distance 9

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    44/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 44

    0

    23 3

    51 9

    43 7

    4 1

    1

    6 3

    4

    3

    52

    1

    7

    32

    42

    1

    6

    A

    B CD

    E

    FG

    H I J

    B.p=A C.p=E D.p=C

    G.p=F

    J.p=II.p=EH.p=E

    A.p=Nil

    E.p=AF.p=D

    5

    Now Q is empty so the final shortest path is

    0

    23 3

    51 9

    43 7

    1

    1

    3

    4

    3

    2

    1

    3

    2

    A

    B CD

    E

    F G

    H I J

    B.p=A C.p=E D.p=C

    G.p=F

    J.p=II.p=EH.p=E

    A.p=Nil

    E.p=AF.p=D

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    45/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 45

    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) ofabove 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 withour 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.

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    46/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 46

    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 weight2. 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 inclusion4. 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.

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    47/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 47

    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)

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    48/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 48

    Execution steps for Kurskals Algorithm

    Create the set of vertexo {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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    49/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 49

    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 insame 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}

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    50/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 50

    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}

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    51/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 51

    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}

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    52/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 52

    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}

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    53/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 53

    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}

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    54/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 54

    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}

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    55/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 55

    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.

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    56/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 56

    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 formsa 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 outerloop 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 parentas 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.

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    57/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 57

    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

    0

    7

    5432

    1

    98

    10

    6

    11

    3

    5

    4

    2 1

    36

    2

    4 5

    3

    4

    6

    5

    3

    6

    7 5

    8

    9

    P=NIL

    D=

    P=NIL

    D=

    P=NIL

    D=

    P=NIL

    D=

    P=NIL

    D=

    P=NIL

    D=

    P=NIL

    D=

    P=NILD=

    P=NIL

    D=

    P=NILD=

    P=NIL

    D=

    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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    58/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 58

    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

    0

    7

    5432

    1

    98

    10

    6

    11

    3

    5

    4

    2 1

    3

    6

    2

    4 5

    3

    4

    6

    5

    3

    6

    7 5

    8

    9

    P=0

    D=5

    P=NIL

    D=

    P=NIL

    D=

    P=0

    D=4

    P=0

    D=3

    P=NIL

    D=0

    P=NIL

    D=

    P=NIL

    D=

    P=NIL

    D=

    P=NIL

    D=

    P=NIL

    D=

    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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    59/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 59

    2.d =6, 5.pred = 1

    0

    7

    5432

    1

    98

    10

    6

    11

    3

    5

    4

    2 1

    36

    2

    4 5

    3

    4

    6

    5

    3

    6

    7 5

    8

    9

    P=0D=5

    P=1D=6

    P=1

    D=3

    P=0

    D=4

    P=0D=3

    P=NILD=0

    P=NILD=

    P=NIL

    D=

    P=NILD=

    P=NIL

    D=

    P=NILD=

    P=NILD=

    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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    60/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 60

    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

    0

    7

    5432

    1

    98

    10

    6

    11

    3

    5

    4

    2 1

    36

    2

    4 5

    3

    4

    6

    5

    3

    6

    7 5

    8

    9

    P=0

    D=5

    P=4

    D=2

    P=1D=3

    P=4D=1

    P=0

    D=3

    P=NIL

    D=0

    P=NILD=

    P=NIL

    D=

    P=NILD=

    P=NIL

    D=

    P=4D=4

    P=NILD=

    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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    61/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 61

    7.d = 5, 7.Pred=3

    0

    7

    5432

    1

    98

    10

    6

    11

    3

    5

    4

    2 1

    3

    6

    2

    4 5

    3

    4

    6

    5

    3

    6

    7 5

    8

    9

    P=3

    D=2

    P=4

    D=2

    P=1

    D=3

    P=4

    D=1

    P=0

    D=3

    P=NIL

    D=0

    P=NIL

    D=

    P=NIL

    D=

    P=NIL

    D=

    P=NIL

    D=

    P=4

    D=4

    P=3

    D=5

    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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    62/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 62

    0

    7

    5432

    1

    98

    10

    6

    11

    3

    5

    4

    2 1

    3

    6

    2

    4 5

    3

    4

    6

    5

    3

    6

    7 5

    8

    9

    P=3

    D=2

    P=4

    D=2

    P=1

    D=3

    P=4

    D=1

    P=0

    D=3

    P=NIL

    D=0

    P=5

    D=5

    P=NIL

    D=

    P=NIL

    D=

    P=NIL

    D=

    P=4

    D=4

    P=3

    D=5

    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

    0

    7

    5432

    1

    98

    10

    6

    11

    3

    5

    4

    2 1

    3

    6

    2

    4 5

    3

    4

    6

    5

    3

    6

    7 5

    8

    9

    P=3

    D=2

    P=4

    D=2

    P=1

    D=3

    P=4

    D=1

    P=0

    D=3

    P=NIL

    D=0

    P=5

    D=5

    P=NIL

    D=

    P=2

    D=4

    P=NIL

    D=

    P=4

    D=4

    P=3

    D=5

    Step 8: Find neighbor of deleted element which is 7, 11, 9

    Check minimum distance as

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    63/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 63

    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

    0

    7

    5432

    1

    98

    10

    6

    11

    3

    5

    4

    2 1

    3

    6

    2

    4 5

    3

    4

    6

    5

    3

    6

    7 5

    8

    9

    P=3

    D=2

    P=4

    D=2

    P=1

    D=3

    P=4

    D=1

    P=0

    D=3

    P=NIL

    D=0

    P=8

    D=3

    P=8

    D=5

    P=2

    D=4

    P=NIL

    D=

    P=4

    D=4

    P=3

    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

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    64/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 64

    0

    7

    5432

    1

    98

    10

    6

    11

    3

    5

    4

    2 1

    3

    6

    2

    4 5

    3

    4

    6

    5

    3

    6

    7 5

    8

    9

    P=3

    D=2

    P=4

    D=2

    P=1

    D=3

    P=4

    D=1

    P=0

    D=3

    P=NIL

    D=0

    P=8

    D=3

    P=8

    D=5

    P=2

    D=4

    P=NIL

    D=

    P=4

    D=4

    P=3

    D=5

    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

    0

    7

    5432

    1

    98

    10

    6

    11

    3

    5

    4

    2 1

    3

    6

    2

    4 5

    3

    4

    6

    5

    3

    6

    7 5

    8

    9

    P=3

    D=2

    P=4

    D=2

    P=1

    D=3

    P=4

    D=1

    P=0

    D=3

    P=NIL

    D=0

    P=8

    D=3

    P=8

    D=5

    P=2

    D=4

    P=6

    D=6

    P=4

    D=4

    P=6

    D=3

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    65/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 65

    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.d = 6,

    10.Pred=6

    0

    7

    5432

    1

    98

    10

    6

    11

    3

    5

    4

    2 1

    3

    6

    2

    4 5

    3

    4

    6

    5

    3

    6

    7 5

    8

    9

    P=3

    D=2

    P=4

    D=2

    P=1

    D=3

    P=4

    D=1

    P=0

    D=3

    P=NIL

    D=0

    P=8

    D=3

    P=8

    D=5

    P=2

    D=4

    P=6

    D=6

    P=4

    D=4

    P=6

    D=3

    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.d = 6,

    10.Pred=6

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    66/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 66

    0

    7

    5432

    1

    98

    10

    6

    11

    3

    5

    4

    2 1

    3

    6

    2

    4 5

    3

    4

    6

    5

    3

    6

    7 5

    8

    9

    P=3

    D=2

    P=4

    D=2

    P=1

    D=3

    P=4

    D=1

    P=0

    D=3

    P=NIL

    D=0

    P=8

    D=3

    P=8

    D=5

    P=2

    D=4

    P=6

    D=6

    P=4

    D=4

    P=6

    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

    0

    7

    5432

    1

    98

    10

    6

    11

    3

    2 1

    3

    2

    4

    3

    4

    3

    6

    5

    P=3

    D=2

    P=4

    D=2

    P=1

    D=3

    P=4

    D=1

    P=0

    D=3

    P=NIL

    D=0

    P=8

    D=3

    P=8

    D=5

    P=2

    D=4

    P=6

    D=6

    P=4

    D=4

    P=6

    D=3

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    67/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 67

    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.

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    68/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 68

    Comparison of Time complexities of algorithms with their

    analysis

    Prims Algorithm Kruskals 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.

    Thus the total time for Prims algorithm is O(V

    lg V+ E lg V)=(E lg V), which is asymptotically

    the same as for our implementation of Kruskals

    algorithm. (Cormen, 2009)

    The complexity for the algorithm is

    O(|E|lg|V|)

    It is determined by the complexity of the

    sorting method applied and the method used

    for cycle detection

    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

    using the Fibonacci heap. If |V| elements are

    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)

    Efficiency of this algorithm can be improved

    by using the sorting algorithms

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    69/70

    INDIVIDUAL ASSIGNMENT

    INDIVIDUAL ASSIGNMENT ALGO Page 69

    BFS DFS Dijkstras

    It find the shortest path, but not

    suitable to find the shortest path

    in a weighted graph.

    Doesnt find the shortest

    path.

    It is appropriate to find the shortest

    path between two vertices in a

    weighted graph.Total running time is O (V+ E). Total running time is

    O(V+E).

    Since the total number of edges in all

    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

    It doesnt require extra knowledge.

    Efficiency of this algorithm can beimproved by using the sorting

    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.

  • 7/27/2019 ALGO1 final Durgesh print.pdf

    70/70

    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 2GRAPH THEORY PROBLEM (MST) ................................................................................................................ 6

    FIGURE 4ADJACENCY LIST SOLUTION FOR GRAPH AT FIGURE1 .......................................................................................... 9

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