Data Structures/Graph/DFSandBFS.cpp
//DFS is similar to BFS with the difference being the order in which vertices are explored
//main difference is that DFS uses a stack instead of a queue
//DFS is done recursively to make use of the implicit stack from recursion
//as opposed to an iterative DFS with an explicit stack

DFS is similar to BFS with the difference being the order in which vertices are explored
main difference is that DFS uses a stack instead of a queue
DFS is done recursively to make use of the implicit stack from recursion
as opposed to an iterative DFS with an explicit stack
DFS is similar to BFS with the difference being the order in which vertices are explored main difference is that DFS uses a stack instead of a queue DFS is done recursively to make use of the implicit stack from recursion as opposed to an iterative DFS with an explicit stack
void Graph::depth_first_search(int start_vertex)
{
    int current_v = start_vertex;
    this->discovered[current_v] = true;
    EdgeNode *p = this->edges[current_v];
    process_vertex_early(current_v);
    while (p != NULL)
    {
        int neighbor_v = p->key;
        if (!this->discovered[neighbor_v])
        {
            this->parent[neighbor_v] = current_v;
            process_edge(current_v, neighbor_v);
            depth_first_search(neighbor_v);
        }
        else if (((!this->processed[neighbor_v]) && (this->parent[current_v] != neighbor_v)) || (this->directed))
        {
            process_edge(current_v, neighbor_v);
        }
        p = p->next;
    }
    this->processed[current_v] = true;
}
void Graph :: depth_first_search ( int start_vertex ) { int current_v = start_vertex ; this -> discovered [ current_v ] = true ; EdgeNode * p = this -> edges [ current_v ] ; process_vertex_early ( current_v ) ; while ( p != NULL ) { int neighbor_v = p -> key ; if ( ! this -> discovered [ neighbor_v ] ) { this -> parent [ neighbor_v ] = current_v ; process_edge ( current_v , neighbor_v ) ; depth_first_search ( neighbor_v ) ; } else if ( ( ( ! this -> processed [ neighbor_v ] ) && ( this -> parent [ current_v ] != neighbor_v ) ) || ( this -> directed ) ) { process_edge ( current_v , neighbor_v ) ; } p = p -> next ; } this -> processed [ current_v ] = true ; }
DFS is similar to BFS with the difference being the order in which vertices are explored
main difference is that DFS uses a stack instead of a queue
DFS is done recursively to make use of the implicit stack from recursion
as opposed to an iterative DFS with an explicit stack
DFS is similar to BFS with the difference being the order in which vertices are explored main difference is that DFS uses a stack instead of a queue DFS is done recursively to make use of the implicit stack from recursion as opposed to an iterative DFS with an explicit stack
//this->discovered[neighbor_v] = true;
 //2nd case is needed because 1st case isn't enough to visit all edges (because 1st case sets all neighbors
 //to discovered which excludes some visits to edges later)
