Skip to content

Commit 361afcd

Browse files
committed
Week 5 - MST and Toposort
1 parent 07694a3 commit 361afcd

12 files changed

+159
-7
lines changed

week2_decomposition2/.DS_Store

6 KB
Binary file not shown.

week2_decomposition2/1_acyclicity/acyclicity.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,34 @@
22

33
import sys
44

5+
visited = []
6+
recStack = []
7+
8+
def isCyclicUtil(adj, v):
9+
global visited
10+
global recStack
11+
if not visited[v]:
12+
visited[v] = True
13+
recStack[v] = True
14+
for w in adj[v]:
15+
if (not visited[w]) and isCyclicUtil(adj, w):
16+
return True
17+
elif recStack[w]:
18+
return True
19+
recStack[v] = False
20+
return False
521

622
def acyclic(adj):
23+
global visited
24+
global recStack
25+
26+
visited = [False] * len(adj)
27+
recStack = [False] * len(adj)
28+
29+
for v in range(len(adj)):
30+
if isCyclicUtil(adj, v):
31+
return 1 #True
32+
733
return 0
834

935
if __name__ == '__main__':

week2_decomposition2/2_toposort/toposort.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22

33
import sys
44

5-
def dfs(adj, used, order, x):
6-
#write your code here
7-
pass
5+
visited = []
6+
order = []
87

8+
def explore(adj, v):
9+
global visited
10+
global order
11+
visited[v] = True
12+
for w in adj[v]:
13+
if not visited[w]:
14+
explore(adj, w)
15+
order.insert(0, v)
916

1017
def toposort(adj):
11-
used = [0] * len(adj)
12-
order = []
13-
#write your code here
18+
global visited
19+
global order
20+
visited = [False] * len(adj)
21+
for v in range(len(adj)):
22+
if not visited[v]:
23+
explore(adj, v)
1424
return order
1525

1626
if __name__ == '__main__':

week2_decomposition2/3_strongly_connected/strongly_connected.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,52 @@
55
sys.setrecursionlimit(200000)
66

77

8+
visited = []
9+
order = []
10+
11+
def reverseGraph(adj):
12+
n = len(adj)
13+
adjr = [[] for _ in range(n)]
14+
for v in range(len(adj)):
15+
for w in adj[v]:
16+
adjr[w].append(v)
17+
return adjr
18+
19+
def explore(adj, v):
20+
global visited
21+
visited[v] = True
22+
for w in adj[v]:
23+
if not visited[w]:
24+
explore(adj, w)
25+
26+
def exploreForOrder(adj, v):
27+
global visited
28+
global order
29+
visited[v] = True
30+
for w in adj[v]:
31+
if not visited[w]:
32+
exploreForOrder(adj, w)
33+
order.insert(0, v)
34+
35+
def toposort(adj):
36+
global visited
37+
global order
38+
visited = [False] * len(adj)
39+
for v in range(len(adj)):
40+
if not visited[v]:
41+
exploreForOrder(adj, v)
42+
return order
43+
844
def number_of_strongly_connected_components(adj):
45+
global visited
946
result = 0
10-
#write your code here
47+
adjr = reverseGraph(adj)
48+
order = toposort(adjr)
49+
visited = [False] * len(adj)
50+
for v in order:
51+
if not visited[v]:
52+
explore(adj, v)
53+
result += 1
1154
return result
1255

1356
if __name__ == '__main__':
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

week5_mst/.DS_Store

6 KB
Binary file not shown.

week5_mst/1_connecting_points/connecting_points.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
11
#Uses python3
22
import sys
33
import math
4+
import queue
5+
6+
def distance(x1, y1, x2, y2):
7+
return ((x1-x2)**2+(y1-y2)**2)**0.5
48

59
def minimum_distance(x, y):
610
result = 0.
711
#write your code here
12+
n = len(x)
13+
14+
cost = [float('inf')] * n
15+
cost[0] = 0
16+
17+
Q = queue.PriorityQueue()
18+
Q.put((0, 0))
19+
20+
processed = set()
21+
22+
while not Q.empty():
23+
d, v = Q.get()
24+
25+
if v not in processed:
26+
result += d
27+
processed.add(v)
28+
29+
for z in range(n):
30+
if z not in processed:
31+
currCost = distance(x[v], y[v], x[z], y[z])
32+
33+
if cost[z] > currCost:
34+
cost[z] = currCost
35+
Q.put((currCost, z))
36+
837
return result
938

1039

week5_mst/2_clustering/clustering.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,53 @@
11
#Uses python3
22
import sys
33
import math
4+
import queue
5+
6+
def distance(x1, y1, x2, y2):
7+
return ((x1-x2)**2+(y1-y2)**2)**0.5
8+
9+
class DisjointSet:
10+
def __init__(self, n):
11+
self.parent = list(range(n))
12+
self.rank = [0] * n
13+
self.regions = n
14+
15+
def find(self, i):
16+
while i != self.parent[i]:
17+
i = self.parent[i]
18+
return i
19+
20+
def union(self, i, j):
21+
i_id = self.find(i)
22+
j_id = self.find(j)
23+
if i_id == j_id:
24+
return
25+
self.regions -= 1
26+
if self.rank[i_id] > self.rank[j_id]:
27+
self.parent[j_id] = i_id
28+
else:
29+
self.parent[i_id] = j_id
30+
if self.rank[i_id] == self.rank[j_id]:
31+
self.rank[j_id] += 1
32+
433

534
def clustering(x, y, k):
635
#write your code here
36+
37+
n = len(x)
38+
D = DisjointSet(n)
39+
Q = queue.PriorityQueue()
40+
for i in range(n):
41+
for j in range(i+1, n):
42+
Q.put((distance(x[i], y[i], x[j], y[j]), (i, j)))
43+
44+
while not Q.empty():
45+
dist, (u, v) = Q.get()
46+
if D.find(u) != D.find(v):
47+
if D.regions == k:
48+
return dist
49+
D.union(u, v)
50+
751
return -1.
852

953

493 KB
Binary file not shown.

0 commit comments

Comments
 (0)