Repository navigation
Expand file tree
/
Copy pathpc.cpp
More file actions
171 lines (144 loc) · 5.03 KB
/
Copy pathpc.cpp
File metadata and controls
171 lines (144 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <vector>
/*
Problem: Print All Combinations (Subsets) of a Set
Given a set (vector) of integers, print all possible combinations (subsets) of its elements.
Example:
Input: {1, 2, 3}
Output:
1 2 3
1 2
1 3
1
2 3
2
3
(empty line for empty set)
Approach:
- Use recursion and backtracking.
- At each index, choose to include or exclude the current element.
- Base case: when idx == input.size(), print the current subset.
Time Complexity: O(2^n * n) (2^n subsets, each up to n elements)
Space Complexity: O(n) (call stack and current subset)
*/
/**
* @brief Recursively finds and prints all subsequences of a vector of integers.
*
* @param input The input vector of integers (read-only).
* @param currentSubsequence The subsequence being built in the current recursive path.
* @param index The current index in the input vector to consider.
*/
void printAllSubsequences(const std::vector<int> &input, std::vector<int> ¤tSubsequence, int index)
{
// Base case: If we have considered all elements, print the current subsequence.
if (index == input.size())
{
for (int num : currentSubsequence)
{
std::cout << num << " ";
}
std::cout << "\n";
return;
}
// Decision 1: Include the current element in the subsequence.
currentSubsequence.push_back(input[index]);
printAllSubsequences(input, currentSubsequence, index + 1);
// Backtrack: Remove the current element to explore the other path.
currentSubsequence.pop_back();
// Decision 2: Do not include the current element in the subsequence.
printAllSubsequences(input, currentSubsequence, index + 1);
}
/**
* @brief Iteratively finds and prints all subsequences using bit manipulation.
*
* @param nums The input vector of integers.
*/
void printAllSubsequencesIterative(const std::vector<int> &nums)
{
int n = nums.size();
// There are 2^n possible subsequences. We iterate from 0 to 2^n - 1.
// (1 << n) is a fast way to compute 2^n.
for (int i = 0; i < (1 << n); ++i)
{
// For each number 'i', we check its bits to form a subsequence.
for (int j = 0; j < n; ++j)
{
// If the j-th bit is set in 'i', include nums[j] in the subsequence.
// (i >> j) & 1 checks if the j-th bit is 1.
if ((i >> j) & 1)
{
std::cout << nums[j] << " ";
}
}
std::cout << "\n";
}
}
/**
* @brief Recursively finds and prints all combinations (subsets) of a vector of integers.
*
* @param input The input vector of integers (read-only).
* @param currentCombination The combination being built in the current recursive path.
* @param index The current index in the input vector to consider.
*/
void printAllComb(const std::vector<int> &input, std::vector<int> ¤tCombination, int index)
{
// Base case: If we have considered all elements, print the current combination.
if (index == input.size())
{
for (int num : currentCombination)
{
std::cout << num << " ";
}
std::cout << "\n";
return;
}
// Include the current element in the combination.
currentCombination.push_back(input[index]);
printAllComb(input, currentCombination, index + 1);
// Backtrack: Remove the current element to explore the other path.
currentCombination.pop_back();
// Exclude the current element from the combination.
printAllComb(input, currentCombination, index + 1);
}
int main()
{
std::cout << "Printing all subsequences (subsets) of {1, 2, 3}:\n";
std::vector<int> nums{1, 2, 3};
std::vector<int> emptySubset;
// printAllSubsequences(nums, emptySubset, 0); // Recursive way
printAllSubsequencesIterative(nums); // Iterative way
std::cout << "\nTest 1: {1,2,3}" << std::endl;
std::vector<int> A{1, 2, 3};
std::vector<int> b;
printAllSubsequences(A, b, 0);
std::cout << "\nTest 2: {5}" << std::endl;
std::vector<int> C{5};
b.clear();
printAllSubsequences(C, b, 0);
std::cout << "\nTest 3: {7,8}" << std::endl;
std::vector<int> D{7, 8};
b.clear();
printAllSubsequences(D, b, 0);
std::cout << "\nTest 4: {} (empty input)" << std::endl;
std::vector<int> E;
b.clear();
printAllSubsequences(E, b, 0);
std::cout << "\nTest 5: {1,2,3,4}" << std::endl;
std::vector<int> F{1, 2, 3, 4};
b.clear();
printAllSubsequences(F, b, 0);
std::cout << "\nPrinting all combinations (subsets) of {1, 2, 3}:\n";
// printAllComb(nums, emptySubset, 0); // Recursive way
// Iterative way not provided for combination due to complexity
std::cout << "\nTest 1: {1,2,3}" << std::endl;
printAllComb(A, b, 0);
std::cout << "\nTest 2: {5}" << std::endl;
printAllComb(C, b, 0);
std::cout << "\nTest 3: {7,8}" << std::endl;
printAllComb(D, b, 0);
std::cout << "\nTest 4: {} (empty input)" << std::endl;
printAllComb(E, b, 0);
std::cout << "\nTest 5: {1,2,3,4}" << std::endl;
printAllComb(F, b, 0);
return 0;
}