Jannat Arora
Jannat Arora

Reputation: 2989

how to transform dfs

I have Depth first searching algorithm whose pseudo code is given below:

   DFS(Vertex v)
    mark v visited
    make an empty Stack S
    push all vertices adjacent to v onto S
    while S is not empty do
        Vertex w is pop off S
        for all Vertex u adjacent to w do
            if u is not visited then
                mark u visited
                push u onto S

Now, I wish to convert the above dfs algorithm to breadth first search. I am implementing the program in C++. I am clueless how to go about the same.

EDIT: I know the pseudo code of bfs. What i am searching for is how to convert the above pseudo code of dfs to bfs.

Upvotes: 0

Views: 747

Answers (1)

Nullbeans
Nullbeans

Reputation: 310

BFS(Vertex v)
    mark v visited
    make an empty Queue Q
    Enqueue all vertices adjacent to v onto Q
    while Q is not empty do
        Vertex w is dequeued from Q
        for all Vertex u adjacent to w do
            if u is not visited then
                mark u visited
                enqueue u into Q

I hope this helps

Upvotes: 3

Related Questions