- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
拥有一个包含Graph G
邻接列表的文件,例如:
0 -> 13,16,20,22,4,5
1 -> 12,13,16,17,19,22,23,24,25,3,4
10 -> 13,14,17,20,23,24
11 -> 12,19,20,22,23
12 -> 15,20,24
13 -> 20,21,22
15 -> 23
17 -> 25
19 -> 20,25
2 -> 16,19,3,7
20 -> 22,23
21 -> 22,23,24
22 -> 25
24 -> 25
3 -> 15,21,4
4 -> 10,12,14,15,16,17,18,19,21,23,5
5 -> 11,16,17,20,23,8,9
6 -> 12,14,18,22
7 -> 14,17,22
8 -> 21,24
9 -> 12,14
我想得到它的拓扑排序,Graph G
是一个有向无环图。
首先我想解析txt文件并将所有内容放入字典中。但是我遇到了一些问题,首先在读取文件时我丢失了一些东西,我错过了 ->
之后的第一个元素:
f = open('topo.txt', 'r')
line_list = f.readlines()
G = {int(line.split('->')[0]): [int(val) for val in line.split(',')[1:] if val] for line in line_list if line}
我会得到:
('G:', {0: [16, 20, 22, 4, 5], 1: [13, 16, 17, 19, 22, 23, 24, 25, 3, 4], 2: [19, 3, 7], 3: [21, 4], 4: [12, 14, 15, 16, 17, 18, 19, 21, 23, 5], 5: [16, 17, 20, 23, 8, 9], 6: [14, 18, 22], 7: [17, 22], 8: [24], 9: [14], 10: [14, 17, 20, 23, 24], 11: [19, 20, 22, 23], 12: [20, 24], 13: [21, 22], 15: [], 17: [], 19: [25], 20: [23], 21: [23, 24], 22: [], 24: []})
[16, 20, 22, 4, 5]
对于每一行,我都缺少一个元素,例如 0 是:[13, 16, 20, 22, 4, 5]
不是 [16, 20, 22, 4, 5]
它错过了 13
然后当使用函数dfs
时出现错误:
for v in G[s]: # for every edge (s, v) KeyError: 16
"""Performs a depth first search in graph G starting from vertex s
Input: G - the input graph in the adjacency list representation via a dictionary
s - the starting vertex
explored - a set of explored vertices
distance - a dictionary representing the topological order of the vertices
current_label - the current order of the topological order, disguised as a mutable list"""
def dfs(G, s, explored, distance, current_label):
explored.add(s)
#print G[s]
for v in G[s]: # for every edge (s, v)
if v not in explored:
dfs(G, v, explored, distance, current_label)
distance[current_label[0]] = s
current_label[0] -= 1
"""Performs and outputs a topological sort of graph G using dfs
Input: G - the input graph in the adjacency list representation via a dictionary
distance - a dictionary representing the topological order of the vertices"""
def topological_sort(G, distance):
explored = set()
current_label = [len(G)]
for v in G.keys():
if v not in explored:
dfs(G, v, explored, distance, current_label)
def main():
f = open('topo.txt', 'r')
line_list = f.readlines()
G = {int(line.split('->')[0]): [int(val) for val in line.split(',')[1:] if val] for line in line_list if line}
print("G:", G)
distance = dict()
topological_sort(G, distance)
topo = iter(sorted(distance.items()))
print("A topological order of G is:")
for _, vertex in topo:
print( vertex + " ")
print()
if __name__ == '__main__':
main()
正确的代码是什么样的?输出应该是
1, 0, 2, 6, 3, 7, 4, 5, 18, 10, 11, 16, 8, 9, 13, 17, 19, 12, 14, 21, 15, 20, 24, 23, 22, 25
最佳答案
line.split(',')[1:]
在 0 -> 13,16,20,22,4,5
上运行时采用部分 16,20,22,4,5
这不是你想要的。它应该是 line.split('->')[1].split(',')
。我个人会更明确地编写此内容以避免双重 .split('->')
调用:
def parse_graph(lines):
G = dict()
for line in lines:
left, right = line.split('->')
G[int(left)] = [int(val) for val in right.split(',')]
return G
...
G = parse_graph(line_list)
接下来,由于并非每个顶点都在 G
中作为键,因此您应该在 dfs
中添加以下行:
#dfs
...
if s in G: #add this
for v in G[s]: # for every edge (s, v)
if v not in explored:
dfs(G, v, explored, distance, current_label, l)
...
#
最后,将 print( vertex + "")
更改为 print( str(vertex), end=' ')
。剩下的好像还好。
您可能需要考虑的另一件事是,不必跟踪两个参数current_label
、距离
,您可以只保留一个列表顶点
>,可以说,它保留了访问过的顶点的顺序。所以改为
distance[current_label[0]] = s
current_label[0] -= 1
你可以有
vertices.append(s)
效果是一样的。然而,最后,您应该打印 reversed(vertices)
这将是您的拓扑顺序。
关于python - 从邻接表中获取图的拓扑顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32857997/
是否可以可视化kubernetes拓扑并看到它在添加/删除/链接对象时实时更新? 我在https://www.youtube.com/watch?v=38SNQPhsGBk上观看了一个视频,其中服务/
我在搜索 Rack 拓扑时发现了这个问题...可能是hadoop认证问题: 您的群集在三个不同的 Rack 中都有从属节点,并且您编写了一个 Rack 拓扑脚本,以将每台计算机分别识别为位于 Rack
我拿了sticky force layout示例并尝试添加额外链接并使用 enter() 更新布局,但随后所有链接都消失了,FireBug 也没有显示任何错误。 这些行不应该添加链接吗? graph.
我需要编写一个订单管理器,将客户(股票、外汇等)订单发送到适当的交易所。客户想要发送订单,但对 FIX 或其他专有协议(protocol)一无所知,只知道发送订单的内部(规范化)格式。我有应用程序(服
我正在尝试使用 Eclipse 在远程主机上提交 Storm 拓扑。 这是我的代码: Config conf = new Config(); conf.setDebug(false); conf.se
这是一个名为 mininet 的流行网络模拟器的拓扑文件 我创建了一个 MultiSwitch() 类,我想将其传递给我的 Topology 类以用作默认开关 有没有办法做到这一点?我对Python不
我从 cat/proc/cpuinfo 中了解到,我正在使用 Intel(R) Core(TM) i5 CPU M 560 @ 2.67GHz。但是我想知道确切的层次结构,比如有多少个套接字,每个套接
我正在学习storm。我对我们可以在 Apache Storm 上一次运行的拓扑数量有疑问。我已经在 Storm 集群上提交了两个拓扑,但一次只运行了一个拓扑。我需要杀死或停用已经存在的拓扑拓扑以运行
我正在尝试理解 topology of queues并交换 MT 在 RabbitMQ 中创建的。 我不能得到这两个陈述: we generate an exchange for each queue
我正在寻找一种方法来测试 Kafka Streams 应用程序。这样我就可以定义输入事件,测试套件会向我显示输出。 如果没有真正的 Kafka 设置,这可能吗? 最佳答案 更新 Kafka 1.1.0
我正在使用 Java 类将拓扑提交到 Storm 集群,我还计划使用 Java 类来终止拓扑。但根据 Storm documentation ,以下命令用于终止拓扑并且没有 Java 方法(这是有正当
Storm jar storm-starter-topologies-0.10.0-beta1.jar storm-starter-master.jar生产拓扑本地 我遇到了错误: Running:
我正在编写一个 dockerized Java Spring 应用程序,该应用程序使用 Apache Storm v1.1.2、Kafka v0.11.0.1、Zookeeper 3.4.6、Eure
我在 jts 拓扑库中有一些多边形。如果我想在 javafx Pane 上绘图,我会这样做: Polygon poly=new Polygon();//javafx //g is geometry
我需要在 Java GUI 应用程序中动态绘制(星形)拓扑。通过星形拓扑,我的意思是这样的: (来源:thebryantadvantage.com) 不需要太花哨,但我不想做得太丑陋和粗糙。我所说的动
我想自动化设置 Mininet 的过程虚拟机,通过 SSH 连接到 VM,在 VM 中启动 Mininet,并初始化拓扑。我需要 session 保持打开状态,以便我可以使用创建的网络向 Minine
我正在尝试重新平衡使用 KafkaSpout 的 Storm 拓扑。我的代码是: TopologyBuilder builder = new TopologyBuilder(); Pr
标题几乎说明了一切,我有一些 Storm 拓扑,我想测量它们的延迟,即来自 Kafka 的消息与最终相关执行的最后一点之间的时间量 bolt 。如果我可以深入研究结果以查看每个 bolt 之间的延迟,
假设我想让一些转换“A”可配置。此转换使用状态存储管理某些状态,并且还需要重新分区,这意味着仅在配置后才会进行重新分区。现在,如果我按照以下方式(或任何其他组合)运行应用程序 3 次(也可能是滚动升级
我目前正在尝试实现与 R 语言集成的 Storm 拓扑。 作为起点,我采用了以下项目 ( https://github.com/allenday/R-Storm ),它通过扩展 ShellBolt 类
我是一名优秀的程序员,十分优秀!