









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: DATA STRUCTURES; Subject: Computer Science; University: Oregon State University; Term: Spring 2003;
Typology: Study notes
1 / 15
This page cannot be seen from the preview
Don't miss anything!










1
, v
2
, …, v
n
same object/state
i
j
i
j
j
i
(if the
edge is undirected then v
i
and v
j
are neighbors or each other)
v
1
v
2
v
4
v
3
v
1
v
2
v
4
v
3
v
1
v
2
v
4
v
3
w
1,
w
2,
w
1,
w
3,
w
4,
v
1
v
2
v
4
v
3
w
1-
w
2-
w
1-
w
3-
w
1-
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Two most common representations:
1: indicates an edge from v
i
to v
j
0: no edge from v
i
to v
j
i , j
instead of 1 and ∞ instead of 0
2
) space for V vertices
i
lists the set of directly connected neighbors
j
i , j
7: Pueblo 0 0 0 0 1 0 0?
6: Princeton 0 0 0 0 0 1? 0
5: Pittsburgh 0 1 0 0 0? 0 0
4: Pierre 1 0 0 0? 0 0 0
3: Phoenix 0 0 1? 0 1 0 1
2: Peoria 0 0? 0 0 1 0 1
1: Pensacola 0? 0 1 0 0 0 0
0: Pendleton? 0 0 1 0 0 0 1
City
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
What about the diagonal
matrix entries?
Is a vertex connected to
itself?
7: Pueblo 0 0 0 0 1 0 0 1
6: Princeton 0 0 0 0 0 1 1 0
5: Pittsburgh 0 1 0 0 0 1 0 0
4: Pierre 1 0 0 0 1 0 0 0
3: Phoenix 0 0 1 1 0 1 0 1
2: Peoria 0 0 1 0 0 1 0 1
1: Pensacola 0 1 0 1 0 0 0 0
0: Pendleton 1 0 0 1 0 0 0 1
City 0 1 2 3 4 5 6 7
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
By convention, a vertex is
usually connected to itself
(though, this is not always the case)
1: indicates that there is a path (of zero or more edges) from v
i
to v
j
0: no path from v
i
to v
j
3
k
class Warshall {
static void warshall(int [][] a) { // Input: initial adjacency matrix.
int n = a.length; // Number of vertices.
for (int k = 0; k < n; k++) { // Add paths of length 2 through v
k
.
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) // Add path from v
i
to v
j
a[i][j] |= a[i][k] & a[k][j]; // going through v
k
.
}
}
static void matrixOutput(int [][] a) { // Print matrix.
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++)
System.out.print(" " + a[i][j]);
System.out.println(" ");
}
}
...
}
class Warshall {
...
static public void main (String [] args) {
int [][] adjacency = {{1, 0, 0, 1, 0, 0, 0, 1}, // Initialize
{0, 1, 0, 1, 0, 0, 0, 0}, // adjacency
{0, 0, 1, 0, 0, 1, 0, 1}, // matrix.
{0, 0, 1, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 1, 0, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 1, 1, 0},
{0, 0, 0, 0, 1, 0, 0, 1}};
warshall (adjacency); // Compute all-pairs reachability matrix.
matrixOutput(adjacency); // Print resulting reachability matrix.
}
}
7: Pueblo 0 0 0 0 1 0 0 1
6: Princeton 0 0 0 0 0 1 1 0
5: Pittsburgh 0 1 0 0 0 1 0 0
4: Pierre 1 0 0 0 1 0 0 0
3: Phoenix 0 0 1 1 0 1 0 1
2: Peoria 0 0 1 0 0 1 0 1
1: Pensacola 0 1 0 1 0 0 0 0
0: Pendleton 1 0 0 1 0 0 0 1
City 0 1 2 3 4 5 6 7
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Initial adjacency matrix
7: Pueblo 0 0 0 0 1 0 0 1
6: Princeton 0 0 0 0 0 1 1 0
5: Pittsburgh 0 1 0 1 0 1 0 0
4: Pierre 1 0 0 1 1 0 0 1
3: Phoenix 0 0 1 1 0 1 0 1
2: Peoria 0 0 1 0 0 1 0 1
1: Pensacola 0 1 0 1 0 0 0 0
0: Pendleton 1 0 0 1 0 0 0 1
City
Add all paths of length 2 that go
through vertex 2 (Peoria)
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Edges already exist (doesn’t add
anything new)
Could potentially define paths of
length 4 in this (the third) iteration
7: Pueblo 0 0 0 0 1 0 0 1
6: Princeton 0 0 0 0 0 1 1 0
5: Pittsburgh 0 1 1 1 0 1 0 1
4: Pierre 1 0 1 1 1 1 0 1
3: Phoenix 0 0 1 1 0 1 0 1
2: Peoria 0 0 1 0 0 1 0 1
1: Pensacola 0 1 1 1 0 1 0 1
0: Pendleton 1 0
City 0 1 2 3 4 5 6 7
Add all paths of length 2 that go
through vertex 3 (Phoenix)
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Notice that it also includes the paths
formed in interations 0 and 1
(resulting, in this case, in new
reachability paths of length 3)
Could potentially define paths of
length 5 in this (the fourth) iteration
7: Pueblo 1 1 1 1 1 1 0 1
6: Princeton 1 1 1 1 1 1 1 1
5: Pittsburgh 1 1 1 1 1 1 0 1
4: Pierre 1 1 1 1 1 1 0 1
3: Phoenix 1 1 1 1 1 1 0 1
2: Peoria 1 1 1 1 1 1 0 1
1: Pensacola 1 1 1 1 1 1 0 1
0: Pendleton 1 1 1 1 1 1 0 1
City
Final result: Every city can reach
every other city, except for Princeton
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
The final matrix has a 1 in row i and
column j if vertex v
j
is reachable
from vertex v
i
via some path
static void warshall(int [][] a) { // Input: initial adjacency matrix.
int n = a.length; // Number of vertices.
for (int k = 0; k < n; k++) { // Add paths of length 2 through v
k
.
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) // Add path from v
i
to v
j
a[i][j] |= a[i][k] & a[k][j]; // going through v
k
.
}
}
initially)
7: Pueblo
6: Princeton ∞ ∞ ∞ ∞ ∞ 1 0 ∞
5: Pittsburgh ∞ 4 ∞ ∞ ∞ 0 ∞ ∞
4: Pierre 2 ∞ ∞ ∞ 0 ∞ ∞ ∞
3: Phoenix ∞ ∞ 4 0 ∞ 10 ∞ 3
2: Peoria ∞ ∞ 0 ∞ ∞ 5 ∞ 3
1: Pensacola ∞ 0 ∞ 5 ∞ ∞ ∞ ∞
0: Pendleton 0
City
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Initial adjacency matrix
2
4
3
3
4
3
5
4
10
8
5
2
class Floyd {
static void floyd(double [][] a) { // Input: initial adjacency matrix.
int n = a.length; // Number of vertices.
for (int k = 0; k < n; k++) { // Add paths of length 2 through v
k
.
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) { // Check path from v
i
to v
j
double m = a[i][k] + a[k][j]; // going through v
k
.
if (m < a[i][j]) a[i][j] = m; // Update if less.
}
}
}
...
}
Of course, we can apply a similar improvement as we did
to Warshall’s algorithm
class Floyd {
static void floyd(double [][] a) { // Input: initial adjacency matrix.
int n = a.length; // Number of vertices.
for (int k = 0; k < n; k++) { // Add paths of length 2 through v
k
.
for (int i = 0; i < n; i++) {
double v = a[i][k]; // If there is a path from v
i
to v
k
:
if (v < Double.POSITIVE_INFINITY)
for (int j = 0; j < n; j++) { // Check path from v
i
to v
j
double m = v + a[k][j]; // going through v
k
.
if (m < a[i][j]) a[i][j] = m; // Update if less.
}
}
}
}
...
}
representation?
i
Initialize set of reachable vertices with v
i
and add v
i
to a stack
While stack is not empty
Get and remove (pop) last vertex v from stack
For all neighbors, v
j
, of v
If v
j
is not is set of reachable vertices, add to stack and reachable set