- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrankingStudentsGraph.py
More file actions
Latest commit
59 lines (45 loc) · 1.66 KB
/
Copy pathrankingStudentsGraph.py
File metadata and controls
59 lines (45 loc) · 1.66 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
56
57
58
59
#!python3
importitertoolsasit
importos
defrelation(A, B):
ifall([a>bfora, binzip(A, B)]):
return">"
elifall([a<bfora, binzip(A, B)]):
return"<"
return"#"
defread_file(FILENAME):
roll_marks= {}
withopen(FILENAME, 'r') asf:
forlineinf.readlines():
roll, *marks=line.split()
roll_marks[roll] = [int(mark) formarkinmarks]
returnroll_marks
defall_relations(FILENAME):
roll_marks=read_file(FILENAME)
rolls=roll_marks.keys()
rels=set()
forroll1, roll2init.combinations(rolls, 2):
rel=relation(roll_marks[roll1], roll_marks[roll2])
ifrel==">":
rels.add(roll1+roll2)
elifrel=="<":
rels.add(roll2+roll1)
returnrels
defall_removables(rels):
defis_removable(first, middle, second):
returnfirst+middleinrelsandmiddle+secondinrels
firsts=set([rel[0] forrelinrels])
seconds=set([rel[1] forrelinrels])
both=firsts&seconds
returnset(first+secondforfirstinfirstsforsecondinsecondsiffirst+secondinrelsformiddleinbothifis_removable(first, middle, second))
defgenerate_graph(FILENAME):
rels=all_relations(FILENAME)
minimal=rels^all_removables(rels)
withopen("partial_orders.dot", "w") asf:
print("digraph partial_orders {", file=f)
forrelationinminimal:
print("\t%s -> %s;"% (relation[0], relation[1]), file=f)
print("}", file=f)
os.system("dot -T png -o partial_orders.png partial_orders.dot")
if__name__=="__main__":
generate_graph('roll_and_marks.txt')