从图表中的节点计算所有可能的路径

如何解决从图表中的节点计算所有可能的路径

我需要一个建议,如何使用程序(以编程方式)计算从开始节点到结束节点的所有可能路径,我可以使用C#,Python或Matlab,但是我不知道哪个更容易,更快速地进行编码和更少的工作量,我也不知道从哪里开始,我还需要自动绘制节点之间的边缘,然后再计算更多公式。 我有以下数据

Node Date         Data
A    2020-01-01   2.09
B    2020-01-05   0.89
C    2020-01-08   3.17
D    2020-01-08   1.15
E    2020-01-15   3.65

我要执行以下操作:

  1. 创建从任何节点到另一个节点的路径(仅一种方式,从较低日期到较高日期),并且必须穿过路径中日期之间的所有节点,例如:

    1.1从(A)到(E) 应该提取以下路径:

    • A,B,C,E
    • A,B,D,E

    由于C和D的日期相同,所以我们有2条不同的路径。

  2. 列表项

解决方法

使用Python {@ 3}}的Python groupby和产品

代码

#include<stdio.h>

int a;

int count2() {
    int b ;
    printf ("\n value of b2 variable for second time  is : %d ",b);   
  }

int count() {
    int b = 2;
    b++;
       
    printf ("\n value of b variable is : %d ",b);
    b = 99;
  }

int main() {
    printf ("value of a variable is : %d ",a);
    count();
    count2();
}

用法

示例1:字符串数据

from itertools import groupby,product

def all_paths(s):
    # Convert string to list of node triplets
    nodes = [n.strip().split() for n in s.split('\n')]
    
    # Skip header and sort by time 
    # (since time is padded,no need to convert to datetime object to sort)
    nodes = sorted(nodes[1:],key=lambda n: n[1])  # sorting by time which is second item in list (n[1])
    
    # Group nodes by time (i.e. list of list,with nodes with same time in same sublist)
    # Just keeping the node field of each item in sublist (i.e. node[0] is Node field)
    labels = [[node[0] for node in v] for k,v in groupby(nodes,lambda n:n[1])]
    
    # Path is the product of the labels (list of lists)
    return list(product(*labels))

示例2:文件data.txt中的数据

data = '''Node Date Data
A 2020-01-01 2.09
B 2020-01-05 0.89
C 2020-01-08 3.17
D 2020-01-08 1.15
E 2020-01-15 3.65'''

print(all_paths(data))

输出(两种情况):

with open('data.txt','r') as fin:
    print(all_paths(fin.read()))
,

这将为每个路径生成一个节点链列表。它使用python列表和字典进行哈希处理-因此,如果要处理大型数据集,它将非常慢。如果是这种情况,请查看熊猫库(groupby)。相同的逻辑将起作用,但是您肯定会在nodes_by_date函数中节省时间。该库中可能还存在其他工具来生成所需的路径。

nodes = [('E',15,3.65),('A',1,2.09),('B',5,.89),('C',8,3.17),('D',1.15)]
#nodes = [('E',1.15),('F',16,100),('G',200),('H',17,1000)]

def all_paths(nodes):
    nbd = nodes_by_date(nodes)
    return get_chains(nbd,0)

def nodes_by_date(nodes):
    # sort by date (not sure what format your data is in,but might be easier to convert to a datenum/utc time)
    nodes = sorted(nodes,key=lambda x: x[1])
    # build a list of lists,each containing all of the nodes with a certain date
    # if your data is larger,look into the pandas library's groupby operation
    dates = {}
    for n in nodes:
        d = dates.get(n[1],[])
        d.append(n)
        dates[n[1]]=d
    return list(dates.values())

def get_chains(nbd,i):
    if i == len(nbd):
        return []
    # depth-first recursion so tails are only generated once
    tails = get_chains(nbd,i+1)
    chains = []
    for n in nbd[i]:
        if len(tails):
            for t in tails:
                newchain = [n]
                # only performant for smaller data
                newchain.extend(t)
                chains.append(newchain)
        # end of recursion
        else:
            chains.append([n])
    return chains

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res