Lädt...


🔧 Negative cycle with Dijskta(Possible but not optimal)


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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;
}

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.

...

🔧 Negative cycle with Dijskta(Possible but not optimal)


📈 99.5 Punkte
🔧 Programmierung

📰 Keychron Q8 keyboard review: Alice layout is interesting, but not optimal


📈 23.27 Punkte
📰 IT Nachrichten

🍏 iOS 18.1 Beta to iOS 18 Stable: Possible, but Not Yet


📈 20.56 Punkte
🍏 iOS / Mac OS

📰 Git.PHP.net Not Compromised in Supply Chain Attack, but User Database Leak Possible


📈 20.56 Punkte
📰 IT Security Nachrichten

🐧 LUKS : possible to encrypt but not ask password at boot ?


📈 20.56 Punkte
🐧 Linux Tipps

📰 Windows Phone Running Not One, Not Two, Not Three But Four Operating Systems - Video


📈 20.12 Punkte
📰 IT Security Nachrichten

📰 Instagram Displayed Negative Related Hashtags For Biden, But Hid Them For Trump


📈 19.94 Punkte
📰 IT Security Nachrichten

🔧 Bootstrap: Strong Merge and Cycle Times, but First Response Time Needs a Revamp


📈 19.9 Punkte
🔧 Programmierung

🪟 Steam Not Responding: How to Fix Steam Not Working on PC (8 Possible Solutions)


📈 19.37 Punkte
🪟 Windows Tipps

📰 Steam Not Responding: How to Fix Steam Not Working on PC (8 Possible Solutions)


📈 19.37 Punkte
🖥️ Betriebssysteme

📰 “If it is not right, do not do it, if it is not true, do not say it.” – Marcus Aurelius


📈 18.93 Punkte
📰 IT Nachrichten

📰 Yelp Is Not Liable For Negative Rating 'Stars' On Website, Says Appeals Court


📈 18.75 Punkte
📰 IT Security

📰 Yelp Is Not Liable For Negative Rating 'Stars' On Website, Says Appeals Court


📈 18.75 Punkte
📰 IT Security

🍏 Morgan Stanley: Apple’s multiyear iPhone upgrade cycle is a ‘when, not an if’


📈 18.72 Punkte
🍏 iOS / Mac OS

🍏 Apple is not expecting an iPhone 16 upgrade super cycle, says analyst


📈 18.72 Punkte
🍏 iOS / Mac OS

🍏 iPhone 16 may not see super cycle after all, according to analyst report


📈 18.72 Punkte
🍏 iOS / Mac OS

🐧 CentOS is just shifted its life cycle position. CentOS is not dead


📈 18.72 Punkte
🐧 Linux Tipps

🔧 The Optimal Choice of Hypothesis Is the Weakest, Not the Shortest


📈 17.35 Punkte
🔧 Programmierung

🔧 "I want the selection to be random but weighted in favor of higher scores, but that's not happening."


📈 16.57 Punkte
🔧 Programmierung

matomo