Skip to content

Commit 20a25b1

Browse files
authored
Create README.md
1 parent 70d6056 commit 20a25b1

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
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/)

0 commit comments

Comments
 (0)