Algorithms‎ > ‎Graph‎ > ‎

### Finding Cycles

 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; }void process_edge(int x, int y){    if (parent[x] != y)    {        printf("Cycle from %d to %d:", y, x);        find_path(y, x, parent);        printf("\n\n");        finished = true;    }    printf("processed edge (%d %d)\n", x, y); }void find_path(int start, int end, int parents[]){    if ((start == end) || (end == -1))        printf("\n%d", start);    else    {        find_path(start, parents[end], parents);        printf(" %d", end);    } }int main(int argc, const char * argv[]) {    graph *myGraph = (graph *)malloc(sizeof(graph));    load_graphC(myGraph, false);    finished = false;    initialize_search(myGraph);    dfs(myGraph, 1);    return 0; }