The Algorithm X in 30 lines https://www.cs.mcgill.ca/~aassaf9/python/algorithm_x.html
if not X:
yield list(solution)
else:
c = min(X, key=lambda c: len(X[c]))
for r in list(X[c]):
solution.append(r)
cols = select(X, Y, r)
for s in solve(X, Y, solution):
yield s
deselect(X, Y, r, cols)
solution.pop()
def select(X, Y, r):
cols = []
for j in Y[r]:
for i in X[j]:
for k in Y[i]:
if k != j:
X[k].remove(i)
cols.append(X.pop(j))
return cols
def deselect(X, Y, r, cols):
for j in reversed(Y[r]):
X[j] = cols.pop()
for i in X[j]:
for k in Y[i]:
if k != j:
X[k].add(i)
In this code I want to parallelize the code fragment:
for r in list(X[c]):
solution.append(r)
cols = select(X, Y, r)
for s in solve(X, Y, solution):
yield s
deselect(X, Y, r, cols)
I made a function:
def ParallelCode(X,Y,r,solution)
print("Hello")
solution.append(r)
cols = select(X, Y, r)
for s in solve(X, Y, solution):
yield s
deselect(X, Y, r, cols)
solution.pop()
solution.pop()
Then call it like this
if not X:
yield list(solution)
else:
c = min(X, key=lambda c: len(X[c]))
processes = [multiprocessing.Process(target=ParallelCode, args=(X, Y,r,solution)) for r in list(X[c])]
for p in processes:
p.start()
for p in processes:
p.join()
When I ran the code, its not working. I print "Hello" inside ParallelCode, even "Hello" is not printed. Could you please help me what is wrong in this code.