Skip to content

Commit 80fd053

Browse files
committed
2 parents 8cf7415 + f9fe9e5 commit 80fd053

File tree

17 files changed

+482
-0
lines changed

17 files changed

+482
-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
6.02 KB
Loading
5.79 KB
Loading
5.96 KB
Loading
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Friends Pairing Problem using Dynamic Programming
2+
Language used : **Python 3**
3+
4+
## 🎯 Aim
5+
The aim of this script is to find out the the total number of ways in which friends can remain single or can be paired up..
6+
7+
## 👉 Purpose
8+
The main purpose of this script is to show the implementation of Dynamic Programming to find the total number of ways in which friends can remain single or can be paired up.
9+
10+
## 📄 Description
11+
Given n friends, each one can remain single or can be paired up with some other friend. Each friend can be paired only once. **Find out the total number of ways in which friends can remain single or can be paired up.**
12+
13+
Examples:
14+
```
15+
Input : n = 3
16+
Output : 4
17+
18+
Input : n = 4
19+
Output : 10
20+
21+
Input : n = 6
22+
Output : 76
23+
```
24+
25+
## 📈 Workflow of the script
26+
- `countFriendsPairing` - This is the main function which provides the number of ways friends can e single.
27+
- `main` - This is the driver code for this code/script.
28+
29+
## 📃 Explanation
30+
```
31+
{1}, {2}, {3} : all single
32+
{1}, {2, 3} : 2 and 3 paired but 1 is single.
33+
{1, 2}, {3} : 1 and 2 are paired but 3 is single.
34+
{1, 3}, {2} : 1 and 3 are paired but 2 is single.
35+
Note that {1, 2} and {2, 1} are considered same.
36+
```
37+
- **Mathematical Explanation**
38+
```
39+
The problem is simplified version of how many ways we can divide n elements into multiple groups.
40+
(here group size will be max of 2 elements).
41+
In case of n = 3, we have only 2 ways to make a group:
42+
1) all elements are individual(1,1,1)
43+
2) a pair and individual (2,1)
44+
In case of n = 4, we have 3 ways to form a group:
45+
1) all elements are individual (1,1,1,1)
46+
2) 2 individuals and one pair (2,1,1)
47+
3) 2 separate pairs (2,2)
48+
```
49+
50+
## 🧮 Algorithm
51+
```
52+
f(n) = ways n people can remain single
53+
or pair up.
54+
55+
For n-th person there are two choices:
56+
1) n-th person remains single, we recur
57+
for f(n - 1)
58+
2) n-th person pairs up with any of the
59+
remaining n - 1 persons. We get (n - 1) * f(n - 2)
60+
61+
Therefore we can recursively write f(n) as:
62+
f(n) = f(n - 1) + (n - 1) * f(n - 2)
63+
```
64+
65+
## 💻 Input and Output
66+
- **Test Case 1 :**
67+
68+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Friends%20Pairing%20Problem/Images/friend2.PNG)
69+
70+
- **Test Case 2 :**
71+
72+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Friends%20Pairing%20Problem/Images/friend1.PNG)
73+
74+
- **Test Case 3 :**
75+
76+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Friends%20Pairing%20Problem/Images/friend3.PNG)
77+
78+
## ⏰ Time and Space complexity
79+
- **Time Complexity:** `O(n)`.
80+
- **Space Complexity:** `O(n)`.
81+
82+
---------------------------------------------------------------
83+
## 🖋️ Author
84+
**Code contributed by, _Abhishek Sharma_, 2021 [@abhisheks008](github.com/abhisheks008)**
85+
86+
[![forthebadge made-with-python](http://ForTheBadge.com/images/badges/made-with-python.svg)](https://www.python.org/)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Problem Statement : Given n friends, each one can
2+
# remain single or can be paired up
3+
# with some other friend. Each friend
4+
# can be paired only once. Find out the
5+
# total number of ways in which friends
6+
# can remain single or can be paired up.
7+
8+
# -----------------------------------------------------------------
9+
10+
# Method : Dynamic Programming
11+
# Abhishek Sharma. 2021
12+
13+
# -----------------------------------------------------------------
14+
15+
# Python program solution of friends pairing problem
16+
17+
# Returns count of ways n people can remain single or paired up.
18+
19+
# -----------------------------------------------------------------
20+
21+
# This is the main function which provides the number of ways
22+
# friends can e single.
23+
24+
def countFriendsPairings(n):
25+
26+
dp = [0 for i in range(n + 1)]
27+
28+
# Filling dp[] in bottom-up manner using
29+
# recursive formula explained above.
30+
31+
for i in range(n + 1):
32+
33+
if(i <= 2):
34+
dp[i] = i
35+
else:
36+
dp[i] = dp[i - 1] + (i - 1) * dp[i - 2]
37+
38+
return dp[n]
39+
40+
# Driver code
41+
42+
print ("- Friends Pairing Problem using Dynamic Programming - ")
43+
print ("------------------------------------------------------")
44+
n = int(input("Enter the number : "))
45+
print ("------------------------------------------------------")
46+
print ()
47+
print ("Output :")
48+
print("Number of ways people can remain single or, paired up : ",countFriendsPairings(n))
49+
50+
51+
# -----------------------------------------------------------------
52+
# Input given :
53+
# - Friends Pairing Problem using Dynamic Programming -
54+
# ------------------------------------------------------
55+
# Enter the number : 3
56+
# ------------------------------------------------------
57+
58+
# Output :
59+
# Number of ways people can remain single or, paired up : 4
60+
61+
# ------------------------------------------------------
62+
63+
# Code contributed by, Abhishek Sharma, 2021

0 commit comments

Comments
 (0)