是否可以(快速)仅找到networkx图中的第一个周期?

时间:2018-11-14 16:14:10

标签: python python-3.x networkx cycle

我有一个定向网络,其中可能有也可能没有循环。我需要找到它们并消除周期性。如果我有networkx DiGraph(G),则可以使用

查找所有循环
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.jakewharton:butterknife:9.0.0-rc1'
    implementation 'com.google.android.gms:play-services-maps:16.0.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc1'
    implementation 'com.google.android.gms:play-services-location:16.0.0'
    implementation 'com.github.david-serrano:locationprovider:1.1'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support:support-media-compat:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    compileOnly 'org.projectlombok:lombok:1.18.4'
    annotationProcessor 'org.projectlombok:lombok:1.18.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

这将创建一个循环返回发生器。

但是,我不想返回所有循环,例如cycle_nodes = nx.simple_cycles(G) ,因为许多循环都是彼此的子集,而修复一个循环将修复其他循环。相反,我只想找到一个循环的第一个实例。由于list(cycle_nodes)是生成器,因此我尝试了

cycle_nodes

仅返回第一个实例。但是,我发现返回第一个实例所需的时间与返回所有实例所需的时间相比并不小:

next(cycle_nodes)

这是否仅是由于我的图的性质(即,第一个循环沿搜索顺序很远),还是有一种更有效的方法来返回任何循环(不一定需要是第一个循环)? / p>

我怀疑可能会有更快的方法的原因是因为当我运行list(cycle_nodes) : 58s next(cycle_nodes) : 44s 时,它只花费一秒钟或两秒钟并返回False,因此很显然,它可以在大约一秒钟的时间内找到至少一个周期。

1 个答案:

答案 0 :(得分:0)

答案很明显。不提供起始节点的算法nx.find_cycle()将迅速返回它找到的第一个循环。我的印象是需要提供一个起始节点RTFM!

相关问题