简体   繁体   中英

Longest path search on a topologically sorted weighted directed acyclic graph, but with a maximum edge count for valid paths

I know that longest/shortest path can be found in linear time by "processing the vertices in a topological order, and calculating the path length for each vertex to be the minimum or maximum length obtained via any of its incoming edges", or to put it more concisely, topologically sorting and finding the critical path.

My problem is that I need to add another restriction, which is maximum number of edges in a valid path. This complicates matters as the "maximum length obtained via any of its incoming edges" for a node may involve more edges, meaning a later higher weighted node may no longer be reachable because the maximum edges has already been reached.

What would be the correct way to go about solving this? Can it still be solved in linear time?

I think I've found a solution that makes it possible to still use topological sorting.

Do the topological sorting followed by critical path approach as normal, but when calculating the longest path to a given node, instead of just calculating one longest path, find the longest path for each path length from 1 to max edges in a valid path, creating a vertex for each of these highest scoring paths.

This basically means you explore all possible edge count variations in paths to each node, meaning the highest scoring path at the end will definitely be the longest path.

Just run the regular algorithm. Once you find a path with the maximum number of edges, simply stop there and return the solution you've found.

Sure you can make that path even longer, but it would then invalidate the maximum number of edges so it wouldn't be a valid solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM