请给出完整VF3 light的python算法 并给出注释 可分步骤给出
时间: 2024-05-15 10:16:12 浏览: 11
以下是完整的 VF3 Light 算法的 Python 代码,其中包含了注释。
```python
class VF3Graph(object):
def __init__(self, nodes, edges):
self.nodes = list(nodes)
self.edges = list(edges)
def has_edge(self, n1, n2):
for e in self.edges:
if (e[0] == n1 and e[1] == n2) or (e[0] == n2 and e[1] == n1):
return True
return False
def out_neigh(self, node):
neigh = []
for e in self.edges:
if e[0] == node:
neigh.append(e[1])
elif e[1] == node:
neigh.append(e[0])
return neigh
def in_neigh(self, node):
return self.out_neigh(node)
def nodes_count(self):
return len(self.nodes)
def edges_count(self):
return len(self.edges)
def has_node(self, node):
return node in self.nodes
class State(object):
def __init__(self, g1, g2, core_1=None, core_2=None, added_1=None, added_2=None):
self.g1 = g1
self.g2 = g2
self.core_1 = {} if core_1 is None else core_1
self.core_2 = {} if core_2 is None else core_2
self.added_1 = [] if added_1 is None else added_1
self.added_2 = [] if added_2 is None else added_2
def __str__(self):
return 'State(core_1={}, core_2={}, added_1={}, added_2={})'.format(self.core_1, self.core_2, self.added_1, self.added_2)
def match_node(self, n1, n2):
for i in range(len(self.added_1)):
if self.added_1[i] == n1 and self.added_2[i] == n2:
return True
return False
def get_unmatched_1(self):
return set(self.g1.nodes) - set(self.core_1.keys()) - set(self.added_1)
def get_unmatched_2(self):
return set(self.g2.nodes) - set(self.core_2.keys()) - set(self.added_2)
def get_out_neigh_1(self, node):
return self.g1.out_neigh(node)
def get_in_neigh_1(self, node):
return self.g1.in_neigh(node)
def get_out_neigh_2(self, node):
return self.g2.out_neigh(node)
def get_in_neigh_2(self, node):
return self.g2.in_neigh(node)
def get_core_len(self):
return len(self.core_1)
def get_next_pair(self):
n1 = None
n2 = None
for n in self.get_unmatched_1():
for m in self.get_unmatched_2():
if self.g1.has_node(n) and self.g2.has_node(m):
if (n1 is None) or (self.g1.out_neigh(n) <= self.g1.out_neigh(n1)):
if (n1 is None) or (len(self.g1.out_neigh(n)) <= len(self.g1.out_neigh(n1))):
if (n2 is None) or (self.g2.out_neigh(m) <= self.g2.out_neigh(n2)):
if (n2 is None) or (len(self.g2.out_neigh(m)) <= len(self.g2.out_neigh(n2))):
n1 = n
n2 = m
if n1 is not None and n2 is not None:
return n1, n2
else:
return None
def match(g1, g2):
q = [State(g1, g2)]
while len(q) > 0:
s = q.pop(0)
# 如果两个图的核大小相同,则返回 True
if s.get_core_len() == g1.nodes_count():
return True
# 获取下一个未匹配的节点对
n1, n2 = s.get_next_pair()
while n1 is not None:
# 如果两个节点的度数相同,则继续匹配
if len(s.get_out_neigh_1(n1)) == len(s.get_out_neigh_2(n2)) and len(s.get_in_neigh_1(n1)) == len(s.get_in_neigh_2(n2)):
# 如果两个节点的出边邻居和入边邻居都相同,则进行匹配
if all(s.match_node(x, y) for x, y in zip(s.get_out_neigh_1(n1), s.get_out_neigh_2(n2))) and all(s.match_node(x, y) for x, y in zip(s.get_in_neigh_1(n1), s.get_in_neigh_2(n2))):
new_s = State(s.g1, s.g2, s.core_1.copy(), s.core_2.copy(), s.added_1[:], s.added_2[:])
new_s.core_1[n1] = n2
new_s.core_2[n2] = n1
new_s.added_1.append(n1)
new_s.added_2.append(n2)
q.insert(0, new_s)
n1, n2 = s.get_next_pair()
# 没有找到匹配,则返回 False
return False
```
算法分为两个类:VF3Graph 和 State。
VF3Graph 类表示一个图,包含节点和边的列表以及一些方法。
State 类表示 VF3 算法的一个状态,包含两个图(g1 和 g2)、两个核(core_1 和 core_2)、两个已添加的节点列表(added_1 和 added_2)以及一些方法。
match 函数是 VF3 算法的主体,接收两个 VF3Graph 实例作为参数并返回一个布尔值,表示两个图是否同构。
在 match 函数中,首先创建一个状态 s,它包含两个图 g1 和 g2。然后,将状态 s 添加到队列 q 中。while 循环用于处理队列中的状态,直到队列为空或找到了匹配为止。
在每次循环中,从队列中弹出状态 s。如果两个图的核大小相同,则返回 True,表示找到了匹配。
然后,通过调用 State 类的 get_next_pair 方法获取下一个未匹配的节点对。如果没有找到未匹配的节点对,则说明无法找到匹配,返回 False。
如果找到了未匹配的节点对(n1 和 n2),则比较它们的度数。如果它们的度数不同,则不进行匹配;否则,继续检查它们的出边邻居和入边邻居是否相同。如果它们的出边邻居和入边邻居都相同,则进行匹配。
如果进行了匹配,则创建一个新状态 new_s,它是状态 s 的副本。然后,将新状态的核和已添加节点列表更新为匹配后的值,并将新状态添加到队列 q 中。这个过程会一直重复,直到队列为空或找到了匹配。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)