allpy
changeset 117:fd7371f14c6d
lib::graph -- improvement of code and fix of bug in add_node function
author | boris <bnagaev@gmail.com> |
---|---|
date | Sat, 23 Oct 2010 20:59:22 +0400 |
parents | 0ef1a722b738 |
children | dd9fa5a8dc5a |
files | lib/block.py lib/graph.py |
diffstat | 2 files changed, 26 insertions(+), 19 deletions(-) [+] |
line diff
1.1 --- a/lib/block.py Sat Oct 23 20:29:40 2010 +0400 1.2 +++ b/lib/block.py Sat Oct 23 20:59:22 2010 +0400 1.3 @@ -62,9 +62,12 @@ 1.4 returns sorted list of positions, representing geometrical core 1.5 delta -- threshold of distance spreading 1.6 """ 1.7 + nodes = self.positions 1.8 + lines = {} 1.9 for i in self.positions: 1.10 for j in self.positions: 1.11 - pass 1.12 + if i < j: 1.13 + 1.14 1.15 1.16
2.1 --- a/lib/graph.py Sat Oct 23 20:29:40 2010 +0400 2.2 +++ b/lib/graph.py Sat Oct 23 20:59:22 2010 +0400 2.3 @@ -35,19 +35,22 @@ 2.4 nodes = set() 2.5 if not lines: 2.6 lines = dict() 2.7 - self.nodes = nodes 2.8 - self.lines = lines 2.9 + self.nodes = copy(nodes) 2.10 + self.lines = copy(lines) 2.11 + 2.12 + @staticmethod 2.13 + def line(k1, k2): 2.14 + return frozenset([k1, k2]) 2.15 2.16 def bounded(self, k1, k2): 2.17 - return k1 == k2 or frozenset([k1, k2]) in self.lines 2.18 + return k1 == k2 or Graph.line(k1, k2) in self.lines 2.19 2.20 def count_one(self, node): 2.21 - return len([node1 for node1 in self.nodes if \ 2.22 - frozenset([node, node1]) in self.lines]) 2.23 + return len([node1 for node1 in self.nodes if self.bounded(node, node1)]) - 1 2.24 2.25 def cost_one(self, node): 2.26 - return sum([self.lines[frozenset([node, node1])] for \ 2.27 - node1 in self.nodes if frozenset([node, node1]) in self.lines]) 2.28 + return sum([self.lines.get(Graph.line(node, node1), 0) for \ 2.29 + node1 in self.nodes if node != node1]) 2.30 2.31 def count_all(self): 2.32 c = dict([(node, 0) for node in self.nodes]) 2.33 @@ -59,15 +62,15 @@ 2.34 2.35 def drop_node(self, node): 2.36 for node1 in self.nodes: 2.37 - self.lines.pop(frozenset([node, node1]), None) 2.38 + self.lines.pop(Graph.line(node, node1), None) 2.39 self.nodes.discard(node) 2.40 2.41 def add_node(self, node, parent_graph): 2.42 self.nodes.add(node) 2.43 for node1 in self.nodes: 2.44 - line = frozenset([node, node1]) 2.45 + line = Graph.line(node, node1) 2.46 if line in parent_graph.lines: 2.47 - self.lines[line] = parent_graph.lines 2.48 + self.lines[line] = parent_graph.lines[line] 2.49 2.50 def drop_nodes(self, nodes): 2.51 for node in nodes: 2.52 @@ -187,7 +190,7 @@ 2.53 cliques = [] 2.54 2.55 while True: 2.56 - graph = Graph(copy(self.nodes), copy(self.lines)) 2.57 + graph = Graph(self.nodes, self.lines) 2.58 for clique in cliques: 2.59 graph.drop_nodes(clique) 2.60 if not graph.nodes or len(cliques) == 2: 2.61 @@ -211,9 +214,9 @@ 2.62 # add nodes, while its is possible 2.63 candidats = {} 2.64 for node in self.nodes: 2.65 - if len([i for i in graph.nodes if frozenset([node, i]) in \ 2.66 - self.lines]) == len(self.nodes): # perl has me 2.67 - graph1 = Graph(copy(graph.nodes), copy(graph.lines)) 2.68 + c = len([i for i in graph.nodes if self.bounded(node, i)]) 2.69 + if c == len(self.nodes): 2.70 + graph1 = Graph(graph.nodes, graph.lines) 2.71 graph1.add_node(node, self) 2.72 candidats[node] = graph1.cost_one(node) 2.73 if not candidats: 2.74 @@ -239,13 +242,14 @@ 2.75 if it raises TimeoutError, executes fast_cliques 2.76 """ 2.77 2.78 - drop_if_count(minsize) 2.79 + self.drop_if_count(minsize) 2.80 2.81 try: 2.82 - cliques = bron_kerbosh(timeout, minsize) 2.83 + cliques = self.bron_kerbosh(timeout, minsize) 2.84 + cliques.sort(key=lambda clique: len(clique), reverse=True) 2.85 except TimeoutError: 2.86 - cliques = fast_cliques(minsize) 2.87 - return sorted(cliques, key=lambda clique: len(clique), reverse=True) 2.88 + cliques = self.fast_cliques(minsize) 2.89 + return cliques 2.90 2.91 if __name__ == "__main__": 2.92 import doctest