如何从Python中删除图形边缘

时间:2014-06-17 09:08:47

标签: python graph

我正在阅读ThinkComplexity一书,我是python的新手,所以我有这段代码:

class Graph(dict):
    def __init__(self, vs=[], es=[]):
        """
        :param vs: list of vertices/nodes in the graph
        :param es: list of edges/connection for the nodes
        :return: Object graph
        """

        for v in vs:
            self.add_vertex(v) #also node
        for e in es:
            self.add_edge(e) #arc/edge/line

     def add_vertex(self, v):
         """
         :param v: Add the node/vertex to the graph
         :return: Nothing just add
         """
         self[v] = {}

     def add_edge(self, e):
         """
         :param e: Add arc/edge/line to the graph here is in both directions as it is undirected graph, if there is a arc already replace it
         :return: Nothing just add
         """
         v, w = e
         self[v][w] = e
         self[w][v] = e

     def get_edge(self, v1, v2):
         try:
             if self != None:
                if self[v1][v2] == self[v2][v1]:
                     print 'got it'
                     return True

        except:
             return None


     def remove_edge(self, e, e2):
         try:
            if self != None:
                 del self[e][e2]
                 del self[e2][e]
                 print 'deleted\n', e[0], e[1]
                 return True

         except:
             return None


     def vertices(self): #get the list of nodes
         nodes = []
         for node in self.keys():
             nodes.append(node.label)
         print nodes, '\n'
         return nodes


    def edges(self):
         list_edges = []
         count = 0
         for node in self:
            for edges in self[node]:
                 count += 1
                 print self[node].values(), count
                 list_edges.append(self[node].values())

         return list_edges

    def out_vertices(self, v): #nodes connected to this node
        connected = []
        for node in v.keys():
            connected.append(node)
            print node, 'this node is connected to', v.keys()

        return connected

    def out_edges(self, v):
        list_out_edges = []
        for ed in v.values():
            print ed, 'edges from to'
            list_out_edges.append(ed)

        return list_out_edges



class Vertex(object): #nodes fro the graph
    def __init__(self, label=''):
        self.label = label

    def __repr__(self):
        return 'Vertex/Node(%s)' % repr(self.label)

    __str__= __repr__


class Edge(tuple):
     def __new__(cls, e1, e2):
        return tuple.__new__(cls, (e1, e2))

     def __repr__(self):
         return 'Edge(%s---%s) <-undirected' % (repr(self[0]), repr(self[1]))

     __str__ = __repr__

在第2章中4.编写一个名为remove_edge的方法,该方法采用边缘并从图中删除对它的所有引用。

我已经完成了get_edge()代码,但删除了我不知道该怎么做,任何帮助?这将是一个贝叶斯网络的起点,所以我想学习python和图形。感谢

1 个答案:

答案 0 :(得分:0)

像这样:

def remove_edge(g, e, e2):
   try:
       if g != None:
           del g[e][e2]
           del g[e2][e]
           print 'deleted\n', e[0], e[1]
       return True

   except:
       return None 

字典中的键可以是对象,不仅仅是str。并且需要两种方式来做,因为是无向的