-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hopdistance.py
32 lines (26 loc) · 1.13 KB
/
Hopdistance.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
# This Class finds the hop distance from a specific input node
class Hop_Dist():
def __init__(self,graph):
self.graph=graph
# This recursive function calculates the hop distance, when the number of step is more than 1
def connect(self,lst_node,step):
if step==0:
return lst_node
# Take into account the neighbours of a specific node
connection=[key_author for author in lst_node for key_author in self.graph.neighbors(author)]
lst_node+=connection
return self.connect(connection,step-1)
# This function calculates the hop distance of a specific input author for the main 3 situations
def hop_distance(self,step, node):
node_connection=[node]
# Case 0: when the step is zero
if step==0:
lst_connect=node_connection
# Case 1: when the step is one
elif step==1:
connection=self.graph.neighbors(node)
lst_connect=node_connection+connection
# Case 3: when the step is more than 1
else:
lst_connect=self.connect(node_connection,step)
return(lst_connect)