The Traveling Salesman Problem (TSP) is a classic NP-hard problem in combinatorial optimization. It involves finding the shortest possible route that visits each city exactly once and returns to the origin city.
TSP has applications in logistics, planning, and manufacturing, where it is crucial to minimize travel costs or time.
The main challenge of TSP is its computational complexity, as the number of possible routes increases factorially with the number of cities.
import java.util.*;
class TSPExample {
private static final int N = 4;
private static final int[][] DISTANCE = {
{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}
};
public static void main(String[] args) {
int[] visited = new int[N];
visited[0] = 1;
System.out.println("Minimum travel cost: " + tsp(DISTANCE, visited, 0, N, 1, 0));
}
private static int tsp(int[][] distance, int[] visited, int currPos, int n, int count, int cost) {
if (count == n && distance[currPos][0] > 0) {
return cost + distance[currPos][0];
}
int ans = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (visited[i] == 0 && distance[currPos][i] > 0) {
visited[i] = 1;
int temp = tsp(distance, visited, i, n, count + 1, cost + distance[currPos][i]);
ans = Math.min(ans, temp);
visited[i] = 0;
}
}
return ans;
}
Console Output:
Minimum travel cost: 80
The Knapsack Problem is a fundamental problem in combinatorial optimization where the goal is to maximize the total value of items placed in a knapsack without exceeding its capacity.
It is widely used in resource allocation, budgeting, and decision-making processes where a limited resource must be optimally utilized.
The complexity arises from the need to consider combinations of items, making it computationally intense for large datasets.
import java.util.*;
class KnapsackExample {
private static final int[] WEIGHTS = {10, 20, 30};
private static final int[] VALUES = {60, 100, 120};
private static final int CAPACITY = 50;
public static void main(String[] args) {
int n = VALUES.length;
System.out.println("Maximum value: " + knapsack(WEIGHTS, VALUES, n, CAPACITY));
}
private static int knapsack(int[] weights, int[] values, int n, int capacity) {
if (n == 0 || capacity == 0) {
return 0;
}
if (weights[n - 1] > capacity) {
return knapsack(weights, values, n - 1, capacity);
} else {
return Math.max(values[n - 1] + knapsack(weights, values, n - 1, capacity - weights[n - 1]),
knapsack(weights, values, n - 1, capacity));
}
}
Console Output:
Maximum value: 220
The Subset Sum Problem involves determining if there is a subset of a given set of integers that sums up to a specific target value.
It is applicable in fields like cryptography, resource allocation, and scheduling, where finding specific sums is necessary.
The exponential number of subsets makes the problem computationally intensive, especially for large sets.
import java.util.*;
class SubsetSumExample {
private static final int[] SET = {3, 34, 4, 12, 5, 2};
private static final int TARGET = 9;
public static void main(String[] args) {
int n = SET.length;
if (isSubsetSum(SET, n, TARGET)) {
System.out.println("Subset with the given sum found.");
} else {
System.out.println("No subset with the given sum exists.");
}
}
private static boolean isSubsetSum(int[] set, int n, int sum) {
if (sum == 0) {
return true;
}
if (n == 0 && sum != 0) {
return false;
}
if (set[n - 1] > sum) {
return isSubsetSum(set, n - 1, sum);
}
return isSubsetSum(set, n - 1, sum) || isSubsetSum(set, n - 1, sum - set[n - 1]);
}
Console Output:
Subset with the given sum found.
The Graph Coloring Problem involves assigning colors to the vertices of a graph such that no two adjacent vertices share the same color, using a minimum number of colors.
It is used in scheduling problems, register allocation in compilers, and frequency assignment in mobile networks.
Finding the chromatic number, which is the smallest number of colors needed, is computationally complex for large graphs.
import java.util.*;
class GraphColoringExample {
private static final int V = 4;
private static final int[][] GRAPH = {
{0, 1, 1, 1},
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0}
};
private static final int[] COLORS = new int[V];
public static void main(String[] args) {
int m = 3; // Number of colors
if (graphColoring(GRAPH, m, 0)) {
System.out.println("Solution exists with the given number of colors.");
} else {
System.out.println("No solution exists with the given number of colors.");
}
}
private static boolean isSafe(int v, int[][] graph, int[] colors, int c) {
for (int i = 0; i < V; i++) {
if (graph[v][i] == 1 && c == colors[i]) {
return false;
}
}
return true;
}
private static boolean graphColoring(int[][] graph, int m, int v) {
if (v == V) {
return true;
}
for (int c = 1; c <= m; c++) {
if (isSafe(v, graph, COLORS, c)) {
COLORS[v] = c;
if (graphColoring(graph, m, v + 1)) {
return true;
}
COLORS[v] = 0;
}
}
return false;
}
Console Output:
Solution exists with the given number of colors.
The Hamiltonian Cycle Problem is about determining whether a Hamiltonian cycle, a cycle that visits each vertex exactly once, exists in a given graph.
It is used in routing, scheduling, and network topology design.
Determining the existence of a Hamiltonian cycle is NP-complete, making it computationally challenging for large graphs.
import java.util.*;
class HamiltonianCycleExample {
private static final int V = 5;
private static final int[][] GRAPH = {
{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0}
};
private static final int[] PATH = new int[V];
public static void main(String[] args) {
Arrays.fill(PATH, -1);
PATH[0] = 0;
if (hamCycle(GRAPH, PATH, 1)) {
System.out.println("Hamiltonian cycle exists.");
} else {
System.out.println("No Hamiltonian cycle exists.");
}
}
private static boolean isSafe(int v, int[][] graph, int[] path, int pos) {
if (graph[path[pos - 1]][v] == 0) {
return false;
}
for (int i = 0; i < pos; i++) {
if (path[i] == v) {
return false;
}
}
return true;
}
private static boolean hamCycle(int[][] graph, int[] path, int pos) {
if (pos == V) {
return graph[path[pos - 1]][path[0]] == 1;
}
for (int v = 1; v < V; v++) {
if (isSafe(v, graph, path, pos)) {
path[pos] = v;
if (hamCycle(graph, path, pos + 1)) {
return true;
}
path[pos] = -1;
}
}
return false;
}
Console Output:
Hamiltonian cycle exists.
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies