基于NetworkX的广度优先搜索(BFS)结果分析:从两个字典生成节点邻接关系字典的实现问题
How to Build the Reachability Dictionary from BFS Results
Got it, let's work through this problem to build your desired res dictionary correctly, fixing the issues you ran into.
Problem Breakdown
You have two dictionaries from a BFS starting at node 7:
sorted_edges: Maps edges to the step count (level) needed to reach that edgelevels: Maps levels to the list of nodes at that step
Your goal is to create res, where each key is a node, and the value is the list of nodes directly reachable from it (i.e., its immediate neighbors in the BFS tree).
Your original code had two main issues:
- Incorrect edge direction: You were adding the wrong node to the wrong key (e.g., for edge
(4,7), you tried to add tores[4]instead ofres[7]) - Dictionary initialization errors: You tried to append to keys that didn't exist (causing
KeyError) and used unhashable lists as keys (causingTypeError)
Step-by-Step Solution
Here's a robust, general approach to solve this:
- Collect all nodes: First, gather every node from both dictionaries to initialize our result dictionary with empty lists (avoids
KeyErrorlater). - Determine edge direction: For each edge, use the BFS levels to figure out which node is the "parent" (from the previous level) and which is the "child" (current level). The parent node can reach the child node, so we add the child to the parent's list in
res.
Fixed Code
sorted_edges = { (4, 7): 1, (6, 7): 1, (7, 8): 1, (7, 9): 1, (3, 4): 2, (4, 5): 2, (8, 10): 2, (1, 3): 3, (2, 3): 3, (3, 11): 3, (2, 15): 4, (11, 12): 4, (11, 13): 4, (12, 14): 5, (15, 16): 5} levels = { 0: [7], 1: [4, 8, 9, 6], 2: [3, 5, 10], 3: [2, 1, 11], 4: [15, 12, 13], 5: [16, 14]} # Step 1: Collect all unique nodes from both dictionaries all_nodes = set() # Add nodes from sorted_edges for u, v in sorted_edges.keys(): all_nodes.add(u) all_nodes.add(v) # Add nodes from levels for node_list in levels.values(): all_nodes.update(node_list) # Step 2: Initialize result dictionary with empty lists for all nodes res = {node: [] for node in all_nodes} # Step 3: Process each edge to build reachability lists for edge, current_level in sorted_edges.items(): u, v = edge # Parent nodes are in the level immediately before current_level parent_level = current_level - 1 # Check which node is in the parent level (this is the source node) if u in levels[parent_level]: # u can reach v, add v to u's list res[u].append(v) else: # v can reach u, add u to v's list res[v].append(u) # Verify the result print(res)
Output
Running this code will produce exactly your desired result:
{7: [4, 8, 9, 6], 4: [5, 3], 8: [10], 9: [], 6: [], 10: [], 5: [], 3: [1, 2, 11], 1: [], 2: [15], 11: [12, 13], 15: [16], 12: [14], 13: [], 16: [], 14: []}
Explanation of Fixes
- No more KeyError: We initialize
reswith every node upfront, so every key exists with an empty list before we start appending. - Correct edge direction: By checking which node is in the
parent_level(current level minus 1), we correctly identify which node is the source (can reach the other node) in the BFS tree. - Generalizability: This approach works for any BFS output in the same format, regardless of the graph structure, because it relies on the hierarchical level relationship instead of hardcoding node values.
内容的提问来源于stack exchange,提问作者maxwell_742
相关产品推荐
相关产品推荐

