Commit 9a61b76f by Yoann Dufresne

### opti: divide the d2 computation time by several order of magnitude

parent a7343c1f
 ... @@ -14,7 +14,9 @@ class Dgraph(object): ... @@ -14,7 +14,9 @@ class Dgraph(object): self.halves = [None,None] self.halves = [None,None] self.connexity = [None,None] self.connexity = [None,None] self.nodes = [self.center] self.nodes = [self.center] self.node_set = set(self.center) self.edges = [] self.edges = [] self.ordered_list = None """ Static method to load a dgraph from a text """ Static method to load a dgraph from a text ... @@ -51,7 +53,12 @@ class Dgraph(object): ... @@ -51,7 +53,12 @@ class Dgraph(object): def put_halves(self, h1, h2, graph): def put_halves(self, h1, h2, graph): self.score = 0 self.score = 0 self.halves[0] = h1 self.halves[0] = h1 for node in h1: self.node_set.add(node) self.halves[1] = h2 self.halves[1] = h2 for node in h2: self.node_set.add(node) self.nodes = sorted([self.center] + self.halves[0] + self.halves[1]) self.nodes = sorted([self.center] + self.halves[0] + self.halves[1]) self.connexity[0] = {key: 0 for key in self.halves[0]} self.connexity[0] = {key: 0 for key in self.halves[0]} self.connexity[1] = {key: 0 for key in self.halves[1]} self.connexity[1] = {key: 0 for key in self.halves[1]} ... @@ -96,18 +103,20 @@ class Dgraph(object): ... @@ -96,18 +103,20 @@ class Dgraph(object): def to_ordered_lists(self): def to_ordered_lists(self): hands = [[],[]] if self.ordered_list is None: for idx in range(2): hands = [[],[]] prev_connectivity = -1 for idx in range(2): for node in self.halves[idx]: prev_connectivity = -1 # group nodes by similar connectivity for node in self.halves[idx]: value = self.connexity[idx][node] # group nodes by similar connectivity if value != prev_connectivity: value = self.connexity[idx][node] hands[idx].append([]) if value != prev_connectivity: prev_connectivity = value hands[idx].append([]) hands[idx][-1].append(node) prev_connectivity = value hands[idx][-1].append(node) return hands[0][::-1] + [[self.center]] + hands[1] self.ordered_list = hands[0][::-1] + [[self.center]] + hands[1] return self.ordered_list def to_node_multiset(self): def to_node_multiset(self): ... @@ -119,7 +128,7 @@ class Dgraph(object): ... @@ -119,7 +128,7 @@ class Dgraph(object): dist = 0 dist = 0 idx1, idx2 = 0, 0 idx1, idx2 = 0, 0 while(idx1 != len(self.nodes) and idx2 != len(other_nodes)): while idx1 != len(self.nodes) and idx2 != len(other_nodes): if self.nodes[idx1] == other_nodes[idx2]: if self.nodes[idx1] == other_nodes[idx2]: idx1 += 1 idx1 += 1 idx2 += 1 idx2 += 1 ... @@ -158,7 +167,13 @@ class Dgraph(object): ... @@ -158,7 +167,13 @@ class Dgraph(object): def __eq__(self, other): def __eq__(self, other): if other == None: if other is None: return False if self.idx == other.idx: return True if self.node_set != other.node_set: return False return False return self.to_ordered_lists() == other.to_ordered_lists() return self.to_ordered_lists() == other.to_ordered_lists() ... ...