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?