next up previous contents
Next: Breadth-First Search: Introduction Up: Graphs: Introduction Previous: Graphs: Introduction   Contents

Depth-First Search: Introduction [169]




\begin{picture}(604,224)(10,605)
\thicklines\put(120,800){\circle{28}}
\put( 40,...
...ox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm DFS Spanning Forest}}}
\end{picture}



main () {                                      void dfs(NODE v) {
NODE v;                                        NODE w;
  for (all nodes v) visited[v] = FALSE;          visited[v] = TRUE;
  for (all nodes v) {                            for (all neighbors w of v)
     if (!visited[v])                               if (!visited[w])
       dfs(v);                                        dfs(w);
}                                               }

Instead of just TRUE or FALSE: mark each visit with label++



DFS: dfs [170]

% gcc -o dfs dfs.c                                     > tcc dfs.c

Data File with Test Case: graph.dat

Adjacency Matrix (as compared to Adjacency List):

10
1 1 0 1 0 0 0 0 0 0
0 1 1 0 0 1 0 0 0 0
1 0 1 1 1 0 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 
0 0 1 0 0 1 0 0 0 0
0 0 0 0 0 1 1 1 0 0 
0 0 0 0 0 1 0 1 0 1
0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 0 1 1

% dfs graph.dat
n: 10 steps: 100

ANALYSIS: for every node, for every neighbor: O( )



DFS: dfs [171]

DFS Spanning Forest:

% dfs graph.dat graph
  0  1  0  0  0  0  0  0  0  0
  0  0  1  0  0  1  0  0  0  0
  0  0  0  1  0  0  0  0  0  0
  0  0  0  0  1  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  1  0  0
  0  0  0  0  0  0  0  0  0  1
  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  1  0



DFS: dfs [172]

% dfs graph.dat step          # "x" = don't try;  "+" = try;  "-" = cancel
 v  w step       v  w step       v  w step       v  w step       v  w step
 1  1    1 x     5  8   22 x     6  4   42 x     7  3   63 x    10  5   83 x    
 1  2    2 +     5  9   23 x     6  5   43 x     7  4   64 x    10  6   84 x    
 2  1    3 x     5 10   24 x     6  6   44 x     7  5   65 x    10  7   85 x    
 2  2    4 x     4  6   25 x     6  7   45 x     7  6   66 +    10  8   86 x    
 2  3    5 +     4  7   26 x     6  8   46 x     7  6   66 -    10  9   87 +    
 3  1    6 +     4  8   27 x     6  9   47 x     7  7   67 x     9  1   88 x    
 3  1    6 -     4  9   28 x     6 10   48 x     7  8   68 +     9  2   89 x    
 3  2    7 x     4 10   29 x     2  7   49 x     8  1   69 x     9  3   90 x    
 3  3    8 x     3  5   30 +     2  8   50 x     8  2   70 x     9  4   91 x    
 3  4    9 +     3  5   30 -     2  9   51 x     8  3   71 x     9  5   92 x    
 4  1   10 x     3  6   31 x     2 10   52 x     8  4   72 x     9  6   93 x    
 4  2   11 x     3  7   32 x     1  3   53 x     8  5   73 x     9  7   94 x    
 4  3   12 x     3  8   33 x     1  4   54 +     8  6   74 +     9  8   95 +    
 4  4   13 x     3  9   34 x     1  4   54 -     8  6   74 -     9  8   95 -    
 4  5   14 +     3 10   35 x     1  5   55 x     8  7   75 x     9  9   96 x    
 5  1   15 x     2  4   36 x     1  6   56 x     8  8   76 x     9 10   97 x    
 5  2   16 x     2  5   37 x     1  7   57 x     8  9   77 x    10 10   98 x    
 5  3   17 x     2  6   38 +     1  8   58 x     8 10   78 +     7  9   99 x    
 5  4   18 x     6  1   39 x     1  9   59 x    10  1   79 x     7 10  100 x    
 5  5   19 x     6  2   40 x     1 10   60 x    10  2   80 x    
 5  6   20 x     6  3   41 +     7  1   61 x    10  3   81 x    
 5  7   21 x     6  3   41 -     7  2   62 x    10  4   82 x



DFS: dfs.c [173]

typedef int NODE;
int A[MAX][MAX], B[MAX][MAX];
int n, label, val[MAX];

void DFS(NODE v){
NODE w;
  val[v] = ++label;
  for (w=1; w<=n; w++){
    if (A[v][w] != 0)
      if (val[w] == 0) {
        B[v][w] = 1; 
        DFS(w);
      } 
  }
}

main(int argc, char *argv[]) {
NODE v;
  getdata(argc,argv);
  label=0;
  for (v=1; v<=n; v++) val[v]=0;
  for (v=1; v<=n; v++)
   if (val[v] == 0) 
     DFS(v);
}



Ted Billard 2001-10-25