Skip to content

Commit 19d99be

Browse files
authored
Merge pull request #35 from shivansh-sopho/master
Added topological sort for DAG.
2 parents 635deba + 1e74d26 commit 19d99be

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Python Program for Floyd Warshall Algorithm
2+
3+
# Number of vertices in the graph
4+
V = 4
5+
6+
# Define infinity as the large enough value. This value will be
7+
# used for vertices not connected to each other
8+
INF = 99999
9+
10+
# Solves all pair shortest path via Floyd Warshall Algorithm
11+
def floydWarshall(graph):
12+
13+
""" dist[][] will be the output matrix that will finally
14+
have the shortest distances between every pair of vertices """
15+
""" initializing the solution matrix same as input graph matrix
16+
OR we can say that the initial values of shortest distances
17+
are based on shortest paths considering no
18+
intermediate vertices """
19+
dist = map(lambda i : map(lambda j : j , i) , graph)
20+
21+
""" Add all vertices one by one to the set of intermediate
22+
vertices.
23+
---> Before start of an iteration, we have shortest distances
24+
between all pairs of vertices such that the shortest
25+
distances consider only the vertices in the set
26+
{0, 1, 2, .. k-1} as intermediate vertices.
27+
----> After the end of a iteration, vertex no. k is
28+
added to the set of intermediate vertices and the
29+
set becomes {0, 1, 2, .. k}
30+
"""
31+
for k in range(V):
32+
33+
# pick all vertices as source one by one
34+
for i in range(V):
35+
36+
# Pick all vertices as destination for the
37+
# above picked source
38+
for j in range(V):
39+
40+
# If vertex k is on the shortest path from
41+
# i to j, then update the value of dist[i][j]
42+
dist[i][j] = min(dist[i][j] ,
43+
dist[i][k]+ dist[k][j]
44+
)
45+
printSolution(dist)
46+
47+
48+
# A utility function to print the solution
49+
def printSolution(dist):
50+
print "Following matrix shows the shortest distances\
51+
between every pair of vertices"
52+
for i in range(V):
53+
for j in range(V):
54+
if(dist[i][j] == INF):
55+
print "%7s" %("INF"),
56+
else:
57+
print "%7d\t" %(dist[i][j]),
58+
if j == V-1:
59+
print ""
60+
61+
62+
63+
# Driver program to test the above program
64+
# Let us create the following weighted graph
65+
"""
66+
10
67+
(0)------->(3)
68+
| /|\
69+
5 | |
70+
| | 1
71+
\|/ |
72+
(1)------->(2)
73+
3 """
74+
graph = [[0,5,INF,10],
75+
[INF,0,3,INF],
76+
[INF, INF, 0, 1],
77+
[INF, INF, INF, 0]
78+
]
79+
# Print the solution
80+
floydWarshall(graph);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#Python program to print topological sorting of a DAG
2+
from collections import defaultdict
3+
4+
#Class to represent a graph
5+
class Graph:
6+
def __init__(self,vertices):
7+
self.graph = defaultdict(list) #dictionary containing adjacency List
8+
self.V = vertices #No. of vertices
9+
10+
# function to add an edge to graph
11+
def addEdge(self,u,v):
12+
self.graph[u].append(v)
13+
14+
# A recursive function used by topologicalSort
15+
def topologicalSortUtil(self,v,visited,stack):
16+
17+
# Mark the current node as visited.
18+
visited[v] = True
19+
20+
# Recur for all the vertices adjacent to this vertex
21+
for i in self.graph[v]:
22+
if visited[i] == False:
23+
self.topologicalSortUtil(i,visited,stack)
24+
25+
# Push current vertex to stack which stores result
26+
stack.insert(0,v)
27+
28+
# The function to do Topological Sort. It uses recursive
29+
# topologicalSortUtil()
30+
def topologicalSort(self):
31+
# Mark all the vertices as not visited
32+
visited = [False]*self.V
33+
stack =[]
34+
35+
# Call the recursive helper function to store Topological
36+
# Sort starting from all vertices one by one
37+
for i in range(self.V):
38+
if visited[i] == False:
39+
self.topologicalSortUtil(i,visited,stack)
40+
41+
# Print contents of the stack
42+
print stack
43+
44+
g= Graph(6)
45+
g.addEdge(5, 2);
46+
g.addEdge(5, 0);
47+
g.addEdge(4, 0);
48+
g.addEdge(4, 1);
49+
g.addEdge(2, 3);
50+
g.addEdge(3, 1);
51+
52+
print "Following is a Topological Sort of the given graph"
53+
g.topologicalSort()

0 commit comments

Comments
 (0)