问题描述
我的程序创建一个完整的图形,打印边缘,然后创建一个邻接表。但是,它效率不高。我希望程序执行以下操作:
import networkx as nx
import matplotlib.pyplot as plt
#Create complete graph
complete_graph = nx.complete_graph(10)
plt.subplot(121)
nx.draw(complete_graph,with_labels=True,font_weight='bold')
#list edges -----I want this to output to a file instead
list(complete_graph.edges)
# A class to represent the adjacency list of the node
class AdjNode:
def __init__(self,data):
self.vertex = data
self.next = None
# A class to represent a graph. A graph
# is the list of the adjacency lists.
# Size of the array will be the no. of the
# vertices "V"
class Graph:
def __init__(self,vertices):
self.V = vertices
self.graph = [None] * self.V
# Function to add an edge in an undirected graph
def add_edge(self,src,dest):
# Adding the node to the source node
node = AdjNode(dest)
node.next = self.graph[src]
self.graph[src] = node
# Adding the source node to the destination as
# it is the undirected graph
node = AdjNode(src)
node.next = self.graph[dest]
self.graph[dest] = node
# Function to print the graph
def print_graph(self):
for i in range(self.V):
print("Adjacency list of vertex {}\n head".format(i),end="")
temp = self.graph[i]
while temp:
print(" -> {}".format(temp.vertex),end="")
temp = temp.next
print(" \n")
# Driver program to the above graph class. -----> instead of coding each edge,I'd like for main to
# read the edge text file created earlier to create the Adjacency List.
if __name__ == "__main__":
V = 10
graph = Graph(V)
graph.add_edge(0,1)
graph.add_edge(0,2)
graph.add_edge(0,3)
graph.add_edge(0,4)
graph.add_edge(0,5)
graph.add_edge(0,6)
graph.add_edge(0,7)
graph.add_edge(0,8)
graph.add_edge(0,9)
graph.add_edge(1,2)
graph.add_edge(1,3)
graph.add_edge(1,4)
graph.add_edge(1,5)
graph.add_edge(1,6)
graph.add_edge(1,7)
graph.add_edge(1,8)
graph.add_edge(1,9)
graph.add_edge(2,3)
graph.add_edge(2,4)
graph.add_edge(2,5)
graph.add_edge(2,6)
graph.add_edge(2,7)
graph.add_edge(2,8)
graph.add_edge(2,9)
graph.add_edge(3,4)
graph.add_edge(3,5)
graph.add_edge(3,6)
graph.add_edge(3,7)
graph.add_edge(3,8)
graph.add_edge(3,9)
graph.add_edge(4,5)
graph.add_edge(4,6)
graph.add_edge(4,7)
graph.add_edge(4,8)
graph.add_edge(4,9)
graph.add_edge(5,6)
graph.add_edge(5,7)
graph.add_edge(5,8)
graph.add_edge(5,9)
graph.add_edge(6,7)
graph.add_edge(6,8)
graph.add_edge(6,9)
graph.add_edge(7,8)
graph.add_edge(7,9)
graph.add_edge(8,9)
graph.print_graph()
输出:
Adjacency list of vertex 0
head -> 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1
Adjacency list of vertex 1
head -> 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 0
Adjacency list of vertex 2
head -> 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 1 -> 0
Adjacency list of vertex 3
head -> 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 2 -> 1 -> 0
Adjacency list of vertex 4
head -> 9 -> 8 -> 7 -> 6 -> 5 -> 3 -> 2 -> 1 -> 0
Adjacency list of vertex 5
head -> 9 -> 8 -> 7 -> 6 -> 4 -> 3 -> 2 -> 1 -> 0
Adjacency list of vertex 6
head -> 9 -> 8 -> 7 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0
Adjacency list of vertex 7
head -> 9 -> 8 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0
Adjacency list of vertex 8
head -> 9 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0
Adjacency list of vertex 9
head -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0
解决方法
完成所需内容的最简单方法是使用json对其进行序列化和反序列化。
您将在Graph
中拥有这样的方法:
import json
def serialize(self) -> str:
graph = {}
for i in range(self.V):
temp = self.graph[i]
tmp_list = []
while temp:
tmp_list.append(temp)
temp = temp.next
graph[i] = json.dumps(tmp_list).replace(",","->")
return json.dumps(graph,indent=4)
它将返回格式如下的字符串:
{
"3": "[1->2->3]","5": "[1->2->3]"
}
您可以将其保存到文件中,也可以做任何您想做的事情。
对于反序列化,我们将需要另一种方法:
@staticmethod
def deserialize(serialized: str) -> "Graph":
serialized = serialized.replace("->",")
graph_dict = json.loads(serialized)
V = len(graph_dict)
graph = Graph(V)
for src in graph_dict.keys():
for dest in graph_dict[src]:
graph.add_edge(src,dest)
return graph
更真实的方法是编写自己的序列化格式,然后使用Interpreter Pattern进行反序列化解析,或者使用lark编写语法并进行解析。