你可以把刚才给我的代码用代码格式再发一遍吗
时间: 2023-12-13 17:04:00 浏览: 79
当然可以,下面是完整的代码,包括将经过的边的编号写入Excel的部分:
```python
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
df = pd.read_excel(r"C:\Users\li'yi'jie\Desktop\运筹学网络规划数据.xlsx")
edges = []
for i in range(len(df)):
edge = { "id": df.loc[i, "边的编号"],
"tail": df.loc[i, "边的尾节点"],
"head": df.loc[i, "边的头节点"],
"length": df.loc[i, "长度"],
"capacity": df.loc[i, "容量"] }
edges.append(edge)
plt.figure(figsize=(15,15))
G = nx.DiGraph()
for edge in edges:
G.add_edge(edge["tail"], edge["head"], weight=edge["length"], id=edge["id"])
all_pairs = dict(nx.all_pairs_dijkstra_path(G, weight='weight'))
rows = []
for start_node, dist_dict in all_pairs.items():
for end_node, path in dist_dict.items():
path_edges = []
for i in range(len(path)-1):
edge_id = G[path[i]][path[i+1]]['id']
path_edges.append(edge_id)
rows.append({'起始节点': start_node, '终止节点': end_node, '最短路径长度': dist_dict[end_node], '经过的边': path_edges})
df_result = pd.DataFrame(rows)
df_result.to_excel('all_pairs.xlsx', index=False)
```
阅读全文