-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloydWarshall.py
82 lines (59 loc) · 2.59 KB
/
FloydWarshall.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# A class to represent a graph object
class Graph:
# Constructor
def __init__(self, edges, N):
# A list of lists to represent an adjacency list
self.adjList = [[] for _ in range(N)]
# stores in-degree of a vertex
# initialize in-degree of each vertex by 0
self.indegree = [0] * N
# add edges to the undirected graph
for (src, dest) in edges:
# add an edge from source to destination
self.adjList[src].append(dest)
# increment in-degree of destination vertex by 1
self.indegree[dest] = self.indegree[dest] + 1
# Recursive function to find all topological orderings of a given DAG
def findAllTopologicalOrders(graph, path, discovered, N):
# do for every vertex
for v in range(N):
# proceed only if the current node's in-degree is 0 and
# the current node is not processed yet
if graph.indegree[v] == 0 and not discovered[v]:
# for every adjacent vertex `u` of `v`, reduce the in-degree of `u` by 1
for u in graph.adjList[v]:
graph.indegree[u] = graph.indegree[u] - 1
# include the current node in the path and mark it as discovered
path.append(v)
discovered[v] = True
# recur
findAllTopologicalOrders(graph, path, discovered, N)
# backtrack: reset in-degree information for the current node
for u in graph.adjList[v]:
graph.indegree[u] = graph.indegree[u] + 1
# backtrack: remove the current node from the path and
# mark it as undiscovered
path.pop()
discovered[v] = False
# print the topological order if all vertices are included in the path
if len(path) == N:
print(path)
# Print all topological orderings of a given DAG
def printAllTopologicalOrders(graph):
# get the total number of nodes in the graph
N = len(graph.adjList)
# create an auxiliary space to keep track of whether the vertex is discovered
discovered = [False] * N
# list to store the topological order
path = []
# find all topological ordering and print them
findAllTopologicalOrders(graph, path, discovered, N)
if __name__ == '__main__':
# List of graph edges as per the above diagram
edges = [(0, 6), (1, 2), (1, 4), (1, 6), (3, 0), (3, 4), (5, 1), (7, 0), (7, 1)]
# total number of nodes in the graph
N = 8
# build a graph from the given edges
graph = Graph(edges, N)
# print all topological ordering of the graph
printAllTopologicalOrders(graph)