两个节点之间可能路径的边列表,显示边的重复性。

huangapple go评论59阅读模式
英文:

List of all possible path of edges between two nodes, showing edges multiplicity

问题

I can help you with the translation. Here's the text you provided in Chinese:

我正在开发一个算法,将电网建模为无向多重图,以便使用Python分析电网上的故障。
我有一些函数,可以获取两个节点之间的所有最短路径(不包含循环),并需要获取考虑它们重复性的边缘。
假设我有源节点“LAG-138”和负载“SCA-24”,我的程序找到一条路径:

[“SCA-24”,“SCA-69”,“SCO-69”,“PAA-69”,“OCA-T4-69”,“OCA-138”,“LVR-138”,“LAG-138”]

还有许多其他路径,但为了简单起见,这里只有一条。
从这个输出中,我可以获取两个节点之间的所有边以及它们的重复性,如下所示:

[‘SCA-24 < - > SCA-69 (1)’, ‘SCA-24 < - > SCA-69 (2)’]
[‘SCO-69 < - > SCA-69 (1)’]
[‘SCO-69 < - > PAA-69 (1)’]
[‘PAA-69 < - > OCA-T4-69 (1)’, ‘PAA-69 < - > OCA-T4-69 (2)’]
[‘OCA-T4-69 < - > OCA-138 (1)’]
[‘OCA-138 < - > LVR-138 (1)’]
[‘LVR-138 < - > LAG-138 (1)’, ‘LVR-138 < - > LAG-138 (2)’]

我从路径序列中的每一对节点获得这个输出。对于每一对节点,我都会获取它们之间的所有边。
现在,如何获取这些输出并获取所有路径?

我需要的输出将如下所示:
[
[‘SCA-24 < - > SCA-69 (1)’, ‘SCO-69 < - > SCA-69 (1)’, ‘SCO-69 < - > PAA-69 (1)’, ‘PAA-69 < - > OCA-T4-69 (1)’, ‘OCA-T4-69 < - > OCA-138 (1)’, ‘LVR-138 < - > LAG-138 (1)’],
[‘SCA-24 < - > SCA-69 (1)’, ‘SCO-69 < - > SCA-69 (1)’, ‘SCO-69 < - > PAA-69 (1)’, ‘PAA-69 < - > OCA-T4-69 (1)’, ‘OCA-T4-69 < - > OCA-138 (1)’, ‘LVR-138 < - > LAG-138 (2)’],
[‘SCA-24 < - > SCA-69 (1)’, ‘SCO-69 < - > SCA-69 (1)’, ‘SCO-69 < - > PAA-69 (1)’, ‘PAA-69 < - > OCA-T4-69 (2)’, ‘OCA-T4-69 < - > OCA-138 (1)’, ‘LVR-138 < - > LAG-138 (1)’],
[‘SCA-24 < - > SCA-69 (1)’, ‘SCO-69 < - > SCA-69 (1)’, ‘SCO-69 < - > PAA-69 (1)’, ‘PAA-69 < - > OCA-T4-69 (2)’, ‘OCA-T4-69 < - > OCA-138 (1)’, ‘LVR-138 < - > LAG-138 (2)’],
[‘SCA-24 < - > SCA-69 (2)’, ‘SCO-69 < - > SCA-69 (1)’, ‘SCO-69 < - > PAA-69 (1)’, ‘PAA-69 < - > OCA-T4-69 (1)’, ‘OCA-T4-69 < - > OCA-138 (1)’, ‘LVR-138 < - > LAG-138 (1)’],
[‘SCA-24 < - > SCA-69 (2)’, ‘SCO-69 < - > SCA-69 (1)’, ‘SCO-69 < - > PAA-69 (1)’, ‘PAA-69 < - > OCA-T4-69 (1)’, ‘OCA-T4-69 < - > OCA-138 (1)’, ‘LVR-138 < - > LAG-138 (2)’],
[‘SCA-24 < - > SCA-69 (2)’, ‘SCO-69 < - > SCA-69 (1)’, ‘SCO-69 < - > PAA-69 (1)’, ‘PAA-69 < - > OCA-T4-69 (2)’, ‘OCA-T4-69 < - > OCA-138 (1)’, ‘LVR-138 < - > LAG-138 (1)’],
[‘SCA-24 < - > SCA-69 (2)’, ‘SCO-69 < - > SCA-69 (1)’, ‘SCO-69 < - > PAA-69 (1)’, ‘PAA-69 < - > OCA-T4-69 (2)’, ‘OCA-T4-69 < - > OCA-138 (1)’, ‘LVR-138 < - > LAG-138 (2)’],
]

这是所有路径的列表,按照边缘排列。这可能是一个简单的问题,但现在我想不出任何解决方法。
请帮忙。

If you have any further questions or need assistance with the problem described in this text, feel free to ask.

英文:

I'm working on a algorithm that models a electric grid as a Undirected Multigraph so I can analyse the faults on the grid using python.

I have the functions that get me all the minimum paths (paths that do not loop) between two nodes and need to get the edges accounting for their multiplicity.

Say I have the source "LAG-138", and the load "SCA-24", my program finds a path:

[&quot;SCA-24&quot;, &quot;SCA-69&quot;, &quot;SCO-69&quot;, &quot;PAA-69&quot;, &quot;OCA-T4-69&quot;, &quot;OCA-138&quot;, &quot;LVR-138&quot;, &quot;LAG-138&quot;]

And many others, but for the sake of simplicity, here's one.

From this output, I can get all the edges between two nodes, and their multiplicity like this:

[&#39;SCA-24 &lt;-&gt; SCA-69 (1)&#39;, &#39;SCA-24 &lt;-&gt; SCA-69 (2)&#39;]
[&#39;SCO-69 &lt;-&gt; SCA-69 (1)&#39;]
[&#39;SCO-69 &lt;-&gt; PAA-69 (1)&#39;]
[&#39;PAA-69 &lt;-&gt; OCA-T4-69 (1)&#39;, &#39;PAA-69 &lt;-&gt; OCA-T4-69 (2)&#39;]
[&#39;OCA-T4-69 &lt;-&gt; OCA-138 (1)&#39;]
[&#39;OCA-138 &lt;-&gt; LVR-138 (1)&#39;]
[&#39;LVR-138 &lt;-&gt; LAG-138 (1)&#39;, &#39;LVR-138 &lt;-&gt; LAG-138 (2)&#39;]

I get this output from every pair of nodes in the sequence of the path. For every pair, I get all the edges between them.

Now, how can I get these outputs and get all the paths?

The output I need from this would be like this:

[
    [&#39;SCA-24 &lt;-&gt; SCA-69 (1)&#39;, &#39;SCO-69 &lt;-&gt; SCA-69 (1)&#39;, &#39;SCO-69 &lt;-&gt; PAA-69 (1)&#39;, &#39;PAA-69 &lt;-&gt; OCA-T4-69 (1)&#39;, &#39;OCA-T4-69 &lt;-&gt; OCA-138 (1)&#39;, &#39;LVR-138 &lt;-&gt; LAG-138 (1)&#39;],
    [&#39;SCA-24 &lt;-&gt; SCA-69 (1)&#39;, &#39;SCO-69 &lt;-&gt; SCA-69 (1)&#39;, &#39;SCO-69 &lt;-&gt; PAA-69 (1)&#39;, &#39;PAA-69 &lt;-&gt; OCA-T4-69 (1)&#39;, &#39;OCA-T4-69 &lt;-&gt; OCA-138 (1)&#39;, &#39;LVR-138 &lt;-&gt; LAG-138 (2)&#39;],
    [&#39;SCA-24 &lt;-&gt; SCA-69 (1)&#39;, &#39;SCO-69 &lt;-&gt; SCA-69 (1)&#39;, &#39;SCO-69 &lt;-&gt; PAA-69 (1)&#39;, &#39;PAA-69 &lt;-&gt; OCA-T4-69 (2)&#39;, &#39;OCA-T4-69 &lt;-&gt; OCA-138 (1)&#39;, &#39;LVR-138 &lt;-&gt; LAG-138 (1)&#39;],
    [&#39;SCA-24 &lt;-&gt; SCA-69 (1)&#39;, &#39;SCO-69 &lt;-&gt; SCA-69 (1)&#39;, &#39;SCO-69 &lt;-&gt; PAA-69 (1)&#39;, &#39;PAA-69 &lt;-&gt; OCA-T4-69 (2)&#39;, &#39;OCA-T4-69 &lt;-&gt; OCA-138 (1)&#39;, &#39;LVR-138 &lt;-&gt; LAG-138 (2)&#39;],
    [&#39;SCA-24 &lt;-&gt; SCA-69 (2)&#39;, &#39;SCO-69 &lt;-&gt; SCA-69 (1)&#39;, &#39;SCO-69 &lt;-&gt; PAA-69 (1)&#39;, &#39;PAA-69 &lt;-&gt; OCA-T4-69 (1)&#39;, &#39;OCA-T4-69 &lt;-&gt; OCA-138 (1)&#39;, &#39;LVR-138 &lt;-&gt; LAG-138 (1)&#39;],
    [&#39;SCA-24 &lt;-&gt; SCA-69 (2)&#39;, &#39;SCO-69 &lt;-&gt; SCA-69 (1)&#39;, &#39;SCO-69 &lt;-&gt; PAA-69 (1)&#39;, &#39;PAA-69 &lt;-&gt; OCA-T4-69 (1)&#39;, &#39;OCA-T4-69 &lt;-&gt; OCA-138 (1)&#39;, &#39;LVR-138 &lt;-&gt; LAG-138 (2)&#39;],
    [&#39;SCA-24 &lt;-&gt; SCA-69 (2)&#39;, &#39;SCO-69 &lt;-&gt; SCA-69 (1)&#39;, &#39;SCO-69 &lt;-&gt; PAA-69 (1)&#39;, &#39;PAA-69 &lt;-&gt; OCA-T4-69 (2)&#39;, &#39;OCA-T4-69 &lt;-&gt; OCA-138 (1)&#39;, &#39;LVR-138 &lt;-&gt; LAG-138 (1)&#39;],
    [&#39;SCA-24 &lt;-&gt; SCA-69 (2)&#39;, &#39;SCO-69 &lt;-&gt; SCA-69 (1)&#39;, &#39;SCO-69 &lt;-&gt; PAA-69 (1)&#39;, &#39;PAA-69 &lt;-&gt; OCA-T4-69 (2)&#39;, &#39;OCA-T4-69 &lt;-&gt; OCA-138 (1)&#39;, &#39;LVR-138 &lt;-&gt; LAG-138 (2)&#39;],
]

A list of all the paths, arrenged among the edges. It's probably a simple problem, but right now I can't think of anything
Please Help.

答案1

得分: 2

这听起来你想要使用itertools.product。

for path in itertools.product(a, b, c, d, ....):
    print(path)

将为你提供每一种可能性,从a中取一个,从b中取一个,依此类推。

如果像我怀疑的那样,你实际上有一个列表,比如paths_list,其中paths_list[0]是从第一个节点到第二个节点的路径,path_lists[1]是从第二个节点到第三个节点的路径,依此类推,

for path in itertools.product(*paths_list):
    ...

这两种方法都会返回一个迭代器。如果你只想要整个列表,那么

list(itertools.product(*paths_list))
英文:

It sounds like you want itertools.product.

for path in itertools.product(a, b, c, d, ....):
    print(path)

will give you every possibility, taking one from a, one from b, etc.

If, as I suspect, you actually have a list, say paths_list where paths_list[0] are the paths from the first node to the second, path_lists[1] are the paths from the second node to the third, etc., then

for path in itertools.product(*paths_list):
    ...

Both of these return an iterator. If you just want the whole list, then

list(itertools.product(*paths_list))

huangapple
  • 本文由 发表于 2023年6月15日 04:01:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定