123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- # Gmatch4py use networkx graph
- import networkx as nx
- import numpy as np
- import matplotlib.pyplot as plt
- # import the GED using the munkres algorithm
- import gmatch4py as gm
-
- # g1=nx.complete_bipartite_graph(2,3)
- # g2=nx.complete_bipartite_graph(2,4)
- # g3=nx.complete_bipartite_graph(2,5)
- # ged=gm.GraphEditDistance(1,1,1,1) # all edit costs are equal to 1
- # result=ged.compare([g1,g2, g3],None)
- # print(result[0][1])
- # ged=gm.GreedyEditDistance(1,1,1,1)
- # result=ged.compare([g1,g2],None)
- # print(result)
- # ged=gm.HED(1,1,1,1)
- # result=ged.compare([g1,g2],None)
- # print(result)
- # ged=gm.BP_2(1,1,1,1)
- # result=ged.compare([g1,g2],None)
- # print(result)
- from temp.args import GraphVAE_Args
-
-
- def graph_show(G, title):
- pos = nx.spring_layout(G, scale=2)
- nx.draw(G, pos, with_labels=True)
- fig = plt.gcf()
- fig.canvas.set_window_title(title)
- plt.show()
- plt.savefig('foo.png')
-
-
- def bfs_seq(G, start_id):
- '''
- get a bfs node sequence
- :param G:
- :param start_id:
- :return:
- '''
- dictionary = dict(nx.bfs_successors(G, start_id))
- start = [start_id]
- output = [start_id]
- while len(start) > 0:
- next = []
- while len(start) > 0:
- current = start.pop(0)
- neighbor = dictionary.get(current)
- if neighbor is not None:
- #### a wrong example, should not permute here!
- # shuffle(neighbor)
- next = next + neighbor
- output = output + next
- start = next
- return output
-
-
- my_graph = nx.Graph()
-
- # Add edges to to the graph object
- # Each tuple represents an edge between two nodes
- my_graph.add_edges_from([
- (0, 1),
- (0, 2),
- (0, 7),
- (1, 2),
- (6, 3),
- (4, 1),
- (7, 2),
- (5, 3)
- ])
- # graphs = list(nx.connected_component_subgraphs(my_graph))
- # grid = nx.grid_2d_graph(2, 3)
- # adj = nx.to_numpy_matrix(grid)
- # args = GraphVAE_Args()
- # if args.permutation_mode:
- # x_idx = np.random.permutation(adj.shape[0])
- # adj = adj[np.ix_(x_idx, x_idx)]
- # adj = np.asmatrix(adj)
- # random_idx_for_delete = np.random.randint(adj.shape[0])
- # deleted_node = adj[:, random_idx_for_delete].copy()
- # for i in range(deleted_node.__len__()):
- # if i >= random_idx_for_delete and i < deleted_node.__len__() - 1:
- # deleted_node[i] = deleted_node[i + 1]
- # elif i == deleted_node.__len__() - 1:
- # deleted_node[i] = 0
- # adj[:, random_idx_for_delete:adj.shape[0] - 1] = adj[:, random_idx_for_delete + 1:adj.shape[0]]
- # adj[random_idx_for_delete:adj.shape[0] - 1, :] = adj[random_idx_for_delete + 1:adj.shape[0], :]
- # adj = np.delete(adj, -1, axis=1)
- # adj = np.delete(adj, -1, axis=0)
- # G = nx.from_numpy_matrix(adj)
- # # then do bfs in the permuted G
- # print(adj)
- # degree_arr = np.sum(adj, axis=0)
- # print(degree_arr)
- # print(np.argmax(degree_arr))
- # start_idx = np.random.randint(adj.shape[0])
- # x_idx = np.array(bfs_seq(G, start_idx))
- # adj = adj[np.ix_(x_idx, x_idx)]
- # x_idx = np.insert(x_idx, x_idx.size, x_idx.size)
- # deleted_node = deleted_node[np.ix_(x_idx)]
- # graph_show(nx.from_numpy_matrix(adj), "2")
- # adj = np.append(adj, deleted_node[:-1], axis=1)
- # deleted_node = deleted_node.reshape(1, -1)
- # adj = np.vstack([adj, deleted_node])
- # graph_show(nx.from_numpy_matrix(adj), "3")
-
- bfs_flag = True
- most_connected_node_flag = True
- bfs_mode_with_arbitrary_node_deleted = True
- distinct_matrix_list = []
- for i in range(5000):
- grid = nx.grid_2d_graph(3, 2)
- adj = nx.to_numpy_matrix(grid)
- x_idx = np.random.permutation(adj.shape[0])
- adj = adj[np.ix_(x_idx, x_idx)]
- adj = np.asmatrix(adj)
- if bfs_flag:
- if bfs_mode_with_arbitrary_node_deleted:
- random_idx_for_delete = np.random.randint(adj.shape[0])
- deleted_node = adj[:, random_idx_for_delete].copy()
- for i in range(deleted_node.__len__()):
- if i >= random_idx_for_delete and i < deleted_node.__len__() - 1:
- deleted_node[i] = deleted_node[i + 1]
- elif i == deleted_node.__len__() - 1:
- deleted_node[i] = 0
- adj[:, random_idx_for_delete:adj.shape[0] - 1] = adj[:, random_idx_for_delete + 1:adj.shape[0]]
- adj[random_idx_for_delete:adj.shape[0] - 1, :] = adj[random_idx_for_delete + 1:adj.shape[0], :]
- adj = np.delete(adj, -1, axis=1)
- adj = np.delete(adj, -1, axis=0)
- G = nx.from_numpy_matrix(adj)
- # then do bfs in the permuted G
- degree_arr = np.sum(adj, axis=0)
- start_idx = np.argmax(degree_arr)
- # start_idx = np.random.randint(adj.shape[0])
- x_idx = np.array(bfs_seq(G, start_idx))
- adj = adj[np.ix_(x_idx, x_idx)]
- # x_idx = np.insert(x_idx, x_idx.size, x_idx.size)
- # deleted_node = deleted_node[np.ix_(x_idx)]
- # adj = np.append(adj, deleted_node[:-1], axis=1)
- # deleted_node = deleted_node.reshape(1, -1)
- # adj = np.vstack([adj, deleted_node])
- else:
- G = nx.from_numpy_matrix(adj)
- start_idx = np.random.randint(adj.shape[0])
- if most_connected_node_flag:
- degree_arr = np.sum(adj, axis=0)
- start_idx = np.argmax(degree_arr)
- x_idx = np.array(bfs_seq(G, start_idx))
- adj = adj[np.ix_(x_idx, x_idx)]
- add_to_list_flag = True
- for j in range(len(distinct_matrix_list)):
- if np.array_equal(adj, distinct_matrix_list[j]):
- add_to_list_flag = False
- if add_to_list_flag:
- distinct_matrix_list.append(adj)
- print(len(distinct_matrix_list))
|