Skip to content

Add function to produce edges in all simple paths (migrated from Trac #732) #718

Closed

Description

@networkx-trac

Original ticket https://networkx.lanl.gov/trac/ticket/732
Reported 2012-06-12 by @hagberg, assigned to trac user sergionery.

The all_simple_paths() function produces nodes. Add a function that produces the edges with optional data.

This ticket continues #713.

Activity

  1. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by trac user sergionery, 2012-06-14

    Changes in simple_paths8.py:

    • Fixed the _edges function to work with any arbitrary hashable data (not just consecutive integers starting at 0).
    • Added new parameter data=(True|False). Now it is possible to (optionally) return the edges data (keys and values).
    • Revised the documentation examples of all_simple_paths function.
    • added some tests at the end of file.

    To do:

    • Add some documentation to all_simple_paths_edges.
  2. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Attachment in Trac by trac user sergionery, 2012-06-14: simple_paths8.py

  3. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Attachment in Trac by trac user sergionery, 2012-07-08: simple_cycles.py

  4. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by trac user sergionery, 2012-07-08

    In order to implement the all_circular_paths function, it was implemented a new version of simple_cycles function. The idea is to use this function in both undirected graphs or multigraphs.

  5. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by trac user sergionery, 2012-07-09

    Changes in simple_paths9.py:

    • added a new function
      '''
      paths_edges(G,path,data=False)
      '''
      that, receiving a graph and a path (simple list of nodes), returns (an iterator for) the edges of path. This function can be used in conjunction with a lot of other functions that return paths (all_simple_paths, all_shortest_paths, simple_cycles) making very simple to retrieve their edges, even for a MULTIGRAPH ^(see NOTE)^.
      This function can receive as input a (Di)Graph or a Multi(Di)Graph, with the optional parameter data=(False|True). When the data parameter is True, the function outputs all edges data, in this format for each edge:
      '''
      (u,v,{data})
      '''
      for (Di)Graphs, or
      '''
      (u,v,{key: {data}})
      '''
      for Multi(Di)Graphs

      Obs:The given path must be a valid path in a graph G - but it is possible to add a try/except to handle this issue.

    • changed the edge function ^(see NOTE)^
      '''
      all_simple_paths_edges
      '''

    • added new edges functions ^(see NOTE)^
      '''
      all_circular_paths_edges, and all_shortest_paths_edges
      '''

    NOTE: Due the new added function path_edges, the 'EDGES' functions became very simple to implement. For this reason, perhaps the NEW 'EDGES' functions:
    '''
    all_simple_paths_edges, all_circular_paths_edges, and all_shortest_paths_edges
    '''
    won't be necessary any more.

  6. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Attachment in Trac by trac user sergionery, 2012-07-09: simple_paths9.py

  7. ghost assigned on Dec 29, 2012
  8. amirc commented on Aug 9, 2016

    @amirc

    Hi,
    Any chance that someone still has the file simple_paths9.py ?
    I'm looking for it because I'm trying to do something similar to this question.

    Thanks,
    Amir.

  9. jfinkels commented on Aug 24, 2016

    @jfinkels
    Contributor

    To get the paths as edges, you can do something like

    >>> import networkx as nx
    >>> G = nx.complete_graph(4)
    >>> for p in map(nx.utils.pairwise, nx.all_simple_paths(G, 0, 3)):
    ...     print(list(p))
    ... 
    [(0, 1), (1, 2), (2, 3)]
    [(0, 1), (1, 3)]
    [(0, 2), (2, 1), (1, 3)]
    [(0, 2), (2, 3)]
    [(0, 3)]

    If you are not using the development version of NetworkX, you can implement pairwise yourself by doing something like pairwise = lambda l: zip(l, l[1:]).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions