Prioritize high-degree neighbors in BFS traversal for NetoworkX storage

This commit is contained in:
yangdx
2025-04-25 09:22:53 +08:00
parent 18b669278f
commit c620f9c4f2

View File

@@ -265,9 +265,14 @@ class NetworkXStorage(BaseGraphStorage):
if depth < max_depth: if depth < max_depth:
# Add neighbor nodes to queue with incremented depth # Add neighbor nodes to queue with incremented depth
neighbors = list(graph.neighbors(current)) neighbors = list(graph.neighbors(current))
queue.extend( # Filter out already visited neighbors
[(n, depth + 1) for n in neighbors if n not in visited] unvisited_neighbors = [n for n in neighbors if n not in visited]
) # Get the degree of each neighbor node
neighbor_degrees = [(n, graph.degree(n)) for n in unvisited_neighbors]
# Sort neighbors by degree in descending order
sorted_neighbors = sorted(neighbor_degrees, key=lambda x: x[1], reverse=True)
# Add sorted neighbors to the queue
queue.extend([(n, depth + 1) for n, _ in sorted_neighbors])
# Check if graph is truncated - if we still have nodes in the queue # Check if graph is truncated - if we still have nodes in the queue
# and we've reached max_nodes, then the graph is truncated # and we've reached max_nodes, then the graph is truncated