繁体   English   中英

如何访问Vertex对象内的邻接表?

[英]How can I access to adjacency lists inside the Vertex object?

我在Python中基于Vertex和图形类构建了一个图形,但是在isCycleUtil()方法中,当我尝试通过for i in self.vert_dict[v]来访问Vertex对象时,出现TypeError:Vertex对象不可迭代。

您可以帮助我修复它并具有功能正确的isCycleUtil方法吗?

class Vertex:
def __init__(self, node):
    self.id = node
    self.adjacent = {}

def __str__(self):
    return str(self.id) + ' adjacent: ' + str([x.id for x in self.adjacent])

def add_neighbor(self, neighbor, weight=0):
    self.adjacent[neighbor] = weight

def get_connections(self):
    return self.adjacent.keys()

def get_id(self):
    return self.id

def get_weight(self, neighbor):
    return self.adjacent[neighbor]

def set_weight(self, neighbor, newvalue):
    self.adjacent[neighbor] = newvalue

和图类:

class Graph:
def __init__(self):
    self.vert_dict = {}
    self.num_vertices = 0

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

def add_vertex(self, node):
    self.num_vertices += 1
    new_vertex = Vertex(node)
    self.vert_dict[node] = new_vertex
    return new_vertex

def get_vertex(self, n):
    if n in self.vert_dict:
        return self.vert_dict[n]
    else:
        return None

def add_edge(self, frm, to, cost = 0):
    if frm not in self.vert_dict:
        self.add_vertex(frm)
    if to not in self.vert_dict:
        self.add_vertex(to)

    self.vert_dict[frm].add_neighbor(self.vert_dict[to], cost)

def get_vertices(self):
    return self.vert_dict.keys()

def isCyclicUtil(self,v,visited,parent):

    #Mark the current node as visited
    if not visited[v]:
        visited[v]= True
        parent[v] = True
        #Recur for all the vertices adjacent to this vertex
        for i in self.vert_dict[v]:
            # If the node is not visited then recurse on it
            if  (not visited[i] and self.isCyclicUtil(i,visited,parent)):
                return True
            elif  (not parent[i]):
                return True
    parent[v] = False
    return False


#Returns true if the graph contains a cycle, else false.
def isCyclic(self):
    # Mark all the vertices as not visited
    visited = {}
    parent = {}
    for i in self.vert_dict:
        visited[i] = False
        parent[i] = False
    # Call the recursive helper function to detect cycle in different
    #DFS trees
    for i in self.vert_dict:
        if(self.isCyclicUtil(i,visited,parent)):
            return True
    return False

为避免此错误,您需要更换:

 for i in self.vert_dict[v]

 for i in self.vert_dict[v].get_connections()

并使用:

visited[v.get_id()] 

代替

visited[v] 

避免KeyError

您的图形是有向的,因此在本主题中描述了正确的算法:

如何检测有向图是否为循环图?

您需要更改isCyclicuntil方法的实现:

def isCyclicUtil(self, v, visited, parent):

    # Mark the current node as visited
    if not visited[v]:
        visited[v] = True
        # Recur for all the vertices adjacent to this vertex
        for i in self.vert_dict[v].get_connections():
            # If the node is not visited then recurse on it
            if visited[i.get_id()]:
                return True
            elif self.isCyclicUtil(i.get_id(), visited, parent):
                return True
    return False

暂无
暂无

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

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