Menu
Coddy logo textTech

Final Challenge #1

Lesson 8 of 9 in Coddy's Bellman-Ford Algorithm - Graph Algorithms course.

challenge icon

Challenge

Medium

Bellman-Ford's superpower is spotting negative cycles.

Write a function named hasNegativeCycle that takes n and the flat edges array (triples, directed) and returns 1 if the graph contains a negative-weight cycle, or 0 otherwise.

Hint: start every distance at 0, relax all edges n - 1 times, then do one more pass. If any edge can still be relaxed, a negative cycle exists.

Try it yourself

#include <stdlib.h>

int hasNegativeCycle(int n, int* edges, int edges_size) {
    // Write code here
    return -1;
}

All lessons in Bellman-Ford Algorithm - Graph Algorithms