Home > Backend Development > C++ > body text

The optimized longest path is NP-complete

王林
Release: 2023-09-06 09:01:07
forward
1044 people have browsed it

The optimized longest path is NP-complete

The "Upgrade Longest Path" problem is a computationally difficult task that is ordered as an NP-complete problem. In this issue, given a graph with weighted edges, the goal is to find the longest path from a predetermined starting hub to a closing hub while enlarging the edge loading. Since possible research methods have evolved significantly, no known polynomial-time computation can solve this problem efficiently in all cases. All things considered, scientists rely on speculative calculations and heuristics to track down the closest ideal arrangement. The trouble with this issue has had an impact in different areas such as transportation, planning operations and booking reservations.

usage instructions

  • Simplify the Hamiltonian path problem

  • Use known NP-complete problems

Simplification of Hamiltonian path problem

One way to solve the "Upgrade Longest Path" problem that is NP-complete is to show a reduction compared to the famous NP-complete problem (called the Hamiltonian Road Problem). The Hamiltonian Road Problem determines whether a given graph contains a path that visits each vertex exactly once.

algorithm

  • Take the Hamiltonian road problem as an example, which is a graph G.

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

  • Assign weight 1 to all edges in G'.

  • Set the starting and ending pivots of the "enhanced longest path" problem to any two unstable pivots in G'.

  • If G has a Hamiltonian path, the "upgraded longest path" in G' will be a similar Hamiltonian path whose edge loading is equal to the number of edges, and the number of edges is equal to the number of vertices. one.

  • If G does not have a Hamiltonian road, then the "streamlined longest path" in G' at this time will be a direct path from the starting hub to the closing hub, where the amount of edge load is equal to the number of edges, It's not exactly the number of short vertices.

  • This decline shows that solving the "improved longest path" is as difficult as solving the Hamiltonian Road problem, making it an NP-complete problem.

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;
}
Copy after login

Output

G does not have a Hamiltonian Path.
Copy after login

Use known NP-complete problems

Another way is to prove that the "reduced longest road" is NP-complete by reducing it from a known NP-complete problem such as the longest road problem or the moving sales representative problem (directly TSP) .

algorithm

  • Given the appearance of the longest path problem, which is a graph G and an integer k that solves for the ideal path length.

  • Make another graph G', no different from G, with similar vertices and edges.

  • Assign weight 1 to all edges in G'.

  • Set the starting and ending pivots of the "enhanced longest path" problem to any two unstable pivots in G'.

  • If G has a longest path of length k, then the "improved longest path" in G' will be a similar longest path with an edge load equal to k.

  • If G does not have the longest path of length k, then there will be no path in G' with an edge load equal to k at this time.

  • Since the longest path problem is known to be NP-completed, this reduction establishes the NP pinnacle of "simplified longest path".

  • Both approaches outline that the "high-level longest path" is NP-complete and therefore, there is no known efficient computation that can handle it in all cases, which shows its computational complexity.

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;
}
Copy after login

Output

false
Copy after login

in conclusion

Moving to the first approach involves reducing the famous Hamiltonian method problem. By transforming the case of the Hamiltonian Road Problem into the case of the "Advanced Longest Road", we show that solving the last problem is somehow as difficult as solving the previous problem and elaborate on its NP implementation.

Method 2 directly explains how to reduce the problem from another known NP-complete problem, the longest path problem, or the moving sales representative problem (TSP). By demonstrating the way in which "improved longest path" can be transformed into these NP-complete problems, we show that it has similar computational complexity and is NP-complete in this way.

The above is the detailed content of The optimized longest path is NP-complete. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template