[Cialug] Need An Algo Suggestion

Jeffrey Ollie jeff at ocjtech.us
Fri Oct 15 18:06:50 UTC 2021


Here's an example in Python that uses the NetworkX graph theory library (
https://networkx.org/):

import networkx as nx
import networkx.algorithms.simple_paths as sp

G = nx.DiGraph()

G.add_edge(1,2)
G.add_edge(2,3)
G.add_edge(2,4)
G.add_edge(3,6)

result=[]

for x in G.nodes:
    for y in G.nodes:
        if x == y:
            continue
        for path in sp.all_simple_paths(G, x, y):
            if path not in result:
                result.append(path)

result.sort()
for path in result:
    print(path)

And the result:

$ python test.py
[1, 2]
[1, 2, 3]
[1, 2, 3, 6]
[1, 2, 4]
[2, 3]
[2, 3, 6]
[2, 4]
[3, 6]

On Thu, Oct 14, 2021 at 4:42 PM Jeffrey Ollie <jeff at ocjtech.us> wrote:

> This is called in general graph theory. Each number is a node and the
> relations between them are edges. I haven't dealt with it much but I'm sure
> that there are a bazillion packages out there that implement various
> algorithms.
>
> On Thu, Oct 14, 2021 at 3:19 PM Todd Walton <tdwalton at gmail.com> wrote:
>
>> I'm not a programmer. Not really. If I had a list of things like this:
>>
>>  1,2
>>  2,3
>>  2,4
>>  3,6
>>
>> how would I map them so that I can see that 1 refers to 2, 2 refers to 3
>> and 4, and 3 refers to 6, so:
>>
>>  1,2,3,6
>>  1,2,4
>>  2,3,6
>>  2,4
>>  3,6
>>
>> Like that? I want to build up those chains. Every chain has an end.
>> There's
>> no infinite loops. But some have multiple paths.
>>
>> I'm not sure how to do that, and I'm not sure what to call it for the
>> purposes of DDGing it.
>>
>> --
>> Todd
>> _______________________________________________
>> Cialug mailing list
>> Cialug at cialug.org
>> https://www.cialug.org/cgi-bin/mailman/listinfo/cialug
>>
>
>
> --
> Jeff Ollie
> The majestik møøse is one of the mäni interesting furry animals in Sweden.
>


-- 
Jeff Ollie
The majestik møøse is one of the mäni interesting furry animals in Sweden.


More information about the Cialug mailing list