yangdx
commited on
Commit
·
3dac148
1
Parent(s):
06e76f6
Fix BFS depth control in NetworkX graph traversal
Browse files
lightrag/kg/networkx_impl.py
CHANGED
|
@@ -303,18 +303,20 @@ class NetworkXStorage(BaseGraphStorage):
|
|
| 303 |
# Use BFS to get nodes
|
| 304 |
bfs_nodes = []
|
| 305 |
visited = set()
|
| 306 |
-
queue = [node_label]
|
| 307 |
|
| 308 |
# Breadth-first search
|
| 309 |
while queue and len(bfs_nodes) < max_nodes:
|
| 310 |
-
current = queue.pop(0)
|
| 311 |
if current not in visited:
|
| 312 |
visited.add(current)
|
| 313 |
bfs_nodes.append(current)
|
| 314 |
|
| 315 |
-
#
|
| 316 |
-
|
| 317 |
-
|
|
|
|
|
|
|
| 318 |
|
| 319 |
# Check if graph is truncated - if we still have nodes in the queue
|
| 320 |
# and we've reached max_nodes, then the graph is truncated
|
|
|
|
| 303 |
# Use BFS to get nodes
|
| 304 |
bfs_nodes = []
|
| 305 |
visited = set()
|
| 306 |
+
queue = [(node_label, 0)] # (node, depth) tuple
|
| 307 |
|
| 308 |
# Breadth-first search
|
| 309 |
while queue and len(bfs_nodes) < max_nodes:
|
| 310 |
+
current, depth = queue.pop(0)
|
| 311 |
if current not in visited:
|
| 312 |
visited.add(current)
|
| 313 |
bfs_nodes.append(current)
|
| 314 |
|
| 315 |
+
# Only explore neighbors if we haven't reached max_depth
|
| 316 |
+
if depth < max_depth:
|
| 317 |
+
# Add neighbor nodes to queue with incremented depth
|
| 318 |
+
neighbors = list(graph.neighbors(current))
|
| 319 |
+
queue.extend([(n, depth + 1) for n in neighbors if n not in visited])
|
| 320 |
|
| 321 |
# Check if graph is truncated - if we still have nodes in the queue
|
| 322 |
# and we've reached max_nodes, then the graph is truncated
|