-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathp4_7.py
More file actions
55 lines (41 loc) · 1.67 KB
/
p4_7.py
File metadata and controls
55 lines (41 loc) · 1.67 KB
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
from utils.graphs import Ditex
from collections import OrderedDict, defaultdict
from typing import List
# TODO Redo as topological sort
def build_nodes(projects: List[chr], deps: List[tuple]):
nodes = {x: Ditex(x) for x in projects}
for (a, b) in deps:
# print(f"Looking at tuple {a} and {b}")
nodes[b].out_edges.add(nodes[a])
nodes[a].in_edges.add(nodes[b])
sn = OrderedDict()
seen_count = len(sn)-1
while seen_count != len(sn): # Check progress
seen_count = len(sn)
for key, node in nodes.items():
if not {edge.val for edge in node.out_edges} - sn.keys():
sn[node.val] = 1
down_build(node, sn)
else:
print(f"node {key} is waiting on {[x.val for x in node.out_edges]}, "
f" seen so far {list(sn.keys())}")
if len(sn) == len(projects):
return list(sn.keys())
else:
raise Exception("No valid build")
def down_build(node: Ditex, seen: dict):
for nodes_child in node.in_edges:
if not nodes_child.out_edges - seen.keys():
print(
f"Adding {nodes_child.val} as out_edges are \
{[x.val for x in nodes_child.out_edges]},\t \
compared to seen {list(seen.keys())}")
seen[nodes_child.val] = 1
down_build(nodes_child, seen)
if __name__ == "__main__":
# projects = ['a', 'b', 'c', 'd', 'e', 'f']
# deps = [('a', 'd'), ('f', 'b'), ('b', 'd'), ('f', 'a'), ('d', 'c')]
projects = ['a', 'b', 'c']
deps = [('c', 'a')]
build_order = build_nodes(projects, deps)
print(f"Build order is {build_order}")