School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
 

Code Snippets

  

C Source Code



Recursive Depth First Search

Recursive Depth First Search function with the needed data structures.

Submitted By: PennyBoki
Actions:
Rating:
Views: 20,445

Language: C

Last Modified: January 14, 2008

Snippet


  1. #define MAX 10
  2.  
  3. //structure for a single linked list
  4. typedef struct element
  5. {
  6.         int info;
  7.         struct element *link;       
  8. }node;
  9.  
  10. //structure for a grapgh
  11. typedef struct graphAdjList
  12. {
  13.         int nodes;
  14.         node * adjList[MAX];
  15. }graph;
  16.  
  17.  
  18. /*
  19.        g is a graph
  20.        start is info of some node
  21.        visited is an array of all the nodes
  22. */
  23. void DFS(graph *g, int start, int visited[])//function for a Depth First Search
  24. {
  25.      visited[start]=1;//set the current node as visited
  26.      
  27.      //printf("%d", start);
  28.  
  29.      node *p;//pointer that is needed for traversing through the graph
  30.  
  31.      for(p=g->adjList[start]; p; p=p->link)
  32.      {
  33.                   if(!visited[p->info]) //if not visited     
  34.                                            DFS(g, p->info, visited);   //visit then node with p->info                 
  35.      }
  36. }

Copy & Paste


Comments

William_Wilson 2008-01-14 11:20:42

It was displaying on a single line for some reason, so I edited it to include the line breaks for you :)

PennyBoki 2008-01-14 14:00:15

Thank you :)

nayak 2009-04-29 11:33:39

houya leksen

nayak 2009-04-29 11:37:37

vai valo leksen


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.