问题描述
我正在尝试按照 https://en.wikipedia.org/wiki/Topological_sorting 中的伪代码为我的贝叶斯网络实现拓扑排序
运行我的代码时出现错误:
for child in children:
TypeError: 'Variable' object is not iterable
当我无法遍历该节点的子节点时,我不知道如何更新该节点的传入边数。需要帮助更改代码,以便我可以在不提示错误消息的情况下正确更新节点的入度计数。 Im 实现的函数是类定义底部的 sorted_nodes 函数。
class BayesianNetwork:
"""
Class representing a Bayesian network.
Nodes can be accessed through self.variables['variable_name'].
Each node is a Variable.
Edges are stored in a dictionary. A node's children can be accessed by
self.edges[variable]. Both the key and value in this dictionary is a Variable.
"""
def __init__(self):
self.edges = defaultdict(lambda: []) # All nodes start out with 0 edges
self.variables = {} # Dictionary of "name":TabularDistribution
def add_variable(self,variable):
"""
Adds a variable to the network.
"""
if not isinstance(variable,Variable):
raise TypeError(f"Expected {Variable}; got {type(variable)}.")
self.variables[variable.name] = variable
def add_edge(self,from_variable,to_variable):
"""
Adds an edge from one variable to another in the network. Both variables must have
been added to the network before calling this method.
"""
if from_variable not in self.variables.values():
raise ValueError("Parent variable is not added to list of variables.")
if to_variable not in self.variables.values():
raise ValueError("Child variable is not added to list of variables.")
self.edges[from_variable].append(to_variable)
def sorted_nodes(self):
"""
TODO: Implement Kahn's algorithm (or some equivalent algorithm) for putting
variables in lexicographical topological order.
Returns: List of sorted variable names.
"""
inDegree = {n : 0 for n in self.variables.values()}
L = [] #Empty list that will contain the sorted nodes
S = [] #List of nodes with 0 incoming edges (in-degree = 0)
for edgelist in self.edges.values(): #If edgelist contains a node,that node has +1 incoming edges
for node in edgelist:
inDegree[node] += 1
#Find nodes with 0 in-degree
for node in inDegree:
if (inDegree[node] == 0):
S.append(node)
#Process nodes with in-degree = 0
while S:
node = S.pop()
L.append(node)
#Update in-degree. The children of the node we removed from S now have one less in-degree
for children in self.edges[node]:
for child in children:
inDegree[child] -= 1
if inDegree[child] == 0:
L.append(child)
#Print error if graph has edges (not acyclic),otherwise return L
if any(node for node in inDegree.values()):
print("Error: Graph has cycles")
else:
return L
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)