Floyd Algorithm

博客介绍了如何使用Floyd算法解决一个关于N个人比赛胜负关系的问题,通过算法找出无法预判的比赛场次。解题思路包括利用传递闭包确定胜负关系,并提供了算法的优化建议,强调了在多组数据处理中初始化变量的重要性。
摘要由CSDN通过智能技术生成

题意:
N 个人玩一个游戏,每两个人都要进行一场比赛,已知M个胜负关系,每个关系为AB,表示A比B强, 胜负关系具有传递性,试问有多少场比赛的胜负无法预先得知?(1≤𝑁,𝑀≤500)
输入:
第一行给出数据组数。
每组数据第一行给出 N 和 M(N , M <= 500)。
接下来 M 行,每行给出 A B,表示 A 可以胜过 B。
输出:
对于每一组数据,判断有多少场比赛的胜负不能预先得知。注意 (a, b) 与 (b, a) 等价,即每一个二元组只被计算一次。
输入样例:
3
3 3
1 2
1 3
2 3
3 2
1 2
2 3
4 2
1 2
3 4
输出样例:
0
0
4
解题思路:
由 1 ≤ 𝑁, 𝑀 ≤ 500 的数据规模可以得知本题要求复杂度为𝑂(𝑛^3),因为胜负关系具有传递性,因此可以用 Floyd 算法求出任意两点的胜负关系(传递闭包),即可求出答案。𝑑𝑖𝑠[𝑎][𝑏]= 1 表示a比b强,𝑑𝑖𝑠[𝑎][𝑏] =0表示a与b的胜负关系不明,𝑑𝑖𝑠[𝑎][𝑏]=0且𝑑𝑖𝑠[b][a] =0即表示a与b的胜负 关系无法预先判断。
注意事项:
floyd算法可用于求取图中任意两点之间的关系,这样的关系包括多源最短路,任意两点的距离关系以及图上的传递闭包,任意两点的连通关系。复杂度为 𝑂(𝑛^3)。当所求为距离关系时的floyd算法为:
1
当所求为点的连通关系时的floyd算法为:
2
可以进行的优化是在于dis[i][k]为0时dis[i][k]&dis[k][j]一定为0,此时不需要进入到第三个循环当中,如下:
3
总结:
一道关于应用floyd算法求解图中两点连通关系的题目,注意floyd算法用于求解图中任意两点的距离关系以及图上任意两点的连通关系时的细微差异。并配合一定的优化剪枝提升算法的效率。注意对于多组输入数据的程序公共变量一定要在每次运行前初始化,避免因为保留了旧值而出错。floyd算法参数中

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
完成以下代码:""" File: fromexample.py Project 12.9 Defines and tests the all pairs shortest paths algorithm of Floyd. Uses the graph from Figure 12.19 of the text, as represented in the file example.txt. """ from graph import LinkedDirectedGraph import random from arrays import Array # Functions for working with infinity def isLessWithInfinity(a, b): """Returns False if a == b or a == INFINITY and b != INFINITY. Otherwise, returns True if b == INFINITY or returns a < b.""" if a == LinkedDirectedGraph.INFINITY and b == LinkedDirectedGraph.INFINITY: return False elif b == LinkedDirectedGraph.INFINITY: return True elif a == LinkedDirectedGraph.INFINITY: return False else: return a < b def addWithInfinity(a, b): """If a == INFINITY or b == INFINITY, returns INFINITY. Otherwise, returns a + b.""" if a == LinkedDirectedGraph.INFINITY or b == LinkedDirectedGraph.INFINITY: return LinkedDirectedGraph.INFINITY else: return a + b def minDistance(a, b): if isLessWithInfinity(a, b): return a else: return b # Define a function that uses Floyd's algorithm def allPairsShortestPaths(matrix): """ please complete the Floyd algorithm here """ pass # Define a function to print a labeled distance matrix def printDistanceMatrix(matrix, table): """Prints the distance matrix with rows and columns labels with the index positions and vertex labels.""" labels = Array(len(table)) index = 0 labelWidth = 0 indexWidth = 0 for label in table: labels[table[label]] = label labelWidth = max(labelWidth, len(str(label))) indexWidth = max(indexWidth, len(str(index))) index += 1 weightWidth = 0 for row in range(matrix.getHeight()): for column in range(matrix.getWidth()): weightWidth = max(weightWidth, len(str(matrix[row][column]))) weightWidth = max(weightWidth, labelWidth, indexWidth) topRowLeftMargin
04-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值