图数据结构 [英] Graph Data Structures

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

问题描述

大家好,


目前我正在开发一个通用的图库,所以我可以针对各种项目进行各种基于图表的分析。 。目前

我正在实现Graph作为字典的包装器。目前我的

实现如下:


t = Graph()

n1 =节点(Node1)

n2 =节点(Test2)

edge1 = Edge(" Test3")

t + = n1 {n1:{}}

t [n1] [n2] = edge1 {n1:{n2:edge1}


但实际上这并不是我想要的结构。我希望

它最终以...... {n1:{n2:edge1},n2:{}}结束。是

无论如何我只能做到这一点????


另外我看着有一个大图并且想知道是否有人

无论如何都知道我可以减少这个

结构的内存需求并提高查询速度。我在想写作

a C扩展吧....这是一个好主意,我会从哪里开始?

或者Python有某种透明的内存访问模块我可以实现



非常感谢提前,


Nathan


PS .....请在下面找到我的代码:


class Graph(object):

def __init __(self,g = { }):

self.graph = g

def __iadd __(self,p):

如果p不在self.graph中:

self.graph [p] = PathsDict()

返回自我

def __getitem __(self,p):

尝试:

返回self.graph [p]

除了KeyError:

引发KeyError(%s不在图表中%(repr) (p)))

def __str __(自我):

返回str(self.graph)

def过滤器(self,filter):

通过


类PathsDict(对象):

def __init __(自我):

self .paths = {}

def __setitem __(self,p,val ):

如果p不在self.paths中:

self.paths [p] = val

def __getitem __(self,p):

返回self.paths [p]

#捕获异常这里

def路径(个体经营):

for k,v in self.paths:

yield(k,v)

def edges(self):

返回self.paths.values ()

def __str __(自我):

返回str(self.paths)

def __len __(自我):

返回len(self.paths)


类Node(对象):

def __init __(self,name):

self.name = name

def __str __(self):

返回self.name


class Edge(字典) :

def __init __(自我,姓名,重量= 1):

self [" name"] = name

self ["体重"] =体重

def __str __(自我):

返回自我[" name"]

Hi All,

Currently I am working on a generic graph library so I can do various
graph based analysis for various projects I have ideas for. Currently
I am implementing Graph as a wrapper around a dictionary. Currently my
implementation works like this:

t = Graph()
n1 = Node("Node1")
n2 = Node("Test2")
edge1 = Edge("Test3")
t += n1 { n1:{}}
t[n1][n2] = edge1 { n1:{n2:edge1}

However this isnt actually ending up with the structure I want. I want
it to finally end up as ...... { n1:{n2:edge1}, n2:{}}. Is
there anyway I can do this simply????

Also I am looking at having a large graph and was wondering if anyone
knew of anyway I could reduce the memory requirements of this
structure and improve the speed of queries on it. I m thinking writing
a C extension for it....is this a good idea and where would I start?
Or does Python have some kind of transparent memory access module I
can implement.

Many Thanks in advance,

Nathan

PS.....Please find my code below:

class Graph(object):
def __init__(self, g= { } ):
self.graph = g
def __iadd__(self, p):
if p not in self.graph:
self.graph[p] = PathsDict()
return self
def __getitem__(self, p):
try:
return self.graph[p]
except KeyError:
raise KeyError( "%s not in graph" %(repr(p)) )
def __str__(self):
return str(self.graph)
def filter(self, filter):
pass

class PathsDict(object):
def __init__(self):
self.paths = { }
def __setitem__(self, p, val):
if p not in self.paths:
self.paths[p] = val
def __getitem__(self, p):
return self.paths[p]
# catch exception here
def paths(self):
for k, v in self.paths:
yield (k, v)
def edges(self):
return self.paths.values()
def __str__(self):
return str(self.paths)
def __len__(self):
return len(self.paths)

class Node(object):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name

class Edge(dict):
def __init__(self, name, weight = 1):
self["name"] = name
self["weight"] = weight
def __str__(self):
return self["name"]

推荐答案

我没看过你的代码,但是

p中有很多图形实现ython。

如果您还没有找到这些:
http://wiki.python.org/moin/PythonGraphApi


如果你只想做一些分析,我认为你需要这个(如它是完全和简单的
):
https ://networkx.lanl.gov/


i还推荐Guido的文章:
http://www.python.org/doc/essays/graphs.html

i haven''t read your code, but there are many graph implementations in
python.
in case you haven''t found these yet:
http://wiki.python.org/moin/PythonGraphApi

if you only want to do some analysis i think you need this one (as it''s
pretty complete and simple):
https://networkx.lanl.gov/

i also recommend Guido''s essay to read:
http://www.python.org/doc/essays/graphs.html


2006年11月25日星期六14:05:27 +0000,Nathan Harmston写道:
On Sat, 25 Nov 2006 14:05:27 +0000, Nathan Harmston wrote:

大家好,


目前我正在开发一个通用图库,所以我可以为我想法的各种项目做各种基于图表的分析。目前

我正在实现Graph作为字典的包装器。目前我的

实现如下:
Hi All,

Currently I am working on a generic graph library so I can do various
graph based analysis for various projects I have ideas for. Currently
I am implementing Graph as a wrapper around a dictionary. Currently my
implementation works like this:



[snip]

http://www.python.org/doc/essays/graphs.html

http:// mail .python.org / pipermail / pyt ... il / 137593.html

希望这会有所帮助。

-

史蒂文。

[snip]

http://www.python.org/doc/essays/graphs.html

http://mail.python.org/pipermail/pyt...il/137593.html
Hope this helps.
--
Steven.


Szabolcs Nagy写道:

.........
Szabolcs Nagy wrote:
.........

如果你只是想做一些分析,我认为你需要这个(因为它'

非常完整和简单):
https://networkx.lanl.gov/



。 ........


目前似乎已经打破了python追溯出现;不是

python和/或trac的好广告

-

Robin Becker

.........

seems to be broken at present with a python traceback coming out; not a
good advert for python and/or trac
--
Robin Becker


这篇关于图数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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