-
Notifications
You must be signed in to change notification settings - Fork 0
/
gap_homomorphism_finder
79 lines (67 loc) · 1.78 KB
/
gap_homomorphism_finder
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
[ -f "./gap_config.sh" ] && {
. ./gap_config.sh
}
[ -z "$GAP" ] && {
>&2 echo "error: please, make sure GAP variable is set"
>&2 echo "(you can create gap_config.sh file, it will then be automatically sourced)"
exit 1
}
[ -f "$GAP" ] || {
>&2 echo "error: seems like [GAP=$GAP] does not point to a file"
exit 1
}
gfile=$1;shift
hfile=$1;shift
[ -z "$gfile" ] && {
>&2 echo "error: please, specify file for G"
exit 1
}
[ -z "$hfile" ] && {
>&2 echo "error: please, specify file for H"
exit 1
}
[ -f "$gfile" ] || {
>&2 echo "error: couldn't find a valid file for G"
exit 1
}
[ -f "$hfile" ] || {
>&2 echo "error: couldn't find a valid file for H"
exit 1
}
create_adjacency_list() {
file=$1; shift
echo "
import json
if __name__ == '__main__':
with open('$file', 'r') as f:
j = json.load(f)
nodes = j['nodes']
nodes = range(len(nodes))
edges = j['edges']
adj = []
for u in nodes:
row = []
for v in nodes:
e1, e2 = [u, v], [v, u]
if e1 in edges or e2 in edges:
row += [v + 1]
adj += [row]
print(adj)
"
}
gap_script="
LoadPackage(\"Digraphs\");
g := Digraph($(env python3 -c "$(create_adjacency_list "$gfile")"));
h := Digraph($(env python3 -c "$(create_adjacency_list "$hfile")"));
# establish somehow if two graphs are homomorphic to each other
Print(\"RESULT $gfile\t$hfile\t\", DigraphHomomorphism(g, h), \"\\n\");
"
# echo "$gap_script" | pygmentize -l shell
start=0
$GAP --quitonbreak <<< "${gap_script}" | while IFS= read -r line; do
[ -z "$(echo "$line" | grep 'RESULT')" ] || start=1
[ $start -eq 1 ] && {
echo "$line"
}
done | tr '\n' ' ' | sed 's/.*RESULT //'