-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm1.R
More file actions
63 lines (51 loc) · 1.14 KB
/
algorithm1.R
File metadata and controls
63 lines (51 loc) · 1.14 KB
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
#Input:
# G = (V, E) /*followee-follower network*/
# H /*maximum number of hops*/
# e = (u,v) followee-follower (u follows v)
algorithm1 <- function(G, H){
V <- data.frame(G[1])
E <- data.frame(G[2])
size <- length(V[,1])
V[2] <- 0
R <- matrix(nrow = size, ncol = size, data = 0)
colnames(R) <- as.character(V[,1])
rownames(R) <- as.character(V[,1])
V[2] <- 0
for (e in 1:nrow(E)){
R[as.character(E[e,1]),as.character(E[e,2])] <- 1
}
for (len in 2:H){
#for each u in V
for (u in V[[1]])
{
u <- as.character(u)
T <- which(R[u,] == 1)#followees of u
V[,2] <- 0 #nv and Vt for each u
for (t in T)
{
t <- as.character(t)
if(t %in% rownames(R))
{
Vt <- which(R[,t] != 0) #/*reachable from t in len-1 hops*/
for (i in Vt)
{
V[i,2] <- V[i,2] + 1
}
}
}
for (v in V[[1]])
{
v <- as.character(v)
if (R[u,v] == 0)
{
R[u,v] <- (1/len)*(V[v,2]/length(T))
if (is.na(R[u,v]))
{
R[u,v] <- 0
}
}
}
}
}
return(R)
}