如何借助DFS与BFS结合计数器实现野火数据排序与排名?
用DFS/BFS结合计数器实现野火数据的排序与排名
首先明确:DFS和BFS是遍历数据集的方式,核心是在遍历过程中用计数器(比如字典)统计目标维度的火灾数量,之后再基于统计结果完成排序/排名。下面针对你的两个需求分别给出实现思路:
一、按野火发生数量从多到少排序季节
步骤1:用计数器统计各季节火灾总数
不管用DFS还是BFS,核心都是遍历每一条数据,累加对应季节的火灾数:
- 初始化一个字典(比如
season_count = {})作为计数器 - 遍历过程中,每读到一条数据,取出
Season字段的值,把对应的Fire Count加到字典的对应键里(如果键不存在就初始化为0再加)
DFS实现(递归遍历,适合内存中加载的结构化数据)
def dfs_count_season(data, index, season_count): # 递归终止条件:遍历完所有数据 if index >= len(data): return # 取出当前数据的季节和火灾数 current_season = data[index]['Season'] current_count = data[index]['Fire Count'] # 更新计数器 season_count[current_season] = season_count.get(current_season, 0) + current_count # 递归遍历下一条数据 dfs_count_season(data, index + 1, season_count) # 调用示例(假设data是加载好的数据集列表) season_count = {} dfs_count_season(data, 0, season_count)
BFS实现(队列遍历,适合大数据流或分块加载的数据)
from collections import deque def bfs_count_season(data): season_count = {} queue = deque() # 初始化队列,放入第一条数据的索引 queue.append(0) while queue: index = queue.popleft() if index >= len(data): continue current_season = data[index]['Season'] current_count = data[index]['Fire Count'] season_count[current_season] = season_count.get(current_season, 0) + current_count # 将下一条数据的索引加入队列 queue.append(index + 1) return season_count # 调用示例 season_count = bfs_count_season(data)
步骤2:对统计结果排序
拿到season_count后,按值从大到小排序:
sorted_seasons = sorted(season_count.items(), key=lambda x: x[1], reverse=True) # 输出结果示例:[('夏季', 1200), ('春季', 900), ...]
二、按指定年份火灾数量排名各州
步骤1:筛选指定年份数据+计数器统计
和上面逻辑类似,只是多了一个年份筛选条件:
DFS实现
def dfs_count_state_by_year(data, index, target_year, state_count): if index >= len(data): return # 只处理目标年份的数据 if data[index]['Year'] == target_year: current_state = data[index]['State'] current_count = data[index]['Fire Count'] state_count[current_state] = state_count.get(current_state, 0) + current_count dfs_count_state_by_year(data, index + 1, target_year, state_count) # 调用示例(比如统计2018年) target_year = 2018 state_count = {} dfs_count_state_by_year(data, 0, target_year, state_count)
BFS实现
def bfs_count_state_by_year(data, target_year): state_count = {} queue = deque([0]) while queue: index = queue.popleft() if index >= len(data): continue if data[index]['Year'] == target_year: current_state = data[index]['State'] current_count = data[index]['Fire Count'] state_count[current_state] = state_count.get(current_state, 0) + current_count queue.append(index + 1) return state_count # 调用示例 state_count = bfs_count_state_by_year(data, 2018)
步骤2:生成各州排名
基于统计结果生成排名,处理并列情况:
# 先按火灾数降序排序 sorted_states = sorted(state_count.items(), key=lambda x: x[1], reverse=True) # 生成排名 ranked_states = [] current_rank = 1 for i, (state, count) in enumerate(sorted_states): if i > 0 and count != sorted_states[i-1][1]: current_rank = i + 1 ranked_states.append((state, count, current_rank)) # 输出结果示例:[('加利福尼亚州', 800, 1), ('得克萨斯州', 750, 2), ...]
关键说明
- DFS和BFS在这里的作用只是遍历数据集,统计逻辑完全一致,选择哪种方式取决于你的数据加载方式(内存数据用DFS递归更简洁,流式/分块数据用BFS队列更灵活)
- 如果你的数据集存储在数据库中,直接用SQL聚合查询(比如
GROUP BY Season ORDER BY SUM(FireCount) DESC)效率更高,但如果必须用DFS/BFS实现,上面的代码逻辑完全可行
内容的提问来源于stack exchange,提问作者Jess
相关产品推荐
相关产品推荐

