Professional Documents
Culture Documents
Assignment No-3
Assignment No-3
return nearestCity;
}
// Function to calculate the TSP tour using the nearest neighbor algorithm
public static ArrayList<Integer> nearestNeighborTSP(int startCity, int[][] distances) {
int n = distances.length;
boolean[] visited = new boolean[n];
ArrayList<Integer> tour = new ArrayList<>();
return tour;
}
return colors;
}
// Function to find all subsets that sum up to a given target using backtracking
void subsetSum(
const std::vector<int>& set,
std::vector<int>& currentSubset,
int currentIndex,
int currentSum,
int targetSum
){
// If the current sum equals the target, print the subset
if (currentSum == targetSum) {
std::cout << "Subset found: ";
for (int num : currentSubset) {
std::cout << num << " ";
}
std::cout << std::endl;
return; // Return to explore other subsets
}
int main() {
// Example set of numbers
std::vector<int> set = {3, 34, 4, 12, 5, 2};
std::cout << "Finding subsets with sum " << targetSum << ":\n";
subsetSum(set, currentSubset, 0, 0, targetSum);
return 0;
}
4. Write a program in C/C++/ Java to find out solution for 0/1 knapsack problem.
→
#include <iostream>
#include <vector>
#include <algorithm> // for std::max
int main() {
// Example weights and values of items
std::vector<int> weights = {1, 3, 4, 5};
std::vector<int> values = {1, 4, 5, 7};
std::cout << "Maximum value that can be obtained with a capacity of " << capacity << " is " <<
maxValue << "." << std::endl;
return 0;
}
5. Write a program in C/C++/ Java to find out live node, E node and dead node from a
given graph.
→
#include <iostream>
#include <vector>
#include <stack>
#include <unordered_set>
while (!stack.empty()) {
int current = stack.top();
stack.pop();
private:
std::vector<std::vector<int>> adjList; // Adjacency list
};
int main() {
Graph g(6);
return 0;
}