-
Notifications
You must be signed in to change notification settings - Fork 0
/
testOtherRandomWalks.py
189 lines (171 loc) · 4.96 KB
/
testOtherRandomWalks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
##!/usr/bin/env python3
## -*- coding: utf-8 -*-
#"""
#Created on Mon Oct 19 14:27:29 2020
#
#@author: georgia
#"""
import community as community_louvain
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
from collections import defaultdict
import copy
import sys
import random
#def messagePropagation(n, N, k, G):
#
# # initialize the T containing the nodes already visited
# T = []
#
## # initialize the weight dict
## weightDict = defaultdict(dict)
#
# # initialize the probability dictionary
# Pr = defaultdict(dict)
#
# while N < k and G.number_of_nodes() > len(T) :
#
# # find the adjacent nodes to node un
# neiOfn = list(G.neighbors(n))
#
# # Ihat is the list of neighbour nodes that haven't been visited yet
# Ihat = neiOfn
# for i in T:
# Ihat.remove(i)
#
## # weight = 1 for all edges in Ihat
## for i in Ihat:
## weightDict[i] = 1
#
#
# Pr = defaultdict(dict)
# for i in Ihat:
# Pr[i] = 1/G.degree[i]
#
# print(Pr)
#
#
#
# # find the index of the edge at random way considering the probabilities of edges
# newnIndex = (np.random.choice(list(Pr.keys()), p=list(Pr.values())))
#
# print(newnIndex)
#
# # find the exact edge from the above index
# newn = list(Pr)[newnIndex]
#
# # find the node reached by the above edge
# newNode = newn[1]
#
#
## # increase by 1 the weight of the above edge
## Graph[un][newNode] += 1
## Graph[newNode][un] += 1
## weightDict[newUn] += 1
#
# # add the edge to the visited edges
# T.add(newn)
#
# # un is now the new edge
# n = newNode
#
# #increase N by 1
# N += 1
#
#
# return G
#
#
#
#G = nx.karate_club_graph()
#
## depends on the preferred community size and depicts how many random walks will be held
#p = 10
#
#k = 10
#
#n = 9
#
#for i in range(1, p+1):
# N = 0 #counter to check the length of the k-path
#
# Graph = messagePropagation(n, N, k, G)
#
#
#for key in Graph:
# for i in Graph[key]:
# Graph[key][i] = Graph[key][i]/p
###############################################################################################################
#import networkx as nx
#import random
#import matplotlib.pyplot as plt
#import operator
#import numpy as np
#
##select random graph using gnp_random_graph() function of networkx
#Graph = nx.karate_club_graph()
#nx.draw(Graph, with_labels=True, node_color='green') #draw the network graph
#plt.figure(figsize=(15,10))
#plt.show() #to show the graph by plotting it
#
## random_node is the start node selected randomly
##random_node = random.choice([i for i in range(Graph.number_of_nodes())])
#random_node = 29
#dict_counter = {} #initialise the value for all nodes as 0
#for i in range(Graph.number_of_nodes()):
# dict_counter[i] = 0
## increment by traversing through all neighbor nodes
#dict_counter[random_node] = dict_counter[random_node]+1
#
#
##Traversing through the neighbors of start node
#for i in range(10):
# list_for_nodes = list(Graph.neighbors(random_node))
# if len(list_for_nodes)==0:# if random_node having no outgoing edges
# random_node = random.choice([i for i in range(Graph.number_of_nodes())])
# dict_counter[random_node] = dict_counter[random_node]+1
# else:
# random_node = random.choice(list_for_nodes) #choose a node randomly from neighbors
# dict_counter[random_node] = dict_counter[random_node]+1
#
#
## using pagerank() method to provide ranks for the nodes
#rank_node = nx.pagerank(Graph)
#
#
##sorting the values of rank and random walk of respective nodes
#sorted_rank = sorted(rank_node.items(), key=operator.itemgetter(1))
#sorted_random_walk = sorted(dict_counter.items(), key=operator.itemgetter(1))
#
#print(sorted_rank)
#print(sorted_random_walk)
#############################################################################################################
#import networkx as nx
#import numpy as np
#import random
#
#
#G = nx.karate_club_graph()
#
#pr = nx.pagerank_numpy(G, personalization={29: 1})
#
#next_node = np.random.choice(list(pr.keys()), p=list(pr.values()))
#
#print(next_node)
#num_walks=10 #number of walks (iteration times)
#num_steps=2 #length of walks
#
#walks = list()
#i = 10 #the starting node
#for walk in range(num_walks):
# curr_walk = [i]
# curr = i
# for step in range(num_steps):
# neighbors = list(G.neighbors(curr))
# if len(neighbors) > 0:
# curr = random.choice(neighbors)
# curr_walk.append(curr)
# walks.append(curr_walk)
#print(walks)