-
Notifications
You must be signed in to change notification settings - Fork 2
/
BA5_N - Topological sort.py
57 lines (44 loc) · 1.2 KB
/
BA5_N - Topological sort.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 7 23:24:02 2020
@author: jasonmoggridge
"""
def adj_list(file):
edges = []
for edge in file:
edges.append(edge.strip().split('->'))
adjacency = {}
for edge in edges:
v = int(edge[0])
U = [int(i) for i in edge[1].split(',')]
if v not in adjacency:
adjacency[v] = U
else:
adjacency[v] += U
for u in U:
if u not in adjacency:
adjacency[u] = []
return adjacency
#
def Topological_Sort(graph):
# dfs builds topsort upon ret from expl'd v.
def dfs(v, topo, visited, graph):
visited[v] = True
for u in graph[v]:
if not visited[u]:
dfs(u, topo, visited, graph)
topo.append()
visited ={}
for v in graph:
visited[v] = False
topo = []
for v in graph:
if not visited[v]:
dfs(v, topo, visited, graph)
return topo[::-1]
# list needs to be inverted #
with open("/Users/jasonmoggridge/Desktop/rosalind_ba5n.txt",'r') as f:
graph = adj_list(f)
topo = Topological_Sort(graph)
print(topo)