简体   繁体   English

使用 R / igraph,有没有办法在考虑到唯一节点属性的计数的情况下找到节点之间的最短路径?

[英]Using R / igraph, is there a way to find a shortest path between nodes taking the count of unique node attributes into account?

I'm trying to figure out how to find the shortest path between two nodes on a graph, using both the weights of the edges and an arbitrary penalty based on the count of the unique attributes of the nodes used.我试图找出如何使用边的权重和基于所用节点的唯一属性计数的任意惩罚来找出图上两个节点之间的最短路径。

I'm trying to keep the problem as general as possible.我试图使问题尽可能普遍。

For example, consider the following graph (simplified to demonstrate):例如,考虑下图(简化演示):

在此处输入图片说明

In R, I might construct my igraph object like:在 R 中,我可能会构造我的 igraph 对象,如:

EDIT - fixed typo in the edge weights!编辑 - 修正了边缘权重中的错字!

library(igraph)

nodes = data.frame(id=c("A","B","C","D","E","F","G","H","I"),
                   colour = c("blue","blue","blue","blue","blue",
                              "red","green","yellow", "red"))

edges = data.frame(from = c("A","B","C","D","E","F","G","H","I","I","I"),
                   to = c("B","C","D","E","F","G","H","A","A","E","F"),
                   #weight = c(5,6,7,5,1,5,3,2,8,8,4)) <<- TYPO - SORRY
                   weight = c(5,6,9,5,1,5,3,2,8,8,4))

g = graph_from_data_frame(edges, directed = F, vertices = nodes)

To get from A to E, I can use igraph functions to calculate the shortest path.为了从 A 到 E,我可以使用 igraph 函数来计算最短路径。

shortest_paths(g, from="A",to="E", output="vpath")[[1]] returns the path AHGFE with distances(g, "A","E") total of 11. shortest_paths(g, from="A",to="E", output="vpath")[[1]]返回路径 AHGFE, distances(g, "A","E")总共为 11。

However - what if I wanted to add a penalty based on the count of unique colours of the nodes?但是 - 如果我想根据节点的独特颜色的数量添加惩罚怎么办? ie for every new coloured node it passes through, the weight is +10.即对于它通过的每个新的有色节点,权重为 +10。

AHGFE = edge weight of 11 and uses 4 colours, so total weight is 11+(4*10)=51 AHGFE = 边权重为 11 并使用 4 种颜色,因此总权重为 11+(4*10)=51

ABCDE = edge weight of 25 uses 1 colour, total weight is 25+(1*10)=35 ABCDE = 25 的边权重使用 1 种颜色,总权重为 25+(1*10)=35

AIE = edge weight of 16, uses 2 colours, total weight is 16+(2*10)=36 AIE = 边权重 16,使用 2 种颜色,总权重为 16+(2*10)=36

AIFE = edge weight of 13, uses 2 colours, total weight is 13+(2*10)=33 AIFE = 边缘权重 13,使用 2 种颜色,总权重为 13+(2*10)=33

AIFE is now the "shortest route". AIFE 现在是“最短路线”。

I think (naively) that the weight needs to be a function of the previously visited node history.我认为(天真地)权重需要是先前访问过的节点历史记录的函数。

I'm currently playing with igraph bfs and dfs without much success to try and understand how it's working but I think I'm barking up the wrong tree.我目前正在使用 igraph bfsdfs尝试并了解它是如何工作的,但没有取得太大成功,但我认为我在咆哮错误的树。

Before I try and reinvent the wheel, is there an existing igraph 'out of the box` function I'm missing to solve this?在我尝试重新发明轮子之前,是否有一个现有的 igraph“开箱即用”功能来解决这个问题?

I'm using R 3.6.0 and igraph 1.2.4.1 but can understand Python also.我正在使用 R 3.6.0 和 igraph 1.2.4.1,但也可以理解 Python。

TIA TIA

An interesting question.一个有趣的问题。 I would define a custom distance/weight function that extracts all paths connecting two vertices from your graph;我将定义一个自定义距离/权重函数,该函数从图中提取连接两个顶点的所有路径; for every path we then (1) calculate the sum of edge weights (this is in essence what distances does), (2) determine the number of unique colours and multiply that value by 10, and (3) calculate a score as the sum of edge weights and scaled numbers of unique colour.然后,对于每条路径,我们 (1) 计算边权重的总和(这实质上就是distances作用),(2)确定独特颜色的数量并将该值乘以 10,以及(3)计算分数作为总和边缘权重和独特颜色的缩放数量。 The "optimal" path is then given by the path with the lowest overall score.然后由具有最低总分的路径给出“最佳”路径。

Here goes:开始:

min_wghtd_dist <- function(g, from, to) {
    pth <- all_simple_paths(g, from, to)
    score <- sapply(pth, function(x) {
        edge_ids <- get.edge.ids(g, head(rep(x, each = 2), -1)[-1])
        edge_sum <- sum(E(g)[edge_ids]$weight)
        col_wght <- 10 * length(unique(V(g)[x]$colour))
        edge_sum + col_wght
    })
    list(path = pth[[which.min(score)]], score = min(score))
}

min_wghtd_dist(g, "A", "E")
#$path
#+ 4/9 vertices, named, from 09c0a2a:
#[1] A I F E
#
#$score
#[1] 33

The path A->I->F->E is correctly identified as the optimal path according to your requirements.根据您的要求,路径A->I->F->E被正确识别为最佳路径。


Sample data样本数据

library(igraph)

nodes = data.frame(id=c("A","B","C","D","E","F","G","H","I"),
                   colour = c("blue","blue","blue","blue","blue",
                              "red","green","yellow", "red"))

edges = data.frame(from = c("A","B","C","D","E","F","G","H","I","I","I"),
                   to = c("B","C","D","E","F","G","H","A","A","E","F"),
                   weight = c(5,6,9,5,1,5,3,2,8,8,4))

g = graph_from_data_frame(edges, directed = F, vertices = nodes)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有可能找到一个起始节点和多个目的节点之间的最短路径? 使用 Networkx 或 OSMnx - is it possible to find the shortest path between an origin node and multiple destination nodes? Using Networkx or OSMnx GraphViz,找到两个节点之间的最短路径 - GraphViz, find the shortest path between two nodes 在有向加权图中,有效地找到两个节点 A 和 B 之间穿过节点 X 的最短路径的成本 - In a directed, weighted graph, efficiently find the cost of the shortest path between two nodes A and B that traverses through a node X 我需要找到源节点和目标节点之间的最短路径(距离)。 鉴于必须包含某些节点 - I need to find the shortest path (distance) between a source node and a target node. Given that certain nodes MUST be included Python networkx加权图在最短路径计算中没有考虑节点的权重? - Python networkx weighted graph not taking into account weight of node in shortest path calculation? 使用 igraph python 查找图巨型组件的直径和平均最短路径长度 - Find diameter and average shortest path length of a graph's giant component using igraph python 如何使用 .networkx 查找特定节点集之间的最短路径 - How to find shortest paths between specific set of nodes using networkx OSMnx:有没有办法找到两个坐标之间的准确最短路径? - OSMnx: Is there a way to find an accurate shortest path between 2 coordinates? 在不添加权重属性的情况下查找图中两个节点之间的所有最短路径 - Find all shortest paths between two nodes in a graph without adding weight attributes 是否有一个函数可以找到两个节点之间的最短路径? - Is there a function that finds the shortest path between two nodes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM