networkx: efficiently find absolute longest path in digraph

There is a linear-time algorithm mentioned at http://en.wikipedia.org/wiki/Longest_path_problem Here is a (very lightly tested) implementation EDIT, this is clearly wrong, see below. +1 for future testing more than lightly before posting import networkx as nx def longest_path(G): dist = {} # stores [node, distance] pair for node in nx.topological_sort(G): pairs = [[dist[v][0]+1,v] for v in … Read more

What could cause NetworkX & PyGraphViz to work fine alone but not together?

There is a small bug in the draw_graphviz function in networkx-1.11 triggered by the change that the graphviz drawing tools are no longer imported into the top level namespace of networkx. The following is a workaround In [1]: import networkx as nx In [2]: G = nx.complete_graph(5) In [3]: from networkx.drawing.nx_agraph import graphviz_layout In [4]: … Read more

Hypergraph with networkx

Did you want something like this use nx.draw_networkx_nodes using nodelist parameter: # Author: Aric Hagberg ([email protected]) import matplotlib.pyplot as plt import networkx as nx G = nx.Graph() G.add_edge(‘a’, ‘b’, weight=0.6) G.add_edge(‘a’, ‘c’, weight=0.2) G.add_edge(‘c’, ‘d’, weight=0.1) G.add_edge(‘c’, ‘e’, weight=0.7) G.add_edge(‘c’, ‘f’, weight=0.9) G.add_edge(‘a’, ‘d’, weight=0.3) elarge = [(u, v) for (u, v, d) in G.edges(data=True) … Read more

Path between two nodes

igraph, another graph module for Python can calculate all the shortest paths between a given pair of nodes. Calculating all the paths does not make sense as you have infinitely many such paths. An example for calculating all the shortest paths from vertex 0: >>> from igraph import Graph >>> g = Graph.Lattice([10, 10], circular=False) … Read more