Skip to content

Commit

Permalink
More search utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tom111 committed Mar 12, 2012
1 parent cbf5347 commit 096be29
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
6 changes: 6 additions & 0 deletions python-minimalistic/K23.py
Expand Up @@ -10,3 +10,9 @@
K23Binomials=[monomial(nvars, b[0]).divide(monomial(nvars, b[1])) for b in K23Edges]

degreeTwo = listMonomials (2, nvars)
degreeThree = listMonomials (3, nvars)
degreeFour = listMonomials (4, nvars)

oneMonomial = degreeTwo[122];

enumerateConnectedComponent (oneMonomial, K23Binomials)
36 changes: 31 additions & 5 deletions python-minimalistic/searching.py
Expand Up @@ -36,16 +36,16 @@ def neighbors (mons, binomials):
pass
return unique (result, idfun=lambda m: str(m.exponents))

def isPresent (l, m):
"""Check if a monomial m is present in a list l"""
for li in l:
if li.isSame(m): return True
return False

def enumerateConnectedComponent (m, binomials):
""" Starting from a monomial m, perform a breadth first exploration of the
connected component"""
print ("Starting breadth-first exploration of connected component")
def isPresent (l, m):
"""Check if m is already in l"""
for li in l:
if li.isSame(m): return True
return False
result = [m]
# This will store previously unseen elements in the next neighborhood
newKids = [m]
Expand All @@ -62,3 +62,29 @@ def isPresent (l, m):
else: print ("Done :)")
return result

def inSameComponent (m1, m2, binomials):
"""
Check if two monomials m1, m2 can be connected via the given binomials
Use breadth-first like in enumeration of a connected component
"""
print ("Starting breadth-first exploration of connected component")
# This will store all seen monomials
known = [m1]
# This will store previously unseen elements in the next neighborhood
newKids = [m1]
while len (newKids) > 0:
newneighbors = neighbors (newKids, binomials)
# Sort the new neighbors into newKids and oldKids
newKids = []
for n in newneighbors:
if n.isSame (m2):
return True
if not isPresent (known, n):
newKids.append(n)
known.append(n)
if newKids != []:
print ("Next iteration will run with " + str(len(newKids)) + " new neighbours.")
else: print ("Done :)")
return False

0 comments on commit 096be29

Please sign in to comment.