子图同构算法Ullmann早在1976年就提出来了,大家有兴趣可以自己去搜索下原文看看。这里我就简要的阐述一下。
给定两个图Q 和 G, 它们相应的矩阵分别是和
.我们的目标就是找到矩阵
算法步骤:
Step1. Setup matrix Mn×m , such that M[i][j]=1, if 1) the i-th vertex in Q hasthe same label as the j-th vertex in G; and 2) the i-th vertex has smallervertex degree than the j-th vertex in G.
Step2.MatrixesM are generated by systematically changing to 0 all but one of the 1’s in eachof the rows of M, subject to the definitory condition that no column of amatrix M may contain more than one 1.
Step3.Verify matrix M’ by the following equation
为了提高算法的效率,采用了如下的Refinement。
Refinement:
Letthe i-th vertex v in Q corresponds to the j-th vertex u in G.Each neighbor vertex of v in Q must correspond to some neighbor vertexof u in G. Otherwise, vcannot correspond to u.
1. Considering the matrix M, for each 1 in M, we refine it by the followingequation. If fails, change 1 to 0 in M.
2. If there exists at least one row (in M) having no 1, we report no subgraphisomorphism from Q to G.
接下来就直接粘贴代码了。
package ucas.iie.graph.action;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import ucas.iie.graph.bean.EdgeBean;
import ucas.iie.graph.bean.GraphBean;
import ucas.iie.graph.bean.VertexBean;
public class IsomorphismImpl {
private ArrayList<GraphBean> query_g;// 查询的子图
private ArrayList<GraphBean> mydb_g;// 图的总数据
public IsomorphismImpl() {
query_g = new ArrayList<GraphBean>();
mydb_g = new ArrayList<GraphBean>();
}
/**
*
* @param query
*