Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the shortest path in a graph without any negative prefixes

Find the shortest path from source to destination in a directed graph with positive and negative edges, such that at no point in the path the sum of edges coming before it is negative. If no such path exists report that too.

I tried to use modified Bellman Ford, but could not find the correct solution.


I would like to clarify a few points :

  1. yes there can be negative weight cycles.
  2. n is the number of edges.
  3. Assume that a O(n) length path exists if the problem has a solution.
  4. +1/-1 edge weights.
like image 446
anirudh Avatar asked Mar 01 '12 02:03

anirudh


People also ask

Does BFS find the shortest path in unweighted graph?

Problem: Given an unweighted undirected graph, we have to find the shortest path from the given source to the given destination using the Breadth-First Search algorithm.

Which graph traversal is used to find the shortest path?

Dijkstra's algorithm (/ˈdaɪkstrəz/ DYKE-strəz) is an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks. It was conceived by computer scientist Edsger W.


1 Answers

Admittedly this isn't a constructive answer, however it's too long to post in a comment...

It seems to me that this problem contains the binary as well as discrete knapsack problems, so its worst-case-running-time is at best pseudo-polynomial. Consider a graph that is connected and weighted as follows:

Graph with initial edge with weight x and then a choice of -a(i) or 0 at each step

Then the equivalent binary knapsack problem is trying to choose weights from the set {a0, ..., an} that maximizes Σ ai where Σ ai < X.

As a side note, if we introduce weighted loops it's easy to construct the unbounded knapsack problem instead.

Therefore, any practical algorithm you might choose has a running time that depends on what you consider the "average" case. Is there a restriction to the problem that I've either not considered or not had at my disposal? You seem rather sure it's an O(n3) problem. (Although what's n in this case?)

like image 149
Kaganar Avatar answered Oct 03 '22 23:10

Kaganar