COMP2521 Week 6 - Digraph Algorithms
COMP2521 Week 6 - Digraph Algorithms
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
Cycle
Checking
Transitive
Closure
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
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
numPagesVisited = 0
while Q is not empty and numPagesVisited < maxPagesToVisit:
currPage = dequeue from Q
visit currPage
numPagesVisited = numPagesVisited + 1
Cycle
Checking
Transitive
Closure
Wiki Game
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
return false
Does this work for
dfsHasCycle(G, v, prev, visited): directed graphs?
visited[v] = 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
return false
COMP2521
23T3 Cycle Checking
Traversal
Cycle
Checking
Pseudocode
Example
Transitive
Closure Problem #1
0 1
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
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
COMP2521
23T3 Cycle Checking
Traversal
Cycle
Checking
Pseudocode
Problem #2
Example
0
2
Cycle
Checking
Pseudocode
Example
0 cycle(0, prev=0)
2 call stack
[0] [1] [2]
visited 1 0 0
COMP2521
23T3 Cycle Checking
Traversal
Cycle
Checking
Pseudocode
Example
0 cycle(0, prev=0)
2 call stack
[0] [1] [2]
visited 1 0 0
COMP2521
23T3 Cycle Checking
Traversal
Cycle
Checking
Pseudocode
Example
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
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
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
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
COMP2521
23T3 Cycle Checking
Pseudocode
Traversal
Cycle hasCycle(G):
Checking create visited array, initialised to false
Pseudocode
create onStack array, initialised to false
Example
return false
onStack[v] = false
return false
COMP2521
23T3 Cycle Checking
Example
Traversal
Cycle
Checking
Pseudocode
Example
5 3
0 1
COMP2521
23T3 Transitive Closure
Traversal
Cycle
Checking
Transitive
Closure
Warshall’s algorithm
Cycle
Checking
Transitive
Closure
Warshall’s algorithm
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
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
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
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
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
return tc
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal
Cycle
Checking
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal
Cycle
Checking
Cycle
Checking
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal
Cycle
Checking
Cycle
Checking
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal
Cycle
Checking
Cycle
Checking
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal
Cycle
Checking
Cycle
Checking
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal
Cycle
Checking
Cycle
Checking
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal
Cycle
Checking
Cycle
Checking
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal
Cycle
Checking
Cycle
Checking
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal
Cycle
Checking
Cycle
Checking
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal
Cycle
Checking
Cycle
Checking
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal
Cycle
Checking
Cycle
Checking
COMP2521
23T3 Warshall’s Algorithm
Example
Traversal
Cycle
Checking
Transitive Finished
Closure
Warshall’s algorithm
Pseudocode
Example
Analysis
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