allpy
view allpy/graph.py @ 517:c755fb38896c
graph.py: optimization
replace weight_one() to weight_all()
author | boris (kodomo) <bnagaev@gmail.com> |
---|---|
date | Thu, 24 Feb 2011 17:37:28 +0300 |
parents | 1f387f24c7c6 |
children | 449bd8ebeb30 |
line source
5 pass
10 """ Undirected weighted graph
12 Data:
13 vertices -- set of elements
14 edges -- {edge: weight}.
15 edge is frozenset([e1, e2])
16 weight is float in (0, 1] or 1 (if all edges are equal)
18 >>> g = Graph(set([1,2,3]), {frozenset([1,2]): 1})
19 >>> g.fast_cliques()
20 Fast algorithm started
21 [frozenset([1, 2]), frozenset([3])]
22 >>> g = Graph(set([1,2,3]), {frozenset([1,2]): 1, frozenset([1,1]): 1})
23 >>> g.fast_cliques()
24 Fast algorithm started
25 [frozenset([1, 2]), frozenset([3])]
26 >>> g = Graph(set([1,2,3,4]), {frozenset([1,2]): 0.98,
27 ... frozenset([1,3]): 0.98, frozenset([2,3]): 0.1,
28 ... frozenset([1,1]): 1})
29 >>> g.fast_cliques()
30 Fast algorithm started
31 [frozenset([1, 2, 3]), frozenset([4])]
32 >>> g.fast_cliques(minsize=2)
33 Fast algorithm started
34 [frozenset([1, 2, 3])]
35 >>> g.bron_kerbosh()
36 Bron and Kerbosh algorithm started
37 [frozenset([1, 2, 3]), frozenset([4])]
38 >>> g.cliques()
39 Bron and Kerbosh algorithm started
40 [frozenset([1, 2, 3]), frozenset([4])]
41 >>> g.cliques(minsize=2)
42 Bron and Kerbosh algorithm started
43 [frozenset([1, 2, 3])]
44 >>> g.vertices
45 set([1, 2, 3, 4])
46 """
59 @staticmethod
61 """ Construct object, representing edge of graph """
65 """ if these two vertices of the graph are bounded with edge """
69 """ Returns sum of weights of all connections of this vertex """
78 """ Returns {vertex: number of connections of this vertex} """
87 """ Remove vertex and all involved edges """
93 """ Add vertex and corresponding edges from parent_graph
95 Added edges should be contained in self graph
96 (takes care of hanging edges)
97 """
105 """ Run drop_vertex for each of given vertices """
110 """ Bron and Kerboch algorithm implementation
112 returns list of cliques
113 clique is frozenset
114 if timeout=-1, it means infinity
115 if timeout has happened, raises TimeoutError
116 """
129 """ return vertex with max count (and weight) """
144 """ return some v from candidates """
148 """ if v from used connected with all from candidates """
152 break
158 """ process call and return list of recursive calls """
192 """ drop vertices untill self becomes a clique """
194 # drop vertices, while its is possible
196 break
203 continue
205 break
213 """ add vertices untill self becomes a nonextendible clique """
215 # add vertices, while its is possible
223 break
232 """ returns list of cliques
234 clique is frozenset
235 """
243 break
250 break
255 """ returns length-sorted list of cliques
257 clique is frozenset
259 try to execute bron_kerbosh
260 if it raises TimeoutError, executes fast_cliques
261 """