day 23, bron kerbosch is more efficient than me
This commit is contained in:
		
							
								
								
									
										23
									
								
								d23/part2_bron_kerbosch.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								d23/part2_bron_kerbosch.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
			
		||||
from collections import defaultdict as DD
 | 
			
		||||
import sys
 | 
			
		||||
 | 
			
		||||
G = DD(set)
 | 
			
		||||
 | 
			
		||||
for line in sys.stdin.read().strip().split('\n'):
 | 
			
		||||
    a, b = line.split('-')
 | 
			
		||||
    G[a].add(b)
 | 
			
		||||
    G[b].add(a)
 | 
			
		||||
 | 
			
		||||
def bron_kerbosch(r, p, x):
 | 
			
		||||
    if len(p) == 0 and len(x) == 0:
 | 
			
		||||
        yield r
 | 
			
		||||
    else:
 | 
			
		||||
        pivot = max(p | x, key=lambda x: len(G[x]))
 | 
			
		||||
        for v in p - G[pivot]:
 | 
			
		||||
            neighbours = G[v]
 | 
			
		||||
            yield from bron_kerbosch(r | {v}, p & neighbours, x & neighbours)
 | 
			
		||||
            p.remove(v)
 | 
			
		||||
            x.add(v)
 | 
			
		||||
 | 
			
		||||
clique = max(bron_kerbosch(set(), set(G.keys()), set()), key=len)
 | 
			
		||||
print(','.join(sorted(clique)))
 | 
			
		||||
@@ -1,7 +1,6 @@
 | 
			
		||||
import sys
 | 
			
		||||
from collections import defaultdict as DD
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
D = DD(set)
 | 
			
		||||
S = set()
 | 
			
		||||
for i in sys.stdin.read().strip().split('\n'):
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user