Depth First & Breadth First Search

  • Upload
    babar2k

  • View
    230

  • Download
    0

Embed Size (px)

Citation preview

  • 7/30/2019 Depth First & Breadth First Search

    1/49

  • 7/30/2019 Depth First & Breadth First Search

    2/49

    DFS (G, s)

    for each vertex u in V[G] //Initialization

    color[u] WHITE //mark each vertex as unvisited

    parent[u] NIL //Set parent of each vertex to

    nil

    End for

    time 0 //Global time to mark visited/traversed

    color[s] GRAY //Mark as visited u

    v[s] time //mark visited time of u

    for each vertex u in V[G] //start exploring graph

    ifcolor[u] WHITE // if vertex is unvisited

    DFS-Visit(u) //build a new DFS-tree from u

    end if

    End for

    End DFS (G, s)

  • 7/30/2019 Depth First & Breadth First Search

    3/49

  • 7/30/2019 Depth First & Breadth First Search

    4/49

    Step 1: Initialization Mark each vertex of graph as unvisited and predecessor

    as null and set a global timestamp to zero .

    Step 2: Start Exploring from starting vertex Start iterating through each unvisited vertex and its

    adjacent vertices until its maximum depth. Mark asvisited.

    Step 3: Repeat Recursively Start exploring node and its adjacent.

    When maximum depth is reached, backtrack to unvisitednode and start visiting its descendant until all nodes andtheir descendants are visited.

    Step 4: Mark the explored nodes as traversed When a node and its adjacent are explored mark it as

    traversed and repeat step 3 if applicable.

  • 7/30/2019 Depth First & Breadth First Search

    5/49

  • 7/30/2019 Depth First & Breadth First Search

    6/49

    DFS (G, s)

    for each vertex u in V[G] //Initialization

    color[u] WHITE //mark each vertex as unvisited

    parent[u] NIL //Set parent of each vertex to nil

    End fortime 0 //Global time to mark visited/traversed

    0

    7

    1

    5

    4

    3

    2

    6

    Starting from 0

    Parent[u] = nil

  • 7/30/2019 Depth First & Breadth First Search

    7/49

    color[s] GRAY //Mark as visited u

    v[s] time //mark visited time of u

    for each vertexxadjacent to u //explore (u, v)

    ifcolor[x] WHITE //if vertex is unvisited

    parent[x]u // set parent of v to u

    DFS-Visit(x) // Recursively call DFS-Visit(u) until

    goal/leaf is found and backtracking starts

    end if

    End for

    0

    7

    1

    5

    4

    3

    2

    6

    Starting from 0

    Parent[u] = nil

    Parent[5] = 0

    Visited time v[0]=1

    v[u] = {0}

    t[u] = {}

  • 7/30/2019 Depth First & Breadth First Search

    8/49

    DFS-Visit(x)

    color[s] GRAY //Mark as visited u

    time time + 1 //Add +1 to time to mark u as visited

    v[x] time //mark visited time of x

    0

    7

    1

    5

    4

    3

    2

    6

    Starting from 0

    Parent[u] = nil

    Visited time v[0]=1

    Set Parent[5] = 0

    Visited time v[5]=2

    v[u] = {0,5}

    t[u] = {}

  • 7/30/2019 Depth First & Breadth First Search

    9/49

    for each vertex vadjacent tox //explore (u, v)

    ifcolor[v] WHITE //if vertex is unvisited

    parent[v] u // set parent of v to u

    DFS-Visit(v) // Recursively call DFS-Visit(u)

    until goal/leaf is found and backtracking startsend if

    End for

    0

    7

    1

    5

    4

    3

    2

    6

    Starting from 0

    Parent[u] = nil

    set parent[5]=0

    Visited time v[5]=2

    Visited time v[0]=1

    v[u] = {0,5}

    t[u] = {}

  • 7/30/2019 Depth First & Breadth First Search

    10/49

    for each vertex vadjacent tox //explore (u, v)

    ifcolor[v] WHITE //if vertex is unvisited

    parent[v] u // set parent of v to u

    DFS-Visit(v) // Recursively call DFS-Visit(u)

    until goal/leaf is found and backtracking startsend if

    End for

    0

    7

    1

    5

    4

    3

    2

    6

    Starting from 0

    Parent[u] = nil

    Mark as white and

    set parent[u]=0

    Visited time v[5]=2

    Visited time v[0]=1

    Visited time

    v[1]=3

    v[u] = {0,5,1}

    t[u] = {}

  • 7/30/2019 Depth First & Breadth First Search

    11/49

    for each vertex vadjacent tox //explore (u, v)

    ifcolor[v] WHITE //if vertex is unvisited

    parent[v] u // set parent of v to u

    DFS-Visit(v) // Recursively call DFS-Visit(u)

    until goal/leaf is found and backtracking startsend if

    End for

    0

    7

    1

    5

    4

    3

    2

    6

    Starting from 0

    Parent[u] = nil

    Mark as white and

    set parent[u]=0

    Visited time v[5]=2

    Visited time v[0]=1

    Visited time

    v[1]=3

    v[u] = {0,5,1}

    t[u] = {}

  • 7/30/2019 Depth First & Breadth First Search

    12/49

    for each vertex vadjacent tox //explore (u, v)

    ifcolor[v] WHITE //if vertex is unvisited

    parent[v] u // set parent of v to u

    DFS-Visit(v) // Recursively call DFS-Visit(u)

    until goal/leaf is found and backtracking startsend if

    End for

    0

    7

    1

    5

    4

    3

    2

    6

    Starting from 0

    Parent[u] = nil

    Mark as white and

    set parent[u]=0

    Visited time v[5]=2

    Visited time v[0]=1

    Visited time

    v[1]=3

    Visited time

    v[4]=4

    v[u] = {0,5,1,4}

    t[u] = {}

  • 7/30/2019 Depth First & Breadth First Search

    13/49

    for each vertex vadjacent tox //explore (u, v)

    ifcolor[v] WHITE //if vertex is unvisited

    parent [v] u // set parent of v to u

    DFS-Visit(v) // Recursively call DFS-Visit(u)

    until goal/leaf is found and backtracking startsend if

    End for

    0

    7

    1

    5

    4

    3

    2

    6

    Starting from 0

    Parent[u] = nil

    Mark as white and

    set parent[u]=0

    Visited time v[5]=2

    Visited time v[0]=1

    Visited time

    v[1]=3

    Visited time

    v[4]=4

    Visited time

    v[3]=5

    v[u] = {0,5,1,4,3}

    t[u] = {}

  • 7/30/2019 Depth First & Breadth First Search

    14/49

    for each vertex vadjacent tox //explore (u, v)

    ifcolor[v] WHITE //if vertex is unvisited

    parent [v] u // set parent of v to u

    DFS-Visit(v) // Recursively call DFS-Visit(u)

    until goal/leaf is found and backtracking startsend if

    End for

    0

    7

    1

    5

    4

    3

    2

    6

    Starting from 0

    Parent[u] = nil

    Mark as white and

    set parent[u]=0

    Visited time v[5]=2

    Visited time v[0]=1

    Visited time

    v[1]=3

    Visited time

    v[4]=4

    Visited time

    v[3]=5

    Visited time

    v[2]=6

    v[u] = {0,5,1,4,3,2}

    t[u] = {}

  • 7/30/2019 Depth First & Breadth First Search

    15/49

    color[u] BLACK // mark u to traversed

    time time + 1 // mark traversed time of u

    t[u] time // Traversed time of u

    End DFS-Visit(u)

    0

    7

    1

    5

    4

    3

    2

    6

    Starting from 0

    Parent[u] = nil

    Mark as white and

    set parent[u]=0

    Visited time v[5]=2

    Visited time v[0]=1

    Visited time

    v[1]=3

    Visited time

    v[4]=4

    Visited time

    v[3]=5

    Visited time

    v[2]=6

    v[u] = {0,5,1,4,3,2}

    t[u] = {2,3,4}

  • 7/30/2019 Depth First & Breadth First Search

    16/49

    color[u] BLACK // mark u to traversed

    time time + 1 // mark traversed time of u

    t[u] time // Traversed time of u

    End DFS-Visit(u)

    0

    7

    1

    5

    4

    3

    2

    6

    Starting from 0

    Parent[u] = nil

    Mark as white and

    set parent[u]=0

    Visited time v[5]=2

    Visited time v[0]=1

    Visited time

    v[1]=3

    Visited time

    v[4]=4

    Visited time

    v[3]=5

    Visited time

    v[2]=6

    v[u] = {0,5,1,4,3,2}

    t[u] = {2,3,4}

  • 7/30/2019 Depth First & Breadth First Search

    17/49

    color[u] BLACK // mark u to traversed

    time time + 1 // mark traversed time of u

    t[u] time // Traversed time of u

    End DFS-Visit(u)

    0

    7

    1

    5

    4

    3

    2

    6

    Starting from 0

    Parent[u] = nil

    Mark as white and

    set parent[u]=0

    Visited time v[5]=2

    Visited time v[0]=1

    Visited time

    v[1]=3

    Visited time

    v[4]=4

    Visited time

    v[3]=5

    Visited time

    v[2]=6

    v[u] = {0,5,1,4,3,2}

    t[u] = {2,3,4,1}

  • 7/30/2019 Depth First & Breadth First Search

    18/49

    color[u] BLACK // mark u to traversed

    time time + 1 // mark traversed time of u

    t[u] time // Traversed time of u

    End DFS-Visit(u)

    0

    7

    1

    5

    4

    3

    2

    6

    Starting from 0

    Parent[u] = nil

    Mark as white and

    set parent[u]=0

    Visited time v[5]=2

    Visited time v[0]=1

    Visited time

    v[1]=3

    Visited time

    v[4]=4

    Visited time

    v[3]=5

    Visited time

    v[2]=6

    Visited time v[7]=7

    v[u] = {0,5,1,4,3,2,7}

    t[u] = {2,3,4,1,5,0}

  • 7/30/2019 Depth First & Breadth First Search

    19/49

    color[u] BLACK // mark u to traversed

    time time + 1 // mark traversed time of u

    t[u] time // Traversed time of u

    End DFS-Visit(u)

    0

    7

    1

    5

    4

    3

    2

    6

    Starting from 0

    Parent[u] = nil

    Mark as white and

    set parent[u]=0

    Visited time v[5]=2

    Visited time v[0]=1

    Visited time

    v[1]=3

    Visited time

    v[4]=4

    Visited time

    v[3]=5

    Visited time

    v[2]=6

    Visited time v[7]=7

    v[u] = {0,5,1,4,3,2,7,6}

    t[u] = {2,3,4,1,5,0,7}

  • 7/30/2019 Depth First & Breadth First Search

    20/49

    color[u] BLACK // mark u to traversed

    time time + 1 // mark traversed time of u

    t[u] time // Traversed time of u

    End DFS-Visit(u)

    0

    7

    1

    5

    4

    3

    2

    6

    Starting from 0

    Parent[u] = nil

    Mark as white and

    set parent[u]=0

    Visited time v[5]=2

    Visited time v[0]=1

    Visited time

    v[1]=3

    Visited time

    v[4]=4

    Visited time

    v[3]=5

    Visited time

    v[2]=6

    Visited time v[7]=7

    v[u] = {0,5,1,4,3,2,7,6}

    t[u] = {2,3,4,1,5,0,7,6}

  • 7/30/2019 Depth First & Breadth First Search

    21/49

    BFS(G, s)

    for each vertex u in V[G] //Initialization

    color[u] := WHITE //Mark as unvisited

    d[u] := infinity //set visited order to infinity

    p[u] := null //set parent to null

    end for

    color[s] := GRAY //Mark Source vertex s as visitedd[s] := 1 //visited/discovery time of s is 1

    ENQUEUE(Q, s) //insert s into Queue (Q)

  • 7/30/2019 Depth First & Breadth First Search

    22/49

    while (Q != ) //While Q is not empty

    U = DEQUEUE(Q) //pick the element from head

    for each vertex v in Adj[u] //loop through each adjacent of u as v

    if (color[v] = WHITE) //if unvisited

    color[v] = GRAY //mark as visited

    d[u] = d[u] + 1 //add +1 in visited order timed[v] = d[u] //set visited time of v to d[u]

    p[v] = u //set previous of v to u

    ENQUEUE(Q, v) insert v into Queue Q

    end for

    color[u] := BLACK //mark u as traversed

    end while

    End BFS(G, s)

  • 7/30/2019 Depth First & Breadth First Search

    23/49

    Step1: Initialization Loop through each vertex of Graph G and mark each vertex as

    unvisited, previous/parent of each vertex as null, anddiscovery/visited time to infinity.

    Step2: Start exploring from starting vertex Mark source/starting vertex s as visited and visited time of s

    as 1 and insert s into the Queue Q.

    Step3: Loop through each vertexs Adjacent until Queue isempty Start looping from starting vertex and visit its adjacent first.

    Step4: Repeat step 3 until Queue is empty

    Repeat loop for adjacent vertex first and then each adjacentdescendents until Queue Q is empty.

    Step5: Mark explored vertex as traversed Mark each vertex traversed after exploring its adjacent or

    siblings and its descendents.

  • 7/30/2019 Depth First & Breadth First Search

    24/49

    0

    7

    1

    5

    4

    3

    2

    6

  • 7/30/2019 Depth First & Breadth First Search

    25/49

    for each vertex u in V[G] //Initialization

    color[u] = WHITE //Mark as unvisited

    d[u] = infinity //set visited order to infinity

    p[u] = null //set parent to null

    end for

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = infinity

    p[0] = null

    d[5] = infinity

    P[5] = null

    d[2] = infinityp[2] = null

    d[7] = infinity

    p[7] = null

    d[6] = infinityp[6] = null

    d[4] = infinity

    p[4] = null

    d[3] = infinityp[3] = null

    d[1] = infinity

    d[1] = null

  • 7/30/2019 Depth First & Breadth First Search

    26/49

    color[s] = GRAY //Mark Source vertex s as visited

    d[s] = 1 //visited/discovery time of s is 1

    ENQUEUE(Q, s) //insert s into Queue (Q)

    0Q

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = infinity

    P[5] = null

    d[2] = infinity

    p[2] = null

    d[7] = infinity

    p[7] = null

    d[6] = infinityp[6] = null

    d[4] = infinity

    p[4] = null

    d[3] = infinity

    p[3] = null

    d[1] = infinity

    d[1] = null

    Q

  • 7/30/2019 Depth First & Breadth First Search

    27/49

    while (Q != ) //While Q is not empty

    U = DEQUEUE(Q) //pick the element from head

    Q

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = infinity

    P[5] = null

    d[2] = infinity

    p[2] = null

    d[7] = infinity

    p[7] = null

    d[6] = infinityp[6] = null

    d[4] = infinity

    p[4] = null

    d[3] = infinity

    p[3] = null

    d[1] = infinity

    d[1] = null

    0Q

  • 7/30/2019 Depth First & Breadth First Search

    28/49

    for each vertex v in Adj[u] //loop through each adjacent of u as v

    if (color[v] = WHITE) //if unvisited

    color[v] = GRAY //mark as visited

    d[u] = d[u] + 1 //add +1 in visited order time

    d[v] = d[u] //set visited time of v to d[u]

    p[v] = u //set previous of v to uENQUEUE(Q, v) //insert v into Queue Q

    end for

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = infinity

    p[2] = null

    d[7] = 2

    p[7] = 0

    d[6] = infinityp[6] = null

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    7 5 3Q

    d[1] = infinity

    d[1] = null

    0Q

  • 7/30/2019 Depth First & Breadth First Search

    29/49

    color[u] := BLACK //mark u as traversed

    end while

    End BFS(G, s)

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = infinity

    p[2] = null

    d[7] = 2

    p[7] = 0

    d[6] = infinityp[6] = null

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    7 5 3Q

    d[1] = infinity

    d[1] = null

    0Q

  • 7/30/2019 Depth First & Breadth First Search

    30/49

    while (Q != ) //While Q is not empty

    U = DEQUEUE(Q) //pick the element from head

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = infinity

    p[2] = null

    d[7] = 2

    p[7] = 0

    d[6] = infinityp[6] = null

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    5 3Q

    d[1] = infinity

    d[1] = null

    0 7Q

  • 7/30/2019 Depth First & Breadth First Search

    31/49

    for each vertex v in Adj[u] //loop through each adjacent of u as v

    if (color[v] = WHITE) //if unvisited

    color[v] = GRAY //mark as visited

    d[u] = d[u] + 1 //add +1 in visited order time

    d[v] = d[u] //set visited time of v to d[u]

    p[v] = u //set previous of v to uENQUEUE(Q, v) //insert v into Queue Q

    end for

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = infinity

    p[2] = null

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    5 3 6Q

    d[1] = 5

    d[1] = 7

    0 7Q

  • 7/30/2019 Depth First & Breadth First Search

    32/49

    color[u] := BLACK //mark u as traversed

    end while

    End BFS(G, s)

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = infinity

    p[2] = null

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    5 3 6Q

    d[1] = 5

    d[1] = 7

    0 7Q

  • 7/30/2019 Depth First & Breadth First Search

    33/49

    while (Q != ) //While Q is not empty

    U = DEQUEUE(Q) //pick the element from head

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = infinity

    p[2] = null

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    3 6Q

    d[1] = 5

    d[1] = 7

    0 7 5Q

  • 7/30/2019 Depth First & Breadth First Search

    34/49

    for each vertex v in Adj[u] //loop through each adjacent of u as v

    if (color[v] = WHITE) //if unvisited

    color[v] = GRAY //mark as visited

    d[u] = d[u] + 1 //add +1 in visited order time

    d[v] = d[u] //set visited time of v to d[u]

    p[v] = u //set previous of v to uENQUEUE(Q, v) //insert v into Queue Q

    end for

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7

    p[2] = 8

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    3 6 2Q

    d[1] = 5

    d[1] = 7

    0 7 5Q

  • 7/30/2019 Depth First & Breadth First Search

    35/49

    color[u] := BLACK //mark u as traversed

    end while

    End BFS(G, s)

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7

    p[2] = 8

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    3 6 2Q

    d[1] = 5

    d[1] = 7

    0 7 5Q

  • 7/30/2019 Depth First & Breadth First Search

    36/49

    while (Q != ) //While Q is not empty

    U = DEQUEUE(Q) //pick the element from head

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7

    p[2] = 8

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    6 2Q

    d[1] = 5

    d[1] = 7

    0 7 5 3Q

  • 7/30/2019 Depth First & Breadth First Search

    37/49

    for each vertex v in Adj[u] //loop through each adjacent of u as v

    if (color[v] = WHITE) //if unvisited

    color[v] = GRAY //mark as visited

    d[u] = d[u] + 1 //add +1 in visited order time

    d[v] = d[u] //set visited time of v to d[u]

    p[v] = u //set previous of v to uENQUEUE(Q, v) //insert v into Queue Q

    end for

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7

    p[2] = 8

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    6 2Q

    d[1] = 5

    d[1] = 7

    0 7 5 3Q

  • 7/30/2019 Depth First & Breadth First Search

    38/49

    color[u] := BLACK //mark u as traversed

    end while

    End BFS(G, s)

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7

    p[2] = 8

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    6 2Q

    d[1] = 5

    d[1] = 7

    0 7 5 3Q

  • 7/30/2019 Depth First & Breadth First Search

    39/49

    while (Q != ) //While Q is not empty

    U = DEQUEUE(Q) //pick the element from head

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7

    p[2] = 8

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    6 2Q

    d[1] = 5

    d[1] = 7

    0 7 5 3Q

  • 7/30/2019 Depth First & Breadth First Search

    40/49

    for each vertex v in Adj[u] //loop through each adjacent of u as v

    if (color[v] = WHITE) //if unvisited

    color[v] = GRAY //mark as visited

    d[u] = d[u] + 1 //add +1 in visited order time

    d[v] = d[u] //set visited time of v to d[u]

    p[v] = u //set previous of v to uENQUEUE(Q, v) //insert v into Queue Q

    end for

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7

    p[2] = 8

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = 8

    p[4] = 1

    d[3] = 4

    p[3] = 0

    6 2 4Q

    d[1] = 5

    d[1] = 7

    0 7 5 3Q

  • 7/30/2019 Depth First & Breadth First Search

    41/49

    color[u] := BLACK //mark u as traversed

    end while

    End BFS(G, s)

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7

    p[2] = 8

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    6 2 4Q

    d[1] = 5

    d[1] = 7

    0 7 5 3Q

  • 7/30/2019 Depth First & Breadth First Search

    42/49

    while (Q != ) //While Q is not empty

    U = DEQUEUE(Q) //pick the element from head

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7

    p[2] = 8

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    2 4Q

    d[1] = 5

    d[1] = 7

    0 7 5 3 6Q

  • 7/30/2019 Depth First & Breadth First Search

    43/49

    for each vertex v in Adj[u] //loop through each adjacent of u as v

    if (color[v] = WHITE) //if unvisited

    color[v] = GRAY //mark as visited

    d[u] = d[u] + 1 //add +1 in visited order time

    d[v] = d[u] //set visited time of v to d[u]

    p[v] = u //set previous of v to uENQUEUE(Q, v) //insert v into Queue Q

    end for

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7

    p[2] = 8

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    2 4Q

    d[1] = 5

    d[1] = 7

    0 7 5 3 6Q

  • 7/30/2019 Depth First & Breadth First Search

    44/49

    color[u] := BLACK //mark u as traversed

    end while

    End BFS(G, s)

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7

    p[2] = 8

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    2 4Q

    d[1] = 5

    d[1] = 7

    0 7 5 3 6Q

  • 7/30/2019 Depth First & Breadth First Search

    45/49

    while (Q != ) //While Q is not empty

    U = DEQUEUE(Q) //pick the element from head

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7p[2] = 8

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    4Q

    d[1] = 5

    d[1] = 7

    0 7 5 3 6 2Q

  • 7/30/2019 Depth First & Breadth First Search

    46/49

    color[u] := BLACK //mark u as traversed

    end while

    End BFS(G, s)

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7p[2] = 8

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    4Q

    d[1] = 5

    d[1] = 7

    0 7 5 3 6 2Q

  • 7/30/2019 Depth First & Breadth First Search

    47/49

    while (Q != ) //While Q is not empty

    U = DEQUEUE(Q) //pick the element from head

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7p[2] = 8

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    Q

    d[1] = 5

    d[1] = 7

    0 7 5 3 6 2 4Q

  • 7/30/2019 Depth First & Breadth First Search

    48/49

    for each vertex v in Adj[u] //loop through each adjacent of u as v

    if (color[v] = WHITE) //if unvisited

    color[v] = GRAY //mark as visited

    d[u] = d[u] + 1 //add +1 in visited order time

    d[v] = d[u] //set visited time of v to d[u]

    p[v] = u //set previous of v to uENQUEUE(Q, v) //insert v into Queue Q

    end for

    0

    7

    1

    5

    4

    3

    2

    6

    d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7p[2] = 8

    d[7] = 2

    p[7] = 0

    d[6] = 6p[6] = 7

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    Q

    d[1] = 5

    d[1] = 7

    0 7 5 3 6 2 4Q

  • 7/30/2019 Depth First & Breadth First Search

    49/49

    color[u] := BLACK //mark u as traversed

    end while

    End BFS(G, s)

    0

    7

    1

    5

    4

    3

    2d[0] = 1

    p[0] = null

    d[5] = 3

    P[5] = 0

    d[2] = 7p[2] = 8

    d[7] = 2

    p[7] = 0

    d[4] = infinity

    p[4] = null

    d[3] = 4

    p[3] = 0

    Q

    d[1] = 5

    d[1] = 7

    0 7 5 3 6 2 4Q