Skip to main content
 首页 » 编程设计

python之找到 Pandas 数据框中两点之间平均边值最高的路径的有效方法

2024年10月01日7yyy_WW

请原谅这个问题看似令人困惑的措辞。这是我想要做的。

给定一个数据框df

Fruit1     Fruit2      Weight 
orange     apple       0.2 
orange     grape       0.4 
orange     pineapple   0.6 
orange     banana      0.8 
apple      grape       0.9 
apple      pineapple   0.3 
apple      banana      0.2 
grape      pineapple   0.1 
pineapple  banana      0.8 

以及对最大允许路径长度的约束,L

我希望返回具有最高平均路径的数据帧(即点/路径长度之间所有边的总和为最大值),其中边由 weight 列表示,假设不超过长度L。

举例说明最高平均路径的含义:

假设我们只有 4 个点 A、B、C 和 D。我们有兴趣找到 A 和 D 之间的最高平均路径。

最高平均路径为 max( (A->D)/1, (A->B + B->D)/2, (A->C + C->D)/2, (A ->B + B->C + C-> D)/3, (A->C + C->B + B-> D)/3 ) 在L=3的情况下

对于 L=2,它将是 max( (A->D)/1, (A->B + B->D)/2, (A->C + C->D)/2 )

在 df 的情况下,对于 L=2,我们会得到类似的结果

Fruit1     Fruit2      Weight   MaxAvgPath(L=2) 
orange     apple       0.2       [orange, grape, apple]   
orange     grape       0.4       [orange, apple, grape] 
orange     pineapple   0.6       [orange, banana, pineapple] 
orange     banana      0.8       [orange, banana] 
apple      grape       0.9       [apple, grape] 
apple      pineapple   0.3       [apple, grape, pineapple] 
apple      banana      0.2       [apple, pineapple, banana]  
grape      pineapple   0.1       [grape, orange, pineapple] 
grape      banana      0.1       [grape, apple, banana] 
pineapple  banana      0.8       [pineapple, banana] 

注意:此边集包含的行刚好足以代表整个网络。例如,虽然 (orange, apple) 存在,但 (apple, orange) 不存在,因为 (apple, orange) 的权重相同。但是,如果包括那些有助于创建解决方案,请随意使用它们并将它们留在最终输出中。镜像对的返回路径应与原始对的返回路径相同。

请您参考如下方法:

感谢您的澄清,因此您要求图形中每对节点之间成本/(边数)最高的路径,其中路径限制为连接边的上限。最长路径问题是 np-hard,因此只有在有限制的情况下才有可能获得有效的解决方案 (参见 https://en.wikipedia.org/wiki/Longest_path_problem)。我认为你的边连接限制人为地强制执行长度为 L 的最长路径,因此可以将其降低为 L 的指数。

networkx 模块可以通过深度优先搜索来寻找我们的简单路径,我们可以通过手动求和权重和排序来对路径进行排名。我们可以为每对节点执行此操作,并将其作为新系列添加到数据框中。

import pandas 
import networkx 
 
# Create a graph from the dataframe 
G = networkx.from_pandas_dataframe(path_frame, 'Fruit1', 'Fruit2', 'Weight') 
 
# Find the longest path between source and target up to length L 
def maxpath_cond(G, source, target, edge_attr, L=None): 
    #Use networkx simple paths function which uses a depth first search 
    paths = networkx.simple_paths.all_simple_paths(G,source, target, L) 
    # Calculate and sort the costs of the path 
    costs = [(pathcost(G, pth, edge_attr), pth) for pth in paths] 
    return sorted(costs, key=lambda x:x[0], reverse=True) 
 
def pathcost(G,path, edge_attr): 
    lp = len(path)-1 
    return sum(G[path[n]][path[n+1]][edge_attr] for n in range(lp))/lp 
#Iterate through the dataframe and create a new series made up of long paths 
mxs = [] 
for n in range(len(path_frame)): 
    src, targ = path_frame.loc[n]['Fruit1'], path_frame.loc[n]['Fruit2'] 
    mxl = maxpath_cond(G, src, targ, 'Weight', 2)[0] 
    mxs.append( mxl[1]) 
 
print(path_frame.assign(MaxAvgPath=mxs)) 

哪些输出:

      Fruit1     Fruit2  Weight                   MaxAvgPath 
0     orange      apple     0.2       [orange, grape, apple] 
1     orange      grape     0.4       [orange, apple, grape] 
2     orange  pineapple     0.6  [orange, banana, pineapple] 
3     orange     banana     0.8             [orange, banana] 
4      apple      grape     0.9               [apple, grape] 
5      apple  pineapple     0.3    [apple, grape, pineapple] 
6      apple     banana     0.2   [apple, pineapple, banana] 
7      grape  pineapple     0.1    [grape, apple, pineapple] 
8  pineapple     banana     0.8          [pineapple, banana]