简体   繁体   English

带有邻接列表的Python中的图形表示

[英]graph representation in Python with adjacency list

I have found the following Graph implementation in Python: 我在Python中找到以下Graph实现:

class Vertex:
    def __init__(self,key):
        self.id = key
        self.connectedTo = {}

    def addNeighbor(self,nbr,weight=0):
        self.connectedTo[nbr] = weight

    def getConnections(self):
        return self.connectedTo.keys()

    def getId(self):
        return self.id

    def getWeight(self,nbr):
        return self.connectedTo[nbr]

class Graph:
    def __init__(self):
        self.vertList = {}
        self.numVertices = 0

    def addVertex(self,key):
        self.numVertices = self.numVertices + 1
        newVertex = Vertex(key)
        self.vertList[key] = newVertex
        return newVertex

    def getVertex(self,n):
        if n in self.vertList:
            return self.vertList[n]
        else:
            return None

    def addEdge(self,f,t,cost=0):
        if f not in self.vertList:
            nv=self.addVertex(f)
        if t not in self.vertList:
            nv=self.addVertex(t)
        self.vertList[f].addNeighbor(self.vertList[t], cost)

    def getVertices(self):
        return self.vertList.keys()

    def __iter__(self):
        return iter(self.vertList.values())


def main():
    g=Graph()
    for i in range(6):
        g.addVertex(i)
    g.addEdge(0,1,5)
    g.addEdge(1,5,4)
    g.addEdge(5,3,6)
    g.addEdge(3,4,5)
    for v in g:
        for w in v.getConnections():
            print v.getId(),",",w.getId(),",",v.getWeight(w)

if __name__=="__main__":
    main()

The program executes fine, but I was wondering if the author put some redundant parts, like in the functions addEdge() and addVertex() . 该程序执行良好,但我想知道作者是否放置了一些多余的部分,例如在函数addEdge()addVertex()

For what I see addEdge has an assignment that it never uses: 对于我所看到的,addEdge具有从未使用的分配:

nv=self.addVertex(t)

also the part that says: 还有说的部分:

if t not in self.vertList:
    nv=self.addVertex(t)

in the same method does not do anything, and the variable nv is not used at all. 在相同的方法中什么也没做,并且变量nv根本不使用。 For what I see this is the implementation of a directed graph so if I want to program a non-directed graph it will suffice to put: 对于我所看到的,这是有向图的实现,因此,如果我要编写无向图,则只需放置以下内容即可:

self.vertList[t].addNeighbor(self.vertList[f], cost)

So I can also get rid of the return newVertex in the addVertex function. 因此,我也可以摆脱addVertex函数中的返回newVertex。 I have done those modifications and the program works still fine. 我已经做了这些修改,程序仍然可以正常工作。 Would those modifications be good and not bring any strange behaviour when I reuse this class? 当我重用该类时,这些修改会很好并且不会带来任何奇怪的行为吗?

And one last question, in the part that says: 最后一个问题,说的是:

for v in g:
    for w in v.getConnections():
        print v.getId(),",",w.getId(),",",v.getWeight(w)

it seems it only iterates over those values in the dictionary of G that has values and not with the empty ones (I mean those that have only a key), is it like that? 似乎只迭代G字典中具有值的值,而不是空值(我的意思是仅具有键的值),是这样吗?

Yes, I think nv= is redundant but self.addVertex(f) is necessary cause you need to add vertex. 是的,我认为nv=是多余的,但是self.addVertex(f)是必需的,因为您需要添加顶点。
Adding self.vertList[t].addNeighbor(self.vertList[f], cost) will make a non-directed graph, true. 添加self.vertList[t].addNeighbor(self.vertList[f], cost)将使一个无向图为true。

it seems it only iterates over those values in the dictionary of G that has values and not with the empty ones (I mean those that have only a key), is it like that? 似乎只迭代G字典中具有值的值,而不是空值(我的意思是仅具有键的值),是这样吗?

No, it will iterate all vertexes but only those with connections(edge) will generate output. 不,它将迭代所有顶点,但是只有那些具有连接(边)的顶点才会生成输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM