路径覆盖

homework3:路径覆盖



a.数据流图

 

 

 

b.数组越界时可能会发生错误

c.不经过while循环,使得 初始条件n=1

d..

点覆盖  {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}

边覆盖

{(1.2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,5),(6,8),(8,9),(9,10),(9,11),(10,2),(11,2),(2,12),(12,13),(13,14),(14,15),(15,13),(13,16)}

主路径覆盖

{(1,2,3,4,5,6,7),

(1,2,3,4,5,9,10),

(1,2,3,4,5,9,11),

(1,2,3,4,5,6,8,9,10),

(1,2,3,4,5,6,8,9,11),

(1,2,12,13,16),

(1,2,12,13,14,15),

(3,4,5,6,8,9,10,2,12,13,14,15),

(3,4,5,6,8,9,10,2,12,13,16),

(3,4,5,9,10,2,12,13,14,15),

(3,4,5,9,11,2,12,13,14,15),

(3,4,5,9,10,2,12,13,16),

(3,4,5,9,10,2,12,13,16),

(3,4,5,6,8,9,11,2,12,13,14,15),

(3,4,5,6,8,9,11,2,12,13,16),

(6,7,5,9,10,2,12,13,14,15),

(6,7,5,9,10,2,12,13,16),

(6,7,5,9,11,2,12,13,14,15),

(6,7,5,9,11,2,12,13,16),

(14,15,13,16),

(13,14,15,13),

(5,6,7,5),

(2,3,4,5,6,8,9,10,2),

(2,3,4,5,6,8,9,11,2),

(2,3,4,5,9,10,2)

(2,3,4,5,9,11,2)}

 

 

Another:第一次上机实验 判断三角形程序

由于三角形的种类有三种,外加一种判断输入参数是否为正整数的返回值,因此主路径测试需求有四个,测试四组用例(2,2,2),(2,3,3),(2,3,4),(-2,5-6)。即可完整覆盖,覆盖率达到100%

 

 

 

 


最小路径覆盖是指在一个加权有向图中找到一条从源节点(起始点)到目标节点(结束点)的路径,该路径恰好经过图中的每一条边一次。在Python中,可以使用迪杰斯特拉算法(Dijkstra's Algorithm)或者贝尔曼-福特算法(Bellman-Ford Algorithm)作为基础,结合贪心策略来找出这样的路径。 这里是一个基本的使用迪杰斯特拉算法实现最小路径覆盖的例子: ```python from heapq import heappop, heappush def shortest_path_with_coverage(graph, start, end): INF = float('inf') # 定义无穷大 dists = {node: INF for node in graph} # 初始化距离字典 prev_nodes = {node: None for node in graph} # 初始化前驱节点字典 dists[start] = 0 # 设置起点的距离为0 queue = [(0, start)] # 使用堆来存储节点及其距离 while queue: curr_dist, curr_node = heappop(queue) # 取出距离最小的节点 # 如果已经访问过更短路径,则跳过当前节点 if dists[curr_node] < curr_dist: continue # 更新相邻节点的距离和前驱节点 for neighbor, weight in graph[curr_node].items(): new_dist = curr_dist + weight if new_dist < dists[neighbor]: dists[neighbor] = new_dist prev_nodes[neighbor] = curr_node heappush(queue, (new_dist, neighbor)) # 将更新后的节点加入堆 # 从终点开始构建路径 path = [] curr_node = end while curr_node is not None: path.append(curr_node) curr_node = prev_nodes[curr_node] path.reverse() # 路径需要从终点反向 return path, dists[end] # 返回路径和总距离 # 示例图,假设这是一个有向图 graph = { 'A': {'B': 1, 'C': 4}, 'B': {'A': 1, 'D': 5}, 'C': {'A': 4, 'D': 1}, 'D': {'B': 5, 'C': 1, 'E': 6}, 'E': {} } start = 'A' end = 'E' result = shortest_path_with_coverage(graph, start, end) path, total_cost = result print("最小路径覆盖:", path) print("总覆盖成本:", total_cost)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值