next up previous contents
Next: NP-Completeness Up: Graphs: Introduction Previous: Minimum Spanning Tree: Intro   Contents

Transitive Closure: Introduction [192]




\begin{picture}(328,154)(26,660)
\thicklines\put( 40,800){\circle{28}}
\put(140,...
...box(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm Transitive Closure}}}
\end{picture}

What nodes can I get to from here?

Train Schedule: can I get from City i to City j somehow?

Warshall's Algorithm: A[i][j] = A[i][k] && A[k][j]

If you can travel to any City from any other City,

what kind of graph is the Transitive Closure?



TC: tc [193]

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

Data File with Test Case: tc.dat

4
0 1 0 1
0 0 1 0
0 0 0 1
0 0 0 0

% tc tc.dat
n: 4 steps: 64

% tc tc.dat graph
  1  1  1  1
  0  1  1  1
  0  0  1  1
  0  0  0  1



TC: tc [194]

% tc tc.dat step
 u  v step         u  v step         u  v step         u  v step
------------------------------------------------------------------ 
 1  1    1 +       1  1   17 +       1  1   33 +       1  1   49 +      
 1  2    2 +       1  2   18 +       1  2   34 +       1  2   50 +      
 1  3    3 -       1  3   19 +       1  3   35 +       1  3   51 +      
 1  4    4 +       1  4   20 +       1  4   36 +       1  4   52 +      
 2  1    5 -       2  1   21 -       2  1   37 -       2  1   53 -      
 2  2    6 +       2  2   22 +       2  2   38 +       2  2   54 +      
 2  3    7 +       2  3   23 +       2  3   39 +       2  3   55 +      
 2  4    8 -       2  4   24 -       2  4   40 +       2  4   56 +      
 3  1    9 -       3  1   25 -       3  1   41 -       3  1   57 -      
 3  2   10 -       3  2   26 -       3  2   42 -       3  2   58 -      
 3  3   11 +       3  3   27 +       3  3   43 +       3  3   59 +      
 3  4   12 +       3  4   28 +       3  4   44 +       3  4   60 +      
 4  1   13 -       4  1   29 -       4  1   45 -       4  1   61 -      
 4  2   14 -       4  2   30 -       4  2   46 -       4  2   62 -      
 4  3   15 -       4  3   31 -       4  3   47 -       4  3   63 -      
 4  4   16 +       4  4   32 +       4  4   48 +       4  4   64 +



TC: tc.c [195]

int A[MAX][MAX], B[MAX][MAX]; /* transitive closure */
int n;

void TC() {
  int i, j, k;

  /* compute B^0 */
  for (i=1; i<=n; i++)
   for (j=1; j<=n; j++){
    B[i][j] = A[i][j];
  }
 
  /* compute B^k for k=1,2,...n */
  for (k=1; k<=n; k++)
   for (i=1; i<=n; i++)
    for (j=1; j<=n; j++)
      if (!B[i][j])
         B[i][j] = B[i][k] && B[k][j];
}

ANALYSIS: O( )



Ted Billard 2001-10-25