对象不可调用?

Object is not callable?

大家好,我不知道如何解决这个问题,据我所知,这是因为我正在使用的对象的属性。

为了您的信息,我在 Python 中制作了一个图,该图需要检查是否所有的顶点和边都已连接。

class graph(object):

    def __init__(self, gdict=None):
        if gdict == None:
            gdict = {}
        self.gdict = gdict


    # return the keys of the dictionary list, or vertices
    def getVertice(self):
        return list(self.gdict.keys())

    # this allows us to obtain the edges for the graph
    # or "values" of the dict key
    def getEdges(self):
        return self.generate_edges()

    def addVertex(self, vertex):
        if vertex not in self.gdict:
            self.gdict[vertex] = []

    def addEdge(self, edge):
        edge = set(edge)
        (vertex1, vertex2) = tuple(edge)
        if vertex1 in self.gdict:
            self.gdict[vertex1].append(vertex2)
        else:
            self.gdict[vertex1] = [vertex2]

    # this generates a list of all edges for the vertices
    def generate_edges(self):
        edges = []
        for vertex in self.gdict:
            for neighbour in self.gdict[vertex]:
                if (neighbour, vertex) not in edges:
                    edges.append((vertex, neighbour))
        return edges

    def find_path(self, startVertex, endVertex, paths=None):
        if paths == None:
            paths = []
        graphs = self.gdict
        paths = paths + [startVertex]
        if startVertex == endVertex:
            return paths
        if startVertex not in graphs:
            return None
        for vertex in graphs[startVertex]:
            if vertex not in paths:
                extendedPath = self.find_path(vertex, endVertex, paths)

                if extendedPath:
                    return extendedPath
        return None

    def findAllPath(self, startVertex, endVertex, paths=None):
        if paths is None:
            paths = []
        graphs = self.gdict
        paths = paths + [startVertex]
        if startVertex == endVertex:
            return [paths]
        if startVertex not in graphs:
            return []
        paths = []
        for vertex in graphs[startVertex]:
            if vertex not in paths:
                extendedPath = self.find_path(vertex, endVertex, paths)
                for p in extendedPath:
                    paths.append(p)
            return paths

    def findisovertices(self):  ##reword this
        """ returns a list of isolated vertices. """
        graphs = self.gdict
        iso = []
        for vertex in graphs:
            print(iso, vertex)
            if not graphs[vertex]:
                iso += [vertex]
        return iso

    def isConnected(self, verticesMet=None, startVertex=None):
        if verticesMet is None:
            verticesMet = set()
        gdict = self.gdict
        vertices = self.gdict()
        if not startVertex:
            startVertex = vertices[0]
        verticesMet.add(startVertex)
        if len(verticesMet) != len(vertices):
            for vertex in gdict[startVertex]:
                if vertex not in verticesMet:
                    if self.isConnected(verticesMet, vertex):
                        return True
        else:
            return True
        return False
    # this function prints the nodes/vertices in the graph
    def completeGraph(self):
        Vertex = len(self.gdict.keys())
        Edges = len(self.gdict.values())
        answer = 2.0 * Edges / (Vertex * (Vertex - 1))
        return answer

graph_elements = ({"a": ["d", "f"],
                   "b": ["c"],
                   "c": ["b", "c", "d", "e"],
                   "d": ["a", "c"],
                   "e": ["c"],
                   "f": ["a"],
                   "z": []
                   })

g = graph(graph_elements)

print("Our vertices are: \n", g.getVertice())
print("#1 | Generate list of all edges: \n", graph.generate_edges(g))

##2 Function to calculate isolated nodes of graph.
isolated = graph.findisovertices(g)
print("#2 | Find isolated nodes:\n", isolated)

# 3.    Function to find a path from a start vertex to an end vertex
path = graph.find_path(g, "a", "c")
print("#3 | Find a path function: \n", path)

# 4.    Function to find all the paths between a start vertex to an end vertex
allPaths = graph.findAllPath(g, "a", "e")
print("#4 | All paths function:\n", allPaths)

# 5. Function to check if graph is connected
connect = graph(g)


print("#5 | Connected graph function \n", connect.isConnected(g))

我不断收到以下错误:

Traceback (most recent call last):
  File "graphsAssign6.py", line 160, in <module>
    print("#5 | Connected graph function \n", connect.isConnected(g))
  File "graphsAssign6.py", line 95, in isConnected
    vertices = self.gdict()
TypeError: 'graph' object is not callable
def isConnected(self, verticesMet=None, startVertex=None):
    if verticesMet is None:
        verticesMet = set()
    gdict = self.gdict
    vertices = self.getVertice()
    if not startVertex:
        startVertex = vertices[0]
    verticesMet.add(startVertex)
    if len(verticesMet) != len(vertices):
        for vertex in gdict[startVertex]:
            if vertex not in verticesMet:
                if self.isConnected(verticesMet, vertex):
                    return True
    else:
        return True
    return False



# 5. Function to check if graph is connected
print("#5 | Connected graph function \n", g.isConnected())

不要创建新的连接 = 图形 (g)。你的 isConnected 应该在 g 内工作。此外,您不应该使用 self.gdict() 获取顶点。这没有意义,您已经有一个名为 getVertice 的函数用于该作业。