'Bellman Ford implementation java
If i were to be given an adjacency list of ArrayList<ArrayList> adjList where adjList.get(0) would contain an ArrayList of all the edges in terms of IntPair, how would I detect for negative cycles?
for (int i = 0; i < V - 1; i++) {
for (IntPair pair : adjList.get(i)) {
int v = pair.first;
int weight = pair.second;
if (dist[i] != INF && dist[i] + weight < dist[v]) {
dist[v] = NEGINF;
}
}
}
Would this implementation be sufficient to make all the nodes in the negative cycle have an estimate of NEGINF?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|