- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我需要在无向和加权图中找到连接的节点。我确实在这里查找了一些建议,但没有人碰巧回答与我的问题相关的问题。这些节点对也碰巧与邻居连接,并且连接时每一对节点在几秒钟内花费一些时间连接。我试图找到连接的组件和相同评论连接的次数以及它们连接多长时间(时间)。
例如:
Node Node time
A B 34
A B 56
A C 09
A D 5464
A C 456
C B 36
C A 345
B C 346
所以所有A B C
都连接了两次
Nodes connected time
[A B C] 1 34+09+36 = 79
[A B C] 1 56+345+346 = 747
预期输出是
Nodes connected time
[A B C] 2 826
And
Node connected time
[A B] 2 90
[B C] 2 382
[A C] 2 354
代码:
import networkx as nx
import numpy as np
from collections import defaultdict
count = defaultdict(int)
time = defaultdict(float)
data = np.loadtxt('USC_Test.txt')
for line in data:
edge_list = [(line[0], line[1])]
G= nx.Graph()
G.add_edges_from(edge_list)
components = nx.connected_components(G)
count['components'] += 1
time['components'] += float(line[2])
print components, count['components'], time['components']
输入:
5454 5070 2755.0
5070 4391 2935.0
1158 305 1.0
5045 3140 48767.0
4921 3140 58405.0
5372 2684 460.0
1885 1158 351.0
1349 1174 6375.0
1980 1174 650.0
1980 1349 650.0
4821 2684 469.0
4821 937 459.0
2684 937 318.0
1980 606 390.0
1349 606 750.0
1174 606 750.0
5045 3545 8133.0
4921 3545 8133.0
3545 3140 8133.0
5045 4243 14863.0
4921 4243 14863.0
4243 3545 8013.0
4243 3140 14863.0
4821 4376 5471.0
4376 937 136.0
2613 968 435.0
5372 937 83.0
错误的输出
我得到的输出是错误的
Last_node_pair total_count_of_line total_time of Entire input data
我应该去哪里
[5045 3140 4921] [number_of_times_same_components_connected] [total_time_components_connected]
最佳答案
这里有几个问题:
这是应该起作用的东西。出于懒惰,我扫了两遍数据。
import networkx as nx
import numpy as np
from collections import defaultdict
count = defaultdict(int)
time = defaultdict(float)
data = np.loadtxt('USC_Test.txt')
G = nx.Graph()
for line in data:
a,b,time = line
G.add_edge(a, b)
results = defaultdict(lambda: list([0, 0.0]))
components = nx.connected_components(G)
component_map = { }
component_stats = defaultdict(lambda: list([0,0.0]))
edge_stats = defaultdict(lambda: list([0,0.0]))
for nodes in components:
for node in nodes:
component_map[int(node)] = tuple(nodes)
for a,b,time in data:
component_stats[component_map[a]][0] += 1
component_stats[component_map[a]][1] += time
if len(component_map[a]) > 2:
edge_stats[(a,b)][0] += 1
edge_stats[(a,b)][1] += time
for nodes,(count,time) in component_stats.iteritems():
print sorted([ int(n) for n in nodes ]), count, time
print
for nodes,(count,time) in edge_stats.iteritems():
print sorted([ int(n) for n in nodes ]), count, time
根据您的输入,这会产生以下输出:
[606, 1174, 1349, 1980] 6 9565.0
[305, 1158, 1885] 2 352.0
[968, 2613] 1 435.0
[937, 2684, 4376, 4821, 5372] 7 7396.0
[4391, 5070, 5454] 2 5690.0
[3140, 3545, 4243, 4921, 5045] 9 184173.0
[1349, 1980] 1 650.0
[937, 4376] 1 136.0
[606, 1980] 1 390.0
[3140, 4921] 1 58405.0
[937, 5372] 1 83.0
[606, 1349] 1 750.0
[4391, 5070] 1 2935.0
[3545, 4921] 1 8133.0
[1158, 1885] 1 351.0
[3140, 3545] 1 8133.0
[2684, 4821] 1 469.0
[2684, 5372] 1 460.0
[937, 2684] 1 318.0
[1174, 1980] 1 650.0
[3140, 5045] 1 48767.0
[5070, 5454] 1 2755.0
[4376, 4821] 1 5471.0
[606, 1174] 1 750.0
[3545, 5045] 1 8133.0
[4243, 4921] 1 14863.0
[3140, 4243] 1 14863.0
[4243, 5045] 1 14863.0
[937, 4821] 1 459.0
[3545, 4243] 1 8013.0
[1174, 1349] 1 6375.0
[305, 1158] 1 1.0
希望对您有所帮助!
关于python - 查找连通分量 Networkx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25506026/
我想使用错误组件显示我的错误消息,但不想在 中加载组件对于经过身份验证的用户,导航菜单也不应显示。 我有这样的应用程序组件.. 我有错误处理程序,它使用 router.navigate 路由
我正在尝试获取 RGB 图像,将其转换为 LAB(又名 CIE L* a* b*)色彩空间,然后提取 L* 分量。 这是我的代码: from skimage import io, color from
我在我的一个模型中定义了以下常量。 export const NEWS_TYPE_TEXT = { News: 'News', Interview: 'Intervie
我有一个Electron(6)/Angular(8)应用程序。 在正面( Angular ),我通过IPCRenderer向背面发送一条消息。 在背面,IPCMain接收消息并执行所需的操作,例如,获
我正在尝试在我的应用程序中创建一个可重用的 quickView 模式,以使用 ng-bootstrap modal library 动态加载任何组件。 就我加载文档中所示的相同示例组件而言,它工作正常
我需要将一个名为“photos”的数组从我的 component.ts 传递到 component.html。这是我的 component.ts 文件 export class PhotosCompo
我有一个按钮,单击该按钮会转到新路线并打开附加到该路线的另一个组件。 有没有一种方法可以从 DOM 中删除我们单击以转到不同组件的组件?示例:当单击“单击我返回主页”按钮时,它会打开另一个组件。在这种
这个问题在这里已经有了答案: Detect click outside Angular component (12 个答案) 5天前关闭。 我知道这方面有无数的问题,我尝试了每一个解决方案,但没有一个
我想将显示值的格式传递给 Angular 分量。 例如: format="'(utc, offset) location (tz)'" === '(UTC -04:00) New York (EDT)
我正在使用 Angular 组件将动态图表加载到我的小部件中: 这是我的组件的示例: angular.module("generalApp").component("chartPie", { temp
假设我有一个组件在被点击时发出一个事件,即 @Component({ selector: 'component-checkout-payment', template:
我有一个问题。 我正在处理另一个人的代码,有一个 JFrame 有很多 JSeparators(他用它们作为“面板”的边框)现在我将它们替换为 JBorderedPanel 类,该类遵循与整体相同的边
所以我在这里想做的是制作一个 Angular 组件并将其注入(inject)到我的 Angular 应用程序中。这是 Angular 分量的代码: (function(angular) { 'use
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
我正在创建一个像这样的可重用组件: submit 我想在属性 isDisabled 为 true 时禁用点击事件,我尝试了类似的操作,但它不起作用。 packages/component/my-b
一种简单的说法是,当 RGB 分量相等时,它们形成灰色。然而,这还不是全部,因为如果它们只有细微的差别,它们看起来仍然是灰色的。 假设观看者具有健康的色彩视觉,我如何确定给定值是否会被视为灰色(大概具
您好我正在尝试使用带 Angular Electron 构建桌面应用程序,主要问题是在用户登录后我找不到正确加载主要组件的方法。正如您在 main.js 中看到的,这是我创建两个窗口(1 个用于登录的
new AngularJS 1.5 中似乎没有“替换”选项组件概念(就像指令一样)。 如果我想要表格行,你有什么建议元素作为组件?就有效的 HTML 而言是不可能的吗? 真实示例:mailBox组件内
我有颜色=#12FFFF。这是这种格式的颜色,其中 12FFFF 是十六进制数。现在我想获取每个独立的 R、G、B 分量的十进制。我该如何在java中做到这一点? 最佳答案 目前尚不清楚你的问题是什么
我需要一些关于 Java 的 ImageIO API 的帮助。我似乎迷失在 ComponentColorModel 类中。我需要逐像素检查 *.png 文件来检测它是灰度图像还是彩色图像。但是,我不知
我是一名优秀的程序员,十分优秀!