There is something that *almost* does that: networkx.predecessor().
That returns a dictionary of predecessors in a shortest path starting
at a given node.
For example
In [28]: G=networkx.Graph()
In [29]: G.add_cycle([0,1,2,3])
In [30]: p=networkx.predecessor(G,0) # shortest path starting at 0
In [31]: p
Out[31]: {0: [], 1: [0], 2: [1, 3], 3: [0]}
Then you just need to identify your target (say 2) and follow the
paths back:
In [32]: p[2]
Out[32]: [1, 3] # both one and 3 are on the shortest path
In [33]: p[1] # predecessor of 1 is the source so we are done
Out[33]: [0]
For weighted graphs you can use dijkstra_predecessor_and_distance()
(which returns two dictionaries).
Aric
I implement another function that returns all possible paths between
two nodes in a directed graph. Here is the code, feel free to improve
it.
def find_pair_all_path(g, frm_id, to_id, p_list, maxhop):
""" given graph g, and pm_list, create a list for all path
g: graph, limit: the maximum hop count
write all paths from frm_id to to_id at allpath_list
"""
debug = 0
path_list = p_list[:]
if debug: print "from %d to %d path %s" % (frm_id, to_id,
path_list)
if len(path_list) > maxhop:
if debug: print "path too long"
return None
elif len(path_list) == 0:
path_list.append(frm_id)
if to_id in path_list:
if debug: print "path contains loop"
return None
neigh = g.neighbors(frm_id)
if debug: print "\tid: %d neighbot: %s" % (frm_id, neigh)
for n in neigh:
if n == to_id:
if debug: print "found destination:", path_list
found_path_list = path_list[:]
found_path_list.append(to_id)
allpath_list.append(found_path_list)
elif n in path_list:
continue
else:
new_path_list = path_list[:]
new_path_list.append(n)
find_pair_all_path(g, n, to_id, new_path_list, maxhop)
return None
if __name__ == "__main__":
g = nx.complete_graph(40)
print("number of nodes:", g.number_of_nodes())
print("number of edges:", g.number_of_edges())
allpath_list = []
find_pair_all_path(g, 0, 1, [], 3)
print allpath_list
William Tu
On Aug 21 2009, 4:58 am, Atanas <atanas.kambu...@gmail.com> wrote:
> Hi,
>
> I implemented a function that returnsallshortest paths between two
> nodes in an undirected graph. Here is the code, feel free to improve
> and include in NetworkX:
>
> def all_shortest_paths(G,a,b):
> """ Return a list ofallshortest paths in graph G between nodes a
2011/4/8 William Tu <u901...@gmail.com>:
> Hi,
>
> I implement another function that returns all possible paths between
> two nodes in a directed graph. Here is the code, feel free to improve
> it.
I'm curious about the use cases for finding all paths between two
nodes, could you please elaborate on the use that you will give to
this function? Note that the OP of this thread was interested in all
*shortest* paths. Your implementation deals with the potentially
infinite paths problem (for graphs with cycles) by establishing a
maximum length for the paths returned (maxhop), is this a problem for
the use that you intend with this function?
I'm interested in this because I'm working on an implementation for
finding node-independent paths between two nodes (paths that only
share the source and target nodes). I have a first approximation (not
optimal) almost finished that I plan to upload to NetworkX Trac
shortly. This is useful as a (first step towards a) fast approximation
to find k-connected components in a moderately large networks (of the
order of hundreds of thousands of nodes and edges), see [1] for a
description of the algorithm and a possible way to approximate the
k-connected components.
Knowing other use cases for related problems could help me developing
code that would also be useful in other scenarios.
Salut!
I've opened a ticket with an implementation of the algorithm proposed
by White and Newman for finding node independent paths:
https://networkx.lanl.gov/trac/ticket/538
It is a first approximation and it's not well tested. As I explain in
the ticket, I have doubts regarding how to implement the search when
the nodes are adjacent and regarding how to implement a refinement
(gaining speed and losing accuracy) proposed by the authors. The later
is quite important for me because I would like to use this algorithm
in moderately large networks.
Comments, suggestions, code or tests would be greatly appreciated.
Salut!
2011/4/9 William Tu <u901...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
> To post to this group, send email to networkx...@googlegroups.com.
> To unsubscribe from this group, send email to networkx-discu...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/networkx-discuss?hl=en.
>
>