allpy
changeset 554:1fb830d7517e
graph: improve expand_to_max_clique()
Do not recalculate candidates and weights at each step
author | boris (kodomo) <bnagaev@gmail.com> |
---|---|
date | Wed, 16 Mar 2011 19:42:58 +0300 |
parents | 1c8fb586d646 |
children | f9feb1d40968 |
files | allpy/graph.py |
diffstat | 1 files changed, 15 insertions(+), 10 deletions(-) [+] |
line diff
1.1 --- a/allpy/graph.py Wed Mar 09 17:10:44 2011 +0300 1.2 +++ b/allpy/graph.py Wed Mar 16 19:42:58 2011 +0300 1.3 @@ -155,20 +155,25 @@ 1.4 1.5 def expand_to_max_clique(self, parent_graph): 1.6 """ add vertices untill self becomes a nonextendible clique """ 1.7 - weights = dict((v, parent_graph.vertex_index(v)) 1.8 - for v in parent_graph) 1.9 + candidates = set() 1.10 + for vertex in set(parent_graph) - set(self): 1.11 + neighbours = set(parent_graph[vertex]) 1.12 + if set(self).issubset(neighbours): 1.13 + candidates.add(vertex) 1.14 + self.weights = {} 1.15 + for v in candidates: 1.16 + weight = parent_graph.vertex_weight(v) 1.17 + self.weights[v] = weight 1.18 while True: 1.19 - candidates = {} 1.20 - for vertex in set(parent_graph) - set(self): 1.21 - neighbours = set(parent_graph[vertex]) 1.22 - if set(self).issubset(neighbours): 1.23 - weight = parent_graph.vertex_weight(vertex) 1.24 - candidates[weight] = vertex 1.25 if not candidates: 1.26 break 1.27 - max_weight = max(candidates.keys()) 1.28 - vertex = candidates[max_weight] 1.29 + _rev = dict((v, k) for (k, v) in self.weights.items()) 1.30 + vertex = _rev[max(_rev.keys())] 1.31 self.add_vertex(vertex, parent_graph) 1.32 + for v in copy(candidates): 1.33 + if v not in parent_graph[vertex]: 1.34 + candidates.discard(v) 1.35 + del self.weights[v] 1.36 1.37 def fast_cliques(self, minsize=1): 1.38 """ returns list of cliques