Labelled acyclic graph and optimum path algorithm + Additional changes#219
Labelled acyclic graph and optimum path algorithm + Additional changes#219adithyaov wants to merge 38 commits intosnowleopard:mainfrom
Conversation
If you'd like to split some of it, I would be happy to lend a hand! |
|
@Avasil Of Course :-). |
|
Awesome! I will have a limited access to the internet for a couple of days (Saturday - Wednesday) so I was thinking about waiting until I come back |
Without adding the function
Ah, I see. In that case, for the time being, I'll add the |
|
@jitwit I agree, this is not completely Dijkstra. But for an acyclic graph one does not need to maintain a set of nodes. One can directly relax the edges in the topological order. I think one can consider this as a simplification of Dijkstra for acyclic graphs.
Ah yes, I think I'll rename it to EDIT: @jitwit The following describes a minor issue. |
Indeed! In fact, we could probably use the same algorithm for finding the longest path too, which would be even more confusing :) Perhaps, we could call it https://hackage.haskell.org/package/algebraic-graphs-0.4/docs/Algebra-Graph-Label.html#t:Optimum We could also look at how such generic path-finding algorithms are called in the literature. |
|
It looks like in the Mohri paper Huang paper attributes shortest paths from topological sorting to Viterbi (1967) and calls the algorithm Huang: https://www.aclweb.org/anthology/C08-5001 |
|
@jitwit Thanks for the pointers! I haven't come across the name "Viterbi algorithm" from Huang's paper. For me, this name is usually associated with this famous algorithm: https://en.wikipedia.org/wiki/Viterbi_algorithm From these options, I'm still in favour of |
|
@snowleopard Yeah, I find Viterbi confusing since it makes me think the semiring is specialized! Also, dag is kind of in the module name, so it's probably unnecessary to include that in the function name.
|
@jitwit I'd stick with the singular name because the source vertex is fixed. I've seen both singular and plural used in the literature, but I generally prefer using singular names in library APIs. |
|
@adithyaov A general comment: it looks to me that there is a more general function that can be factored out of your implementation. Something |
Ah, I see. Having such a function in the library would be useful. I guess, I'll create something like this and implement |
| -- TODO: Improve documentation for 'fold' | ||
| -- TODO: Make 'fold' more efficient | ||
| -- TODO: Add tests for fold | ||
| -- | fold takes any function with the signature @e -> a -> a -> s -> s@. |
There was a problem hiding this comment.
This doesn't match the type signature.
There was a problem hiding this comment.
Yes, my bad. Will change it.
| -- input state. If one assumes the acyclic graph as a dependency | ||
| -- graph and the vertices as resources, then, the expected function | ||
| -- for fold takes all the dependents of the resource, the resource | ||
| -- and the input state in that order and pproduces an output state. |
| addP v1 m = | ||
| let adjust v2 e = Map.adjust ((e, v1):) v2 | ||
| in Map.foldrWithKey adjust m (am ! v1) | ||
| process (m, s) v2 = (addP v2 m, f (m ! v2) v2 s) |
There was a problem hiding this comment.
The tuple seems unnecessary, let's remove it.
There was a problem hiding this comment.
Yes, I could have made fold a recursive function. But, I think, using inbuilt folds has an advantage of being more efficient (although I'm not sure how much).
|
I've added some more comments. @adithyaov There are a few |
|
Also, the CI fails with a compile error. |
|
@snowleopard I plan to address most of the TODOs before the merge. I apologize for the delay. |
|
@adithyaov Thanks, and don't worry about the delay. |
|
@snowleopard That is an interesting failure. I think it's because I changed the arbitrary instance. Edit: Ah I see, It was because of how |
|
Please don't review this PR yet, I'll include a small writeup with all the major changes in this PR. This would make reviewing easier. I'll probably squash a few commits as well. |
This is a draft implementation of labeled acyclic graph required if one wants to make algorithms on acyclic graphs. There is still a lot of work to be done including writing tests.