Algorithms‎ > ‎Graph‎ > ‎

Depth First Search

int dfstime;

int entry_time[MAXV + 1];

int exit_time[MAXV + 1];

bool finished;


void dfs(graph *g, int v)

{

    edgenode *p;

    int y;

    

    if (finished) return;

    

    discovered[v] = true;

    dfstime = dfstime + 1;

    entry_time[v] = dfstime;

    

    process_vertex_early(v);

    p = g->edges[v];

    while(p != NULL) {

        y = p->y;

        if (discovered[y] == false) {

            parent[y] = v;

            process_edge(v, y);

            dfs(g, y);

        }

        else if (!processed[y] || (g->directed))

            process_edge(v, y);

        

        if (finished) return;

        

        p = p->next;

    }

    

    

    process_vertex_late(v);

    dfstime = dfstime + 1;

    exit_time[v] = dfstime;

    

    processed[v] = true;

}


int main(int argc, const char * argv[]) {

    graph *myGraph = (graph *)malloc(sizeof(graph));

    load_graphA(myGraph, false);

    finished = false;

    initialize_search(myGraph);

    dfs(myGraph, 1);

}