Reputation: 33398
I'm given a graph which can have more than one arch between 2 nodes.
Example :
4 nodes 1->2 2->3 3->4 3->4 1->4
Which is the optim way to find the number of the roads from node A to node B ?
The answer for the example is 3 : 1->2->3->4 ; 1->2->3->4 and 1->4
Limit for nodes and archs are 1 000 000
I'm only thinking at a brute force algorithm.
Any other ideas? Edit:
graph is acyclic
Upvotes: 1
Views: 746
Reputation: 1
There is no optimal way. This Problem is a NP Complete problem. http://en.wikipedia.org/wiki/Feedback_vertex_set
You can only find good solutions
Upvotes: 0
Reputation: 108810
If the graph is cyclic then the result is +infinity, since you can run in a cycle as often as you like.
One idea that might work on an acyclic directed graph:
Ordering in the nodes isn't trivial though. But I'm sure you can find an algorithm for that, since it's a common problem when using DAGs.
Upvotes: 1