Graph Matching Algorithm

Table of contents

1. Subgraph isomorphism algorithm realizes graph matching

2. Use points to build undirected graphs


1. Subgraph isomorphism algorithm realizes graph matching

Given two point sets, how to judge whether the two point sets are the same can be matched using the subgraph isomorphism algorithm. The procedure is as follows:

# -*- coding: utf-8 -*-
"""
Created on Thu Jun  1 11:24:23 2023

@author: whd
"""

import networkx as nx

# 创建第一个图
G1 = nx.Graph()
G1.add_edges_from([(1, 2), (1, 3), (2, 3), (3, 4)])

# 创建第二个图
G2 = nx.Graph()
#G2.add_edges_from([(10, 20), (10, 30), (20, 30), (30, 40)])
G2.add_edges_from([(30, 40),(10, 20), (20, 30), (10, 30)])

# 使用子图同构算法进行匹配
GM = nx.algorithms.isomorphism.GraphMatcher(G1, G2)

# 打印匹配结果
if GM.is_isomorphic():
    print("图匹配成功!")
    mapping = GM.mapping
    print("匹配节点映射关系:", mapping)
else:
    print("图匹配失败!")
  • All points are enlarged by the same multiple, and the position of the point is changed to complete the matching. [For example, if you put all the points 10 times or 100 times here, or change the position of any result point, you can complete the match]
  • But as long as any value is not enlarged in the same proportion, for example, the last value becomes (10,31), the matching will fail. This algorithm completes rigid matching and is extremely sensitive to scale changes and affine.

2. Use points to build undirected graphs

"""
Created on Thu Jun  1 11:17:50 2023

@author: whd
"""

import networkx as nx
import matplotlib.pyplot as plt

# 创建一个空的无向图
graph = nx.Graph()

# 添加节点
points = [(0, 5), (2, 10), (3, 6),(4,8)]
for i, point in enumerate(points):
    graph.add_node(i, pos=point)  # 将点坐标作为节点属性保存

# 添加边
# 这里可以根据需要定义边的连接关系,例如根据距离或其他几何条件来决定是否添加边
# 这里以连接所有节点为例,将所有节点两两之间都添加边
for i in range(len(points)):
    for j in range(i + 1, len(points)):
        graph.add_edge(i, j)

# 可视化图
pos = nx.get_node_attributes(graph, 'pos')
nx.draw(graph, pos=pos, with_labels=True, node_size=200)
plt.show()

The effect is as follows:

 

 

Guess you like

Origin blog.csdn.net/u013035197/article/details/130998910