0% found this document useful (0 votes)
31 views

COMP2521 Week 6 - Digraph Algorithms

The document discusses algorithms for digraph (directed graph) traversal, cycle checking, and transitive closure. It describes breadth-first search (BFS) and depth-first search (DFS) algorithms for digraph traversal. For cycle checking in digraphs, it notes that the algorithm used for undirected graphs does not work and provides pseudocode of a modified DFS approach. However, it also identifies that this modified approach still does not correctly detect all cycles in digraphs.

Uploaded by

Hysan Dax
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

COMP2521 Week 6 - Digraph Algorithms

The document discusses algorithms for digraph (directed graph) traversal, cycle checking, and transitive closure. It describes breadth-first search (BFS) and depth-first search (DFS) algorithms for digraph traversal. For cycle checking in digraphs, it notes that the algorithm used for undirected graphs does not work and provides pseudocode of a modified DFS approach. However, it also identifies that this modified approach still does not correctly detect all cycles in digraphs.

Uploaded by

Hysan Dax
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

COMP2521

23T3

Traversal

Cycle
Checking

Transitive
Closure COMP2521 23T3
Digraph Algorithms

Kevin Luxa
[email protected]

digraph traversal
cycle checking
warshall’s algorithm

COMP2521
23T3 Directed Graphs (Digraphs)
Traversal

Cycle
Checking

Transitive
Closure

Reminder: directed graphs are graphs where…


• Each edge (v, w) has a source v and a destination w
• Unlike undirected graphs, v → w 6= w → v
COMP2521
23T3 Digraph Applications
Traversal

Cycle
Checking

Transitive
Closure

domain vertex is… edge is…


WWW web page hyperlink
chess board state legal move
scheduling task precedence
program function function call
journals article citation
make target dependency

COMP2521
23T3 Digraph Traversal
Traversal
Application

Cycle
Checking

Transitive
Closure
Same as for undirected graphs:

bfs(G, src):
dfs(G, src):
initialise visited array
initialise visited array
mark src as visited
dfsRec(G, src, visited)
enqueue src into Q
while Q is not empty:
dfsRec(G, v, visited):
v = dequeue from Q
mark v as visited
for each edge (v, w) in G:
for each edge (v, w) in G:
if visited[w] = false:
if w has not been visited:
mark w as visited
dfsRec(G, w, visited)
enqueue w into Q
COMP2521
23T3 Digraph Traversal
Application - Web Crawling
Traversal
Application

Cycle
Checking

Transitive
Closure

Web crawling
Visit a subset of the web…
…to index
…to cache locally

Which traversal method? BFS or DFS?

Note: we can’t use a visited array, as we don’t know how many webpages
there are. Instead, use a visited set.

COMP2521
23T3 Digraph Traversal
Application - Web Crawling
Traversal
Application

Cycle Web crawling algorithm:


Checking

Transitive webCrawl(startingUrl, maxPagesToVisit):


Closure
create visited set
add startingUrl to visited set
enqueue startingUrl into Q

numPagesVisited = 0
while Q is not empty and numPagesVisited < maxPagesToVisit:
currPage = dequeue from Q

visit currPage
numPagesVisited = numPagesVisited + 1

for each hyperlink on currPage:


if hyperlink not in visited set:
add hyperlink to visited set
enqueue hyperlink into Q
COMP2521
23T3 Digraph Traversal
Application - Web Crawling
Traversal
Application

Cycle
Checking

Transitive
Closure

Wiki Game

Given two Wikipedia articles,


navigate from the first article to the second
in as few clicks as possible.

COMP2521
23T3 Cycle Checking
Traversal

Cycle
Checking
Pseudocode
Example
In directed graphs,
Transitive a cycle is a directed path
Closure
where the start vertex = end vertex

1 3
0
2
4
5 6

This graph has three distinct cycles:


0-4-0, 2-5-6-2, 3-3
COMP2521
23T3 Cycle Checking
Traversal
Recall: Cycle checking for undirected graphs:
Cycle
Checking
Pseudocode
hasCycle(G):
Example initialise visited array to false
Transitive for each vertex v in G:
Closure if visited[v] = false:
if dfsHasCycle(G, v, v, visited):
return true

return false
Does this work for
dfsHasCycle(G, v, prev, visited): directed graphs?
visited[v] = true

for each edge (v, w) in G:


if w = prev:
continue
if visited[w] = true:
return true
else if dfsHasCycle(G, w, v, visited):
return true

return false

COMP2521
23T3 Cycle Checking
Traversal
Recall: Cycle checking for undirected graphs:
Cycle
Checking
Pseudocode
hasCycle(G):
Example initialise visited array to false
Transitive for each vertex v in G:
Closure if visited[v] = false:
if dfsHasCycle(G, v, v, visited):
return true

return false
Does this work for
dfsHasCycle(G, v, prev, visited): directed graphs?
visited[v] = true

for each edge (v, w) in G: No


if w = prev:
continue
if visited[w] = true:
return true
else if dfsHasCycle(G, w, v, visited):
return true

return false
COMP2521
23T3 Cycle Checking
Traversal

Cycle
Checking
Pseudocode
Example

Transitive
Closure Problem #1

Algorithm ignores edge to previous vertex


and therefore does not detect the following cycle:

0 1

Simple fix: Don’t ignore edge to previous vertex

COMP2521
23T3 Cycle Checking
Traversal

Cycle
Checking
Pseudocode hasCycle(G):
Example
initialise visited array to false
Transitive for each vertex v in G:
Closure if visited[v] = false:
if dfsHasCycle(G, v, visited):
return true

return false Does this work for


dfsHasCycle(G, v, visited): directed graphs?
visited[v] = true

for each edge (v, w) in G:


if visited[w] = true:
return true
else if dfsHasCycle(G, w, visited):
return true

return false
COMP2521
23T3 Cycle Checking
Traversal

Cycle
Checking
Pseudocode hasCycle(G):
Example
initialise visited array to false
Transitive for each vertex v in G:
Closure if visited[v] = false:
if dfsHasCycle(G, v, visited):
return true

return false Does this work for


dfsHasCycle(G, v, visited): directed graphs?
visited[v] = true
No!
for each edge (v, w) in G:
if visited[w] = true:
return true
else if dfsHasCycle(G, w, visited):
return true

return false

COMP2521
23T3 Cycle Checking
Traversal

Cycle
Checking
Pseudocode
Problem #2
Example

Transitive Algorithm can detect cycles when there is none,


Closure
for example:

0
2

Algorithm starts at 0, recurses into 1 and 2,


backtracks to 0, sees that 2 has been visited,
and concludes there is a cycle
COMP2521
23T3 Cycle Checking
Traversal

Cycle
Checking
Pseudocode
Example

Transitive Consider a cycle check on this graph (starting at 0):


Closure

0 cycle(0, prev=0)
2 call stack
[0] [1] [2]

visited 1 0 0

COMP2521
23T3 Cycle Checking
Traversal

Cycle
Checking
Pseudocode
Example

Transitive Consider a cycle check on this graph (starting at 0):


Closure

0 cycle(0, prev=0)
2 call stack
[0] [1] [2]

visited 1 0 0
COMP2521
23T3 Cycle Checking
Traversal

Cycle
Checking
Pseudocode
Example

Transitive Consider a cycle check on this graph (starting at 0):


Closure

1
cycle(1, prev=0)
0 cycle(0, prev=0)
2 call stack
[0] [1] [2]

visited 1 1 0

COMP2521
23T3 Cycle Checking
Traversal

Cycle
Checking
Pseudocode
Example

Transitive Consider a cycle check on this graph (starting at 0):


Closure

1
cycle(1, prev=0)
0 cycle(0, prev=0)
2 call stack
[0] [1] [2]

visited 1 1 0
COMP2521
23T3 Cycle Checking
Traversal

Cycle
Checking
Pseudocode
Example

Transitive Consider a cycle check on this graph (starting at 0):


Closure

1 cycle(2, prev=1)
cycle(1, prev=0)
0 cycle(0, prev=0)
2 call stack
[0] [1] [2]

visited 1 1 1

COMP2521
23T3 Cycle Checking
Traversal

Cycle
Checking
Pseudocode
Example

Transitive Consider a cycle check on this graph (starting at 0):


Closure

1 cycle(2, prev=1)
cycle(1, prev=0)
0 cycle(0, prev=0)
2 call stack
[0] [1] [2]

visited 1 1 1
COMP2521
23T3 Cycle Checking
Traversal

Cycle
Checking
Pseudocode
Example

Transitive
Idea:
Closure

To properly detect a cycle,


check if neighbour is already on the call stack

When the graph is undirected,


this can be done by checking the visited array,
but this doesn’t work for directed graphs!

Need to use separate array to


keep track of when a vertex is on the call stack

COMP2521
23T3 Cycle Checking
Pseudocode
Traversal

Cycle hasCycle(G):
Checking create visited array, initialised to false
Pseudocode
create onStack array, initialised to false
Example

Transitive for each vertex v in G:


Closure
if visited[v] = false:
if dfsHasCycle(G, v, visited, onStack):
return true

return false

dfsHasCycle(G, v, visited, onStack):


visited[v] = true
onStack[v] = true

for each edge (v, w) in G:


if onStack[w] = true:
return true
else if visited[w] = false:
if dfsHasCycle(G, w, visited, onStack):
return true

onStack[v] = false
return false
COMP2521
23T3 Cycle Checking
Example
Traversal

Cycle
Checking
Pseudocode
Example

Transitive Check if a cycle exists in this graph:


Closure

5 3

0 1

COMP2521
23T3 Transitive Closure
Traversal

Cycle
Checking

Transitive
Closure
Warshall’s algorithm

Problem: computing reachability

Given a digraph G it is potentially useful to know:


• Is vertex t reachable from vertex s?
COMP2521
23T3 Transitive Closure
Traversal

Cycle
Checking

Transitive
Closure
Warshall’s algorithm

One way to implement a reachability check:


• Use BFS or DFS starting at s
• This is O(V + E) in the worst case
• Only feasible if reachability is an infrequent operation

What about applications that frequently need to check reachability?

COMP2521
23T3 Transitive Closure
Traversal

Cycle
Checking

Transitive
Closure
Warshall’s algorithm Idea

Construct a V × V matrix
that tells us whether there is a path (not edge)
from s to t, for s, t ∈ V

This matrix is called the transitive closure (tc) matrix


(or reachability matrix)

tc[s][t] is true if there is a path from s to t, false otherwise


COMP2521
23T3 Transitive Closure
Traversal 0 1
Cycle
Checking

Transitive
Closure
5 6 2
Warshall’s algorithm

4 3

[0] [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] [6]

[0] 0 0 0 0 0 0 1 [0] 1 1 1 0 0 0 1
[1] 1 0 1 0 0 0 0 [1] 1 1 1 0 0 0 1
[2] 0 0 1 0 0 0 0 [2] 0 0 1 0 0 0 0
[3] 0 0 1 0 1 0 0 [3] 0 0 1 1 1 0 1
[4] 0 0 0 1 0 0 1 [4] 0 0 1 1 1 0 1
[5] 0 0 0 0 0 0 1 [5] 1 1 1 0 0 0 1
[6] 0 1 1 0 0 0 0 [6] 1 1 1 0 0 0 1
adjacency matrix reachability matrix

COMP2521
23T3 Transitive Closure
Traversal

Cycle
Checking

Transitive
Closure
Warshall’s algorithm

One way to compute reachability matrix:


• Perform BFS/DFS from every vertex

Another way ⇒ Warshall’s algorithm:


• Simple algorithm that does not require a graph traversal
COMP2521
23T3 Warshall’s Algorithm
Traversal

Cycle
Main idea:
Checking • There is a path from s to t if:
Transitive
Closure
• There is an edge from s to t, or
Warshall’s algorithm
Pseudocode
Example
Analysis

0 2

1 3

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

COMP2521
23T3 Warshall’s Algorithm
Traversal

Cycle
Main idea:
Checking • There is a path from s to t if:
Transitive
Closure
• There is an edge from s to t, or
Warshall’s algorithm • There is a path from s to t via vertex 0, or
Pseudocode
Example
Analysis

0 2 0 2

1 3 1 3

0 0 1 0 0 0 1 0
1 0 0 1 1 0 1 1
0 0 0 0 0 0 0 0
0 1 0 0 0 1 0 0
COMP2521
23T3 Warshall’s Algorithm
Traversal

Cycle
Main idea:
Checking • There is a path from s to t if:
Transitive
Closure
• There is an edge from s to t, or
Warshall’s algorithm • There is a path from s to t via vertex 0, or
• There is a path from s to t via vertex 0 and/or 1, or
Pseudocode
Example
Analysis

0 2 0 2 0 2

1 3 1 3 1 3

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

COMP2521
23T3 Warshall’s Algorithm
Traversal

Cycle
Main idea:
Checking • There is a path from s to t if:
Transitive
Closure
• There is an edge from s to t, or
Warshall’s algorithm • There is a path from s to t via vertex 0, or
There is a path from s to t via vertex 0 and/or 1, or
Pseudocode
Example •
There is a path from s to t via vertex 0, 1 and/or 2, or
Analysis

0 2 0 2 0 2 0 2

1 3 1 3 1 3 1 3

0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0
1 0 0 1 1 0 1 1 1 0 1 1 1 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 1 0 0 1 1 1 1 1 1 1 1
COMP2521
23T3 Warshall’s Algorithm
Traversal

Cycle
Main idea:
Checking • There is a path from s to t if:
Transitive
Closure
• There is an edge from s to t, or
Warshall’s algorithm • There is a path from s to t via vertex 0, or
There is a path from s to t via vertex 0 and/or 1, or
Pseudocode
Example •
There is a path from s to t via vertex 0, 1 and/or 2, or
Analysis

• …
• There is a path from s to t via any of the other vertices

0 2 0 2 0 2 0 2 0 2

1 3 1 3 1 3 1 3 1 3

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

COMP2521
23T3 Warshall’s Algorithm
Traversal

Cycle
Checking
On the k-th iteration, the algorithm determines if a path exists between two
Transitive vertices s and t using just 0, … , k as intermediate vertices
Closure
Warshall’s algorithm
Pseudocode

On the k-th iteration


Example
Analysis
k
s If we have:
(1) a path from s to k
(2) a path from k to t
(using only vertices 0 to k − 1)

t
COMP2521
23T3 Warshall’s Algorithm
Traversal

Cycle
Checking
On the k-th iteration, the algorithm determines if a path exists between two
Transitive vertices s and t using just 0, … , k as intermediate vertices
Closure
Warshall’s algorithm
Pseudocode

On the k-th iteration


Example
Analysis
k
s If we have:
(1) a path from s to k
(2) a path from k to t
(using only vertices 0 to k − 1)

Then we have a path from s to t


t
using vertices from 0 to k

if tc[s][k] and tc[k][t]:


tc[s][t] = true

COMP2521
23T3 Warshall’s Algorithm
Pseudocode
Traversal

Cycle
Checking

Transitive warshall(A):
Closure
Warshall’s algorithm
Inputs: n × n adjacency matrix A
Pseudocode Output: n × n reachability matrix
Example
Analysis
create tc matrix, initialised to false

for each vertex s in G:


for each vertex t in G:
if A[s][t]:
tc[s][t] = true

for each vertex k in G: // from 0 to n - 1


for each vertex s in G:
for each vertex t in G:
if tc[s][k] and tc[k][t]:
tc[s][t] = true

return tc
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Find transitive closure of this graph


Closure
Warshall’s algorithm
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 0 1
[2] 0 0 0 0
1 3
[3] 0 1 0 0

COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Initialise tc with edges of original graph


Closure
Warshall’s algorithm
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 0 1
[2] 0 0 0 0
1 3
[3] 0 1 0 0
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive First iteration: k = 0


Closure
Warshall’s algorithm
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 0 1
[2] 0 0 0 0
1 3
[3] 0 1 0 0

COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive First iteration: k = 0


Closure
Warshall’s algorithm
There is a path 1 → 0 and a path 0 → 2
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 0 1
[2] 0 0 0 0
1 3
[3] 0 1 0 0
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive First iteration: k = 0


Closure
Warshall’s algorithm
There is a path 1 → 0 and a path 0 → 2
Pseudocode
Example So there is a path 1 → 2
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 1 1
[2] 0 0 0 0
1 3
[3] 0 1 0 0

COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive First iteration: k = 0


Closure
Warshall’s algorithm Done
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 1 1
[2] 0 0 0 0
1 3
[3] 0 1 0 0
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Second iteration: k = 1


Closure
Warshall’s algorithm
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 1 1
[2] 0 0 0 0
1 3
[3] 0 1 0 0

COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Second iteration: k = 1


Closure
Warshall’s algorithm
There is a path 3 → 1 and a path 1 → 0
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 1 1
[2] 0 0 0 0
1 3
[3] 0 1 0 0
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Second iteration: k = 1


Closure
Warshall’s algorithm
There is a path 3 → 1 and a path 1 → 0
Pseudocode
Example So there is a path 3 → 0
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 1 1
[2] 0 0 0 0
1 3
[3] 1 1 0 0

COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Second iteration: k = 1


Closure
Warshall’s algorithm
There is a path 3 → 1 and a path 1 → 2
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 1 1
[2] 0 0 0 0
1 3
[3] 1 1 0 0
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Second iteration: k = 1


Closure
Warshall’s algorithm
There is a path 3 → 1 and a path 1 → 2
Pseudocode
Example So there is a path 3 → 2
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 1 1
[2] 0 0 0 0
1 3
[3] 1 1 1 0

COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Second iteration: k = 1


Closure
Warshall’s algorithm
There is a path 3 → 1 and a path 1 → 3
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 1 1
[2] 0 0 0 0
1 3
[3] 1 1 1 0
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Second iteration: k = 1


Closure
Warshall’s algorithm
There is a path 3 → 1 and a path 1 → 3
Pseudocode
Example So there is a path 3 → 3
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 1 1
[2] 0 0 0 0
1 3
[3] 1 1 1 1

COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Second iteration: k = 1


Closure
Warshall’s algorithm Done
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 1 1
[2] 0 0 0 0
1 3
[3] 1 1 1 1
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Third iteration: k = 2


Closure
Warshall’s algorithm
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 1 1
[2] 0 0 0 0
1 3
[3] 1 1 1 1

COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Third iteration: k = 2


Closure
Warshall’s algorithm
No pairs (s, t) such that there are paths s → 2 and 2 → t
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 1 1
[2] 0 0 0 0
1 3
[3] 1 1 1 1
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Third iteration: k = 2


Closure
Warshall’s algorithm Done
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 1 1
[2] 0 0 0 0
1 3
[3] 1 1 1 1

COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Fourth iteration: k = 3


Closure
Warshall’s algorithm
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 1 1
[2] 0 0 0 0
1 3
[3] 1 1 1 1
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Fourth iteration: k = 3


Closure
Warshall’s algorithm
There is a path 1 → 3 and a path 3 → 1
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 0 1 1
[2] 0 0 0 0
1 3
[3] 1 1 1 1

COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Fourth iteration: k = 3


Closure
Warshall’s algorithm
There is a path 1 → 3 and a path 3 → 1
Pseudocode
Example So there is a path 1 → 1
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 1 1 1
[2] 0 0 0 0
1 3
[3] 1 1 1 1
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Fourth iteration: k = 3


Closure
Warshall’s algorithm Done
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 1 1 1
[2] 0 0 0 0
1 3
[3] 1 1 1 1

COMP2521
23T3 Warshall’s Algorithm
Example
Traversal

Cycle
Checking

Transitive Finished
Closure
Warshall’s algorithm
Pseudocode
Example
Analysis

[0] [1] [2] [3]


0 2
[0] 0 0 1 0
[1] 1 1 1 1
[2] 0 0 0 0
1 3
[3] 1 1 1 1
COMP2521
23T3 Warshall’s Algorithm
Analysis
Traversal

Cycle
Checking

Transitive
Closure
Warshall’s algorithm
Pseudocode
Example
Analysis
Analysis:
• Time complexity: O(V 3 )
• Three nested loops iterating over all vertices
• Space complexity: O(V 2 )
• Reachability matrix is V × V
• Benefit: checking reachability between vertices is now O(1)
• Makes up for slow setup (O(V 3 )) if reachability is a very frequent operation

COMP2521
23T3 Feedback
Traversal

Cycle
Checking

Transitive https://forms.office.com/r/aPF09YHZ3X
Closure
Warshall’s algorithm
Pseudocode
Example
Analysis

You might also like