简体   繁体   中英

Shortest paths for directed acyclic graphs

So basically, I have an n by m array of float values and I'm trying to find the shortest path between the any of the 1st row of values, and any of the mth row of values. Node (i, j) in the graph has children {(i, j+1), (i-1, j+1), i+1, j+1)} for any node that is not on the edge (0 < i < n-1) and is not in the bottom row (j < m-1) . I'm looking for an algorithm to solve this specific problem in good time. My current line of thought revolves around an A* search, but let me know what you think.

  • List item

The dynamic solutions are O(NM) or O(M^2). And it can't go under that - here's a counterexample for any better solution. Let's say you want to find a path between the leftmost element of the first row and the leftmost element of the last row. Let's look at a matrix of this form:

10000000000000
11000000000000
11100000000000
11110000000000
11111000000000
11111100000000
11111110000000
11111111000000
11111110000000
11111100000000
11111000000000
11110000000000
11100000000000
11000000000000
10000000000000

"1s" are the elements you could potentially go through on the path from the source to the destination element. "0s" are the elements you can't pass through.

The number of "1s" is of NM/4 order, so O(NM) (actually, Min(NM, M^2), see below). So, an algorithm which reads each of the 1s in this matrix will be of >=O(NM) complexity.

An algorithm which doesn't read all the "1s" will on the other hand be incorrect.

Proof : Let the numbers in the matrix be weights. Pick a "1" the algorithm doesn't read. Change that 1 to 0.5. The algorithm fails for this input as the optimal path now goes through an element it never reads (as none of the elements it read the first time changed, it will read the same elements this time too, unless it's nondeterministic, in which case it's a random chance whether it will work, which makes it incorrect too).

However, good O(NM) solutions should work just fine for 1000x1000 matrices (under a second). You can optimize it to Min(M^2, MN) if you only ever hit the elements you have to hit (for example, in my example matrix if the width is increased to 10000000, you don't have to read the added elements). Similarly, if the height is increased to 10000000, you don't have M^2 order reads because you don't go beyond the borders of the matrix. However, as you say both dimensions are very large, this is of little help I guess.

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