diff --git a/code/01-graphs-adjacency-list.c b/code/01-graphs-adjacency-list.c index 99183fa..9c74d76 100644 --- a/code/01-graphs-adjacency-list.c +++ b/code/01-graphs-adjacency-list.c @@ -5,8 +5,17 @@ void print_adj_list(int arr[5][5]) { // Initialize to all zeros int adj_list[5][5] = {{0}}; - // Traverse the given matrix - // FIXME: add the necessary code here + for (int i = 0; i < 5; ++i) { + printf("%d: ", i + 1); + for (int j = 0; j < 5; ++j) { + if (arr[i][j] == 1) { + adj_list[i][adj_list[i][0]] = j; // Store the neighbor in the list + printf("%d ", j + 1); + adj_list[i][0]++; // Increment the count of neighbors for this vertex + } + } + printf("\n"); + } } /** Main Function */ diff --git a/code/01-graphs-adjacency-matrix.c b/code/01-graphs-adjacency-matrix.c index d46aa4c..71874ca 100644 --- a/code/01-graphs-adjacency-matrix.c +++ b/code/01-graphs-adjacency-matrix.c @@ -5,11 +5,23 @@ int N, M; /** Function to create an adjacency matrix */ void create_adj_matrix(int adj[][N + 1], int arr[][2]) { - // Initialize all value to zero - // FIXME: add the necessary code here + // Initialize all values to zero + for (int i = 0; i < N + 1; i++) { + for (int j = 0; j < N + 1; j++) { + adj[i][j] = 0; + } + } - // Traverse the array of edges and update the values - // FIXME: add the necessary code here + // Traverse the array of edges + for (int i = 0; i < M; i++) { + // Find X and Y of edges + int x = arr[i][0]; + int y = arr[i][1]; + + // Update value to 1 + adj[x][y] = 1; + adj[y][x] = 1; + } } /** Function to print an adjacency matrix */ @@ -24,7 +36,7 @@ void print_adj_matrix(int adj[][N + 1]) { } } -/** Main function */ +/** Main Function */ int main() { // Number of vertices N = 5; diff --git a/code/02-graphs-eulerian-path.c b/code/02-graphs-eulerian-path.c index fb7abb6..434bac9 100644 --- a/code/02-graphs-eulerian-path.c +++ b/code/02-graphs-eulerian-path.c @@ -16,33 +16,73 @@ void find_path(int graph[N][N]) { int path_length = 0; // Find out number of edges each vertex has - // FIXME: Add the code here + for (int i = 0; i < N; i++) { + numofadj[i] = 0; + for (int j = 0; j < N; j++) { + numofadj[i] += graph[i][j]; + } + } // Find out how many vertex has odd number edges - // FIXME: Add the code here + int startpoint = 0, numofodd = 0; + for (int i = N - 1; i >= 0; i--) { + if (numofadj[i] % 2 == 1) { + numofodd++; + startpoint = i; + } + } // If number of vertex with odd number of edges is greater than two return "No // Solution". - // FIXME: Add the code here + if (numofodd > 2) { + printf("No Solution\n"); + return; + } // If there is a path find the path // Initialize empty stack and path take the starting current as discussed - // FIXME: Add the code here + int cur = startpoint; // Loop will run until there is element in the stack or current edge has some // neighbour. - // FIXME: Add the code here - - // If the current vertex has at least one neighbour add the current vertex - // to stack, remove the edge between them and set the current to its - // neighbour. - // FIXME: Add the code here + while (top > 0 || numofadj[cur] > 0) { + // If current node has not any neighbour add it to path and pop stack set + // new current to the popped element + if (numofadj[cur] == 0) { + path[path_length++] = cur; + if (top > 0) { + cur = stack[--top]; + } else { + break; + } + } + + // If the current vertex has at least one neighbour add the current vertex + // to stack, remove the edge between them and set the current to its + // neighbour. + else { + for (int i = 0; i < N; i++) { + if (graph[cur][i] == 1) { + stack[top++] = cur; + graph[cur][i] = 0; + graph[i][cur] = 0; + numofadj[cur]--; + numofadj[i]--; + cur = i; + break; + } + } + } + } // Add the last vertex to the path - // FIXME: Add the code here + path[path_length++] = cur; // Print the path - // FIXME: Add the code here + for (int i = path_length - 1; i >= 0; i--) { + printf("%d -> ", path[i]); + } + printf("\n"); } /** Main function */ diff --git a/code/02-graphs-hamiltonian-cycle.c b/code/02-graphs-hamiltonian-cycle.c index d931e05..995ff6e 100644 --- a/code/02-graphs-hamiltonian-cycle.c +++ b/code/02-graphs-hamiltonian-cycle.c @@ -15,13 +15,76 @@ void print_solution(int path[]) { printf("\n"); } +/** A utility function to check if the vertex v can be added at index + * 'pos' in the Hamiltonian cycle constructed so far + * (stored in 'path[]') + */ +int is_safe(int v, int graph[N][N], int path[], int pos) { + // Check if this vertex is an adjacent vertex of the previously added vertex. + if (graph[path[pos - 1]][v] == 0) return 0; + + // Check if the vertex has already been included. + // This step can be optimized by creating an array of size N + for (int i = 0; i < pos; i++) + if (path[i] == v) return 0; + + return 1; +} + +/** A recursive utility function to solve Hamiltonian cycle problem */ +int hamiltonian_cycle_util(int graph[N][N], int path[], int pos) { + // base case: If all vertices are included in Hamiltonian cycle + if (pos == N) { + // And if there is an edge from the last included vertex to the + // first vertex + if (graph[path[pos - 1]][path[0]] == 1) + return 1; + else + return 0; + } + + // Try different vertices as a next candidate in Hamiltonian cycle. + // We don't try for 0 as we included 0 as starting point in + // hamiltonian_cycle() + for (int v = 1; v < N; v++) { + // Check if this vertex can be added to Hamiltonian cycle + if (is_safe(v, graph, path, pos)) { + path[pos] = v; + + // recur to construct rest of the path + if (hamiltonian_cycle_util(graph, path, pos + 1) == 1) return 1; + + // If adding vertex v doesn't lead to a solution, then remove it + path[pos] = -1; + } + } + + // If no vertex can be added to Hamiltonian Cycle constructed so far, + // then return false + return 0; +} + /** This function solves the Hamiltonian cycle problem using Backtracking. + * It mainly uses hamiltonian_cycle_util() to solve the problem. * It returns false if there is no Hamiltonian Cycle possible, * otherwise return true and prints the path. */ int hamiltonian_cycle(int graph[N][N]) { - // FIXME: Implement the function - // print_solution(path); + int path[N]; + for (int i = 0; i < N; i++) path[i] = -1; + + // Let us put vertex 0 as the first vertex in the path. + // If there is a Hamiltonian cycle, + // then the path can be started from any point + // of the cycle as the graph is undirected + path[0] = 0; + if (hamiltonian_cycle_util(graph, path, 1) == 0) { + printf("Solution does not exist\n"); + printf("\n"); + return 0; + } + + print_solution(path); return 1; } diff --git a/code/03-tree-detection.c b/code/03-tree-detection.c index 775dc8c..149f7dd 100644 --- a/code/03-tree-detection.c +++ b/code/03-tree-detection.c @@ -13,7 +13,13 @@ int search_cycle(int graph[N][N], int v, int *visited, int parent) { visited[v] = 1; for (int i = 0; i < N; ++i) { - // FIXME: Implement the function + if (graph[v][i]) { + if (!visited[i]) { + if (search_cycle(graph, i, visited, v)) return 1; + } else if (i != parent) { // cycle detected + return 1; + } + } } return 0; } @@ -40,10 +46,11 @@ int is_tree(int graph[N][N]) { // Perform depth-first search from vertex 0 to check if all vertices // are reachable (connected) - // FIXME: Implement the function + has_cycle = search_cycle(graph, 0, visited, -1); + if (has_cycle) return 0; // Check if all vertices have been visited - // FIXME: Implement the function + if (!all_visited(visited)) return 0; // Return true if no cycle is found and all return 1; diff --git a/code/04-subset_sum.c b/code/04-subset_sum.c index dba2030..9d51e66 100644 --- a/code/04-subset_sum.c +++ b/code/04-subset_sum.c @@ -7,7 +7,20 @@ * @return 1 if there is a subset that sums to the target, 0 otherwise */ int subset_sum(int set[], int n, int target) { - // FIXME: Implement the subset sum algorithm + // Iterate through all possible subsets + for (int i = 0; i < (1 << n); i++) { + int sum = 0; + for (int j = 0; j < n; j++) { + // Verify if the j-th element is in the subset + if (i & (1 << j)) { + sum += set[j]; + } + } + // If the subset sums to the target, return 1 + if (sum == target) { + return 1; + } + } return 0; } diff --git a/code/05-knapsack.c b/code/05-knapsack.c index f778c1b..c2dab45 100644 --- a/code/05-knapsack.c +++ b/code/05-knapsack.c @@ -19,7 +19,23 @@ int max(int a, int b) { return (a > b) ? a : b; } * @return The maximum value that can be obtained with the given capacity. */ int knapsack(int W, int weights[], int values[], int n) { - // FIXME: Implement the knapsack algorithm + // Base case: no items left or no capacity left + if (n == 0 || W == 0) { + return 0; + } + + // If the weight of the nth item is more than the remaining capacity, it + // cannot be included + if (weights[n - 1] > W) { + return knapsack(W, weights, values, n - 1); + } else { + // Return the maximum of two cases: + // 1. nth item included + // 2. nth item not included + return max( + values[n - 1] + knapsack(W - weights[n - 1], weights, values, n - 1), + knapsack(W, weights, values, n - 1)); + } } /** diff --git a/solutions/01-graphs-adjacency-list.c b/solutions/01-graphs-adjacency-list.c deleted file mode 100644 index 9c74d76..0000000 --- a/solutions/01-graphs-adjacency-list.c +++ /dev/null @@ -1,32 +0,0 @@ -#include - -/** Function to create an adjacency list */ -void print_adj_list(int arr[5][5]) { - // Initialize to all zeros - int adj_list[5][5] = {{0}}; - - for (int i = 0; i < 5; ++i) { - printf("%d: ", i + 1); - for (int j = 0; j < 5; ++j) { - if (arr[i][j] == 1) { - adj_list[i][adj_list[i][0]] = j; // Store the neighbor in the list - printf("%d ", j + 1); - adj_list[i][0]++; // Increment the count of neighbors for this vertex - } - } - printf("\n"); - } -} - -/** Main Function */ -int main() { - // Given matrix - int arr[5][5] = { - {1, 0, 1, 0, 0}, {0, 1, 0, 0, 0}, {0, 1, 0, 0, 1}, - {0, 0, 0, 0, 1}, {1, 0, 0, 1, 0}, - }; - - print_adj_list(arr); - - return 0; -} diff --git a/solutions/01-graphs-adjacency-matrix.c b/solutions/01-graphs-adjacency-matrix.c deleted file mode 100644 index 71874ca..0000000 --- a/solutions/01-graphs-adjacency-matrix.c +++ /dev/null @@ -1,57 +0,0 @@ -#include - -/** N vertices and M edges */ -int N, M; - -/** Function to create an adjacency matrix */ -void create_adj_matrix(int adj[][N + 1], int arr[][2]) { - // Initialize all values to zero - for (int i = 0; i < N + 1; i++) { - for (int j = 0; j < N + 1; j++) { - adj[i][j] = 0; - } - } - - // Traverse the array of edges - for (int i = 0; i < M; i++) { - // Find X and Y of edges - int x = arr[i][0]; - int y = arr[i][1]; - - // Update value to 1 - adj[x][y] = 1; - adj[y][x] = 1; - } -} - -/** Function to print an adjacency matrix */ -void print_adj_matrix(int adj[][N + 1]) { - // Traverse the adj[][] - for (int i = 1; i < N + 1; i++) { - for (int j = 1; j < N + 1; j++) { - // Print the value at adj[i][j] - printf("%d ", adj[i][j]); - } - printf("\n"); - } -} - -/** Main Function */ -int main() { - // Number of vertices - N = 5; - - // Given edges - int arr[][2] = {{1, 2}, {2, 3}, {4, 5}, {1, 5}}; - - // Number of edges - M = sizeof(arr) / sizeof(arr[0]); - - int adj[N + 1][N + 1]; - - create_adj_matrix(adj, arr); - - print_adj_matrix(adj); - - return 0; -} diff --git a/solutions/02-graphs-eulerian-path.c b/solutions/02-graphs-eulerian-path.c deleted file mode 100644 index 434bac9..0000000 --- a/solutions/02-graphs-eulerian-path.c +++ /dev/null @@ -1,140 +0,0 @@ -#include - -/** Number of vertices in the graph */ -#define N 5 -/** Maximum length of the path */ -#define MAX_PATH_LENGTH 20 - -/** Find Eulerian Path in a graph - * @param graph Adjacency matrix of the graph - */ -void find_path(int graph[N][N]) { - int numofadj[N]; - int stack[N]; - int path[MAX_PATH_LENGTH]; - int top = 0; - int path_length = 0; - - // Find out number of edges each vertex has - for (int i = 0; i < N; i++) { - numofadj[i] = 0; - for (int j = 0; j < N; j++) { - numofadj[i] += graph[i][j]; - } - } - - // Find out how many vertex has odd number edges - int startpoint = 0, numofodd = 0; - for (int i = N - 1; i >= 0; i--) { - if (numofadj[i] % 2 == 1) { - numofodd++; - startpoint = i; - } - } - - // If number of vertex with odd number of edges is greater than two return "No - // Solution". - if (numofodd > 2) { - printf("No Solution\n"); - return; - } - - // If there is a path find the path - // Initialize empty stack and path take the starting current as discussed - int cur = startpoint; - - // Loop will run until there is element in the stack or current edge has some - // neighbour. - while (top > 0 || numofadj[cur] > 0) { - // If current node has not any neighbour add it to path and pop stack set - // new current to the popped element - if (numofadj[cur] == 0) { - path[path_length++] = cur; - if (top > 0) { - cur = stack[--top]; - } else { - break; - } - } - - // If the current vertex has at least one neighbour add the current vertex - // to stack, remove the edge between them and set the current to its - // neighbour. - else { - for (int i = 0; i < N; i++) { - if (graph[cur][i] == 1) { - stack[top++] = cur; - graph[cur][i] = 0; - graph[i][cur] = 0; - numofadj[cur]--; - numofadj[i]--; - cur = i; - break; - } - } - } - } - - // Add the last vertex to the path - path[path_length++] = cur; - - // Print the path - for (int i = path_length - 1; i >= 0; i--) { - printf("%d -> ", path[i]); - } - printf("\n"); -} - -/** Main function */ -int main() { - /* Test case 1 - - 0 --- 1 - | | \ - | 2--3 - 4 - - */ - int graph1[N][N] = {{0, 1, 0, 0, 1}, - {1, 0, 1, 1, 0}, - {0, 1, 0, 1, 0}, - {0, 1, 1, 0, 0}, - {1, 0, 0, 0, 0}}; - - printf("Test Case 1:\n"); - find_path(graph1); - - /* Test case 2 - - 0 -- 1 -- 2 - /| / \ | \ - 3--4 5 - - */ - int graph2[N][N] = {{0, 1, 0, 1, 1}, - {1, 0, 1, 0, 1}, - {0, 1, 0, 1, 1}, - {1, 1, 1, 0, 0}, - {1, 0, 1, 0, 0}}; - - printf("Test Case 2:\n"); - find_path(graph2); - - /* Test case 3 - - 0 --- 1 - /|\ |\ - 2 4---5 3 - - */ - int graph3[N][N] = {{0, 1, 0, 0, 1}, - {1, 0, 1, 1, 1}, - {0, 1, 0, 1, 0}, - {0, 1, 1, 0, 1}, - {1, 1, 0, 1, 0}}; - - printf("Test Case 3:\n"); - find_path(graph3); - - return 0; -} diff --git a/solutions/02-graphs-hamiltonian-cycle.c b/solutions/02-graphs-hamiltonian-cycle.c deleted file mode 100644 index 995ff6e..0000000 --- a/solutions/02-graphs-hamiltonian-cycle.c +++ /dev/null @@ -1,124 +0,0 @@ -#include - -/** Number of vertices in the graph */ -#define N 5 - -/** A utility function to print solution */ -void print_solution(int path[]) { - printf("Solution Exists: "); - - for (int i = 0; i < N; i++) printf("%d ", path[i]); - - // Let us print the first vertex again to show the complete cycle - printf("%d ", path[0]); - printf("\n"); - printf("\n"); -} - -/** A utility function to check if the vertex v can be added at index - * 'pos' in the Hamiltonian cycle constructed so far - * (stored in 'path[]') - */ -int is_safe(int v, int graph[N][N], int path[], int pos) { - // Check if this vertex is an adjacent vertex of the previously added vertex. - if (graph[path[pos - 1]][v] == 0) return 0; - - // Check if the vertex has already been included. - // This step can be optimized by creating an array of size N - for (int i = 0; i < pos; i++) - if (path[i] == v) return 0; - - return 1; -} - -/** A recursive utility function to solve Hamiltonian cycle problem */ -int hamiltonian_cycle_util(int graph[N][N], int path[], int pos) { - // base case: If all vertices are included in Hamiltonian cycle - if (pos == N) { - // And if there is an edge from the last included vertex to the - // first vertex - if (graph[path[pos - 1]][path[0]] == 1) - return 1; - else - return 0; - } - - // Try different vertices as a next candidate in Hamiltonian cycle. - // We don't try for 0 as we included 0 as starting point in - // hamiltonian_cycle() - for (int v = 1; v < N; v++) { - // Check if this vertex can be added to Hamiltonian cycle - if (is_safe(v, graph, path, pos)) { - path[pos] = v; - - // recur to construct rest of the path - if (hamiltonian_cycle_util(graph, path, pos + 1) == 1) return 1; - - // If adding vertex v doesn't lead to a solution, then remove it - path[pos] = -1; - } - } - - // If no vertex can be added to Hamiltonian Cycle constructed so far, - // then return false - return 0; -} - -/** This function solves the Hamiltonian cycle problem using Backtracking. - * It mainly uses hamiltonian_cycle_util() to solve the problem. - * It returns false if there is no Hamiltonian Cycle possible, - * otherwise return true and prints the path. - */ -int hamiltonian_cycle(int graph[N][N]) { - int path[N]; - for (int i = 0; i < N; i++) path[i] = -1; - - // Let us put vertex 0 as the first vertex in the path. - // If there is a Hamiltonian cycle, - // then the path can be started from any point - // of the cycle as the graph is undirected - path[0] = 0; - if (hamiltonian_cycle_util(graph, path, 1) == 0) { - printf("Solution does not exist\n"); - printf("\n"); - return 0; - } - - print_solution(path); - return 1; -} - -/** Main function */ -int main() { - /* Let us create the following graph - (0)--(1)--(2) - | / \ | - | / \ | - | / \ | - (3)-------(4) */ - int graph1[N][N] = { - {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}, - }; - - // Print the solution - printf("Test Case 1:\n"); - hamiltonian_cycle(graph1); - - /* Let us create the following graph - (0)--(1)--(2) - | / \ | - | / \ | - | / \ | - (3) (4) */ - int graph2[N][N] = { - {0, 1, 0, 1, 0}, {1, 0, 1, 1, 1}, {0, 1, 0, 0, 1}, - {1, 1, 0, 0, 0}, {0, 1, 1, 0, 0}, - }; - - // Print the solution - printf("Test Case 2:\n"); - hamiltonian_cycle(graph2); - - return 0; -} diff --git a/solutions/03-tree-detection.c b/solutions/03-tree-detection.c deleted file mode 100644 index 149f7dd..0000000 --- a/solutions/03-tree-detection.c +++ /dev/null @@ -1,92 +0,0 @@ -#include - -/** Number of vertices in the graph */ -#define N 5 - -/** Search for a cycle in a graph - * @param `graph`: adjacency matrix of the graph - * @param `v`: current vertex - * @param `visited`: array of visited vertices - * @param `parent`: parent of the current vertex - * @return `1` if a cycle is found, `0` otherwise - */ -int search_cycle(int graph[N][N], int v, int *visited, int parent) { - visited[v] = 1; - for (int i = 0; i < N; ++i) { - if (graph[v][i]) { - if (!visited[i]) { - if (search_cycle(graph, i, visited, v)) return 1; - } else if (i != parent) { // cycle detected - return 1; - } - } - } - return 0; -} - -/** Check if all vertices have been visited - * @param `visited`: array of visited vertices - * @return `1` if all vertices have been visited, `0` otherwise - */ -int all_visited(int *visited) { - int i; - // Find first unvisited vertex - for (i = 0; i < N && visited[i]; ++i) - ; - return i == N; // Return true if no unvisited vertices found, false otherwise -} - -/** Check if a graph is a tree - * @param `graph`: adjacency matrix of the graph - * @return `1` if the graph is a tree, `0` otherwise - */ -int is_tree(int graph[N][N]) { - int visited[N] = {0}; - int has_cycle; - - // Perform depth-first search from vertex 0 to check if all vertices - // are reachable (connected) - has_cycle = search_cycle(graph, 0, visited, -1); - if (has_cycle) return 0; - - // Check if all vertices have been visited - if (!all_visited(visited)) return 0; - - // Return true if no cycle is found and all - return 1; -} - -/** Main function */ -int main() { - // Tree example - /* - (0)--(1)--(2) - | - | - | - (3)-------(4) - */ - int graph1[N][N] = {{0, 1, 0, 1, 0}, - {1, 0, 1, 0, 0}, - {0, 1, 0, 0, 0}, - {1, 0, 0, 0, 1}, - {0, 0, 0, 1, 0}}; - printf("Test Case 1: Is Tree? %d\n", is_tree(graph1)); - - /* - // Non-tree example (has at least one cycle) - (0)--(1)--(2) - | / \ | - | / \ | - | / \ | - (3)-------(4) - */ - int graph2[N][N] = {{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}}; - printf("Test Case 2: Is Tree? %d\n", is_tree(graph2)); - - return 0; -} diff --git a/solutions/04-subset_sum.c b/solutions/04-subset_sum.c deleted file mode 100644 index 9d51e66..0000000 --- a/solutions/04-subset_sum.c +++ /dev/null @@ -1,40 +0,0 @@ -#include - -/** Function that verifies if there is a subset that sums to the target - * @param set[] The set of numbers - * @param n The size of the set - * @param target The target sum - * @return 1 if there is a subset that sums to the target, 0 otherwise - */ -int subset_sum(int set[], int n, int target) { - // Iterate through all possible subsets - for (int i = 0; i < (1 << n); i++) { - int sum = 0; - for (int j = 0; j < n; j++) { - // Verify if the j-th element is in the subset - if (i & (1 << j)) { - sum += set[j]; - } - } - // If the subset sums to the target, return 1 - if (sum == target) { - return 1; - } - } - return 0; -} - -/** Main function */ -int main() { - int set[] = {1, 2, 3, 4, 5}; - int n = sizeof(set) / sizeof(set[0]); - int target = 9; - - if (subset_sum(set, n, target)) { - printf("Yes, there is a subset that sums to %d\n", target); - } else { - printf("No, there isn't a subset that sums to %d\n", target); - } - - return 0; -} diff --git a/solutions/05-knapsack.c b/solutions/05-knapsack.c deleted file mode 100644 index c2dab45..0000000 --- a/solutions/05-knapsack.c +++ /dev/null @@ -1,58 +0,0 @@ -#include - -/** - * Function to find the maximum of two integers. - * - * @param a The first integer. - * @param b The second integer. - * @return The maximum of the two integers. - */ -int max(int a, int b) { return (a > b) ? a : b; } - -/** - * Recursive function to solve the 0/1 Knapsack problem using brute force. - * - * @param W The remaining capacity of the knapsack. - * @param weights[] Array containing the weights of the items. - * @param values[] Array containing the values of the items. - * @param n The number of items remaining to consider. - * @return The maximum value that can be obtained with the given capacity. - */ -int knapsack(int W, int weights[], int values[], int n) { - // Base case: no items left or no capacity left - if (n == 0 || W == 0) { - return 0; - } - - // If the weight of the nth item is more than the remaining capacity, it - // cannot be included - if (weights[n - 1] > W) { - return knapsack(W, weights, values, n - 1); - } else { - // Return the maximum of two cases: - // 1. nth item included - // 2. nth item not included - return max( - values[n - 1] + knapsack(W - weights[n - 1], weights, values, n - 1), - knapsack(W, weights, values, n - 1)); - } -} - -/** - * Main function to demonstrate the brute force solution for the knapsack - * problem. - */ -int main() { - int values[] = {60, 100, 120}; // Values of the items - int weights[] = {10, 20, 30}; // Weights of the items - int W = 50; // Maximum capacity of the knapsack - int n = sizeof(values) / sizeof(values[0]); // Number of items - - // Calculate the maximum value that can be carried in the knapsack - int max_value = knapsack(W, weights, values, n); - - // Print the result - printf("Maximum value that can be carried: %d\n", max_value); - - return 0; -}