Graph path algorithms

Graphs
Path Algorithms
© Gunnar Gotshalks
GraphPaths–1
Path Existence
• Path existence is a fundamental question
• Adjacency matrix corresponds well to the problem
»
Shows existence of paths of length 1 between vertices
• Consider matrix multiplication, the following is one term in
the product
n
Cr,c =
" Ar, p * B p,c
p=1
= Ar,1 * B1,c + Ar,2 * B2,c + ...+ Ar,n * Bn,c
• What does it mean when A = B and A is an adjacency
matrix?
!
© Gunnar Gotshalks
GraphPaths–2
Path Existence – 2
n
Cr,c =
" Ar, p * A p,c
p=1
= Ar,1 * A1,c + Ar,2 * A2,c + ...+ Ar,n * An,c
• Assume we store 0 and 1 in the adjacency matrix
»
!
A [ r , p ] = 1 means an edge goes from r to p
A [p , c ] = 1 means an edge goes from p to k
A [ r , p ] * A [p , c ] = 1 means a path exists between r & c
> In particular, the path is of length 2 and goes through
vertex p
© Gunnar Gotshalks
GraphPaths–3
Path Existence – 3
n
Cr,c =
" Ar, p * A p,c
p=1
= Ar,1 * A1,c + Ar,2 * A2,c + ...+ Ar,n * An,c
• As a consequence
»
!
C [ r , c ] ≠ 0 ↔ a path of length 2 exists between r and c
∃ p : 1 .. N • A [ r , p ] * A [p , c ] ≠ 0
> In fact, C [ r , c ] = q , means there are q paths of length
2 between r and c
© Gunnar Gotshalks
GraphPaths–4
Path Existence – 4
A2 = A * A
• The above expression, gives the number of paths of length
two between any pair of vertices
! Ap, gives the number of paths of length p
• By extension,
between any pair of vertices
• The following expression gives the number of paths of
lengths 1 .. n (the longest interesting path, if one considers a
Hamiltonian cycle as a path) between any pair of vertices
P = A + A 2 + A 3 + ...+ A n"1 + A n
© Gunnar Gotshalks
!
GraphPaths–5
Computing the Path Existence Matrix
• We can naively compute the expression as written
• O ( N max ( O ( matrix mult ) , O ( matrix add ) )
P←A
T←A
// Paths of length 1
// For computing longer paths
LI: ∀ j : 1 .. k–1 • P includes paths of length j
∧ T gives paths of length k–1
for k : 2 .. n do
T←T*A
P←P+T
end
© Gunnar Gotshalks
// Compute paths of length k and add to P
// Paths one edge longer
// Include the longer paths
GraphPaths–6
Matrix Multiplication
• O(N3)
Loop invariants
n
Cr,c =
" Ar, p * B p,c
p=1
for r : 1 .. n do // Rows of A
C [ 1 .. r–1 , * ]
contains result
!
C [ 1 .. r–1 , 1 .. c–1 ]
contains result
t = ∑( A[ r , 1..p–1 ]
* B[ 1..p–1 , c ] )
© Gunnar Gotshalks
for c : 1 .. n do
t←0
// Columns of B
// Initial result
for p : 1 .. n do
t←t+A[r,p]*B[p,c]
end
C[r,c]←t
end
end
GraphPaths–7
Matrix Addition
• O(N2)
Cr,c = Ar,c + Br,c
Loop invariants
C [ 1 .. r–1 , * ] !
contains result
C [ 1 .. r–1 , 1 .. c–1 ]
contains result
© Gunnar Gotshalks
for r : 1 .. n do // Rows of A & B
for c : 1 .. n do
// Columns of A & B
C [ r , c ] ← A [r , c ] + B [r , c ]
end
end
GraphPaths–8
Big O Naïve Path Computation
• O ( N max ( O ( matrix mult ) , O ( matrix add ) )
• Substituting from the previous slides
»
O ( N * N 3 ) = O ( N4 )
• Using the Generalized Warshall algorithm, we can reduce
this to
»
O ( N3 )
© Gunnar Gotshalks
GraphPaths–9
Boolean Path Matrix
• Suppose we want a Boolean path matrix that has True if
and only if a path of some length exists between vertices
»
We can modify the previous algorithm to use the logical
operators ∨ and ∧ in place of + and *
• But Warshall discovered a more efficient algorithm than
the previous one, which was simplified by Floyd.
© Gunnar Gotshalks
GraphPaths–10
Floyd-Warshall Algorithm
P←A
for p : 1 .. n do
// Start with adjacency matrix
// Extend paths from length 1 to p
for r : 1 .. n do
// Rows of the result
if P [ r , p ] then
// Have something to add
for c : 1 .. n do
// Columns of the result
P [ r , c ] ← P [ r , c ] ∨( (P [ r , p ] ∧ P [ p , c ] ))
end
fi
end
end
© Gunnar Gotshalks
existing paths r to c
plus
paths from r to p joined with
paths from p to c
GraphPaths–11
Variations
• The Floyd-Warshall algorithm, as given, results in a matrix
that shows if a path of any length exists between every
pair of vertices
> It will include paths with self-loops and cycles
• Suppose we want the following matrices
»
»
The shortest path
The list of edges for each path in all possible paths
• These and many other algorithms are variations on the
algorithm
© Gunnar Gotshalks
GraphPaths–12
Generalized Floyd-Warshall Algorithm
• Matrix multiplication is a special case of operating on
matrices with two different operators
»
»
For matrix multiplication we use the pair < + , * >
But other pairs of operators are possible
<∨,
<
∧>
∨ , || >
or & and
or & string catenation
< min , + >
And many others depending upon the functions you
want to provide and the result wanted
• We can write a generalized algorithm to take all possible
variations into account
© Gunnar Gotshalks
GraphPaths–13
Generalized Floyd-Warshall Algorithm – 2
O ( N3 * max ( O ( ⊕ ) , O ( ⊗ ) ) )
P←A
for c : 1 .. n do
for r : 1 .. n do
suitable condition
is satisfied
combine with
general plus
if P [ r , c ] satisfies COND then
join with
general times
for p : 1 .. n do
P [ r , p ] ← P [ r , p ] ⊕ (P [ r , p ] ⊗ P [ c , p ] )
end
fi
end
end
© Gunnar Gotshalks
path info r to p
path info c to p
path info r to c
Data Structures Theory and Practice
A. T. Berztiss, Academic Press, 1968, p206-207
GraphPaths–14
Variation 1: Boolean Path Matrix
• Adjacency matrix is Boolean
> Substitute
∨ for ⊕
and
∧ for ⊗
> The condition becomes
if P [ r , c ] then
> The work statement becomes
P [ r , c ] ← P [ r , c ] ∨ (P [ r , p ] ∧ P [ p , c ] )
⇒ P[r,c]←P[r,c]∨P[p,c]
• Result on conclusion of the algorithm
∀ r , c : V(G) • P [ r , c ] ↔ a path exists from r to c
© Gunnar Gotshalks
GraphPaths–15
Variation 2: Minimum Path Length
• Adjacency matrix contains edge lengths, where no edge is
∞ (too large a value for the result)
> Substitute
min for ⊕
and
+ for ⊗
> The condition becomes
if P [ r , c ] != ∞ then
> The work statement becomes
P [ r , c ] ← min ( P [ r , p ] , ( P [ r , p ] + P [ p , c ] ) )
• Result on conclusion of the algorithm
∀ r , c : V(G) • P [ r , c ] = minimum length of a path
from r to c
© Gunnar Gotshalks
GraphPaths–16
Variation 3: Edge Names for Minimum Path
• Adjacency matrix contains
»
Edge lengths and lists of names of edges
> Substitute
MPN for ⊕ and CLN for ⊗
where MPN keeps minimum length and the name of the
corresponding path
CLN adds to path length and catenates the names
of the sub-paths
© Gunnar Gotshalks
GraphPaths–17
Variation 4: Edge Names in all Paths
• Adjacency matrix contains lists of names of the edges,
where a name is a string
> Substitute
AL for ⊕ and CLP for ⊗
where AL is “Add to List”
CLP is “Create list of all possible pairs”
> The condition becomes
if ~ isEmptyList ( P [ r , c ] ) then
> The work statement becomes
P [ r , p ] ← AL ( P [ r , p ] , CLP ( P [ r , c ] , P [ c , p ] ) )
• Result on conclusion of the algorithm
∀ r , c : V(G) • P [ r , c ] is a list of edge names that
give a path from r to c
© Gunnar Gotshalks
GraphPaths–18
Variation 4: Example Path Names
• Assume single character names for edges
• Suppose
P [ r , p ] contains < ABC , DEF , HIJK > – 3 paths
P [ r , c ] contains < AB , XY >
– 2 paths
P [ c , p ] contains < L , M , PQR >
– 3 paths
• Then
P [ r , p ] ← AL ( P [ r , p ] , CLP ( P [ r , c ] , P [ c , p ] ) )
• Gives the result
P [ r , p ] = < ABC , DEF , HIJK
, ABL , ABM , ABPQR
– original paths
– joined paths
, XYL , XYM , XYPQR >
© Gunnar Gotshalks
GraphPaths–19
Applicable to Semirings
• Limited only by your imagination
» Provided elements of the matrix and the operators are a
semiring – < S , ⊕ , ⊗ , 0 , 1 > has the following properties
S is a set of elements
< S , ⊕ , 0 > has the following properties
– (a ⊕ b ) ⊕ c = a ⊕ ( b ⊕ c )
associative
– 0⊕a=a⊕0=a
identity
– a⊕b=b⊕a
commutative
< S , ⊗ , 1 > has the following properties
– (a ⊗ b) ⊗ c = a ⊗ (b ⊗ c)
associative
– 1⊗a=a⊗1=a
identity
Combined properties
– a ⊗ ( b ⊕ c ) = (a ⊗ b) ⊕ (a ⊗ c )
– (a ⊕ b ) ⊗ c = (a ⊗ c) ⊕ (b ⊗ c )
– 0⊗a=a⊗0=0
© Gunnar Gotshalks
distributive
distributive
annihilation
GraphPaths–20
Applicable to Semirings– 2
»
If the elements of the adjacency matrix are in square
matrices A, B and C then the following holds for matrix
multiplication and matrix addition.
n
> C = A * B is computed using
Cr,c = " Ar, p * B p,c
p=1
> C = A + B is computed using
Cr,c = Ar,c + Br,c
!
– While < N , + , * , 0 , 1 > is a semiring the FloydWharshall algorithm!fails to find the number of paths,
which is the expected result
© Gunnar Gotshalks
GraphPaths–21
Transitive Closure
• The transitive closure graph T of a graph G is
»
V(T)=V(G)
» ∀ u , v : V  path ( u , v ) in G • < u , v > ∈ E ( T )
> If a path exists in G between vertices u and v,
then edge < u , v > is in T
• The transitive closure is computed using the generalized
Floyd-Warshall algorithm with the operators ∨ and ∧
© Gunnar Gotshalks
GraphPaths–22
Transitive Closure Example
1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 0
10 0
11 0
12 0
2
1
1
1
1
1
1
1
1
0
0
0
0
3
1
1
1
1
1
1
1
1
0
0
0
0
4
1
1
1
1
1
1
1
1
0
0
0
0
5
1
1
1
1
1
1
1
1
0
0
0
0
6
1
1
1
1
1
1
1
1
0
0
0
0
7
1
1
1
1
1
1
1
1
0
0
0
0
8
1
1
1
1
1
1
1
1
0
0
0
0
9 10 11 12
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 1 1 0
1 1 1 0
1 1 1 0
0 0 0 0
Blocks of 1’s down the main diagonal give the
Connected components – note 0 on the main diagonal
is a single vertex component with no self-loop
© Gunnar Gotshalks
GraphPaths–23
Weighted Graph
• Variations 2..4 of the Floyd-Warshall algorithm were done
using weighted graphs
• A weighted graph is a triple
»
»
G=<V,E,W>
W is a set of weights or attributes associated with the
edge
> An edge is more than a pair of vertices
• Some meanings of weight
»
»
»
Length of an edge (distance between cities)
Flow capacity (oil pipeline, network connection speed)
Name of edge – airline flight number, highway number
© Gunnar Gotshalks
GraphPaths–24
Finding a Path with Backtracking Traversal
Require initial call ∀ v : V ( G ) • v . visited = False
initial call stack . isEmpty
vertex ≠ Void ∧ ~ vertex . visited
O ( V–1! )
path ( vertex, endVertex : Vertex_Descriptor ) is
stack . push ( vertex ) ; vertex . visited ← True
if vertex = endVertex then process_path_in_stack
else
adjVertex ← vertex . adjacent_list . first
while adjVertex ≠ Void do
if ~ adjVertex . visited then
An example of
path ( adjVertex , endVertex )
a backtracking
fi
algorithm
adjVertex ← vertex . adjacent_list . next
end
fi
vertex . visited ← False ; stack . pop
end
© Gunnar Gotshalks
GraphPaths–25