R: 编写基于图的函数

huangapple go评论51阅读模式
英文:

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 &lt;- c(&quot;John&quot;, &quot;Alex&quot;, &quot;Jason&quot;, &quot;Matt&quot;, &quot;Tim&quot;, &quot;Luke&quot;, &quot;Shawn&quot;, &quot;Henry&quot;, &quot;Steven&quot;, &quot;Scott&quot;, &quot;Adam&quot;, &quot;Jeff&quot;, &quot;Connor&quot;, &quot;Peter&quot;, &quot;Andrew&quot;, &quot;Dave&quot;, &quot;Daniel&quot;, &quot;Benjamin&quot;, &quot;Joseph&quot;, &quot;Martin&quot;)

# Create an empty graph with 20 nodes
g &lt;- make_empty_graph(20)

# Add random edges between nodes to represent friendships
set.seed(123)  # for reproducibility
num_edges &lt;- 40
edge_list &lt;- sample(names, size = num_edges * 2, replace = TRUE)
edge_list &lt;- matrix(edge_list, ncol = 2, byrow = TRUE)
g &lt;- graph_from_edgelist(edge_list, directed = FALSE)

# Set the node names to be the names vector
V(g)$name &lt;- names

# Plot the graph
plot(g, vertex.label.cex = 0.7, vertex.label.color = &quot;black&quot;, vertex.label.dist = 2)

R: 编写基于图的函数

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 &quot;ceiling&quot; 

# Calculate the distance from original_node to all other nodes
distances &lt;- distances(g, original_node)

# Find the node furthest away from original node
max_distance &lt;- max(distances)
furthest_node &lt;- which(distances == max_distance)

# Find the degree of the furthest node
furthest_degree &lt;- max(as.numeric(degree(g, furthest_node)))
radius &lt;- 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 &lt;- 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 &lt;- length( neighbors_degree_1_to_max)

# randomly select number of nodes to search
nodes_to_search &lt;- 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 &lt;- cookies$cookies

# loop till you find the desired clique
repeat {
  rand_neighbours &lt;- ego(g, order = 2, nodes = original_node) %&gt;%
    unlist() %&gt;%
    sample(sample.int(length(.) + 1, 1) - 1)

  clique_cookie_sum &lt;- sum(V(g)$cookie[rand_neighbours])

  if (clique_cookie_sum &gt; 50 &amp; clique_cookie_sum &lt; 100) {
    grp &lt;- subgraph(g, rand_neighbours)
    g &lt;- subgraph(g, !V(g) %in% rand_neighbours) # update `g` by subtracting `grp` 
    break
  }
}

and you will see

&gt; V(grp)
+ 4/4 vertices, named, from 52d6fc7:
[1] Steven Adam   Andrew Martin
&gt; V(grp)$cookie
[1] 13 12 20 23
&gt; sum(V(grp)$cookie)
[1] 68

huangapple
  • 本文由 发表于 2023年3月1日 11:54:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定