完成以下代码:""" 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
时间: 2023-06-06 14:05:16 浏览: 135
以下是完成代码的步骤:
1. 导入所需的模块及类。代码中需要使用LinkedDirectedGraph类和Array类,因此需要导入graph和arrays模块。
2. 创建一个LinkedDirectedGraph对象,并读取文件example.txt中的图数据,将其添加到图中。
3. 实现Floyd算法,计算所有节点对之间的最短路径,将结果保存到一个二维数组中。
4. 输出结果数组,展示所有节点对之间的最短路径。
相关问题
代码如下:""" 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
这段代码定义并测试 Floyd 的全部最短路径算法,使用的是文本图 12.19 中的图,该图以 example.txt 文件的形式表示。有从graph中导入LinkedDirectedGraph,从arrays中导入Array,还有随机数生成函数。
python start_vm_ec2.py "'pts-qa12.cbxcloud.com':'35.160.168.99','pts-qa11.cbxcloud.com':'52.24.85.138'" Then it will generate HostnameExtIP_dic={'pts-qa11.cbxcloud.com':'52.24.85.138','pts-qa12.cbxcloud.com':'35.160.168.99'} when execute the py script.
Here is a sample Python script that takes a dictionary of hostnames and IP addresses as input and generates another dictionary with the same information in a different order:
```
def start_vm_ec2(input_dict):
# Split input string into key-value pairs
input_list = [pair.split(":") for pair in input_dict.split(",")]
# Convert key-value pairs to a dictionary
input_dict = {pair[0]: pair[1] for pair in input_list}
# Reverse the order of the items in the dictionary
hostname_ext_ip_dict = {v: k for k, v in input_dict.items()}
return hostname_ext_ip_dict
input_dict = "'pts-qa12.cbxcloud.com':'35.160.168.99','pts-qa11.cbxcloud.com':'52.24.85.138'"
print("HostnameExtIP_dic=", start_vm_ec2(input_dict))
```
This script defines a function `start_vm_ec2` that takes a string as input and converts it into a dictionary. It then reverses the order of the items in the dictionary and returns the result.
阅读全文