Logo

dev-resources.site

for different kinds of informations.

Negative cycle with Dijskta(Possible but not optimal)

Published at
10/10/2024
Categories
dsa
algorithms
graph
Author
priya2422
Categories
3 categories in total
dsa
open
algorithms
open
graph
open
Author
9 person written this
priya2422
open
Negative cycle with Dijskta(Possible but not optimal)

I decided to experiment with Dijkstra’s algorithm to solve a problem that may involve negative cycles. While Dijkstra’s algorithm isn't optimal for graphs with negative weights, I found that it can be adapted with some modifications.

Thought Process Behind Using Dijkstra's Algorithm for Negative Cycles:

The core issue with negative cycles is that they can lead to continuously decreasing distances, causing incorrect results. Normally, Dijkstra's algorithm works by updating the distance to a node when a shorter path is found, which happens within an 'if' block that compares the new distance to the previously calculated one. However, with a negative cycle, this comparison will keep returning true because the total distance keeps decreasing as we go around the cycle.

To handle this, my approach stores the paths for each node. The next time we visit a node, we check whether the node itself is already part of the path leading to it. If the node is found in its own path, it indicates a cycle. This means we are revisiting the node through a cycle that potentially decreases the distance, leading us into the if block where we detect the negative cycle. On the other hand, if we find a shorter path that doesn’t form a cycle, the node's distance is updated as usual.

Here’s my code implementation, which adapts Dijkstra’s algorithm while incorporating path tracking:

vector<int> bellman_ford(int V, vector<vector<int>>& edges, int S) {
    vector<vector<pair<int,int>>> adj(V);
    for(int i=0;i<edges.size();i++){
        adj[edges[i][0]].push_back({edges[i][1],edges[i][2]});
    }
    vector<int> dist(V,1e8);
    dist[S] = 0;
    vector<vector<int>> path(V);
    path[S] = {S};
    queue<pair<int,int>> q;
    q.push({S,0});

    while(!q.empty()) {
        int node = q.front().first;
        int dis = q.front().second;
        q.pop();

        for(auto it: adj[node]) {
            if(dis + it.second < dist[it.first]) {
                vector<int> temp = path[node];

                // Check if the node is already in its path (cycle detection)
                if(find(temp.begin(), temp.end(), it.first) != temp.end()) {
                    return {-1}; // Negative cycle detected
                }

                temp.push_back(it.first);
                path[it.first] = temp;
                q.push({it.first, dis + it.second});
                dist[it.first] = dis + it.second; // Update distance
            }
        }
    }
    return dist;
}
Enter fullscreen mode Exit fullscreen mode

This approach ensures that the block is entered only when a reducing distance is detected, either due to a linear path or a negative cycle. If a cycle is found in the path, the function returns -1 to indicate the presence of a negative cycle. Otherwise, it updates the distance accordingly.

Let me know your thoughts on this.

graph Article's
30 articles in total
Favicon
Get a gist of graph data structure here...
Favicon
Find safest walk through the grid
Favicon
Number of islands
Favicon
Negative cycle with Dijskta(Possible but not optimal)
Favicon
YugabyteDB as a Graph database with PuppyGraph
Favicon
Remove Methods from project
Favicon
Visualize the preferences of cats
Favicon
How to Determine if a Graph is Not Simple Without Checking Every Edge for Loops or Parallelism
Favicon
Disjoint Set Graph Learning
Favicon
Bellman ford algorithm(Single Source Shorted Path in DAG)
Favicon
Safely restructure your codebase with Dependency Graphs
Favicon
Unveiling the Connections: A Beginner's Guide to Graph Theory
Favicon
How is Graph stored in Memory?
Favicon
Graph problems are not hard
Favicon
Como andam as suas Relações?
Favicon
Merge Intervals : A unique Graph-based approach
Favicon
Explaining LinkedIn profile, "1st," "2nd," and "3rd" values.
Favicon
Neo4j and the Power of Graph Databases in Data Science
Favicon
Exploring the Implementation of Graph Data in OceanBase
Favicon
Solving "Word Search" problem
Favicon
Powershell script to call microsoft graph and send email using azure app registration
Favicon
A developer’s introduction to graph databases
Favicon
Graph in R with Grouping Letters from the Tukey, LSD, Duncan Test, agricolae Package
Favicon
How to: Get up to speed and scale with Aerospike Graph on Google Cloud Marketplace
Favicon
Preview Geometry Nodes on web using React
Favicon
Improved Data Point Graph Widget for Cumulocity IoT
Favicon
Six Degrees of Separation Using Apache AGE
Favicon
Next Big Data System
Favicon
The Usability of Graph Data and Graph Algorithms: Unleashing the Power of Connections
Favicon
Accelerate Domain Learning: Explore Application Dependencies with RailsGraph

Featured ones: