def largest_component(self): visited = [False] * (max(self.graph) + 1) max_component_size = 0 for i in self.graph: if not visited[i]: component_size = 0 self.DFS(i, visited) for j in visited: if j: component_size += 1 max_component_size = max(max_component_size, component_size) return max_component_size
时间: 2024-02-14 15:04:49 浏览: 91
这是 Graph 类中的 largest_component 方法,它用于查找图形中最大的连通组件的大小。它首先创建一个 visited 数组,用于跟踪每个顶点是否被访问过。然后,对于图形中的每个顶点 i,如果 i 还没有被访问过,则使用 DFS 方法遍历与 i 相关的所有节点。在 DFS 遍历期间,visited 数组中已访问的顶点将被标记为 True。完成遍历后,我们计算 visited 数组中 True 的数量,该数量表示与 i 相关的连通组件的大小。我们将 component_size 添加到 max_component_size 中,如果 component_size 大于 max_component_size,则更新 max_component_size。最后,我们返回 max_component_size,它是图形中最大连通组件的大小。
相关问题
class AbstractGreedyAndPrune(): def __init__(self, aoi: AoI, uavs_tours: dict, max_rounds: int, debug: bool = True): self.aoi = aoi self.max_rounds = max_rounds self.debug = debug self.graph = aoi.graph self.nnodes = self.aoi.n_targets self.uavs = list(uavs_tours.keys()) self.nuavs = len(self.uavs) self.uavs_tours = {i: uavs_tours[self.uavs[i]] for i in range(self.nuavs)} self.__check_depots() self.reachable_points = self.__reachable_points() def __pruning(self, mr_solution: MultiRoundSolution) -> MultiRoundSolution: return utility.pruning_multiroundsolution(mr_solution) def solution(self) -> MultiRoundSolution: mrs_builder = MultiRoundSolutionBuilder(self.aoi) for uav in self.uavs: mrs_builder.add_drone(uav) residual_ntours_to_assign = {i : self.max_rounds for i in range(self.nuavs)} tour_to_assign = self.max_rounds * self.nuavs visited_points = set() while not self.greedy_stop_condition(visited_points, tour_to_assign): itd_uav, ind_tour = self.local_optimal_choice(visited_points, residual_ntours_to_assign) residual_ntours_to_assign[itd_uav] -= 1 tour_to_assign -= 1 opt_tour = self.uavs_tours[itd_uav][ind_tour] visited_points |= set(opt_tour.targets_indexes) # update visited points mrs_builder.append_tour(self.uavs[itd_uav], opt_tour) return self.__pruning(mrs_builder.build()) class CumulativeGreedyCoverage(AbstractGreedyAndPrune): choice_dict = {} for ind_uav in range(self.nuavs): uav_residual_rounds = residual_ntours_to_assign[ind_uav] if uav_residual_rounds > 0: uav_tours = self.uavs_tours[ind_uav] for ind_tour in range(len(uav_tours)): tour = uav_tours[ind_tour] quality_tour = self.evaluate_tour(tour, uav_residual_rounds, visited_points) choice_dict[quality_tour] = (ind_uav, ind_tour) best_value = max(choice_dict, key=int) return choice_dict[best_value] def evaluate_tour(self, tour : Tour, round_count : int, visited_points : set): new_points = (set(tour.targets_indexes) - visited_points) return round_count * len(new_points) 如何改写上述程序,使其能返回所有已经探索过的目标点visited_points的数量,请用代码表示
可以在 `solution()` 方法中添加一个变量来记录已经探索过的目标点数量,然后在每次更新 `visited_points` 后更新这个变量。下面是修改后的代码:
```
class AbstractGreedyAndPrune():
def __init__(self, aoi: AoI, uavs_tours: dict, max_rounds: int, debug: bool = True):
self.aoi = aoi
self.max_rounds = max_rounds
self.debug = debug
self.graph = aoi.graph
self.nnodes = self.aoi.n_targets
self.uavs = list(uavs_tours.keys())
self.nuavs = len(self.uavs)
self.uavs_tours = {i: uavs_tours[self.uavs[i]] for i in range(self.nuavs)}
self.__check_depots()
self.reachable_points = self.__reachable_points()
def __pruning(self, mr_solution: MultiRoundSolution) -> MultiRoundSolution:
return utility.pruning_multiroundsolution(mr_solution)
def solution(self) -> Tuple[MultiRoundSolution, int]:
mrs_builder = MultiRoundSolutionBuilder(self.aoi)
for uav in self.uavs:
mrs_builder.add_drone(uav)
residual_ntours_to_assign = {i : self.max_rounds for i in range(self.nuavs)}
tour_to_assign = self.max_rounds * self.nuavs
visited_points = set()
explored_points = 0
while not self.greedy_stop_condition(visited_points, tour_to_assign):
itd_uav, ind_tour = self.local_optimal_choice(visited_points, residual_ntours_to_assign)
residual_ntours_to_assign[itd_uav] -= 1
tour_to_assign -= 1
opt_tour = self.uavs_tours[itd_uav][ind_tour]
new_points = set(opt_tour.targets_indexes) - visited_points
explored_points += len(new_points)
visited_points |= new_points # update visited points
mrs_builder.append_tour(self.uavs[itd_uav], opt_tour)
return self.__pruning(mrs_builder.build()), explored_points
class CumulativeGreedyCoverage(AbstractGreedyAndPrune):
def evaluate_tour(self, tour : Tour, round_count : int, visited_points : set):
new_points = set(tour.targets_indexes) - visited_points
return round_count * len(new_points)
def local_optimal_choice(self, visited_points, residual_ntours_to_assign):
choice_dict = {}
for ind_uav in range(self.nuavs):
uav_residual_rounds = residual_ntours_to_assign[ind_uav]
if uav_residual_rounds > 0:
uav_tours = self.uavs_tours[ind_uav]
for ind_tour in range(len(uav_tours)):
tour = uav_tours[ind_tour]
quality_tour = self.evaluate_tour(tour, uav_residual_rounds, visited_points)
choice_dict[quality_tour] = (ind_uav, ind_tour)
best_value = max(choice_dict, key=int)
return choice_dict[best_value]
class Node: def __init__(self, num, name, intro): self.num = num self.name = name self.intro = intro self.visited = False class Edge: def __init__(self, fr, to, weight): self.fr = fr self.to = to self.weight = weight class Graph: def __init__(self): self.nodes = [] self.edges = {} def add_node(self, node): self.nodes.append(node) self.edges[node] = [] def add_edge(self, edge): self.edges[edge.fr].append(edge) self.edges[edge.to].append(Edge(edge.to, edge.fr, edge.weight))graph = Graph() with open('data.txt', 'r', encoding='utf-8') as f: for line in f.readlines(): items = line.strip().split(',') num = int(items[0]) name = items[1] intro = items[2] node = Node(num, name, intro) graph.add_node(node) for i in range(3, len(items), 2): neighbor = int(items[i]) weight = int(items[i+1]) edge = Edge(node, graph.nodes[neighbor-1], weight) graph.add_edge(edge) data.txt里怎么写
data.txt 的格式应该是每一行代表一个节点和它的邻居节点以及边的权重,具体格式如下:
节点编号,节点名称,节点介绍,邻居节点1,边的权重1,邻居节点2,边的权重2,...
例如:
1,Node1,This is Node1,2,10,3,15,4,20
2,Node2,This is Node2,1,10,3,5
3,Node3,This is Node3,1,15,2,5,4,8
4,Node4,This is Node4,1,20,3,8
阅读全文