Skip to content

Commit

Permalink
Add example for distributing a Breadth First Search (#246)
Browse files Browse the repository at this point in the history
* Add example for distributing a Breadth First Search

* Remove explorer decorator
  • Loading branch information
StefanKennedy authored Mar 29, 2017
1 parent b350bd8 commit db34caf
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions examples/breadth_first_search_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from os import path
import sys
# Append to path so that this script can access the dipla package
sys.path.append(path.abspath('../dipla'))

from dipla.api import Dipla

@Dipla.scoped_distributable(count=4)
def bfs(grid, index, count):
from queue import Queue
q = Queue()
# Start in the center of the grid
q.put((len(grid)//2, len(grid)//2))
seen = set()
ans = []

# Split the grid into 4 quadrants, and calculate the bounds of each
bounds = [(0, 0), (1, 0), (0, 1), (1, 1)]
half_grid = len(grid)//2
min_x = bounds[index][0] * half_grid
min_y = bounds[index][1] * half_grid
max_x = min_x + half_grid
max_y = min_y + half_grid

# Standard breadth first search
while not q.empty():
popped = q.get()
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for xy in directions:
newx = popped[0] + xy[0]
newy = popped[1] + xy[1]

if newx < min_x or newx > max_x or newy < min_y or newy > max_y:
continue
new = (newx, newy)
if new in seen:
continue
if grid[new[1]][new[0]] == 2:
continue
if grid[new[1]][new[0]] == 1:
ans.append(new)
seen.add(new)
q.put(new)
return ans

grid = [
[1, 0, 2, 2, 0, 0, 0],
[2, 0, 2, 2, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0],
[2, 2, 2, 0, 0, 0, 0],
[0, 0, 2, 0, 0, 2, 0],
[1, 0, 2, 0, 2, 1, 0],
[0, 0, 0, 0, 0, 0, 0]
]

# Apply a distributable function to the stream of values from
# the above source
bfs_result = Dipla.apply_distributable(bfs, [[grid]])

out = Dipla.get(bfs_result)

u = []
for y in out:
u += [(x[0], x[1]) for x in y]

for o in set(u):
print(o)

0 comments on commit db34caf

Please sign in to comment.