-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrivingEdgeDeletion.py
121 lines (94 loc) · 4.2 KB
/
DrivingEdgeDeletion.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
import EdgeDeletion as ed
import networkx as nx
import matplotlib.pyplot as plt
import graphFunctions as gf
import treeDecompositionForward as td
import JessDotParsing as jd
import EdgeDeletionForward as ed
import time
import sys
def printDelNice(delValues):
for guy in delValues:
print guy, " | ", delValues[guy]
def getChildren(tree, postorder, node):
children = []
nodeInd = postorder.index(node)
neighbours = tree.neighbors(node)
for i in range(nodeInd):
if postorder[i] in neighbours:
children.append(postorder[i])
return children
def forwardAlgorithm(graph, tree, root, h, k, orderOfComputation):
delValuesTable = {}
for guy in orderOfComputation:
kind = tree.node[guy ]["kind"]
delValuesThis = {}
print "Processing node " + str(guy) + " which is kind " + str(kind)
start = time.time()
if kind =="LEAF":
delValuesThis = td.get_del_values_leaf(graph, tree, guy, None, h, k)
delValuesTable[guy] = delValuesThis
elif kind == "FORGET":
children = getChildren(tree, orderOfComputation, guy)
delValuesThis = td.get_del_values_forget(graph, tree, guy, children, h, k, delValuesTable)
delValuesTable[guy] = delValuesThis
elif kind == "INTRODUCE":
children = getChildren(tree, orderOfComputation, guy)
delValuesThis = td.get_del_values_introduce(graph, tree, guy, children, h, k, delValuesTable)
delValuesTable[guy] = delValuesThis
else:# We must have a join
children = getChildren(tree, orderOfComputation, guy)
delValuesThis = td.get_del_values_join(graph, tree, guy, children, h, k, delValuesTable)
delValuesTable[guy] = delValuesThis
if delValuesThis == None or len(delValuesThis) == 0:
print "No solution here, new detection method. Failed to make del values for " + str(guy)
return None
minDelValue = min(delValuesThis.values())
if minDelValue == ed.INFINITY:
print "No solution here."
return None
end = time.time()
print "took time " + str(end-start) + " to find a number of del values " + str(len(delValuesThis))
return delValuesTable
fileOfFiles = sys.argv[1]
# "singleFile"
baseFiles = []
for line in open(fileOfFiles):
baseFiles.append(line.strip())
startString = "TreeDecomp"
endString = ".dgf.txt"
for baseFile in baseFiles:
decompName = startString + baseFile + endString
tree = jd.readDot(decompName)
root = tree.nodes()[0]
neighbours = tree.neighbors(root)
td.make_it_nice(tree, root, neighbours)
graph = gf.read_edges_from_file(baseFile, " ")
root = tree.nodes()[0]
tree = td.get_nice_tree_decomp(tree, root)
orderOfComputation = list(nx.dfs_postorder_nodes(tree,root))
delValuesTable = {}
for guy in orderOfComputation:
print str(guy) +" has kind " + tree.node[guy ]["kind"] + " and bag " + tree.node[guy ]["label"] + " and neighbours " + str(tree.neighbors(guy))
maxK = len(graph.edges())
maxH = 6
hSolved = False
start1 = time.time()
for h in range(5, maxH):
for k in range(6, maxK):
start = time.time()
print "Testing at " + str(k) + " removals " + " for component of size " + str(h)
delValuesTable = forwardAlgorithm(graph, tree, root, h, k, orderOfComputation)
if delValuesTable != None:
print "Solution found at " + str(k) + " removals " + " for component of size " + str(h)
end = time.time()
if delValuesTable != None:
print "Solution! Elapsed time for h = " + str(h) + " and k = " + str(k) + " is " + str(end - start) + " file " + baseFile
end1 = time.time()
print "TABLERESULT," + baseFile + "," + str(h) + "," + str(k) + "," + str(end1 - start1)
hSolved = True
break
else:
print "No! Elapsed time for h = " + str(h) + " and k = " + str(k) + " is " + str(end - start) + " file " + baseFile
sys.stdout.flush()