Omar Shehab
Omar Shehab

Reputation: 1092

KeyError in nx.algorithms.approximation.traveling_salesman_problem()

I have written a code which generates 10 random graphs and solve the traveling salesman problem using NetworkX. I am getting KeyError: 1.

Following is my code.

import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import time
import csv
import pandas as pd

# Function to generate a random graph
def generate_random_graph(num_nodes, edge_prob):
    return nx.erdos_renyi_graph(num_nodes, edge_prob)

# Function to solve TSP and track time
def solve_tsp(graph):
    start_time = time.time()
    cycle = nx.algorithms.approximation.traveling_salesman_problem(graph, cycle=True)
    end_time = time.time()
    elapsed_time = end_time - start_time
    return cycle, elapsed_time

# Number of nodes in the graph
num_nodes = 10

data_points = []
csv_data = []

# Generate 10 random graphs, solve TSP, and record data
for i in range(10):
    edge_prob = np.random.rand()  # Random edge probability
    graph = generate_random_graph(num_nodes, edge_prob)
    density = nx.density(graph)
    
    # Solve TSP
    cycle, elapsed_time = solve_tsp(graph)
    
    # Record data
    data_points.append((density, elapsed_time))
    
    # Prepare adjacency matrix for CSV
    adj_matrix = nx.adjacency_matrix(graph).todense().tolist()
    
    csv_data.append([adj_matrix, density, elapsed_time, cycle])

I am getting the following error:

KeyError                                  Traceback (most recent call last)
Cell In[7], line 8
      5 density = nx.density(graph)
      7 # Solve TSP
----> 8 cycle, elapsed_time = solve_tsp(graph)
     10 # Record data
     11 data_points.append((density, elapsed_time))

Cell In[3], line 4, in solve_tsp(graph)
      2 def solve_tsp(graph):
      3     start_time = time.time()
----> 4     cycle = nx.algorithms.approximation.traveling_salesman_problem(graph, cycle=True)
      5     end_time = time.time()
      6     elapsed_time = end_time - start_time

File ~/anaconda3/envs/py39/lib/python3.9/site-packages/networkx/utils/backends.py:412, in _dispatch.__call__(self, backend, *args, **kwargs)
    409 def __call__(self, /, *args, backend=None, **kwargs):
    410     if not backends:
    411         # Fast path if no backends are installed
--> 412         return self.orig_func(*args, **kwargs)
    414     # Use `backend_name` in this function instead of `backend`
    415     backend_name = backend

File ~/anaconda3/envs/py39/lib/python3.9/site-packages/networkx/algorithms/approximation/traveling_salesman.py:320, in traveling_salesman_problem(G, weight, nodes, cycle, method)
    318         if u == v:
    319             continue
--> 320         GG.add_edge(u, v, weight=dist[u][v])
    321 best_GG = method(GG, weight)
    323 if not cycle:
    324     # find and remove the biggest edge

KeyError: 1

How can I resolve it?

Upvotes: 0

Views: 38

Answers (0)

Related Questions