Optimized Longest Path is NP Complete


The "Upgraded Longest Way" issue is a computationally difficult undertaking, ordered as NP-complete. In this issue, given a diagram with weighted edges, the goal is to find the longest way from a predetermined beginning hub to a closure hub while expanding the amount of edge loads. Because of the remarkable development in possible ways to investigate, no known polynomial-time calculation can tackle this issue productively for all cases. All things considered, scientists resort to surmised calculations and heuristics to track down close ideal arrangements. The trouble of this issue has reasonable ramifications in different fields, like transportation, planned operations, and undertaking booking.

Methods Used

  • Reduction from Hamiltonian Path Problem

  • Using Known NP-Complete Problems

Reduction from Hamiltonian Path Problem

One way to deal with the issue that "Upgraded Longest Way" is NP-finished is by showing a decrease from the notable NP-complete issue called the Hamiltonian Way issue. The Hamiltonian Way issue finds out if a given diagram contains a way that visits each vertex precisely once.

Algorithm

  • Take a case of the Hamiltonian Way issue, which is a diagram G.

  • Make another chart G', indistinguishable from G, with similar vertices and edges.

  • Dole out weight 1 to all edges in G'.

  • Set the beginning and finishing hubs for the "Enhanced Longest Way" issue to be any two erratic hubs in G'.

  • In the event that G has a Hamiltonian Way, the "Upgraded Longest Way" in G' will be a similar Hamiltonian Way with an amount of edge loads equivalent to the quantity of edges, which is equivalent to the quantity of vertices short one.

  • On the off chance that G doesn't have a Hamiltonian Way, then, at that point, the "Streamlined Longest Way" in G' will be a straightforward way from the beginning hub to the closure hub with an amount of edge loads equivalent to the quantity of edges, which is not exactly the quantity of vertices short one.

  • This decrease exhibits that tackling "Improved Longest Way" is pretty much as hard as taking care of the Hamiltonian Way issue, making it NP-complete.

Example

#include <iostream>
#include <vector>
#include <functional>

using namespace std;

bool hasHamiltonianPath(const vector<vector<int>>& graph, int 
start, int finish) {
   int n = graph.size();
   vector<int> path;
   vector<bool> visited(n, false);
   function<bool(int)> dfs;
   dfs = [&](int u) {
      visited[u] = true;
      path.push_back(u);
      if (u == finish && path.size() == n)
         return true;
      for (int v = 0; v < n; ++v) {
         if (!visited[v] && graph[u][v]) {
            if (dfs(v))
               return true;
         }
      }
      visited[u] = false;
      path.pop_back();
      return false;
   };
   return dfs(start);
}

int main() {
   int n = 5;
   vector<vector<int>> graph(n, vector<int>(n, 0));
   graph[0][1] = graph[1][0] = 1;
   graph[1][2] = graph[2][1] = 1;
   graph[2][3] = graph[3][2] = 1;
   graph[3][4] = graph[4][3] = 1;
   vector<vector<int>> graph_prime = graph;
   int start = 0, finish = 3;
   if (hasHamiltonianPath(graph, start, finish))
      cout << "G has a Hamiltonian Path.\n";
   else
      cout << "G does not have a Hamiltonian Path.\n";
   return 0;
}

Output

G does not have a Hamiltonian Path.

Using Known NP-Complete Problems

Another methodology is to show that "Streamlined Longest Way" is NP-finished by lessening it from a known NP-complete issue, like the Longest Way issue or the Mobile Sales rep Issue (TSP straightforwardly)..

Algorithm

  • Given an occurrence of the Longest Way issue, which is a diagram G and a whole number k addressing the ideal way length.

  • Make another diagram G', indistinguishable from G, with similar vertices and edges.

  • Dole out weight 1 to all edges in G'.

  • Set the beginning and finishing hubs for the "Enhanced Longest Way" issue to be any two erratic hubs in G'.

  • In the event that G has a longest way of length k, the "Improved Longest Way" in G' will be a similar longest way with an amount of edge loads equivalent to k.

  • In the event that G doesn't have a longest way of length k, then, at that point, there will be no way in G' with an amount of edge loads equivalent to k.

  • Since the Longest Way issue is known to be NP-finished, this decrease lays out the NP-culmination of "Streamlined Longest Way."

  • Both of these methodologies outline that "Advanced Longest Way" is NP-finished, and accordingly, there is no known productive calculation to tackle it for all occasions, showing its computational intricacy.

Example

#include <iostream>
#include <vector>

class Graph {
public:
   int V; // Number of vertices
   std::vector<std::vector<int>> adj;

   Graph(int V) : V(V) {
      adj.resize(V, std::vector<int>(V, 0));
   }

   void addEdge(int u, int v) {
      adj[u][v] = 1;
      adj[v][u] = 1;
   }

   bool hasEnhancedLongestWay(int k, int start, int end) {
      return false;
   }
};

int main() {
   int V = 5; // Number of vertices
   Graph G(V);
   G.addEdge(0, 1);
   G.addEdge(1, 2);
   G.addEdge(2, 3);
   G.addEdge(3, 4);

   int k = 3;
   int start = 0;
   int end = 4;
   bool hasEnhancedLongestWay = G.hasEnhancedLongestWay(k, start, end);
   std::cout << std::boolalpha << hasEnhancedLongestWay << 
std::endl;

   return 0;
}

Output

false

Conclusion

Moving towards first method includes lessening the issue from the notable Hamiltonian Way issue. By changing a case of the Hamiltonian Way issue into an occurrence of "Advanced Longest Way," we showed that tackling the last issue is to some extent as hard as settling the previous, laying out its NP-fulfillment.

Method 2 straightforwardly explains about decreasing the issue from one more known NP-complete issue, the Longest Way issue or the Mobile Sales rep Issue (TSP). By demonstrating the way that "Improved Longest Way" can be changed into these NP-complete issues, we showed that it has a similar computational intricacy and is in this manner NP-complete.

Updated on: 04-Aug-2023

78 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements