[pypy-svn] r73734 - in pypy/branch/blackhole-improvement/pypy/tool/algo: . test

arigo at codespeak.net arigo at codespeak.net
Wed Apr 14 13:42:33 CEST 2010


Author: arigo
Date: Wed Apr 14 13:42:31 2010
New Revision: 73734

Modified:
   pypy/branch/blackhole-improvement/pypy/tool/algo/color.py
   pypy/branch/blackhole-improvement/pypy/tool/algo/test/test_color.py
Log:
Implement size_of_largest_clique().


Modified: pypy/branch/blackhole-improvement/pypy/tool/algo/color.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/tool/algo/color.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/tool/algo/color.py	Wed Apr 14 13:42:31 2010
@@ -21,6 +21,7 @@
         self.neighbours[v2].add(v1)
 
     def lexicographic_order(self):
+        """Enumerate a lexicographic breath-first ordering of the nodes."""
         sigma = [set(self.neighbours)]
         result = []
         while sigma:
@@ -42,8 +43,20 @@
                     newsigma.append(s2)
             sigma = newsigma
 
-
-_emptyset = frozenset()
+    def size_of_largest_clique(self):
+        """Assuming that the graph is chordal, compute the size of
+        the largest clique in it."""
+        result = 0
+        seen = set()
+        for v in self.lexicographic_order():
+            num = 1
+            for n in self.neighbours[v]:
+                if n in seen:
+                    num += 1
+            if num > result:
+                result = num
+            seen.add(v)
+        return result
 
 
 ##class Unit(object):

Modified: pypy/branch/blackhole-improvement/pypy/tool/algo/test/test_color.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/tool/algo/test/test_color.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/tool/algo/test/test_color.py	Wed Apr 14 13:42:31 2010
@@ -24,3 +24,4 @@
         'cebad', 'cebda', 'ceabd', 'ceadb', 'cedba', 'cedab',
         'cabde', 'cabed', 'cadbe', 'cadeb', 'caebd', 'caedb',
         ]
+    assert dg.size_of_largest_clique() == 3



More information about the Pypy-commit mailing list