Skip to content

Commit d066399

Browse files
Merge branch 'master' into directory-update
2 parents 7d9f6b3 + 85d62bb commit d066399

File tree

3 files changed

+400
-60
lines changed

3 files changed

+400
-60
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/**
2+
* @file
3+
* @brief Implementation of the Unbounded 0/1 Knapsack Problem
4+
*
5+
* @details
6+
* The Unbounded 0/1 Knapsack problem allows taking unlimited quantities of each item.
7+
* The goal is to maximize the total value without exceeding the given knapsack capacity.
8+
* Unlike the 0/1 knapsack, where each item can be taken only once, in this variation,
9+
* any item can be picked any number of times as long as the total weight stays within
10+
* the knapsack's capacity.
11+
*
12+
* Given a set of N items, each with a weight and a value, represented by the arrays
13+
* `wt` and `val` respectively, and a knapsack with a weight limit W, the task is to
14+
* fill the knapsack to maximize the total value.
15+
*
16+
* @note weight and value of items is greater than zero
17+
*
18+
* ### Algorithm
19+
* The approach uses dynamic programming to build a solution iteratively.
20+
* A 2D array is used for memoization to store intermediate results, allowing
21+
* the function to avoid redundant calculations.
22+
*
23+
* @author [Sanskruti Yeole](https://github.com/yeolesanskruti)
24+
* @see dynamic_programming/0_1_knapsack.cpp
25+
*/
26+
27+
#include <iostream> // Standard input-output stream
28+
#include <vector> // Standard library for using dynamic arrays (vectors)
29+
#include <cassert> // For using assert function to validate test cases
30+
#include <cstdint> // For fixed-width integer types like std::uint16_t
31+
32+
/**
33+
* @namespace dynamic_programming
34+
* @brief Namespace for dynamic programming algorithms
35+
*/
36+
namespace dynamic_programming {
37+
38+
/**
39+
* @namespace Knapsack
40+
* @brief Implementation of unbounded 0-1 knapsack problem
41+
*/
42+
namespace unbounded_knapsack {
43+
44+
/**
45+
* @brief Recursive function to calculate the maximum value obtainable using
46+
* an unbounded knapsack approach.
47+
*
48+
* @param i Current index in the value and weight vectors.
49+
* @param W Remaining capacity of the knapsack.
50+
* @param val Vector of values corresponding to the items.
51+
* @note "val" data type can be changed according to the size of the input.
52+
* @param wt Vector of weights corresponding to the items.
53+
* @note "wt" data type can be changed according to the size of the input.
54+
* @param dp 2D vector for memoization to avoid redundant calculations.
55+
* @return The maximum value that can be obtained for the given index and capacity.
56+
*/
57+
std::uint16_t KnapSackFilling(std::uint16_t i, std::uint16_t W,
58+
const std::vector<std::uint16_t>& val,
59+
const std::vector<std::uint16_t>& wt,
60+
std::vector<std::vector<int>>& dp) {
61+
if (i == 0) {
62+
if (wt[0] <= W) {
63+
return (W / wt[0]) * val[0]; // Take as many of the first item as possible
64+
} else {
65+
return 0; // Can't take the first item
66+
}
67+
}
68+
if (dp[i][W] != -1) return dp[i][W]; // Return result if available
69+
70+
int nottake = KnapSackFilling(i - 1, W, val, wt, dp); // Value without taking item i
71+
int take = 0;
72+
if (W >= wt[i]) {
73+
take = val[i] + KnapSackFilling(i, W - wt[i], val, wt, dp); // Value taking item i
74+
}
75+
return dp[i][W] = std::max(take, nottake); // Store and return the maximum value
76+
}
77+
78+
/**
79+
* @brief Wrapper function to initiate the unbounded knapsack calculation.
80+
*
81+
* @param N Number of items.
82+
* @param W Maximum weight capacity of the knapsack.
83+
* @param val Vector of values corresponding to the items.
84+
* @param wt Vector of weights corresponding to the items.
85+
* @return The maximum value that can be obtained for the given capacity.
86+
*/
87+
std::uint16_t unboundedKnapsack(std::uint16_t N, std::uint16_t W,
88+
const std::vector<std::uint16_t>& val,
89+
const std::vector<std::uint16_t>& wt) {
90+
if(N==0)return 0; // Expect 0 since no items
91+
std::vector<std::vector<int>> dp(N, std::vector<int>(W + 1, -1)); // Initialize memoization table
92+
return KnapSackFilling(N - 1, W, val, wt, dp); // Start the calculation
93+
}
94+
95+
} // unbounded_knapsack
96+
97+
} // dynamic_programming
98+
99+
/**
100+
* @brief self test implementation
101+
* @return void
102+
*/
103+
static void tests() {
104+
// Test Case 1
105+
std::uint16_t N1 = 4; // Number of items
106+
std::vector<std::uint16_t> wt1 = {1, 3, 4, 5}; // Weights of the items
107+
std::vector<std::uint16_t> val1 = {6, 1, 7, 7}; // Values of the items
108+
std::uint16_t W1 = 8; // Maximum capacity of the knapsack
109+
// Test the function and assert the expected output
110+
assert(unboundedKnapsack(N1, W1, val1, wt1) == 48);
111+
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N1, W1, val1, wt1) << std::endl;
112+
113+
// Test Case 2
114+
std::uint16_t N2 = 3; // Number of items
115+
std::vector<std::uint16_t> wt2 = {10, 20, 30}; // Weights of the items
116+
std::vector<std::uint16_t> val2 = {60, 100, 120}; // Values of the items
117+
std::uint16_t W2 = 5; // Maximum capacity of the knapsack
118+
// Test the function and assert the expected output
119+
assert(unboundedKnapsack(N2, W2, val2, wt2) == 0);
120+
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N2, W2, val2, wt2) << std::endl;
121+
122+
// Test Case 3
123+
std::uint16_t N3 = 3; // Number of items
124+
std::vector<std::uint16_t> wt3 = {2, 4, 6}; // Weights of the items
125+
std::vector<std::uint16_t> val3 = {5, 11, 13};// Values of the items
126+
std::uint16_t W3 = 27;// Maximum capacity of the knapsack
127+
// Test the function and assert the expected output
128+
assert(unboundedKnapsack(N3, W3, val3, wt3) == 27);
129+
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N3, W3, val3, wt3) << std::endl;
130+
131+
// Test Case 4
132+
std::uint16_t N4 = 0; // Number of items
133+
std::vector<std::uint16_t> wt4 = {}; // Weights of the items
134+
std::vector<std::uint16_t> val4 = {}; // Values of the items
135+
std::uint16_t W4 = 10; // Maximum capacity of the knapsack
136+
assert(unboundedKnapsack(N4, W4, val4, wt4) == 0);
137+
std::cout << "Maximum Knapsack value for empty arrays: " << unboundedKnapsack(N4, W4, val4, wt4) << std::endl;
138+
139+
std::cout << "All test cases passed!" << std::endl;
140+
141+
}
142+
143+
/**
144+
* @brief main function
145+
* @return 0 on successful exit
146+
*/
147+
int main() {
148+
tests(); // Run self test implementation
149+
return 0;
150+
}
151+

graph/topological_sort.cpp

Lines changed: 177 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,189 @@
1-
#include <algorithm>
2-
#include <iostream>
3-
#include <vector>
4-
5-
int number_of_vertices,
6-
number_of_edges; // For number of Vertices (V) and number of edges (E)
7-
std::vector<std::vector<int>> graph;
8-
std::vector<bool> visited;
9-
std::vector<int> topological_order;
10-
11-
void dfs(int v) {
12-
visited[v] = true;
13-
for (int u : graph[v]) {
14-
if (!visited[u]) {
15-
dfs(u);
1+
/**
2+
* @file
3+
* @brief [Topological Sort
4+
* Algorithm](https://en.wikipedia.org/wiki/Topological_sorting)
5+
* @details
6+
* Topological sorting of a directed graph is a linear ordering or its vertices
7+
* such that for every directed edge (u,v) from vertex u to vertex v, u comes
8+
* before v in the oredering.
9+
*
10+
* A topological sort is possible only in a directed acyclic graph (DAG).
11+
* This file contains code of finding topological sort using Kahn's Algorithm
12+
* which involves using Depth First Search technique
13+
*/
14+
15+
#include <algorithm> // For std::reverse
16+
#include <cassert> // For assert
17+
#include <iostream> // For IO operations
18+
#include <stack> // For std::stack
19+
#include <stdexcept> // For std::invalid_argument
20+
#include <vector> // For std::vector
21+
22+
/**
23+
* @namespace graph
24+
* @brief Graph algorithms
25+
*/
26+
namespace graph {
27+
28+
/**
29+
* @namespace topological_sort
30+
* @brief Topological Sort Algorithm
31+
*/
32+
namespace topological_sort {
33+
/**
34+
* @class Graph
35+
* @brief Class that represents a directed graph and provides methods for
36+
* manipulating the graph
37+
*/
38+
class Graph {
39+
private:
40+
int n; // Number of nodes
41+
std::vector<std::vector<int>> adj; // Adjacency list representation
42+
43+
public:
44+
/**
45+
* @brief Constructor for the Graph class
46+
* @param nodes Number of nodes in the graph
47+
*/
48+
Graph(int nodes) : n(nodes), adj(nodes) {}
49+
50+
/**
51+
* @brief Function that adds an edge between two nodes or vertices of graph
52+
* @param u Start node of the edge
53+
* @param v End node of the edge
54+
*/
55+
void addEdge(int u, int v) { adj[u].push_back(v); }
56+
57+
/**
58+
* @brief Get the adjacency list of the graph
59+
* @returns A reference to the adjacency list
60+
*/
61+
const std::vector<std::vector<int>>& getAdjacencyList() const {
62+
return adj;
63+
}
64+
65+
/**
66+
* @brief Get the number of nodes in the graph
67+
* @returns The number of nodes
68+
*/
69+
int getNumNodes() const { return n; }
70+
};
71+
72+
/**
73+
* @brief Function to perform Depth First Search on the graph
74+
* @param v Starting vertex for depth-first search
75+
* @param visited Array representing whether each node has been visited
76+
* @param graph Adjacency list of the graph
77+
* @param s Stack containing the vertices for topological sorting
78+
*/
79+
void dfs(int v, std::vector<int>& visited,
80+
const std::vector<std::vector<int>>& graph, std::stack<int>& s) {
81+
visited[v] = 1;
82+
for (int neighbour : graph[v]) {
83+
if (!visited[neighbour]) {
84+
dfs(neighbour, visited, graph, s);
1685
}
1786
}
18-
topological_order.push_back(v);
87+
s.push(v);
1988
}
2089

21-
void topological_sort() {
22-
visited.assign(number_of_vertices, false);
23-
topological_order.clear();
24-
for (int i = 0; i < number_of_vertices; ++i) {
90+
/**
91+
* @brief Function to get the topological sort of the graph
92+
* @param g Graph object
93+
* @returns A vector containing the topological order of nodes
94+
*/
95+
std::vector<int> topologicalSort(const Graph& g) {
96+
int n = g.getNumNodes();
97+
const auto& adj = g.getAdjacencyList();
98+
std::vector<int> visited(n, 0);
99+
std::stack<int> s;
100+
101+
for (int i = 0; i < n; i++) {
25102
if (!visited[i]) {
26-
dfs(i);
103+
dfs(i, visited, adj, s);
27104
}
28105
}
29-
reverse(topological_order.begin(), topological_order.end());
106+
107+
std::vector<int> ans;
108+
while (!s.empty()) {
109+
int elem = s.top();
110+
s.pop();
111+
ans.push_back(elem);
112+
}
113+
114+
if (ans.size() < n) { // Cycle detected
115+
throw std::invalid_argument("cycle detected in graph");
116+
}
117+
return ans;
30118
}
31-
int main() {
32-
std::cout
33-
<< "Enter the number of vertices and the number of directed edges\n";
34-
std::cin >> number_of_vertices >> number_of_edges;
35-
int x = 0, y = 0;
36-
graph.resize(number_of_vertices, std::vector<int>());
37-
for (int i = 0; i < number_of_edges; ++i) {
38-
std::cin >> x >> y;
39-
x--, y--; // to convert 1-indexed to 0-indexed
40-
graph[x].push_back(y);
41-
}
42-
topological_sort();
43-
std::cout << "Topological Order : \n";
44-
for (int v : topological_order) {
45-
std::cout << v + 1
46-
<< ' '; // converting zero based indexing back to one based.
119+
} // namespace topological_sort
120+
} // namespace graph
121+
122+
/**
123+
* @brief Self-test implementation
124+
* @returns void
125+
*/
126+
static void test() {
127+
// Test 1
128+
std::cout << "Testing for graph 1\n";
129+
int n_1 = 6;
130+
graph::topological_sort::Graph graph1(n_1);
131+
graph1.addEdge(4, 0);
132+
graph1.addEdge(5, 0);
133+
graph1.addEdge(5, 2);
134+
graph1.addEdge(2, 3);
135+
graph1.addEdge(3, 1);
136+
graph1.addEdge(4, 1);
137+
std::vector<int> ans_1 = graph::topological_sort::topologicalSort(graph1);
138+
std::vector<int> expected_1 = {5, 4, 2, 3, 1, 0};
139+
std::cout << "Topological Sorting Order: ";
140+
for (int i : ans_1) {
141+
std::cout << i << " ";
142+
}
143+
std::cout << '\n';
144+
assert(ans_1 == expected_1);
145+
std::cout << "Test Passed\n\n";
146+
147+
// Test 2
148+
std::cout << "Testing for graph 2\n";
149+
int n_2 = 5;
150+
graph::topological_sort::Graph graph2(n_2);
151+
graph2.addEdge(0, 1);
152+
graph2.addEdge(0, 2);
153+
graph2.addEdge(1, 2);
154+
graph2.addEdge(2, 3);
155+
graph2.addEdge(1, 3);
156+
graph2.addEdge(2, 4);
157+
std::vector<int> ans_2 = graph::topological_sort::topologicalSort(graph2);
158+
std::vector<int> expected_2 = {0, 1, 2, 4, 3};
159+
std::cout << "Topological Sorting Order: ";
160+
for (int i : ans_2) {
161+
std::cout << i << " ";
47162
}
48163
std::cout << '\n';
164+
assert(ans_2 == expected_2);
165+
std::cout << "Test Passed\n\n";
166+
167+
// Test 3 - Graph with cycle
168+
std::cout << "Testing for graph 3\n";
169+
int n_3 = 3;
170+
graph::topological_sort::Graph graph3(n_3);
171+
graph3.addEdge(0, 1);
172+
graph3.addEdge(1, 2);
173+
graph3.addEdge(2, 0);
174+
try {
175+
graph::topological_sort::topologicalSort(graph3);
176+
} catch (std::invalid_argument& err) {
177+
assert(std::string(err.what()) == "cycle detected in graph");
178+
}
179+
std::cout << "Test Passed\n";
180+
}
181+
182+
/**
183+
* @brief Main function
184+
* @returns 0 on exit
185+
*/
186+
int main() {
187+
test(); // run self test implementations
49188
return 0;
50189
}

0 commit comments

Comments
 (0)