Python NetworkX simple_cycles Alternative Solution? - python

I’m very new to Python and NetworkX but I recently have learned how to do a few things to achieve what I’m after.
My problem involves a 75-node, 144-edge graph (from adjacency list) that I need to find all simple cycles within.
I was able to generate the cycles once using this code:
import NetworkX as nx
import numpy as np
G = nx.read_adjlist(‘myList.dat’);
print(list(nx.simple_cycles(G.to_directed())));
This worked initially but I realized I had forgotten to add a couple edges. After modifying the adjacency list, I ran the code again, but this time it just hung and produced no output.
I also tried replacing nx.simple_cycles() with nx.all_simple_paths() without converting the graph to directed, generating all paths from a node to the node right next to it. This could’ve gotten me closer to my answer, but it also hung with no output.
I know the more nodes and edges, the more time it will take, but is there a way to break up the results so that they are actually produced or something similar?
It could also be helpful to generate all simple cycles of length n, since I could then go through each integer until I reach the largest cycles.
If there is no possible way to do this using the method I chose, what are some alternative ways I could go about solving this problem?
Thank you in advance to anyone to helps out :)

Related

How to efficiently generate multiple random graphs with random edge weights in networkx

I would like to generate multiple Erdos-Renyi graphs with random edge weights. However, my code works quite slow since there are two nested loops. I was wondering if someone can help me with improving my code.
import networkx as nx
import random
#Suppose I generate 1000 different random graphs
for _ in range(1000):
#Let's say I will have 100 nodes and the connection probability is 0.4
G= nx.fast_gnp_random_graph(100,0.4)
#Then, I assign random edge weights.
for (u, v) in G.edges():
G.edges[u,v]['weight'] = random.randint(15,5000)
When I run a similar code block in R using igraph, it is super fast regardless of the size of the network. What are some alternative ways that I can accomplish the same task without facing slow execution time?
This benchmark shows the performance of many graphs libraries (from different languages). It confirms NetworkX is very slow. The graph-tool Python package seems a significantly faster alternative to NetworkX. Please note that the performance of a given package is dependent of what you want to achieve because the performance of a graph algorithm is very dependent of the chosen internal representation.

Is there a more efficient way to calculate the shortest path problem than networkx in python?

I am calculating the shortest path from one source to one goal on a weighted graph with networkx and single_source_dijkstra.
However, I run into memory problems.
Is there a more efficient way to calculate this? An alternative to Networkx? See my code:
cost, shortestpath = nx.single_source_dijkstra(graph, startpointcoords, secondptcoords,cutoff=10000000)
The bidirectional dijkstra algorithm should produce a significant improvement. Here is the documentation.
A good analogy would be in 3D: place one balloon at point x and expand it till it reaches point y. The amount of air you put in is proportional to the cube of the distance between them. Now put a balloon at each point and inflate both until they touch. The combined volume of air is only 1/4 of the original. In higher dimensions (which is a closer analogy to most networks), there is even more reduction.
Apparently the A* Algorithm of networkx is way more efficient. Afterwards I calculate the length of the resulting path with the dijkstra algorithm I posted.
Perhaps try using another algorithm? Your graph may have too many vertices but few edges, in which case you could use Bellman-Ford bellman_ford_path() link in networkX
Another solution would be to use another python package, for example the answers to this question has different possible libraries.
The last solution would be to implement your own algorithm! Perhaps Gabow's algorithm, but you would have to be very efficient for example by using numpy with numba

Matrix clustering using Python

I'm working on a growing matrix of data. I found that probably the best way to make my computations faster, I need to clusterize it somewhat in a way of this: Clusterized matrix
My matrix shows connections between nodes on a graph with their weights on the intersections.
I made a graph using NetworkX and noticed it does something similar. Screenshot: NX's Graph
Maybe I could use NetworkX's code to cluster it instead of growing my code by another function?
If not, then any python way of doing it would be helpful. I read many tutorial on hierarchical clustering but it all seems to be about connecting points in a two-dimensional space, not in the graph-space with given 'distances'.

Finding a tree decomposition of a graph in Python

I need an algorithm that can find a tree decomposition given a graph in Python, the graphs will be small so it doesn't need to be very efficient. I have looked around but cannot find much on this at all. Anyone know of a package that can do this or some sudo code I could use to make my own?
Have you looked at networkx? See: https://networkx.github.io/documentation/latest/reference/algorithms/approximation.html

Finding the Path of all Edges on a Graph

I'm trying to get the path on a graph which covers all edges, and traverses them only once.
This means there will only be two "end" points - which will have an odd-number of attached nodes. These end points would either have one connecting edge, or be part of a loop and have 3 connections.
So in the simple case below I need to traverse the nodes in this order 1-2-3-4-5 (or 5-4-3-2-1):
In the more complicated case below the path would be 1-2-3-4-2 (or 1-2-4-3-2):
Below is also a valid graph, with 2 end-points: 1-2-4-3-2-5
I've tried to find the name of an algorithm to solve this, and thought it was the "Chinese Postman Problem", but implementing this based on code at https://github.com/rkistner/chinese-postman/blob/master/postman.py didn't provide the results I expected.
The Eulerian path looks almost what is needed, but the networkx implementation will only work for closed (looped) networks.
I also looked at a Hamiltonian Path - and tried the networkx algorithm - but the graph types were not supported.
Ideally I'd like to use Python and networkx to implement this, and there may be a simple solution that is already part of the library, but I can't seem to find it.
You're looking for Eulerian Path that visits every edge exactly once. You can use Fleury's algorithm to generate the path. Fleury's algorithm has O(E^2) time complexity, if you need more efficient algorithm check Hierholzer's algorithm which is O(E) instead.
There is also an unmerged pull request for the networkx library that implements this. The source is easy to use.
(For networkx 1.11 the .edge has to be replaced with .edge_iter).
This is known as the Eulerian Path of a graph. It has now been added to NetworkX as eulerian_path().

Categories