BFSand DFS

Embed Size (px)

Citation preview

  • 8/8/2019 BFSand DFS

    1/66

    Elementary Graph Algorithm

    Breadth First Search(BFS)

    AND

    Depth First Search(DFS)

    By :-

    Prithviraj MohantyLect.EAST,Bhubaneswar

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    2/66

    Review: Graph searching

    [email protected] 10/11/2009

    Simplest searching algorithm.

    Application:-Prim`s Algorithm: for findingminimum spanning tree andDijkstra`s singlesource shortest path algorithm.It finds the source vertex s first and discovers

    all vertex rechable from s.

    It discovers all vertices at distance k from sbefore discovering vertices at distance k+1

  • 8/8/2019 BFSand DFS

    3/66

    Review: Graph Searching

    Given: a graph G = (V, E), directed or

    undirected

    Goal: methodically explore every vertex andevery edge

    Ultimately: build a tree on the graph

    Pick a vertex as the root

    Choose certain edges to produce a tree

    Note: might also build aforestif graph is not

    connected

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    4/66

    Review: Breadth-First Search

    Explore a graph, turning it into a tree

    One vertex at a time

    Expand frontier of explored vertices across the

    breadth of the frontier

    Builds a tree over the graph

    Pick asource vertex to be the root

    Find (discover) its children, then their children,

    etc.

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    5/66

    Review: Breadth-First Search

    Again will associate vertex colors to guide

    the algorithm

    White vertices have not been discovered

    All vertices start out white

    Gray vertices are discovered but not fully explored

    They may be adjacent to white vertices

    Black vertices are discovered and fully explored They are adjacent only to black and gray vertices

    Explore vertices by scanning adjacency list of

    gray [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    6/66

    BFS - algorithm

    [email protected] 10/11/2009

    BFS(G, s) // G is the graph and s is the starting node

    1 for each vertex u V [G] - {s}

    2 do color[u] WHITE // color of vertex u

    3 d[u] // distance from source s to vertex u

    4 [u] NIL // predecessor of u

    5 color[s] GRAY

    6 d[s] 0

    7 [s] NIL

    8 Q // Q is a F IFO - queue

  • 8/8/2019 BFSand DFS

    7/66

    BFS algorithm contd..

    [email protected] 10/11/2009

    9 ENQUEUE(Q, s)

    10 while Q // iterates as long as there are gray vertices. Lines 10-18

    11 do u DEQUEUE(Q)

    12 for each vAdj[u]

    13 do if color[v] = WHITE // discover the undiscovered adjacentvertices

    14 then color[v] GRAY // enqueued whenever painted

    gray15 d[v] d[u] + 1

    16 [v] u

    17 ENQUEUE(Q, v)18 color[u] BLACK // painted black whenever dequeued

  • 8/8/2019 BFSand DFS

    8/66

    Breadth-First Search: Example

    g

    g

    g

    g

    g

    g

    g

    g

    r s t u

    v w x y

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    9/66

    Breadth-First Search: Example

    g

    g

    0

    g

    g

    g

    g

    g

    r s t u

    v w x y

    sQ:

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    10/66

    Breadth-First Search: Example

    1

    g

    0

    1

    g

    g

    g

    g

    r s t u

    v w x y

    wQ: r

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    11/66

    Breadth-First Search: Example

    1

    g

    0

    1

    2

    2

    g

    g

    r s t u

    v w x y

    rQ: t x

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    12/66

    Breadth-First Search: Example

    1

    2

    0

    1

    2

    2

    g

    g

    r s t u

    v w x y

    Q: t x v

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    13/66

  • 8/8/2019 BFSand DFS

    14/66

    Breadth-First Search: Example

    1

    2

    0

    1

    2

    2

    3

    3

    r s t u

    v w x y

    Q: v u y

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    15/66

    Breadth-First Search: Example

    1

    2

    0

    1

    2

    2

    3

    3

    r s t u

    v w x y

    Q: u y

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    16/66

    Breadth-First Search: Example

    1

    2

    0

    1

    2

    2

    3

    3

    r s t u

    v w x y

    Q: y

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    17/66

    Breadth-First Search: Example

    1

    2

    0

    1

    2

    2

    3

    3

    r s t u

    v w x y

    Q:

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    18/66

    Breadth first search - analysis

    [email protected] 10/11/2009

    Enqueue and Dequeue happen only once

    for each node. - O(V).

    Sum of the lengths of adjacency lists (E) (for adirected graph) and (2E) (for an undirected graph)

    Initialization overhead - O(V)

    Total runtime -O(V+E)

  • 8/8/2019 BFSand DFS

    19/66

    Breadth-First Search: Properties

    BFS calculates theshortest-path distance to

    the source node

    Shortest-path distance

    H

    (s,v) = minimum numberof edges from s to v, org if v not reachable from s

    Proof given in the book

    BFS builds breadth-first tree, in which paths to

    root represent shortest paths in G

    Thus can use BFS to calculate shortest path from

    one vertex to another in O(V+E) time

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    20/66

    Breadth-First Search: Properties

    contd

    Lemma:1.1-Let G=(V,E) be a directed or undirectedgraph and let s V be an arbitary vertex.Then for any

    edge (u,v) E, H(s,v) H(s,u)+1 Lemma:1.2-Let G=(V,E) be a directed or undirected

    and suppose that BFS is run on G from a given sourc

    vertex s V.Then upon termination ,for each vertex

    vV,then value d[v] computed by BFS satisfiesd[v] H(s,v)

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    21/66

    Depth-First Search

    It searches deeper the graph when possible.

    Starts at the selected node and explores as far aspossible along each branch before backtracking.

    Vertices go through white, gray and black stages ofcolor.

    White initially

    Gray when discovered first

    Black when finished i.e. the adjacency list of thevertex is completely examined.

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    22/66

    Depth-First Search contd.

    Also records timestamps for each vertex

    d[v] when the vertex is first discovered

    f[v] when the vertex is finished

    T hese timestamps are integers between 1 and2V,since there is one discovery event and onefinishing event for each vertex in V.So forevery vertx v ,d[v]

  • 8/8/2019 BFSand DFS

    23/66

    Depth-First Search: algorithm

    DFS(G)

    {

    for each vertex u G->V

    {

    u->color = WHITE;}

    time = 0;

    for each vertex u G->V

    {

    if (u->color == WHITE)

    DFS_Visit(u);

    }

    }

    DFS_Visit(u)

    {

    u->color = GREY;

    time = time+1;

    u->d = time;

    for each v u->Adj[]

    {

    if (v->color == WHITE)

    DFS_Visit(v);

    }

    u->color = BLACK;time = time+1;

    u->f = time;

    }

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    24/66

    Depth-First Search: algorithm

    DFS(G)

    1 for each vertex u V [G]2 do color[u] WHITE // color all

    vertices white, set their parents NIL

    3 [u] NIL4 time 0 // zero out time5 for each vertex u V [G] // call only for

    unexplored vertices

    6 do if color[u] = WHITE // this may resultin multiple sources

    7 then DFS-VISIT(u)

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    25/66

    Depth-First Search: algorithm

    contd..

    DFS-VISIT(u)

    1 color[u] GRAY White vertex u has just beendiscovered.

    2 time time +13 d[u] time // record the discovery time4 for each v Adj[u] Explore edge(u, v).

    5 do if color[v] = WHITE

    6 then [v] u // set the parent value

    7 DFS-VISIT(v) // recursive call

    8 color[u] BLACK Blacken u; it is finished.

    9 f [u]

  • 8/8/2019 BFSand DFS

    26/66

    Depth-First Search: The Code

    DFS(G)

    {

    for each vertex u G->V

    {

    u->color = WHITE;}

    time = 0;

    for each vertex u G->V

    {

    if (u->color == WHITE)

    DFS_Visit(u);

    }

    }

    DFS_Visit(u)

    {

    u->color = GREY;

    time = time+1;

    u->d = time;

    for each v u->Adj[]

    {

    if (v->color == WHITE)

    DFS_Visit(v);

    }

    u->color = BLACK;time = time+1;

    u->f = time;

    }

    What does u->f [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    27/66

    Depth-First Search: The Code

    DFS(G)

    {

    for each vertex u G->V

    {

    u->color = WHITE;}

    time = 0;

    for each vertex u G->V

    {

    if (u->color == WHITE)

    DFS_Visit(u);

    }

    }

    DFS_Visit(u)

    {

    u->color = GREY;

    time = time+1;

    u->d = time;

    for each v u->Adj[]

    {

    if (v->color == WHITE)

    DFS_Visit(v);

    }

    u->color = BLACK;time = time+1;

    u->f = time;

    }

    Willall

    vertic

    es eventuall

    y becolored b

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    28/66

    Depth-First Search: The Code

    DFS(G)

    {

    for each vertex u G->V

    {

    u->color = WHITE;}

    time = 0;

    for each vertex u G->V

    {

    if (u->color == WHITE)

    DFS_Visit(u);

    }

    }

    DFS_Visit(u)

    {

    u->color = GREY;

    time = time+1;

    u->d = time;

    for each v u->Adj[]

    {

    if (v->color == WHITE)

    DFS_Visit(v);

    }

    u->color = BLACK;time = time+1;

    u->f = time;

    }

    What w

    illbe t

    he runn

    ing t

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    29/66

    Depth-First Search: The Code

    DFS(G)

    {

    for each vertex u G->V

    {

    u->color = WHITE;}

    time = 0;

    for each vertex u G->V

    {

    if (u->color == WHITE)

    DFS_Visit(u);

    }

    }

    DFS_Visit(u)

    {

    u->color = GREY;

    time = time+1;

    u->d = time;

    for each v u->Adj[]

    {

    if (v->color == WHITE)

    DFS_Visit(v);

    }

    u->color = BLACK;time = time+1;

    u->f = time;

    }

    Running time: O(n2) because callDFS_Visit on each vertex,

    and the loop over Adj[]can run as many as |V| [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    30/66

    Depth-First Search: The Code

    DFS(G)

    {

    for each vertex u G->V

    {

    u->color = WHITE;}

    time = 0;

    for each vertex u G->V

    {

    if (u->color == WHITE)

    DFS_Visit(u);

    }

    }

    DFS_Visit(u)

    {

    u->color = GREY;

    time = time+1;

    u->d = time;

    for each v u->Adj[]{

    if (v->color == WHITE)

    DFS_Visit(v);

    }

    u->color = BLACK;time = time+1;

    u->f = time;

    }

    BUT, there is actually a tighter bound.

    How many times willDFS_Visit() actually be [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    31/66

    Depth-First Search: The Code

    DFS(G)

    {

    for each vertex u G->V

    {

    u->color = WHITE;}

    time = 0;

    for each vertex u G->V

    {

    if (u->color == WHITE)

    DFS_Visit(u);

    }

    }

    DFS_Visit(u)

    {

    u->color = GREY;

    time = time+1;

    u->d = time;

    for each v u->Adj[]{

    if (v->color == WHITE)

    DFS_Visit(v);

    }

    u->color = BLACK;time = time+1;

    u->f = time;

    }

    So, running time of DFS = O(V+E)[email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    32/66

  • 8/8/2019 BFSand DFS

    33/66

    DFS Example

    source

    vertex

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    34/66

    DFS Example

    1 | | |

    |||

    | |

    source

    vertexd f

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    35/66

    DFS Example

    1 | | |

    |||

    2 | |

    source

    vertexd f

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    36/66

    DFS Example

    1 | | |

    ||3 |

    2 | |

    source

    vertexd f

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    37/66

    DFS Example

    1 | | |

    ||3 | 4

    2 | |

    source

    vertexd f

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    38/66

  • 8/8/2019 BFSand DFS

    39/66

    DFS Example

    1 | | |

    |5 | 63 | 4

    2 | |

    source

    vertexd f

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    40/66

    DFS Example

    1 | 8 | |

    |5 | 63 | 4

    2 | 7 |

    source

    vertexd f

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    41/66

    DFS Example

    1 | 8 | |

    |5 | 63 | 4

    2 | 7 |

    source

    vertexd f

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    42/66

  • 8/8/2019 BFSand DFS

    43/66

    DFS Example

    1 | 8 | |

    |5 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    44/66

    DFS Example

    1 | 8 |11 |

    |5 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    45/66

    DFS Example

    1 |12 8 |11 |

    |5 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    46/66

    DFS Example

    1 |12 8 |11 13|

    |5 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    47/66

    DFS Example

    1 |12 8 |11 13|

    14|5 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    48/66

    DFS Example

    1 |12 8 |11 13|

    14|155 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    49/66

    DFS Example

    1 |12 8 |11 13|16

    14|155 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    50/66

    Properties of DFS

    DFS produces a forest.

    Important property of DFS is that discovery and

    finishing times haveparenthesis structure.

    Parenthesis theorem:-

    In any DFS of a(directed or undirected)graph

    G=(V,E),for any two vertices u and v extactly one of

    the following three conditions holds:

    Case-1:The intervals [d[u],f[u]] and [d[v],f[v]] entirelydisjoint and neither u nor v is a descendentof the

    other in DFS forest.

    [email protected] y 10/11/2009

  • 8/8/2019 BFSand DFS

    51/66

    Properties of DFS contd...

    Case 2:d[u]

  • 8/8/2019 BFSand DFS

    52/66

  • 8/8/2019 BFSand DFS

    53/66

    DFS Example

    1 |12 8 |11 13|16

    14|155 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    Tree edges

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    54/66

  • 8/8/2019 BFSand DFS

    55/66

  • 8/8/2019 BFSand DFS

    56/66

    DFS: Kinds of edges

    DFS introduces an important distinction

    among edges in the original graph:

    Tree edge: encounter new (white) vertex

    Back edge: from descendent to ancestor

    Forward edge: from ancestor to descendent

    Not a tree edge, though

    From grey node to black node

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    57/66

    DFS Example

    1 |12 8 |11 13|16

    14|155 | 63 | 4

    2 | 7 9 |10

    source

    vertexd f

    Tree edges Backedges Forward edges

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    58/66

  • 8/8/2019 BFSand DFS

    59/66

  • 8/8/2019 BFSand DFS

    60/66

    DFS: Kinds of edges

    DFS introduces an important distinction

    among edges in the original graph:

    Tree edge: encounter new (white) vertex

    Back edge: from descendent to ancestor

    Forward edge: from ancestor to descendent

    Cross edge: between a tree or subtrees

    Note: tree & back edges are important; mostalgorithms dont distinguish forward & cross

    edges.

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    61/66

    DFS: Kinds Of Edges

    Thm 23.9: If G is undirected, a DFS producesonly tree and back edges

    Proof by contradiction:

    Assume theres a forward edge

    But F? edge must actually be a

    back edge (why?)

    source

    F?

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    62/66

    DFS: Kinds Of Edges

    Thm 23.9: If G is undirected, a DFS producesonly tree and back edges

    Proof by contradiction:

    Assume theres a cross edge

    But C? edge cannot be cross:

    must be explored from one of the

    vertices it connects, becoming a tree

    vertex, before other vertex is explored

    So in fact the picture is wrongboth

    lower tree edges cannot in fact be

    tree edges

    source

    C?

    [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    63/66

    DFS And Graph Cycles

    Thm: An undirected graph is acyclic iff a DFS

    yields no back edges

    If acyclic, no back edges (because a back edge

    implies a cycle

    If no back edges, acyclic

    No back edges implies only tree edges (Why?)

    Only tree edges implies we have a tree or a forest

    Which by definition is acyclic

    Thus, can run DFS to find whether a graph has

    a [email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    64/66

    DFS And Cycles

    How would you modify the code to detect cycles?

    DFS(G)

    {

    for each vertex u G->V

    {

    u->color = WHITE;

    }

    time = 0;

    for each vertex u G->V

    {

    if (u->color == WHITE)

    DFS_Visit(u);

    }

    }

    DFS_Visit(u)

    {

    u->color = GREY;

    time = time+1;u->d = time;

    for each v u->Adj[]

    {

    if (v->color == WHITE)

    DFS_Visit(v);}

    u->color = BLACK;

    time = time+1;

    u->f = time;

    }[email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    65/66

    DFS And Cycles

    What willbe the running time?

    DFS(G)

    {

    for each vertex u G->V

    {

    u->color = WHITE;

    }

    time = 0;

    for each vertex u G->V

    {

    if (u->color == WHITE)

    DFS_Visit(u);

    }

    }

    DFS_Visit(u)

    {

    u->color = GREY;

    time = time+1;u->d = time;

    for each v u->Adj[]

    {

    if (v->color == WHITE)

    DFS_Visit(v);}

    u->color = BLACK;

    time = time+1;

    u->f = time;

    }[email protected] 10/11/2009

  • 8/8/2019 BFSand DFS

    66/66

    DFS And Cycles

    What willbe the running time?

    A: O(V+E)

    We can actually determine if cycles exist in

    O(V) time:

    In an undirected acyclic forest, |E| e |V| - 1 So count the edges: if ever see |V| distinct edges,

    must have seen a back edge along the way