设计图表学习计划 [英] Designing a graph study program

查看:69
本文介绍了设计图表学习计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一些图形算法(最小生成树,呼吸

搜索第一个拓扑排序等等)并且更好地理解

理论我'用python实现它们......


我制作了自己的图类,构造函数就是这样:

类图:

" in forma di matrice e''una matrice normale,in form di lista uso un

dizionario"

def __init __(self,nodes,edges, dir = False,weight = []):#inizializzatore

dell''oggetto,di default in forma di lista di adiacenza e undirected

#il grafo puo''essere anche pesato

" di default uso la lista di adiacenza per rappresentare il grafo,

usare set"

self.adj_list = {}

self.nodes = nodes

self.edges = edge#in modo da avere comodi questi dati,se

bidirezionale non ci sono tutti

self.weight = weight

self.dir = dir#an che questo deve essere raggiungibile

节点中的n:

self.adj_list [n] = []

节点中的n:

如果dir:#se ho la direzione guardo l''ordine dei vertici nel lato

如果n == e [ 0]:

self.adj_list [n] .append(e [1])

elif n in e:

other = e [ ((e.index(n))+ 1)%2]

self.adj_list [n] .append(其他)

如果重量:

self.w = {}

for idx in range(len(edges)):

self.w [edges [idx]] = weight [idx] #signgno in corrispondenza


那么我想画图,我发现pydot正在工作

非常好。

但是我想这样做,一个交互式程序,看看

算法是否有效...

例如在呼吸搜索中,每次算法

为节点着色,程序应该重绘图形。哪些模块

我应该用于图形(我使用macosX,我想要一些交叉

平台)。


现在我正在做这样的事情


def draw_graph(self):

""" disegna un grafo con pydot""" ;

导入os

输出=''graph.png''

self.dot_graph.write_png(输出)

com =''打开''+输出

os.popen(com)


我觉得这很丑陋而且不适合我的目的。


我还创建了一个表示矩阵的类(对于

图的矩阵视图)但我发现numpy有一个非常完整的实现,我想b $ b我会选择它。


非常感谢,如果您有任何建议/链接请

写吧:)

I''m studying some graphs algorithm (minumum spanning tree, breath
search first topological sort etc etc...) and to understand better the
theory I''m implementing them with python...

I made my own graph class, the constructor is simply this:
class graph:
"in forma di matrice e'' una matrice normale, in forma di lista uso un
dizionario"
def __init__(self,nodes,edges,dir=False,weight=[]): # inizializzatore
dell''oggetto, di default in forma di lista di adiacenza e undirected
# il grafo puo'' essere anche pesato
"di default uso la lista di adiacenza per rappresentare il grafo,
usare set"
self.adj_list = {}
self.nodes = nodes
self.edges = edges # in modo da avere comodi questi dati, se
bidirezionale non ci sono tutti
self.weight = weight
self.dir = dir # anche questo deve essere raggiungibile
for n in nodes:
self.adj_list[n] = []
for n in nodes:
for e in edges:
if dir: # se ho la direzione guardo l''ordine dei vertici nel lato
if n == e[0]:
self.adj_list[n].append(e[1])
elif n in e:
other = e[((e.index(n))+1) % 2]
self.adj_list[n].append(other)
if weight:
self.w = {}
for idx in range(len(edges)):
self.w[edges[idx]] = weight[idx] # assegno in corrispondenza

Well then I wanted to draw graphs and I found that pydot is working
really nicely.
BUT I''d like to do this, an interactive program to see ho the
algorithms works...
For example in the breath search first, every time the algorithm
colors a node, the program should redraw the graphs. Which modules
should I use for graphics (I use macosX and I''d like something cross
platforms).

Now I''m doing something like this

def draw_graph(self):
"""disegna un grafo con pydot"""
import os
output = ''graph.png''
self.dot_graph.write_png(output)
com = ''open ''+output
os.popen(com)

which I think is very ugly and not fitting very well for my purpose.

I also created a class to represent matrix (for the matrix view of the
graph) but I found that numpy has a very complete implementation, I
think I''ll go with it.

Thank you very much, if you have any kind of suggestions/links please
write it :)

推荐答案

>
>

那么我想画图,我发现pydot正在工作

非常好。

但是我想这样做,一个交互式程序,看看

算法是否有效...

例如在呼吸搜索中,每次算法

为节点着色,程序应该重绘图形。哪些模块

我应该用于图形(我使用macosX,我想要一些交叉

平台)。
Well then I wanted to draw graphs and I found that pydot is working
really nicely.
BUT I''d like to do this, an interactive program to see ho the
algorithms works...
For example in the breath search first, every time the algorithm
colors a node, the program should redraw the graphs. Which modules
should I use for graphics (I use macosX and I''d like something cross
platforms).



使用捆绑的Tkinter。我已经使用TCL / Tk在我的
毕业典礼中实现了类似的东西,并且Tk完全适合这项任务。


Diez

Use the bundled Tkinter. I''ve implemented a similar thing back in my under
graduation days using TCL/Tk, and Tk is perfectly suited for that task.

Diez


On 8 Mag,13:02,Diez B. Roggisch < d ... @nospam.web.dewrote:
On 8 Mag, 13:02, "Diez B. Roggisch" <d...@nospam.web.dewrote:

那么我想画图,我发现pydot是工作

非常好。

但是我想这样做,这是一个互动程序,可以看到

算法的工作原理......

例如在呼吸搜索中,每次算法

为节点着色时,程序应该重绘图形。哪些模块

我应该用于图形(我使用macosX,我想要一些交叉

平台)。
Well then I wanted to draw graphs and I found that pydot is working
really nicely.
BUT I''d like to do this, an interactive program to see ho the
algorithms works...
For example in the breath search first, every time the algorithm
colors a node, the program should redraw the graphs. Which modules
should I use for graphics (I use macosX and I''d like something cross
platforms).



使用捆绑的Tkinter。我使用TCL / Tk在我的
毕业典礼上实现了类似的东西,Tk非常适合这项任务。


Diez


Use the bundled Tkinter. I''ve implemented a similar thing back in my under
graduation days using TCL/Tk, and Tk is perfectly suited for that task.

Diez



好​​的,非常感谢,我会尝试的。

但我有一些设计上的疑惑,我想保留算法(对于

示例bfs)尽可能干净,独立于绘图

方法。

我怎么能让它迈出一步通过算法没有更多的b $ b复杂代码?也许使用线程?


谢谢

Ok thank you very much I''ll try with that.
But I have some design doubts, I''d like to keep the algorithm (for
example bfs) as clean as possible, being independent from the drawing
methods.
And how could I make it step through algorithms without having a more
complicated code? Maybe using threads?

Thanks


In< 11 ********** ************@n59g2000hsh.googlegroups .com> ;, andrea写道:
In <11**********************@n59g2000hsh.googlegroups .com>, andrea wrote:

但我有一些设计疑虑,我会喜欢保持算法(对于

示例bfs)尽可能干净,独立于绘图

方法。

我怎么能让它逐步通过算法而不需要更多的b $ b复杂代码?也许使用线程?
But I have some design doubts, I''d like to keep the algorithm (for
example bfs) as clean as possible, being independent from the drawing
methods.
And how could I make it step through algorithms without having a more
complicated code? Maybe using threads?



创建一个通知某些观察者的API。关于访问

节点,遍历或添加egde等操作。这样你就可以注册

在图形和GUI之间转换的回调。


如果你不想改变算法或图形和节点类这个

通知可以在某种程度上由包装类注入。


对于算法的细粒度观察,你可能会尝试

实现一步一步的调试器。


Ciao,

Marc''BlackJack''Rintsch

Create an API that informs some "observer" about actions like visiting a
node, traversing or adding an egde and so on. This way you can register
callbacks that translate between the graph and the GUI.

If you don''t want to change the algorithm or graph and node classes this
notification can be injected by wrapper classes to some degree.

For very fine grained observation of an algorithm you might try to
implement a step by step debugger.

Ciao,
Marc ''BlackJack'' Rintsch

这篇关于设计图表学习计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆