Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16,...
-
Upload
tyrone-kelley -
Category
Documents
-
view
226 -
download
0
description
Transcript of Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16,...
![Page 1: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/1.jpg)
Introduction to Graphs
15-211
Fundamental Data Structures and Algorithms
Klaus SutnerMarch 16, 2004
![Page 2: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/2.jpg)
Announcements
- HW 6 is about to be released.Start asap.
- Reading: Chapter 14 in MAW.
There are quite few definitions there, make sure you understand the ideas andconcepts.
- Extra credit: Write a one-page essay about roving eyeballs.
![Page 3: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/3.jpg)
Introduction to Graphs
![Page 4: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/4.jpg)
Graphs — an overviewvertices
![Page 5: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/5.jpg)
Graphs — an overview
PIT
BOS
JFK
DTW
LAX
SFO
labelled vertices
![Page 6: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/6.jpg)
Graphs — an overview
PIT
BOS
JFK
DTW
LAX
SFO
labelled vertices
edges
![Page 7: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/7.jpg)
undirected
Graphs — an overview
PIT
BOS
JFK
DTW
LAX
SFO
labelled vertices
edges
![Page 8: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/8.jpg)
labelled edges
Graphs — an overview
PIT
BOS
JFK
DTW
LAX
SFO
labelled vertices
1987
2273
3442145
2462
618
211318
190
![Page 9: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/9.jpg)
Terminology
- vertices (aka nodes, points)
- edges (aka arcs, lines)directed or undirected (digraphs and ugraphs)multiple or singleloops
- vertex labels
- edge labels
G = (V,E) or G = (V,E,lab)
![Page 10: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/10.jpg)
Edges
- directed (x,y) or just xy
x is the source and y the target of the edge
- undirected {x,y} or just xy
Note that {x,x} means: undirected loop at x.
- edge is incident upon vertex x and y
![Page 11: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/11.jpg)
Degrees
- directed out-degree of x: the number of edges (x,y) in-degree of x: the number of edges (y,x)
degree: sum of in-degree and out-degree
- undirected degree of x: the number of edges {x,y}
Degrees are often important in determining the running time of an algorithm.
![Page 12: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/12.jpg)
Graphs are Everywhere
![Page 13: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/13.jpg)
Examples
- Roadmaps
- Communication networks
- WWW
- Electrical circuits
- Task schedules
![Page 14: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/14.jpg)
Graphs as models Physical objects are often modeled
by meshes, which are a particular kind of graph structure.
By Jonathan Shewchuk
![Page 15: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/15.jpg)
Web Graph
<href …>
<href …>
<href …>
<href …>
<href …>
<href …>
<href …>
Web Pages are nodes (vertices)HTML references are links (edges)
![Page 16: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/16.jpg)
Relationship graphs Graphs are also used to model
relationships among entities.Scheduling and resource constraints.Inheritance hierarchies.
15-113 15-151
15-211
15-212 15-213
15-312
15-411 15-412
15-251
15-45115-462
![Page 17: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/17.jpg)
More Generally
Suppose we have a system with a collection of possible configurations. Suppose further that a configuration can change into a next configuration (transition, non-deterministic).
Model by a graph
G = ( configurations, transitions )
Evolution of the system corresponds to a path in the graph.
![Page 18: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/18.jpg)
Example: Games
The game of Hanoi with 5 disks corresponds to the following graph:
![Page 19: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/19.jpg)
Solving a Game
A solution is just a path in the graph:
![Page 20: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/20.jpg)
Discrete Math View
Can think of a graph G = (V,E) as a binary relation E on V.
E.g.
G undirected: relation symmetric
G loop-free: relation irreflexive
But this does not address additional labeling and layout information.
![Page 21: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/21.jpg)
Path Problems
A path from vertex a to vertex b in a graph G is a sequence of vertices
a = x0, x1, x2 ,..., xk = b
such that (xi, xi+1) is an edge in G for i = 0,...,k-1.k is the length of the path.
Vertex b is reachable from a if there is a path from a to b.
R(a) is the set of all vertices reachable from a.
![Page 22: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/22.jpg)
Distance
A distance from vertex a to vertex b is the length of the shortest path from a to b (infinity if there is no such path).
If the edges are labeled by a cost (a real number) the length of a path
a = x0, x1, x2 ,..., xk = b
is defined to be the sum of the edge-costs cost(xi, xi+1) .
So in the unlabeled case each edge is assumed to have cost 1.
![Page 23: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/23.jpg)
Exercise
Find a good way to compute the distance of any two Hanoi configurations.
![Page 24: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/24.jpg)
Connectivity
A graph G is connected if R(a) = V for all vertices a.
For an undirected graph this is equivalent to R(a) = V for some vertex a.
A connected component of a ugraph G is a set C that is connected (meaning R(a) = C for all a in C) and that is a maximal such.
For digraphs the situation is more complicated, postpone.
![Page 25: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/25.jpg)
Typical Graph Problems
Connectivity
Given a graph G, check if G is connected.
Connected Components
Given a ugraph G, compute its connected components.
![Page 26: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/26.jpg)
Typical Graph Problems
Shortest Path
Given a graph G and vertices a and b, find a shortest path from a to b.
Distance
Given a graph G, compute the distance between any pair of vertices.
![Page 27: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/27.jpg)
Representing Graphs
![Page 28: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/28.jpg)
Representing Graphs
We need a data structure to represent graphs.
Crucial parameters:
n = number of verticese = number of edges
Note that e may be quadratic in n.
Size of a graph is n + e.
![Page 29: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/29.jpg)
Representing Graphs
Ignoring labels, we may assume that V = {1,2,...,n}.
Need to represent E.
- Edge lists
- Adjacency lists
- Adjacency matrices
- Succinct representation
![Page 30: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/30.jpg)
Supporting Operations
We need to be able to perform operations such as the following:
- insert/delete a vertex- insert/delete an edge- check whether (x,y) is an edge- given x, enumerate its neighbors y
Enumerating neighbors is crucial in many graph algorithms.
Example: Compute degrees.
![Page 31: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/31.jpg)
Edge Lists
A list of pairs (x,y) of vertices.
May be implemented by an array of pairs.
Size: (e)
Running time:
edge query ?
neighbor enumeration ?
![Page 32: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/32.jpg)
Adjacency Lists
An array A of size n of lists of vertices:
A[x] = list of all neighbors of x.
Size: (n+e)
Running time:
edge query ?
neighbor enumeration ?
![Page 33: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/33.jpg)
Adjacency Matrices
An n by n boolean array A:
A[x,y] = true iff (x,y) is an edge.
Size: (n2)
Running time:
edge query ?
neighbor enumeration ?
![Page 34: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/34.jpg)
Adjacency Matrices
Size alone often rules out the use of adjacency matrices.
But for small graphs very important alternative implementation.
Can exploit bit-parallelism or even special purpose parallel hardware (matrix multiplication).
Also a very nice conceptual tool.
![Page 35: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/35.jpg)
Succinct Representation
For large n one often cannot afford to keep an explicit representation of the adjacencies.
But one may be able to get by with functions:
boolean edgeQ( vertex x, vertex y )
VertexList neighbors( vertex x )
Typical example: the web graph.
![Page 36: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/36.jpg)
Example: Edge List
1
4
76
3 5
2 (1,3) (1,4) (2,4) (2,5) (2,4)(3,6) (4,6) (4,7) (5,4) (5,7)
natural order, but could be arbitrarily permuted
![Page 37: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/37.jpg)
Example: Adjacency List
1
4
76
3 5
2
natural order, but lists could be arbitrarily permuted
7654321 3 4
4 563 64 7
7
![Page 38: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/38.jpg)
Example: Adjacency Matrix
1
4
76
3 5
2
76
xx5xxx4
x3xx2
xx17654321
![Page 39: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/39.jpg)
Choosing a representation Size of V relative to size of E is a primary
factor. Dense: e/n is large Sparse: e/n is small Adjacency matrix is expensive if the graph is
sparse. Adjacency list is expensive if the graph is
dense.
Dynamic changes to V. Adjacency matrix is expensive to copy/extend if
V is extended.
![Page 40: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/40.jpg)
A Connectivity Algorithm
How do we test whether a given graph G is connected?
For an undirected graph we can
- pick any vertex v- compute R = R(v)- check if |R| = n
In the directed case we can repeat for all v (there are better algorithms).
Either way, the key problem is to compute R(v).
![Page 41: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/41.jpg)
Inductive Attack
Note that
- v is in R(v)- x in R(v) and (x,y) an edge implies y in R(v)
This can be used to construct R(v) in stages.
Edge (x,y) requires attention if x is in R but y is not.
An edge (that requires attention) is relaxed (or receives attention) when the missing endpoint is placed into R.
![Page 42: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/42.jpg)
A Reachability Algorithm
R = {v};while( some edge (x,y) requires attention )
add y to R; // relax the edge
Claim: The algorithm always terminates.
Proof: An edge can receive attention at most once.
So the loop executes at most e times.
![Page 43: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/43.jpg)
CorrectnessR = {v};while( some edge (x,y) requires attention )
add y to R;
Claim: Upon completion of the algorithm R = R(v).
Proof: “R is a subset of R(v)” is a loop-invariant.
Suppose x is in R(v) but not in R. Choose one such x with minimal distance d from v. Then there is some vertex y that is in R and such that (y,x) is an edge. But then this edge requires attention, contradiction.
![Page 44: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/44.jpg)
Efficiency
R = {v};while( some edge (x,y) requires attention )
add y to R;
We have to specify a way to pick edges that require attention.
Place new vertices into a container (stack or queue) and then check all incident edges.
![Page 45: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/45.jpg)
Breadth First Search
bfs( vertex s ){ Q.enqueue( s ); mark s; // put x into R while( !Q.empty() ) { x = Q.dequeue(); forall (x,y) in E do if( y not marked ) { // relax edges Q.enqueue(y); mark y; // put y into R } }}
![Page 46: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/46.jpg)
BFS
Correctness is already taken care of.
Efficiency:
Using adjacency lists the forall loop is linear in the number of edges starting at vertex x.
So total running time is O(n+e).
How about edge lists? How about adjacency matrices?
![Page 47: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/47.jpg)
BFS and Distance
BFS uses a queue.
As a consequence, vertices are traversed in order of non-decreasing distance from the starting point and we can easily modify the algorithm to compute distance:
dist[v] = 0;...
dist[y] = dist[x] + 1;
Exercise: Prove that this modification really works.
![Page 48: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/48.jpg)
Depth First Search
dfs( vertex x ){ mark x;
forall (x,y) in E do if( y not marked ) dfs( y ); // explore edge}
Stack is hidden via recursion.
![Page 49: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/49.jpg)
DFS
Again: correctness taken care of.
Running time is O(n+e) given adjacency lists.
DFS is a real workhorse: has many variants that solve a number of computational graph theory problems.
![Page 50: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/50.jpg)
Application: Closure
Given a binary relation S on {1,2,...,n}, the transitive reflexive closure trc(S) is the least relation R such that
- x S y implies x R y- x R x for all x- x R y and y R z implies x R z
If we model the relation by a graph, trc(S) can be computed by repeated calls to DFS (or BFS).
Good solution if the graph is sparse.
![Page 51: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/51.jpg)
Warshall's Algorithm
But when S is dense one might as well bite the bullet and use a cubic (in n) algorithm that has good constants.
Compute a n by n by n boolean matrix B whose first slice B[.,.,0] is the adjacency matrix of S plus diagonal:
for( k = 1; i <= n; k++ ) for( x = 1; x <= n; i++ ) for( y = 1; y <= n; j++ ) B[x,y,k] = B[x,y,k-1] || ( B[x,k,k-1] && B[k,y,k-1] );
Upon completion, B[.,.,n] is the adjacency matrix for the transitive reflexive closure of S.
![Page 52: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/52.jpg)
Warshall's Algorithm
for( k = 1; i <= n; k++ ) for( x = 1; x <= n; i++ ) for( y = 1; y <= n; j++ ) B[x,y,k] = B[x,y,k-1] || ( B[x,k,k-1] && B[k,y,k-1] );
Upon completion, B[.,.,n] is the adjacency matrix for the transitive reflexive closure of S.
What is the space complexity of this method?
![Page 53: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Klaus Sutner March 16, 2004.](https://reader035.fdocuments.net/reader035/viewer/2022062311/5a4d1b5a7f8b9ab0599aaa79/html5/thumbnails/53.jpg)
Warshall's Algorithm
Code is beautifully simple, but correctness is far from obvious.
Claim: B[x,y,k] = 1 iff there is a path from x to y using only intermediate vertices in {1,2,...,k}.
Proof: By induction on k.
Effectively we erase vertices higher than k and then put them back in.
Example of dynamic programming, more later.