print "recheck with bigQ: " , bigQ; while len(bigQ)!=0: p = bigQ.pop() print "p.id=",p.id,"p.x,p.y",p.x, p.y if len(shouldHaveAll.difference(p.covered))!=0: # not contain all required nodes print "discards p id=",p.id,"since don't covered all should cover." pass; else: #generate several Q' for e2y in p.g.out_edges(): if node_match(p.g.node[e2y[1]],p.g.node[p.y]): # edges every x->y r = Pattern(); r.g = p.g.copy(); r.x = p.x; r.y = e2y[1]; print r.x,"---",r.y print "edgesize,b4",len(r.g.edges()),r.g.degree(r.y) r.g.remove_edge(e2y[0],e2y[1]); print "edgesize,aft",len(r.g.edges()),r.g.degree(r.y) if r.g.degree(r.y)==0: print "remove y", r.y r.g.remove_node(r.y); # print float(suppThreshold)/confThreshold; if computeSupport(baseG,r) <= float(suppThreshold)/confThreshold: # WARN: r will be 0; sigma.append(r);
def union2patterns(sp,op): # first sp then op # input two graph patterns and generate a list of new patterns dLimit = min(sp.diameter(),op.diameter()); # print "dlimit = ", dLimit; newPatternList = list(); # last node L = "ln"; # diameter D = "di"; # mappings M = "m" mapList = list(); tMapList = list(); # init d = dict(); d[D] = 0; d[M] = dict(); d[M][sp.x] = op.x; d[L] = sp.x; tMapList.append(d); # pop out from templist and find new mappings, start from last node while len(tMapList)!=0: # first add it to final maplist; cmap = tMapList.pop(); # print cmap; mapList.append(cmap); if cmap[D]>= dLimit: print "terminate due to the diameter limit." continue; for snb in sp.g.neighbors(cmap[L]): snb_label = sp.g.node[snb]["l"]; # print "snbLabel in snb", snb_label; # print op.g.nodes(data=True); for onb in op.g.neighbors(cmap[M][cmap[L]]): onb_label = op.g.node[onb]["l"] if onb_label == snb_label: # print snb ,"->", onb; # print "potential cmap[M]:",cmap[M]; # find a new map; if snb not in cmap and onb not in cmap[M].values(): # print "confirmed as new."; mapd = dict(); mapd[M] = cmap[M].copy(); # depth +=1 mapd[D]=cmap[D]+1; mapd[M][snb] = onb; mapd[L] = snb; # print mapd; tMapList.append(mapd); # print "merged result:",len(mapList) for mapping in mapList: # print "last=",mapping[L],"d=",mapping[D],mapping[M]; # print "mappingD = ",mapping[M] # generate new patterns np = Pattern(); np.x = sp.x; np.y = sp.y; np.g = nx.union(sp.g,op.g,"SO"); # print "after norename new graph nodes = ", np.g.nodes(data=True) # print "after norename new graph edges = ", np.g.edges() # print "mapping" ,mapping[M]; # mapping for k in mapping[M]: # virtual node, to be merged and removed. vnode = "O"+mapping[M][k]; target = "S"+k; # print "vnode=",vnode,"target=",target; for eo in np.g.out_edges(vnode): if np.g.has_edge(target, eo[1]) == False: np.g.add_edge(target,eo[1]); np.g.remove_edge(eo[0],eo[1]); for ei in np.g.in_edges(vnode): if np.g.has_edge(ei[0], target) == False: np.g.add_edge(ei[0],target); np.g.remove_edge(ei[0],ei[1]); np.g.remove_node(vnode); # print "after norename new graph edges -2 = ", np.g.edges() # rename rename = dict(); index = 1; for node in np.g.nodes(): rename[node] = str(index); index = index +1; np.x = rename["S"+np.x]; np.y = rename["S"+np.y]; np.g = nx.relabel_nodes(np.g,rename, copy=False) # add merged originals np.covered = sp.covered.union(op.covered); # WARN:np is always be supergraph of sp and op, # but networkx will not allways get subgraph_isomorphism # since networkx only support node-induced graph isomorphism # GM1 = isomorphism.DiGraphMatcher(np.g,sp.g,node_match); # print "subgraph_isomorphism with source graph=",GM1.subgraph_is_isomorphic(); # GM2 = isomorphism.DiGraphMatcher(np.g,op.g,node_match); # print "subgraph_isomorphism with others graph=",GM2.subgraph_is_isomorphic(); # if(GM1.subgraph_is_isomorphic()==False): # print "s nodes = ", sp.g.nodes(data=True) # print "s graph = ",sp.g.edges(); # print "=====================" # print "new graph nodes = ", np.g.nodes(data=True) # print "new graph = ",np.g.edges(); # if(GM2.subgraph_is_isomorphic()==False): # print "o nodes = ", op.g.nodes(data=True) # print "o graph = ",op.g.edges(); # print "x=",op.x, "y=",op.y; # print "=====================" # print "new graph nodes = ", np.g.nodes(data=True) # print "new graph = ",np.g.edges(); # print "x=",np.x, "y=",np.y; newPatternList.append(np); return newPatternList;
def union2patterns(sp,op): # first sp then op # input two graph patterns and generate a new pattern dLimit = min(sp.diameter(),op.diameter()); # print "dlimit = ", dLimit; # newPatternList = list(); # last node L = "ln"; # diameter D = "di"; # mappings M = "m" mapList = list(); tMapList = list(); # init d = dict(); d[D] = 0; d[M] = dict(); d[M][sp.x] = op.x; d[L] = sp.x; tMapList.append(d); # pop out from templist and find new mappings, start from last node while len(tMapList)!=0: # first add it to final maplist; cmap = tMapList.pop(); # print cmap; mapList.append(cmap); if cmap[D]>= dLimit: print "terminate due to the diameter limit." continue; for snb in sp.g.neighbors(cmap[L]): snb_label = sp.g.node[snb]["l"]; # print "snbLabel in snb", snb_label; # print op.g.nodes(data=True); for onb in op.g.neighbors(cmap[M][cmap[L]]): onb_label = op.g.node[onb]["l"] if onb_label == snb_label: # print snb ,"->", onb; # print "potential cmap[M]:",cmap[M]; # find a new map; if snb not in cmap and onb not in cmap[M].values(): # print "confirmed as new."; mapd = dict(); mapd[M] = cmap[M].copy(); # depth +=1 mapd[D]=cmap[D]+1; mapd[M][snb] = onb; mapd[L] = snb; # print mapd; tMapList.append(mapd); # pop the last one mapping = mapList.pop(); if len(mapping[M].keys())==2: # only x matches x return None; # for mapping in mapList: # print "last=",mapping[L],"d=",mapping[D],mapping[M]; print "mappingD = ",mapping[M] # generate new patterns np = Pattern(); np.x = sp.x; np.y = sp.y; np.g = nx.union(sp.g,op.g,"SO"); # print "after norename new graph nodes = ", np.g.nodes(data=True) # print "after norename new graph edges = ", np.g.edges() # print "mapping" ,mapping[M]; # mapping for k in mapping[M]: # virtual node, to be merged and removed. vnode = "O"+mapping[M][k]; target = "S"+k; # print "vnode=",vnode,"target=",target; for eo in np.g.out_edges(vnode): if np.g.has_edge(target, eo[1]) == False: np.g.add_edge(target,eo[1]); np.g.remove_edge(eo[0],eo[1]); for ei in np.g.in_edges(vnode): if np.g.has_edge(ei[0], target) == False: np.g.add_edge(ei[0],target); np.g.remove_edge(ei[0],ei[1]); np.g.remove_node(vnode); # if mapping[M][k] in op.y: # tempSY.add(k); # tempOY.remove(mapping[M][k]); # print "after norename new graph edges -2 = ", np.g.edges() # rename rename = dict(); index = 1; for node in np.g.nodes(): rename[node] = str(index); index = index +1; np.x = rename["S"+np.x]; np.y = rename["S"+np.y]; np.g = nx.relabel_nodes(np.g,rename, copy=False) # add merged originals np.covered = sp.covered.union(op.covered); return np;