Package obitools :: Package graph :: Package algorithms :: Module clique
[hide private]
[frames] | no frames]

Source Code for Module obitools.graph.algorithms.clique

  1  from logging import debug 
  2  import time 
  3  import sys 
  4   
  5   
  6   
  7  _maxsize=0 
  8  _solution=0 
  9  _notbound=0 
 10  _sizebound=0 
 11   
12 -def cliqueIterator(graph,minsize=1,node=None):
13 global _maxsize,_solution,_notbound,_sizebound 14 _maxsize=0 15 _solution=0 16 _notbound=0 17 _sizebound=0 18 starttime = time.time() 19 20 if node: 21 node = graph.getNode(node) 22 index = node.index 23 clique= set([index]) 24 candidates= set(graph.neighbourIndexSet(index=index)) 25 else: 26 clique=set() 27 candidates = set(x.index for x in graph) 28 29 30 # candidates = set(x for x in candidates 31 # if len(graph.neighbourIndexSet(index=x) & candidates) >= (minsize - 1)) 32 33 34 for c in _cliqueIterator(graph,clique,candidates,set(),minsize,start=starttime): 35 yield c
36 37 38 39 40
41 -def _cliqueIterator(graph,clique,candidates,notlist,minsize=0,start=None):
42 global _maxsize,_solution,_notbound,_sizebound 43 44 # Speed indicator 45 lclique = len(clique) 46 lcandidates = len(candidates) 47 notmin = lcandidates 48 notfix = None 49 50 for n in notlist: 51 nnc = candidates - graph.neighbourIndexSet(index=n) 52 nc = len(nnc) 53 if nc < notmin: 54 notmin=nc 55 notfix=n 56 notfixneib = nnc 57 58 if lclique > _maxsize or not _solution % 1000 : 59 if start is not None: 60 top = time.time() 61 delta = top - start 62 speed = _solution / delta 63 start = top 64 else: 65 speed = 0 66 print >>sys.stderr,"\rCandidates : %-5d Maximum clique size : %-5d Solutions explored : %10d speed = %5.2f solutions/sec sizebound=%10d notbound=%10d " % (lcandidates,_maxsize,_solution,speed,_sizebound,_notbound), 67 sys.stderr.flush() 68 if lclique > _maxsize: 69 _maxsize=lclique 70 71 # print >>sys.stderr,'koukou' 72 73 if not candidates and not notlist: 74 yield set(clique) 75 else: 76 while notmin and candidates and (lclique + len(candidates)) >= minsize: 77 # count explored solution 78 _solution+=1 79 80 if notfix is None: 81 nextcandidate = candidates.pop() 82 else: 83 nextcandidate = notfixneib.pop() 84 candidates.remove(nextcandidate) 85 86 clique.add(nextcandidate) 87 88 neighbours = graph.neighbourIndexSet(index=nextcandidate) 89 90 nextcandidates = candidates & neighbours 91 nextnot = notlist & neighbours 92 93 nnc = candidates - neighbours 94 lnnc=len(nnc) 95 96 for c in _cliqueIterator(graph, 97 set(clique), 98 nextcandidates, 99 nextnot, 100 minsize, 101 start): 102 yield c 103 104 105 clique.remove(nextcandidate) 106 107 notmin-=1 108 109 if lnnc < notmin: 110 notmin = lnnc 111 notfix = nextcandidate 112 notfixneib = nnc 113 114 if notmin==0: 115 _notbound+=1 116 117 notlist.add(nextcandidate) 118 else: 119 if (lclique + len(candidates)) < minsize: 120 _sizebound+=1
121