2

I had a question regarding networkx and creating a random path on this graph. However, say I want to generate a random path on graph which ends back at its beginning.

This post: (python networkx: How to get a random path?) explains how you can get a random path. However using path = next(nx.all_simple_paths(G, source=0, target=5))takes very long to compute as I have a graph with 5000 nodes and edges and does not take into account that the path can have a maximum length and needs to be back at its starting point.

MVC example, say I wanted to compute an random route from random startpoint which can have length 5 or smaller and needs to be back at its original startpunt in at the end of the path from the following graph:

import random
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import math
import pandas as pd
from matplotlib import animation
#from JSAnimation import IPython_display
%matplotlib inline

# initialise graph object
G = nx.Graph()
color_map =[]
G.add_node(1, pos=(1, 0)); color_map.append('r')
G.add_node(2, pos=(2, 0)); color_map.append('r')
G.add_node(3, pos=(3, -1)); color_map.append('r')
G.add_node(4, pos=(3, 1)); color_map.append('r')
G.add_node(5, pos=(4, -1)) ;color_map.append('r')
G.add_node(6, pos=(4, 1)); color_map.append('r')
G.add_node(7, pos=(5, 0)); color_map.append('r')
G.add_node(8, pos=(6, 0));  color_map.append('r')

e = [(1, 2, 1), 
    (2, 3, 1), 
    (2, 4, 2), 
    (3, 5, 5), 
    (4, 6, 2), 
    (5, 7, 1), 
    (4,5, 2),
    (6,3, 1),
     (7,2, 3),
    (6, 7, 2), 
    (7, 8, 1)]

G.add_weighted_edges_from(e)
labels = nx.get_edge_attributes(G,'weight')

nx.draw(G,nx.get_node_attributes(G, 'pos'))
nx.draw_networkx_edge_labels(G,nx.get_node_attributes(G, 'pos'),edge_labels=labels)
nx.draw_networkx_labels(G,nx.get_node_attributes(G, 'pos')

How would you setup a code for this?

Dominique Fortin
  • 2,212
  • 15
  • 20
Jeroen123
  • 55
  • 8
  • This is a very non-trivial problem. I would separate the problem from the code implementation and cross-post to one of the mathematical stackexchange sites -- for computer science or mathematics, for example. – Paul Brodersen Oct 31 '18 at 10:55
  • You can get the reachable nodes by [taking the adjacency matrix to the N/2 power](https://math.stackexchange.com/questions/1411108/raising-an-adjacency-matrix-to-a-power-why-does-it-work), where N is the length of the path. – Paul Brodersen Oct 31 '18 at 10:58
  • Are the paths always fairly short? If so, then creating a smaller subgraph based on the adjacency matrix power might be sufficient such that you can then create all paths and filter for paths where the start and end point are the same. Otherwise, we need to think of something smarter. Can you link to a non-toy example edge list (ideally in csv format), and indicate median and maximum path lengths? – Paul Brodersen Oct 31 '18 at 11:11
  • No the paths are not necessarily short, therefore the approach with the adjacency matrix will take too much computing time I think. @PaulBrodersen – Jeroen123 Nov 01 '18 at 09:30
  • I'll upload a non-toy example edge list in csv format :-) I'll go search for one that represents my network the best. It could be fairly any large enough road network where the crossings are nodes and the streets are edges. Later today I will upload this :-) @PaulBrodersen – Jeroen123 Nov 01 '18 at 09:33
  • @PaulBrodersen I've upload an csv file to this throwaway dropbox account. Anyone with this link can download the cvs file. https://www.dropbox.com/s/aswpo7mh1tcv97j/roads.csv?dl=0 – Jeroen123 Nov 03 '18 at 14:56
  • @PaulBrodersen Seems that the conversion from shape file to cvs resulted in a csv file with only nodes. Therefore I created a new link to a folder containing the shape file: https://www.dropbox.com/sh/v4i6jtv1ty0j6f5/AABtFyB7_MhPiYeS__mqLA12a?dl=0 – Jeroen123 Nov 03 '18 at 15:28
  • If computing a matrix power takes too long for your application in mind, then I am out of ideas. Taking the 100th power of a 5000x5000 random, *dense* matrix of floats took 1.5 sec on my machine. Doing this with sparse boolean matrices should only be quicker. – Paul Brodersen Nov 05 '18 at 13:16
  • You are probably right! But as I am new to programming with python (learned myself), I don't really know how to setup a code for this. Would you happen to have a simple example which I can use? :-) Thanks for your help!! @PaulBrodersen – Jeroen123 Nov 12 '18 at 09:17

0 Answers0