diff --git a/Dynamic Programming/Bell Numbers/Images/bell1.png b/Dynamic Programming/Bell Numbers/Images/bell1.png new file mode 100644 index 00000000..74b817bc Binary files /dev/null and b/Dynamic Programming/Bell Numbers/Images/bell1.png differ diff --git a/Dynamic Programming/Bell Numbers/Images/bell2.png b/Dynamic Programming/Bell Numbers/Images/bell2.png new file mode 100644 index 00000000..4c47b4b1 Binary files /dev/null and b/Dynamic Programming/Bell Numbers/Images/bell2.png differ diff --git a/Dynamic Programming/Bell Numbers/Images/bell3.png b/Dynamic Programming/Bell Numbers/Images/bell3.png new file mode 100644 index 00000000..42f49e5b Binary files /dev/null and b/Dynamic Programming/Bell Numbers/Images/bell3.png differ diff --git a/Dynamic Programming/Bell Numbers/README.md b/Dynamic Programming/Bell Numbers/README.md new file mode 100644 index 00000000..a95ba9d3 --- /dev/null +++ b/Dynamic Programming/Bell Numbers/README.md @@ -0,0 +1,87 @@ +# Bell Numbers Problem using Dynamic Programming +Language used : **Python 3** + +## ๐ŸŽฏ Aim +The aim of this script is to find out the nth Bell numbers using Dynamic Programming method.. + +## ๐Ÿ‘‰ Purpose +The main purpose of this script is to show the implementation of Dynamic Programming to find out nth Bell Numbers. + +## ๐Ÿ“„ Description +- **What is Bell Number ?** -- Let `S(n, k)` be total number of partitions of n elements into k sets. The value of nโ€™th Bell Number is sum of `S(n, k)` for `k = 1 to n`. Value of `S(n, k)` can be defined recursively as, `S(n+1, k) = k*S(n, k) + S(n, k-1)`. +- Given a set of n elements, find number of ways of partitioning it. +Examples: +``` +Input: n = 2 +Output: Number of ways = 2 +Explanation: Let the set be {1, 2} + { {1}, {2} } + { {1, 2} } +``` +## ๐Ÿ“ˆ Workflow of the script +- `bellNumber` -- This is the working function which will check and return the nth bell numbers one after another using the Dynamic Programming method. +- `main` - This is the driver program for this script. + +## ๐Ÿ“ƒ Explanation +When we add a (n+1)โ€™th element to k partitions, there are two possibilities. +1) It is added as a single element set to existing partitions, i.e, S(n, k-1) +2) It is added to all sets of every partition, i.e., k*S(n, k) +S(n, k) is called Stirling numbers of the second kind +First few Bell numbers are 1, 1, 2, 5, 15, 52, 203, โ€ฆ. +A Simple Method to compute nโ€™th Bell Number is to one by one compute S(n, k) for k = 1 to n and return sum of all computed values. Refer this for computation of S(n, k). +A Better Method is to use Bell Triangle. Below is a sample Bell Triangle for first few Bell Numbers. +``` +1 +1 2 +2 3 5 +5 7 10 15 +15 20 27 37 52 +``` + +## ๐Ÿงฎ Algorithm +The triangle is constructed using below formula. +``` +// If this is first column of current row 'i' +If j == 0 + // Then copy last entry of previous row + // Note that i'th row has i entries + Bell(i, j) = Bell(i-1, i-1) + +// If this is not first column of current row +Else + // Then this element is sum of previous element + // in current row and the element just above the + // previous element + Bell(i, j) = Bell(i-1, j-1) + Bell(i, j-1) +``` + +**Interpretation :** +Then Bell(n, k) counts the number of partitions of the set {1, 2, โ€ฆ, n + 1} in which the element k + 1 is the largest element that can be alone in its set. +For example, Bell(3, 2) is 3, it is count of number of partitions of {1, 2, 3, 4} in which 3 is the largest singleton element. There are three such partitions: +``` + {1}, {2, 4}, {3} + {1, 4}, {2}, {3} + {1, 2, 4}, {3}. +``` +## ๐Ÿ’ป Input and Output +- **Test Case 1 :** + +![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Bell%20Numbers/Images/bell1.png) + +- **Test Case 2 :** + +![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Bell%20Numbers/Images/bell2.png) + +- **Test Case 3 :** + +![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Bell%20Numbers/Images/bell3.png) + + +## โฐ Time and Space complexity +- **Time Complexity:** `O(n^2)`. + +--------------------------------------------------------------- +## ๐Ÿ–‹๏ธ Author +**Code contributed by, _Abhishek Sharma_, 2021 [@abhisheks008](github.com/abhisheks008)** + +[![forthebadge made-with-python](http://ForTheBadge.com/images/badges/made-with-python.svg)](https://www.python.org/) diff --git a/Dynamic Programming/Bell Numbers/bell_numbers.py b/Dynamic Programming/Bell Numbers/bell_numbers.py new file mode 100644 index 00000000..9356c0f6 --- /dev/null +++ b/Dynamic Programming/Bell Numbers/bell_numbers.py @@ -0,0 +1,65 @@ +# A Python program to find n'th Bell number +# Approach : Dynamic Programming +# Abhishek S, 2021 + +# ---------------------------------------------------------- + +# Problem statement : Given a set of n elements, find the +# number of ways of partitioning it. + +# ---------------------------------------------------------- + +# Solution : Used the Dynamic Approach to solve the problem. + +# This is the working function which will check and return the +# nth bell numbers one after another using the Dynamic Programming +# method + +def bellNumber(n): + + bell = [[0 for i in range(n+1)] for j in range(n+1)] + bell[0][0] = 1 + for i in range(1, n+1): + + # Explicitly fill for j = 0 + bell[i][0] = bell[i-1][i-1] + + # Fill for remaining values of j + for j in range(1, i+1): + bell[i][j] = bell[i-1][j-1] + bell[i][j-1] + + return bell[n][0] + +# Driver program +print ("- Bell Numbers using Dynamic Programming Approach -") +print ("---------------------------------------------------") +print () +z = int(input("Enter the number of numbers you wanna print : ")) +print () +print ("---------------------------------------------------") +print () +print ("Output : ") +print () +for n in range(z): + print('Bell Number', n, 'is', bellNumber(n)) + + + +# ---------------------------------------------------------- + +# Enter the number of numbers you wanna print : 6 + +# ---------------------------------------------------------- + +# Output : + +# Bell Number 0 is 1 +# Bell Number 1 is 1 +# Bell Number 2 is 2 +# Bell Number 3 is 5 +# Bell Number 4 is 15 +# Bell Number 5 is 52 + +# ---------------------------------------------------------- + +# Code contributed by, Abhishek Sharma, 2021