Breadth-First and Depth-First Search

Pseudocode for BFS and DFS

BFS(G, s){
    Q.push(s) // a queue
    Mark s as visited // how?

    while(Q is not empty)
        u = Q.pop
        for each(v in adjList(u)){
             if(v not visited){ // how?
                Q.push(v)
                Mark v as visited //how?
            }
        }
    }
}


DFS(G, s){
    S.push(s) // a stack

    while(S is not empty)
        u = S.pop
        if(u not visited){
            Mark u as visited // how?
            for each(v in adjList(u)){
                 // this check is not necessary
                 if(v not visited){ // how?
                    S.push(v)
                }
            }
        }
    }
}

Differences between BFS and DFS: