Menu

Shortestpath from Python

Help
Pascal
2019-01-02
2019-06-29
  • Pascal

    Pascal - 2019-01-02

    Hi
    I have successfully installed Tulip from pip3. The C++ library provides a floydWarshall_SPAP function. Is it avalaible from Python ? Could you please provide a simple and minimal working example ? I'm completely new to Tulip. Thanks

     

    Last edit: Pascal 2019-01-02
    • brenoust

      brenoust - 2019-01-04

      Dear Pascal,

      The python bindings have just been pushed recently (but I don't think it is
      in the bundle yet)
      https://github.com/Tulip-Dev/tulip/issues/39.

      One usage would be:
      tlp.selectShortestPaths(graph, tlp.node(1), tlp.node(2), tlp.AllPaths,
      graph['viewMetric'], graph['viewSelection'])

      the paths are stored in the edges (and connected nodes) of graph with
      graph['viewSelection'] value equal to True.

      For reference, here are the details:

      ret = tlp.selectShortestPaths(graph, src, tgt, pathType, weights, selection)

      :param graph: The graph to compute on. :type graph: :class:tlp.Graph
      :param src: The source node of the paths :type node: :class:tlp.node
      :param tgt: The target node of the paths :type node: :class:tlp.node
      :param pathType: The type of path to consider :type pathType: tlp.OnePath,
      tlp.OneDirectedPath, tlp.OneReversedPath, tlp.AllPaths,
      tlp.AllDirectedPaths, tlp.AllReversedPaths (Reversed means directed with
      reversed direction)
      :param weights: A graph property giving the edges weight if weighted paths
      have to be considered. Can be set to null to select unweighted paths. :type
      weights:class:tlp.DoubleProperty
      :param selection: The graph property to consider as selection. :type
      selection:class:tlp.BooleanProperty
      :rtype: boolean (True if a path is found)
      :throws: an exception if one of the source or target nodes does not belong
      to the graph.

      Best,

      Benjamin

      On Wed, 2 Jan 2019 at 21:45, Pascal elastika@users.sourceforge.net wrote:

      Hi
      I have successfully installed Tulip from pip3. The C++ library provides a
      floydWarshall_SPAP function. Is it avalaible from Python ? Could you
      provide a simple and minimal working example, I'm completely new to Tulip.
      Thanks


      Shortestpath from Python
      https://sourceforge.net/p/auber/discussion/206283/thread/ae78b9363d/?limit=25#8580


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/auber/discussion/206283/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       
      • Pascal

        Pascal - 2019-01-04

        Hi Benjamin and thanks for your detailed answer

        The version installed on my machine is 5.2.1 and doesn't support selectShortestPaths:

        >>> from tulip import tlp
        >>> tlp.selectShortestPaths
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
          File "/home/po/.local/lib/python3.6/site-packages/tulip/__init__.py", line 41, in __getattr__
            raise AttributeError(name)
        AttributeError: selectShortestPaths
        

        Upgrading via pip3 doesn't help.

        By the way, does some docs provide a tutorial for Python users (I don't use C++) in order to get familiar with Tulip fundamentals? Any advice?

         
        • brenoust

          brenoust - 2019-01-05

          Yes it is not yet in the last release, you'd probably have to compile Tulip
          yourself or wait just a little bit for the next release to come out.
          By the way, sourceforge is used only for releasing binaries, I would
          suggest you to reach the Github repository for up-to-date information:
          https://github.com/Tulip-Dev/tulip

          As for all the python documentation, you can find it here, alongside with
          quite a lot of examples:
          http://tulip.labri.fr/Documentation/current/tulip-python/html/index.html
          --
          Benjamin

          On Fri, 4 Jan 2019 at 19:35, Pascal elastika@users.sourceforge.net wrote:

          Hi Benjamin and thanks for your detailed answer

          The version installed on my machine is 5.2.1 and doesn't support
          selectShortestPaths:

          from tulip import tlp>>> tlp.selectShortestPathsTraceback (most recent call last):
          File "<stdin>", line 1, in <module>
          File "/home/po/.local/lib/python3.6/site-packages/tulip/init.py", line 41, in getattr
          raise AttributeError(name)AttributeError: selectShortestPaths</module></stdin>

          Upgrading via pip3 doesn't help.

          By the way, does some docs provide a tutorial for Python users (I don't
          use C++) in order to get familiar with Tulip fundamentals? Any advice?


          Shortestpath from Python
          https://sourceforge.net/p/auber/discussion/206283/thread/ae78b9363d/?limit=25#8580/2e7c/0d5c


          Sent from sourceforge.net because you indicated interest in
          https://sourceforge.net/p/auber/discussion/206283/

          To unsubscribe from further messages, please visit
          https://sourceforge.net/auth/subscriptions/

           
  • Pascal

    Pascal - 2019-01-05

    Ok, I will wait for the next release. And thanks for the pointer to the Python docs.

     
  • Pascal

    Pascal - 2019-06-27

    Hi!
    I have upgraded to version 5.3 and now selectShortestPaths is recognised, great.

    Unfortunately, did'nt find many Python demo. Can someone kindly provide an example showing selectShortestPaths basic usage? For instance, given a directed weighted graph such as

    wedges =[[1,2, 3.2], [2,3, 4.2], [1,3, 5.0]]

    how can we retrieve the all pair shortest path matrix? Thanks in advance.

     
    • brenoust

      brenoust - 2019-06-28

      Hi Pascal,

      You may refer to the github post here:
      https://github.com/Tulip-Dev/tulip/issues/39#issuecomment-451337822

      Best,

      Benjamin

      On Fri, 28 Jun 2019 at 03:59, Pascal elastika@users.sourceforge.net wrote:

      Hi!
      I have upgraded to version 5.3 and now selectShortestPaths is recognised,
      great.

      Unfortunately, did'nt find many Python demo. Can someone kindly provide an
      example showing selectShortestPaths basic usage? For instance, given a
      directed weighted graph such as

      wedges =[[1,2, 3.2], [2,3, 4.2], [1,3, 5.0]]

      how can we retrieve the all pair shortest path matrix? Thanks in advance.

      Shortestpath from Python
      https://sourceforge.net/p/auber/discussion/206283/thread/ae78b9363d/?limit=25#06c9


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/auber/discussion/206283/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       
      • Pascal

        Pascal - 2019-06-28

        Thanks. The problem is that the link you provide is more or less equivalent to help(selectShortestPaths) and still requires a lot of digging before getting a concrete case to work. I was able to do this kind of stuff with other libraries (NetworkX, Networkit, Graph-Tool, Igraph, SageMath for instance) because their docs provide some Python examples. But they are very few in Tulip documentation. So, it would be very appreciated if somebody could kindly put in action the selectShortestPaths function against the directed graph with for instance wedges =[[1,2, 3.2], [2,3, 4.2], [1,3, 5.0]] ? Thanks in advance, Pascal

         
        • brenoust

          brenoust - 2019-06-28

          Hi Pascal,

          I feel a bit of frustration over our documentation in your message. We are
          very sorry, this is a new feature of the Python API here, so the doc may
          not be fully explicit.
          And the feature may be improved to a more pythonic way in the future
          (please do not hesitate to PR if you wish to contribute).
          A basic example of the feature is:

          tlp.selectShortestPaths(graph, tlp.node(1), tlp.node(2), tlp.AllPaths,
          graph['viewMetric'], graph['viewSelection'])

          The first argument is the graph in which you want to compute your shortest
          path,

          the second is your source node,
          the third is your target node,

          the fourth is the kind of path you want to find:
          - only one path considering the graph as undirected (tlp.OnePath),
          - only one path considering the graph as directed (tlp.OneDirectedPath),
          - only one path considering the graph as directed in the opposit direction (
          tlp.OneReversedPath),
          - all paths considering the graph as undirected (tlp.AllPath),
          - all paths considering the graph as directed (tlp.AllDirectedPaths),
          - all paths considering the graph as directed in the opposit direction (
          tlp.AllReversedPaths),

          the fifth argument is a weight property
          the sixth argument is the selection (a boolean property) in which the
          results will be stored

          a boolean is returned to tell you if or not a path has been found
          the results are stored in a boolean selection (graph['viewSelection']) for
          which all nodes and edges statisfying your condition are set to True.
          That means you can directly create a subgraph from the results of the
          search.

          However, it seems that you want to directly transpose the logic of a
          different API to Tulip.
          I am not sure what you mean by "wedges =[[1,2, 3.2], [2,3, 4.2], [1,3, 5.0]]",
          but I suppose you would like to define the directed weighted graph in an
          online manner.
          The logic behind Tulip is a bit different to other libraries, because Tulip
          handles hierarchies of graphs and subgraphs, with property inheritance, and
          does so in an optimized fashion.

          From your example, you could create the graph like this:

          graph = tlp.newGraph()

          n1 = graph.addNode()
          n2 = graph.addNode()
          n3 = graph.addNode()

          e1 = graph.addEdge(n1, n2)
          e2 = graph.addEdge(n2, n3)
          e3 = graph.addEdge(n1, n3)

          graph['weight'][e1] = 3.2
          graph['weight'][e2] = 4.2
          graph['weight'][e3] = 5.0

          Given this example, you could search the shortest path:

          this property will store the results

          graph.getBooleanProperty('shortest path')
          tlp.selectShortestPaths(graph, n1, n2, tlp.OnePath, graph['weight'],
          graph['shortest path'])

          Then you can iterate over this graph, for example with a DFS search (maybe
          that was the tricky part?):

          create a subgraph from the result of the search

          sg = addSubGraph(graph['shortest path'])
          for n in sg.dfs(n1):
          #do something

          And you can do a lot more cool stuff from there, such as measuring
          centrality in the results, laying out the corresponding subgraph etc. etc.
          I hope this can help you!
          If you have some feature request, sure you will, or other issues, please
          try to post them on our github, as we have moved to this platform.

          Thank you very much,

          Benjamin

          On Fri, 28 Jun 2019 at 16:09, Pascal elastika@users.sourceforge.net wrote:

          Thanks. The problem is that the link you provide is more or less
          equivalent to help(selectShortestPaths) and still requires a lot of digging
          before getting a concrete case to work. I was able to do this kind of stuff
          with other libraries (NetworkX, Networkit, Graph-Tool, Igraph, SageMath for
          instance) because their docs provide some Python examples. But they are
          very few in Tulip documentation. So, it would be very appreciated if
          somebody could kindly put in action the selectShortestPaths function
          against the directed graph with for instance wedges =[[1,2, 3.2], [2,3,
          4.2]
          , [1,3, 5.0]] ? Thanks in advance, Pascal


          Shortestpath from Python


          Sent from sourceforge.net because tulipdev@labri.fr is subscribed to
          https://sourceforge.net/p/auber/discussion/206283/

          To unsubscribe from further messages, a project admin can change settings
          at https://sourceforge.net/p/auber/admin/discussion/forums. Or, if this is
          a mailing list, you can unsubscribe from the mailing list.

           
          • Pascal

            Pascal - 2019-06-28

            I really appreciate your time Benjamin, your example is very helpfull, many thanks. You are right, Tulip approach is different but the library seems very powerfull and efficient. I will follow Tulip on Github!

             

Log in to post a comment.