Skip to content

Commit cc995ad

Browse files
Merge pull request #175 from abhisheks008/main
Bell Numbers Problem using Dynamic Programming
2 parents 818550f + 20a25b1 commit cc995ad

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed
13.5 KB
Loading
9.04 KB
Loading
19.1 KB
Loading
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Bell Numbers Problem using Dynamic Programming
2+
Language used : **Python 3**
3+
4+
## 🎯 Aim
5+
The aim of this script is to find out the nth Bell numbers using Dynamic Programming method..
6+
7+
## 👉 Purpose
8+
The main purpose of this script is to show the implementation of Dynamic Programming to find out nth Bell Numbers.
9+
10+
## 📄 Description
11+
- **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)`.
12+
- Given a set of n elements, find number of ways of partitioning it.
13+
Examples:
14+
```
15+
Input: n = 2
16+
Output: Number of ways = 2
17+
Explanation: Let the set be {1, 2}
18+
{ {1}, {2} }
19+
{ {1, 2} }
20+
```
21+
## 📈 Workflow of the script
22+
- `bellNumber` -- This is the working function which will check and return the nth bell numbers one after another using the Dynamic Programming method.
23+
- `main` - This is the driver program for this script.
24+
25+
## 📃 Explanation
26+
When we add a (n+1)’th element to k partitions, there are two possibilities.
27+
1) It is added as a single element set to existing partitions, i.e, S(n, k-1)
28+
2) It is added to all sets of every partition, i.e., k*S(n, k)
29+
S(n, k) is called Stirling numbers of the second kind
30+
First few Bell numbers are 1, 1, 2, 5, 15, 52, 203, ….
31+
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).
32+
A Better Method is to use Bell Triangle. Below is a sample Bell Triangle for first few Bell Numbers.
33+
```
34+
1
35+
1 2
36+
2 3 5
37+
5 7 10 15
38+
15 20 27 37 52
39+
```
40+
41+
## 🧮 Algorithm
42+
The triangle is constructed using below formula.
43+
```
44+
// If this is first column of current row 'i'
45+
If j == 0
46+
// Then copy last entry of previous row
47+
// Note that i'th row has i entries
48+
Bell(i, j) = Bell(i-1, i-1)
49+
50+
// If this is not first column of current row
51+
Else
52+
// Then this element is sum of previous element
53+
// in current row and the element just above the
54+
// previous element
55+
Bell(i, j) = Bell(i-1, j-1) + Bell(i, j-1)
56+
```
57+
58+
**Interpretation :**
59+
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.
60+
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:
61+
```
62+
{1}, {2, 4}, {3}
63+
{1, 4}, {2}, {3}
64+
{1, 2, 4}, {3}.
65+
```
66+
## 💻 Input and Output
67+
- **Test Case 1 :**
68+
69+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Bell%20Numbers/Images/bell1.png)
70+
71+
- **Test Case 2 :**
72+
73+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Bell%20Numbers/Images/bell2.png)
74+
75+
- **Test Case 3 :**
76+
77+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Bell%20Numbers/Images/bell3.png)
78+
79+
80+
## ⏰ Time and Space complexity
81+
- **Time Complexity:** `O(n^2)`.
82+
83+
---------------------------------------------------------------
84+
## 🖋️ Author
85+
**Code contributed by, _Abhishek Sharma_, 2021 [@abhisheks008](github.com/abhisheks008)**
86+
87+
[![forthebadge made-with-python](http://ForTheBadge.com/images/badges/made-with-python.svg)](https://www.python.org/)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# A Python program to find n'th Bell number
2+
# Approach : Dynamic Programming
3+
# Abhishek S, 2021
4+
5+
# ----------------------------------------------------------
6+
7+
# Problem statement : Given a set of n elements, find the
8+
# number of ways of partitioning it.
9+
10+
# ----------------------------------------------------------
11+
12+
# Solution : Used the Dynamic Approach to solve the problem.
13+
14+
# This is the working function which will check and return the
15+
# nth bell numbers one after another using the Dynamic Programming
16+
# method
17+
18+
def bellNumber(n):
19+
20+
bell = [[0 for i in range(n+1)] for j in range(n+1)]
21+
bell[0][0] = 1
22+
for i in range(1, n+1):
23+
24+
# Explicitly fill for j = 0
25+
bell[i][0] = bell[i-1][i-1]
26+
27+
# Fill for remaining values of j
28+
for j in range(1, i+1):
29+
bell[i][j] = bell[i-1][j-1] + bell[i][j-1]
30+
31+
return bell[n][0]
32+
33+
# Driver program
34+
print ("- Bell Numbers using Dynamic Programming Approach -")
35+
print ("---------------------------------------------------")
36+
print ()
37+
z = int(input("Enter the number of numbers you wanna print : "))
38+
print ()
39+
print ("---------------------------------------------------")
40+
print ()
41+
print ("Output : ")
42+
print ()
43+
for n in range(z):
44+
print('Bell Number', n, 'is', bellNumber(n))
45+
46+
47+
48+
# ----------------------------------------------------------
49+
50+
# Enter the number of numbers you wanna print : 6
51+
52+
# ----------------------------------------------------------
53+
54+
# Output :
55+
56+
# Bell Number 0 is 1
57+
# Bell Number 1 is 1
58+
# Bell Number 2 is 2
59+
# Bell Number 3 is 5
60+
# Bell Number 4 is 15
61+
# Bell Number 5 is 52
62+
63+
# ----------------------------------------------------------
64+
65+
# Code contributed by, Abhishek Sharma, 2021

0 commit comments

Comments
 (0)