Warshall's Algorithm: Transitive Closure - Slides | CSCE 350, Study notes of Computer Science

Material Type: Notes; Professor: Huang; Class: DATA STRUCTR&ALGORITHMS; Subject: Computer Science & Engineering; University: University of South Carolina - Columbia; Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 09/02/2009

koofers-user-twg
koofers-user-twg 🇺🇸

5

(1)

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CSCE 350:
Data Structures and Algorithms
Chin-Tser Huang
University of South Carolina
04/03/2009 2
Announcement
Reading assignment: Chapter 8.2, 8.4
Homework 6 was assigned on Monday
and is due on Wednesday, April 8 in
class
Please download it from class website
Have your answers neatly typed
Explain your answer clearly and adequately
by showing the steps
04/03/2009 3
Warshall’s Algorithm: Transitive
Closure
•
•Computes the transitive closure of a relation
Computes the transitive closure of a relation
•
•(Alternatively: all paths in a directed graph)
(Alternatively: all paths in a directed graph)
•
•Example of transitive closure:
Example of transitive closure:
3
4
2
1
0 0 1 0
1 0 0 1
0 0 0 0
0 1 0 0
0 0 1 0
1 1 1
1 1 1
0 0 0 0
1
11 1 1
1 1
3
4
2
1
adjacency matrix transitive closure
04/03/2009 4
Warshall’s Algorithm
Main idea: a path exists between two vertices i, j, iff
there is an edge from i to j; or
there is a path from i to j going through vertex 1;
or
there is a path from i to j going through vertex 1
and/or 2; or
there is a path from i to j going through vertex 1,
2, and/or 3; or
...
there is a path from i to j going through any of
the other vertices
04/03/2009 5
Warshall’s Algorithm
3
4
2
1
3
4
2
1
3
4
2
1
3
4
2
1
R(0)
0 0 1 0
1 0 0 1
0 0 0 0
0 1 0 0
R(1)
0 0 1 0
1 0 1
11
0 0 0 0
0 1 0 0
R(2)
0 0 1 0
1 0 1 1
0 0 0 0
1
1 1 1 1
1 1
R(3)
0 0 1 0
1 0 1 1
0 0 0 0
1 1 1 1
R(4)
0 0 1 0
1 1
11 1
0 0 0 0
1 1 1 1
3
4
2
1
construct solution through series of boolean matrices
R
(0), …,
R
(
n
)
using increasing subsets of the vertices allowed as intermediate
04/03/2009 6
Warshall’s Algorithm
In the kth stage determine if a path exists between two
vertices i, j using just vertices among 1,…,k
R(k-1)[i,j] (path using just 1 ,…,k-1)
R(k)[i,j] = or
(R(k-1)[i,k] and R(k-1)[k,j]) (path from i to k
and from kto i
using just 1 ,…,k-1)
i
j
k
kth stage
{
pf3

Partial preview of the text

Download Warshall's Algorithm: Transitive Closure - Slides | CSCE 350 and more Study notes Computer Science in PDF only on Docsity!

CSCE 350:

Data Structures and Algorithms

Chin-Tser Huang

[email protected]

University of South Carolina

04/03/2009 2

Announcement

„ Reading assignment: Chapter 8.2, 8.

„ Homework 6 was assigned on Monday and is due on Wednesday, April 8 in class

„ Please download it from class website

„ Have your answers neatly typed

„ Explain your answer clearly and adequately

by showing the steps

04/03/2009 3

Warshall’s Algorithm: Transitive

Closure

  • • Computes the transitive closure of a relationComputes the transitive closure of a relation
  • • (Alternatively: all paths in a directed graph)(Alternatively: all paths in a directed graph)
  • •^ Example of transitive closure:Example of transitive closure: 3

0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0

0 0 1 0 1 1 11 1 1 0 0 0 0 11 1 1 11 1

adjacency matrix transitive closure 04/03/2009 4

Warshall’s Algorithm

Main idea: a path exists between two vertices i, j, iff ƒ there is an edge from i to j; or ƒ there is a path from i to j going through vertex 1; or ƒ there is a path from i to j going through vertex 1 and/or 2; or ƒ there is a path from i to j going through vertex 1, 2, and/or 3; or ƒ ... ƒ there is a path from i to j going through any of the other vertices

04/03/2009 5

Warshall’s Algorithm

3

2 4

1

3

2 4

1

3

2 4

1

3

2 4

1

R(0) 0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0

R(1) 0 0 1 0 1 0 11 1 0 0 0 0 0 1 0 0

R(2) 0 0 1 0 1 0 1 1 0 0 0 0 11 1 1 11 1

R(3) 0 0 1 0 1 0 1 1 0 0 0 0 1 1 1 1

R(4) 0 0 1 0 1 11 1 1 0 0 0 0 1 1 1 1

3

2 4

1

construct solution through series of boolean matricesR(0), …,R (n) using increasing subsets of the vertices allowed as intermediate

04/03/2009 6

Warshall’s Algorithm

In the k th^ stage determine if a path exists between two vertices i, j using just vertices among 1,…, k

R (k-1)[ i,j ] (path using just 1 ,…, k- 1) R (k)[ i,j ] = or ( R (k-1)^ [ i,k ] and R (k-1)[ k,j ]) (path from i to k and from k to i using just 1 ,…, k- 1) i

j

k

k th^ stage

04/03/2009 7

Warshall’s Algorithm (matrix

generation)

Recurrence relating elementsR(k)^ to elements ofR(k-1)^ is:

R(k)[i,j] =R (k-1)^ [i,j] or (R (k-1)^ [i,k] andR(k-1)[k,j])

It implies the following rules for generatingR (k)^ fromR (k-1)^ :

Rule 1 If an element in rowi and columnj is 1 inR (k-1)^ , it remains 1 inR (k)

Rule 2 If an element in rowi and columnj is 0 inR (k-1)^ , it has to be changed to 1 inR (k)^ if and only if the element in its rowi and columnk and the element in its columnj and rowk are both 1’s inR (k-1) 04/03/2009 8

Warshall’s Algorithm (matrix

generation)

04/03/2009 9

Warshall’s Algorithm (example)

3

2 4

R (0) =
R (1) =
R (2) =
R (3) =
R (4) =

04/03/2009 10

Warshall’s Algorithm (pseudocode

and analysis)

Time efficiency: Space efficiency:

Θ(n^3 ) Matrices can be written over their predecessors

04/03/2009 11

Floyd’s Algorithm: All pairs

shortest paths

Problem: In a weighted (di)graph, find shortest paths between every pair of vertices Same idea: construct solution through series of matricesD (0)^ , …, D (n)^ using increasing subsets of the vertices allowed as intermediate Example: 2

2

3 6

7

04/03/2009 12

Floyd’s Algorithm (matrix

generation)

On the k- th iteration, the algorithm determines shortest paths between every pair of vertices i, j that use only vertices among 1,…, k as intermediate D ( k )[ i,j ] = min { D ( k -1)[ i,j ], D ( k -1)[ i,k ] + D ( k -1)^ [ k,j ]}

i

j

k

DD (( kk -1)-1)^ [[ i,ji,j ]]

DD (( kk - -1)1)^ [[ i,ki,k ]]

D D (( kk - -1)1)^ [[ k,jk,j ]]