使用 Python Library any python library 生成有向图
Generate a directed Graph using Python Library any python library
我正在 Python 中实现 GeeksForGeeks 的 Bellman ford 算法。我想使用一些库(如 pyplot 或 networkx 或类似的东西)生成图表(图表形式而不是字典类型 - 这很容易)。我希望图表 UI 包含节点、边和相应的成本。
from collections import defaultdict
#Class to represent a graph
class Graph:
def __init__(self,vertices):
self.V= vertices #No. of vertices
self.graph = [] # default dictionary to store graph
# function to add an edge to graph
def addEdge(self,u,v,w):
self.graph.append([u, v, w])
# utility function used to print the solution
def printArr(self, dist):
print("Vertex Distance from Source")
for i in range(self.V):
print("%d \t\t %d" % (i, dist[i]))
# The main function that finds shortest distances from src to
# all other vertices using Bellman-Ford algorithm. The function
# also detects negative weight cycle
def BellmanFord(self, src):
# Step 1: Initialize distances from src to all other vertices
# as INFINITE
dist = [float("Inf")] * self.V
dist[src] = 0
# Step 2: Relax all edges |V| - 1 times. A simple shortest
# path from src to any other vertex can have at-most |V| - 1
# edges
for i in range(self.V - 1):
# Update dist value and parent index of the adjacent vertices of
# the picked vertex. Consider only those vertices which are still in
# queue
for u, v, w in self.graph:
if dist[u] != float("Inf") and dist[u] + w < dist[v]:
dist[v] = dist[u] + w
# Step 3: check for negative-weight cycles. The above step
# guarantees shortest distances if graph doesn't contain
# negative weight cycle. If we get a shorter path, then there
# is a cycle.
for u, v, w in self.graph:
if dist[u] != float("Inf") and dist[u] + w < dist[v]:
print "Graph contains negative weight cycle"
return
# print all distance
self.printArr(dist)
g = Graph(5)
g.addEdge(0, 1, -1)
g.addEdge(0, 2, 4)
g.addEdge(1, 2, 3)
g.addEdge(1, 3, 2)
g.addEdge(1, 4, 2)
g.addEdge(3, 2, 5)
g.addEdge(3, 1, 1)
g.addEdge(4, 3, -3)
我想要在终端或单独文件中的图形是(基于上面的代码):
如果您检查此 tutorial for networkx,您会发现创建有向图以及绘制它是多么容易。
差不多,有向图或简单图都是一样的(API 明智),绘图也很简单,使用 Matplotlib 生成它。
你可以制作一个 Tk 应用程序,它允许你手动输入节点和边,并将它们存储在列表框中,并绘制一个图形,在这个函数中,这不会是拖放,但是,它仍然可以帮助您即时可视化图表。
和这个 Matplotlib tutorial,将告诉您如何将它嵌入到 TK 应用程序中。
ekiim 的 link 文档非常有用。这是我为绘制图形所做的代码:
import networkx as nx
import matplotlib.pyplot as plt
G=nx.DiGraph()
G.add_node(0),G.add_node(1),G.add_node(2),G.add_node(3),G.add_node(4)
G.add_edge(0, 1),G.add_edge(1, 2),G.add_edge(0, 2),G.add_edge(1, 4),G.add_edge(1, 3),G.add_edge(3, 2),G.add_edge(3,1),G.add_edge(4,3)
nx.draw(G, with_labels=True, font_weight='bold')
plt.show()
此代码免费打印有向图。我尝试按成本打印,但输出结果因成本混乱而严重失真。一些成本写在空白处,而边缘只有一两个。因此,如果有人知道实施它,那将非常有用。
我正在 Python 中实现 GeeksForGeeks 的 Bellman ford 算法。我想使用一些库(如 pyplot 或 networkx 或类似的东西)生成图表(图表形式而不是字典类型 - 这很容易)。我希望图表 UI 包含节点、边和相应的成本。
from collections import defaultdict
#Class to represent a graph
class Graph:
def __init__(self,vertices):
self.V= vertices #No. of vertices
self.graph = [] # default dictionary to store graph
# function to add an edge to graph
def addEdge(self,u,v,w):
self.graph.append([u, v, w])
# utility function used to print the solution
def printArr(self, dist):
print("Vertex Distance from Source")
for i in range(self.V):
print("%d \t\t %d" % (i, dist[i]))
# The main function that finds shortest distances from src to
# all other vertices using Bellman-Ford algorithm. The function
# also detects negative weight cycle
def BellmanFord(self, src):
# Step 1: Initialize distances from src to all other vertices
# as INFINITE
dist = [float("Inf")] * self.V
dist[src] = 0
# Step 2: Relax all edges |V| - 1 times. A simple shortest
# path from src to any other vertex can have at-most |V| - 1
# edges
for i in range(self.V - 1):
# Update dist value and parent index of the adjacent vertices of
# the picked vertex. Consider only those vertices which are still in
# queue
for u, v, w in self.graph:
if dist[u] != float("Inf") and dist[u] + w < dist[v]:
dist[v] = dist[u] + w
# Step 3: check for negative-weight cycles. The above step
# guarantees shortest distances if graph doesn't contain
# negative weight cycle. If we get a shorter path, then there
# is a cycle.
for u, v, w in self.graph:
if dist[u] != float("Inf") and dist[u] + w < dist[v]:
print "Graph contains negative weight cycle"
return
# print all distance
self.printArr(dist)
g = Graph(5)
g.addEdge(0, 1, -1)
g.addEdge(0, 2, 4)
g.addEdge(1, 2, 3)
g.addEdge(1, 3, 2)
g.addEdge(1, 4, 2)
g.addEdge(3, 2, 5)
g.addEdge(3, 1, 1)
g.addEdge(4, 3, -3)
我想要在终端或单独文件中的图形是(基于上面的代码):
如果您检查此 tutorial for networkx,您会发现创建有向图以及绘制它是多么容易。
差不多,有向图或简单图都是一样的(API 明智),绘图也很简单,使用 Matplotlib 生成它。
你可以制作一个 Tk 应用程序,它允许你手动输入节点和边,并将它们存储在列表框中,并绘制一个图形,在这个函数中,这不会是拖放,但是,它仍然可以帮助您即时可视化图表。
和这个 Matplotlib tutorial,将告诉您如何将它嵌入到 TK 应用程序中。
ekiim 的 link 文档非常有用。这是我为绘制图形所做的代码:
import networkx as nx
import matplotlib.pyplot as plt
G=nx.DiGraph()
G.add_node(0),G.add_node(1),G.add_node(2),G.add_node(3),G.add_node(4)
G.add_edge(0, 1),G.add_edge(1, 2),G.add_edge(0, 2),G.add_edge(1, 4),G.add_edge(1, 3),G.add_edge(3, 2),G.add_edge(3,1),G.add_edge(4,3)
nx.draw(G, with_labels=True, font_weight='bold')
plt.show()
此代码免费打印有向图。我尝试按成本打印,但输出结果因成本混乱而严重失真。一些成本写在空白处,而边缘只有一两个。因此,如果有人知道实施它,那将非常有用。