请教如何寻找有向无环图(DAG)的最长路径以及两个顶点之间的路径总数
如题
我知道是先要拓扑排序,问题是排序之后的算法该如何?我看WIKI上的算法是这样的,但实在看不懂,也不懂该怎么实现:
algorithm dag-longest-path is
input:
Directed acyclic graph G
output:
Length of the longest path
length_to = array with |V(G)| elements of type int with default value 0
for each vertex v in topOrder(G) do
for each edge (v, w) in E(G) do
if length_to[w] <= length_to[v] + weight(G,(v,w)) then
length_to[w] = length_to[v] + weight(G, (v,w))
return max(length_to[v] for v in V(G))
public static int longestPath(int node,int[] topo){ int[] dist = new int[topo.length]; for(int i:topo){ if(isArc(node,i)){ if(dist[i]<dist[node]+1){ dist[i] = dist[node] + 1; } } } return getMax(dist); }