英文:
R: Writing Graph Based Functions
问题
我理解你想要的是关于如何完成这个任务的指导。以下是你的R代码的进一步步骤:
Step 4: Add the number of cookies
在第4步中,你需要添加原始节点以及第3步中选择的节点的饼干数量。你可以按以下方式计算:
# 选择节点
selected_nodes <- sample(V(neighbors_degree_1_to_max), nodes_to_search)
# 计算饼干总数
total_cookies <- sum(cookies[cookies$names %in% c(original_node, V(neighbors_degree_1_to_max)$name[selected_nodes]), "cookies"])
Step 5: IF the total in Step 4 is greater than 50 or less than 100 : END. ELSE Restart from Step 1
在第5步中,你需要检查总饼干数量是否在50到100之间。如果是,那么结束;否则,重新从步骤1开始。
if (total_cookies > 50 && total_cookies < 100) {
# 结束
cat("Total cookies:", total_cookies, "Finish")
break
} else {
# 重新开始
cat("Total cookies:", total_cookies, "Restarting from Step 1\n")
# 从这里重新开始,你可以将整个过程封装到一个循环中
}
Step 6: Subtract all selected names from the original graph
在第6步中,你需要从原始图中删除第3步中选择的节点。
# 从图中删除选定的节点
g <- delete.vertices(g, c(original_node, V(neighbors_degree_1_to_max)$name[selected_nodes]))
现在,你可以将整个过程封装到一个循环中,以便多次执行。希望这有助于你完成你的任务。
英文:
I have a graph network for a group of friends that looks like this:
set.seed(123)
library(igraph)
# Define a vector of names
names <- c("John", "Alex", "Jason", "Matt", "Tim", "Luke", "Shawn", "Henry", "Steven", "Scott", "Adam", "Jeff", "Connor", "Peter", "Andrew", "Dave", "Daniel", "Benjamin", "Joseph", "Martin")
# Create an empty graph with 20 nodes
g <- make_empty_graph(20)
# Add random edges between nodes to represent friendships
set.seed(123) # for reproducibility
num_edges <- 40
edge_list <- sample(names, size = num_edges * 2, replace = TRUE)
edge_list <- matrix(edge_list, ncol = 2, byrow = TRUE)
g <- graph_from_edgelist(edge_list, directed = FALSE)
# Set the node names to be the names vector
V(g)$name <- names
# Plot the graph
plot(g, vertex.label.cex = 0.7, vertex.label.color = "black", vertex.label.dist = 2)
Each of these friends has a certain number of cookies :
cookies = data.frame(names = names, cookies = as.integer(rnorm(length(names), 20,10)))
I am trying to write a function with the following steps:
- Step 1: Randomly select a (original) node
- Step 2: Random select a "radius" (degree)
- Step 3: Randomly select a number of nodes to search within the radius of the original node (but original node must be connected to these nodes)
- Step 4: Add the number of cookies of the original node and all cookies from the nodes selected in Step 3
- Step 5: IF the total in Step 4 is greater than 50 or less than 100 : END . ELSE Restart from Step 1
- Step 6: Subtract all selected names from original graph
As an an example:
- Suppose I select Scott
- Radius = 3
- Number of Nodes = 4 (Matt, Jason, John, Adam) - notice how all these names can be traced back to Scott
- Add number of cookies for Scott, Matt, Jason, John , Adam (25 + 16 + 16 + 14 + 16 = 87)
- Since 87 is greater than 50 and less than 100 - finish
- Remove "Scott, Matt, Jason, John , Adam" from the graph
I tried to write the R code as follows:
#STEP 1
original_node = sample(V(g), 1)$name
#STEP 2: Find the degree of the farthest node from the original node to be the max "ceiling"
# Calculate the distance from original_node to all other nodes
distances <- distances(g, original_node)
# Find the node furthest away from original node
max_distance <- max(distances)
furthest_node <- which(distances == max_distance)
# Find the degree of the furthest node
furthest_degree <- max(as.numeric(degree(g, furthest_node)))
radius <- sample(0:furthest_degree, 1)
# STEP 3 :
# Find the neighbors of original node with degrees ranging from 1 to max
neighbors_degree_1_to_max <- induced.subgraph(g, unlist(ego(g, order=2, nodes=original_node)))
# Find the number of neighbors with degrees ranging from 1 to max
num_neighbors_degree_1_to_max <- length( neighbors_degree_1_to_max)
# randomly select number of nodes to search
nodes_to_search <- sample(0:num_neighbors_degree_1_to_max, 1)
# randomly select individual nodes within nodes_to_search
# stuck from here
But I am not sure how to proceed from here, can someone please show me what to do?
Thanks!
References:
答案1
得分: 2
# 为顶点添加属性`cookie`
V(g)$cookie <- cookies$cookies
# 循环直到找到所需的团
repeat {
rand_neighbours <- ego(g, order = 2, nodes = original_node) %>%
unlist() %>%
sample(sample.int(length(.) + 1, 1) - 1)
clique_cookie_sum <- sum(V(g)$cookie[rand_neighbours])
if (clique_cookie_sum > 50 & clique_cookie_sum < 100) {
grp <- subgraph(g, rand_neighbours)
g <- subgraph(g, !V(g) %in% rand_neighbours) # 通过减去`grp`更新`g`
break
}
}
你将看到
> V(grp)
+ 4/4 vertices, named, from 52d6fc7:
[1] Steven Adam Andrew Martin
> V(grp)$cookie
[1] 13 12 20 23
> sum(V(grp)$cookie)
[1] 68
英文:
You can do like this
# add attribute `cookie` to vertices
V(g)$cookie <- cookies$cookies
# loop till you find the desired clique
repeat {
rand_neighbours <- ego(g, order = 2, nodes = original_node) %>%
unlist() %>%
sample(sample.int(length(.) + 1, 1) - 1)
clique_cookie_sum <- sum(V(g)$cookie[rand_neighbours])
if (clique_cookie_sum > 50 & clique_cookie_sum < 100) {
grp <- subgraph(g, rand_neighbours)
g <- subgraph(g, !V(g) %in% rand_neighbours) # update `g` by subtracting `grp`
break
}
}
and you will see
> V(grp)
+ 4/4 vertices, named, from 52d6fc7:
[1] Steven Adam Andrew Martin
> V(grp)$cookie
[1] 13 12 20 23
> sum(V(grp)$cookie)
[1] 68
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论