def random_walk_multi_layers(multi_layers_net, walk_iters=100, walk_legth=160, save_random_walk=False): ''' :param multi_layers_net: list,表示一个多层的大网络,每一层都是一个小网络,并且每一层的节点都相同 :param output_file: str,表示输出文件的路径,文件内容为节点及其对应的向量 :param walk_iters: int,表示单个节点游走的次数 :param walk_legth: int,表示一个节点在网络中游走的步数 :param save_random_walk: boolean,表示是否保存随机游走的路径 :return: ''' multi_layers = defaultdict() for i in range(len(multi_layers_net)): G = read_graph(multi_layers_net[i], weighted=True) print("{} layer -> {} nodes and {} edges.".format( i, len(nx.nodes(G)), len(nx.edges(G)))) multi_layers[i] = G nodes = multi_layers[0].nodes() walks = [] for index, node in enumerate(nodes): time1 = time.clock() max_weights = list() for key, G in multi_layers.items(): nei_weight = set() for nei_node in G.neighbors(node): nei_weight.add(float(G[node][nei_node]['weight'])) max_weights.append(max(nei_weight)) select_layer = max_weights.index(max(max_weights)) for walk_iter in range(walk_iters): walk = random_walk(multi_layers[select_layer], node, walk_legth) walks.append(walk) time2 = time.clock() print("{} * {} -> {} layer: cost {}s".format(index, node, select_layer + 1, time2 - time1)) if save_random_walk: FileUtil.write2DemList2File(walks, "./random_walk.txt") return walks
def random_walk(edges, num_walks=100, walk_length=160): time1 = time.clock() nx_G = read_graph(edges, weighted=True, directed=False) print("{} nodes, {} edges.".format(len(nx.nodes(nx_G)), len(nx.edges(nx_G)))) time2 = time.clock() print("It cost {}s to read edges.".format(time2 - time1)) G = Graph(nx_G, p=1, q=1) print("generate transition matrix......") G.preprocess_transition_probs() time3 = time.clock() print("It cost {}s to generate transition matrix.".format(time3 - time2)) print("begin to random walk......") walks = G.simulate_walks(num_walks=num_walks, walk_length=walk_length) time4 = time.clock() print("It cost {}s to random walk.".format(time4 - time3)) FileUtil.write2DemList2File(walks, "./node2vec_walks.txt") return walks