Skip to content

Commit

Permalink
neighbor search: skip visited nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
myrho committed Dec 12, 2023
1 parent 0014475 commit df91192
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions gsrest/service/entities_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def key_accessor(neighbor):
result = \
await bfs(request, entity, key_accessor,
list_neighbors, stop_neighbor, match_neighbor,
depth)
depth, skip_visited=True)

async def resolve(neighbor):
if not with_tag:
Expand Down Expand Up @@ -332,14 +332,18 @@ async def bfs(request,
list_neighbors,
stop_neighbor,
match_neighbor,
max_depth=3):
max_depth=3,
skip_visited=True):

# collect matching paths
matching_paths = []

# maintain a queue of paths
queue = []

# visited nodes
visited = set()

start = True

# count number of requests
Expand Down Expand Up @@ -387,12 +391,13 @@ def retrieve_neighbor(last):
for neighbors, path in zip(list_of_neighbors, paths):
for neighbor in neighbors:

id = key_accessor(neighbor)
new_path = list(path)
new_path.append(neighbor)

# found path
if match_neighbor(neighbor):
request.app.logger.debug(f"MATCH {key_accessor(neighbor)}")
request.app.logger.debug(f"MATCH {id}")
matching_paths.append(new_path)
continue

Expand All @@ -403,9 +408,17 @@ def retrieve_neighbor(last):

# stop if stop criteria fulfilled
if (stop_neighbor(neighbor)):
request.app.logger.debug(f"STOP {key_accessor(neighbor)}")
request.app.logger.debug(f"STOP {id}")
continue

# stop if node was already visited
if id in visited:
request.app.logger.debug(f"ALREADY VISITED {id}")
continue

if skip_visited:
visited.add(id)

queue.append(new_path)

if len(queue) == 0 or run_time > SEARCH_TIMEOUT:
Expand Down

0 comments on commit df91192

Please sign in to comment.