图中最长的路径

编程入门 行业动态 更新时间:2024-10-09 16:26:10
本文介绍了图中最长的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

由于最近2天,我试图找到一些逻辑计算graph.I最长路径知道我可以很容易找到它的DAG和在总体上是多项式时间algorithm.Formally,我想要实现启发式计算最长路径,morever,如果概率P被赋予与边缘是present图中,我们如何解决problem..help ...

Since last 2 days,i'm trying to find some logic for calculating longest path in graph.I know i can find it easily for DAGs and in general it is polynomial time algorithm.Formally,I want to implement heuristic for computing longest path,morever,if probability p is given with which an edge is present in graph,how can we solve the problem..help...

推荐答案

计算最长路径不能在多项式时间内,据我所知完成。下面的Java实现的最长路径算法找到一个积极的加权图对于一个给定源的最长路径,但它需要指数时间在最坏的情况下。

Calculating longest path cannot be done in polynomial time as far as I know. Following java implementation of the longest path algorithm finds the longest path of a positive weighted graph for a given source but it takes exponential time in its worst case.

public class LongestPath { static int times; public double initLongestPath(ArrayList<Vertex> V,Vertex source){ for(Vertex u:V){ u.setVisited(false); } return getLongestPath(source); } public double getLongestPath(Vertex v){ ++times; System.out.println(times); double w,dist,max=0; v.setVisited(true); for(Edge e:v.getOutGoingEdges()){ if(!e.getToNode().isVisited()){ dist=e.getWeight()+getLongestPath(e.getToNode()); if(dist>max) max=dist; } } v.setVisited(false); return max; }

}

更多推荐

图中最长的路径

本文发布于:2023-11-30 07:27:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图中   路径   最长

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!