forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transitive_Closure_DFS.py
52 lines (41 loc) · 1.33 KB
/
Transitive_Closure_DFS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# This class represents a directed graph using adjacency
class Graph:
def __init__(self, vertices):
# No. of vertices
self.V = vertices
# default dictionary to store graph
self.graph = {}
# To store transitive closure
self.tc = [[0 for j in range(self.V)] for i in range(self.V)]
# function to add an edge to graph
def add_edge(self, u, v):
if u in self.graph:
self.graph[u].append(v)
else:
self.graph[u] = [v]
# A recursive DFS traversal function that finds
# all reachable vertices for s
def dfs_util(self, s, v):
# Mark reachability from s to v as true.
self.tc[s][v] = 1
# Find all the vertices reachable through v
for i in self.graph[v]:
if self.tc[s][i] == 0:
self.dfs_util(s, i)
# The function to find transitive closure. It uses
# recursive dfs_util()
def transitive_closure(self):
# Call the recursive helper function to print DFS
# traversal starting from all vertices one by one
for i in range(self.V):
self.dfs_util(i, i)
print(self.tc)
g = Graph(4)
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)
print("Transitive closure matrix is")
g.transitive_closure()