Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find the distinct subsets from a given array by backtracking using C#?
The distinct subsets problem involves finding all unique combinations of a specific size from a given array using backtracking. This technique systematically explores all possible combinations by adding elements to the current subset and then removing them to explore other possibilities.
When we specify a target size, the algorithm generates all combinations that contain exactly that many elements. For example, with array [1,2,3] and target size 2, we get combinations "1,2", "1,3", and "2,3".
How Backtracking Works for Subsets
The backtracking algorithm follows these steps −
Choose − Add an element to the current subset
Explore − Recursively find combinations with remaining elements
Unchoose − Remove the element and try the next possibility
Example
Here's a complete implementation that finds all distinct subsets of size 2 from array [1,2,3] −
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication {
public class BackTracking {
public void Subsets(int[] array) {
List<int> currentList = new List<int>();
List<string> results = new List<string>();
BackTrackingCombination(array, 2, 0, currentList, results);
Console.WriteLine("All distinct subsets of size 2:");
foreach (var item in results) {
Console.WriteLine(item);
}
}
public void BackTrackingCombination(int[] array, int size, int startIndex,
List<int> currentList, List<string> results) {
if (currentList.Count == size) {
StringBuilder s = new StringBuilder();
foreach (var item in currentList) {
s.Append(item);
}
results.Add(s.ToString());
return;
}
for (int i = startIndex; i < array.Length; i++) {
currentList.Add(array[i]);
BackTrackingCombination(array, size, i + 1, currentList, results);
currentList.Remove(array[i]);
}
}
}
class Program {
static void Main(string[] args) {
BackTracking b = new BackTracking();
int[] arrs = { 1, 2, 3 };
b.Subsets(arrs);
}
}
}
The output of the above code is −
All distinct subsets of size 2: 12 13 23
Finding All Possible Subsets
To find all subsets (not just a specific size), we can modify the approach −
using System;
using System.Collections.Generic;
namespace ConsoleApplication {
public class AllSubsets {
public void FindAllSubsets(int[] array) {
List<List<int>> results = new List<List<int>>();
List<int> currentSubset = new List<int>();
GenerateSubsets(array, 0, currentSubset, results);
Console.WriteLine("All distinct subsets:");
foreach (var subset in results) {
Console.Write("[");
for (int i = 0; i < subset.Count; i++) {
Console.Write(subset[i]);
if (i < subset.Count - 1) Console.Write(",");
}
Console.WriteLine("]");
}
}
private void GenerateSubsets(int[] array, int index, List<int> currentSubset,
List<List<int>> results) {
results.Add(new List<int>(currentSubset));
for (int i = index; i < array.Length; i++) {
currentSubset.Add(array[i]);
GenerateSubsets(array, i + 1, currentSubset, results);
currentSubset.RemoveAt(currentSubset.Count - 1);
}
}
}
class Program {
static void Main(string[] args) {
AllSubsets generator = new AllSubsets();
int[] arrs = { 1, 2, 3 };
generator.FindAllSubsets(arrs);
}
}
}
The output of the above code is −
All distinct subsets: [] [1] [1,2] [1,2,3] [1,3] [2] [2,3] [3]
Key Points
Time Complexity − O(2^n) where n is the array length, as each element can be included or excluded
Space Complexity − O(n) for the recursion stack depth
The
startIndexparameter ensures we don't create duplicate combinationsBacktracking explores all possibilities systematically by making choices and undoing them
Conclusion
Backtracking provides an elegant solution for finding distinct subsets by systematically exploring all combinations. The technique uses recursion to build subsets incrementally, ensuring no duplicates are generated by controlling the starting index for each recursive call.
