Skip to content

Commit 9b93d05

Browse files
authored
Merge branch 'prathimacode-hub:main' into main
2 parents 4520d84 + 0ca0e0f commit 9b93d05

File tree

26 files changed

+1034
-0
lines changed

26 files changed

+1034
-0
lines changed
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Nth Catalan Numbers using Dynamic Programming
2+
Language used : **Python 3**
3+
4+
## 🎯 Aim
5+
The aim of this script is to find out the n'th catalan numbers using Dynamic Programming.
6+
7+
## 👉 Purpose
8+
The main purpose of this script is to show the implementation of Dynamic programming on finding out the n'th catalan numbers.
9+
10+
## 📄 Description
11+
Catalan numbers are defined as a mathematical sequence that consists of positive integers, which can be used to find the number of possibilities of various combinations.
12+
13+
**Examples:**
14+
```
15+
Input : k = 12
16+
Output : 1 1 2 5 14 42 132 429 1430 4862 16796 58786
17+
18+
Input : k = 10
19+
Output : 1 1 2 5 14 42 132 429 1430 4862
20+
21+
Input : k = 8
22+
Output : 1 1 2 5 14 42 132 429
23+
```
24+
25+
## 📈 Workflow of the script
26+
- `catalan` - The main function of the script to find out the catalan numbers using Dynamic Approach.
27+
- `main` - This is the driver code for this code/script.
28+
- `k` - User given integer which signifies the upper range of the Catalan series.
29+
30+
## 🧮 Algorithm
31+
Let's see how it works,
32+
- Create an array `catalan[]` for storing `ith` Catalan number.
33+
- Initialize, `catalan[0]` and `catalan[1]` = 1
34+
- Loop through `i = 2` to the given Catalan number `n`.
35+
- Loop throught `j = 0` to `j < i` and Keep adding value of `catalan[j] * catalan[i – j – 1]` into `catalan[i]`.
36+
- Finally, `return catalan[n]`.
37+
38+
## 💻 Input and Output
39+
- **Test Case 1 :**
40+
41+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Nth%20Catalan%20Numbers/Images/catalan-1.png)
42+
43+
- **Test Case 2 :**
44+
45+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Nth%20Catalan%20Numbers/Images/catalan-2.png)
46+
47+
- **Test Case 3 :**
48+
49+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Nth%20Catalan%20Numbers/Images/catalan-3.png)
50+
51+
52+
## ⏰ Time and Space complexity
53+
- **Time Complexity:** `O(n^2)`.
54+
- **Space Complexity:** `O(n)`.
55+
56+
---------------------------------------------------------------
57+
## 🖋️ Author
58+
**Code contributed by, _Abhishek Sharma_, 2022 [@abhisheks008](github.com/abhisheks008)**
59+
60+
[![forthebadge made-with-python](http://ForTheBadge.com/images/badges/made-with-python.svg)](https://www.python.org/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Problem Name: Nth Catalan Numbers
2+
# Approach: Dynamic Programming
3+
4+
# -----------------------------------------------------------------------------------
5+
6+
# Problem Statement: Catalan numbers are defined as a mathematical sequence
7+
# that consists of positive integers, which can be used to
8+
# find the number of possibilities of various combinations.
9+
# Find out the catalan numbers in between the range from 0 to N.
10+
11+
12+
# -----------------------------------------------------------------------------------
13+
14+
# Constraints:
15+
# n -> Integer taken as the range of the Catalan Numbers.
16+
# catalan[] -> Array of Catalan Numbers from 0 to n.
17+
18+
# -----------------------------------------------------------------------------------
19+
20+
# A dynamic programming based function to find nth
21+
# Catalan number
22+
23+
def catalan(n):
24+
if (n == 0 or n == 1):
25+
return 1
26+
27+
# Table to store results of subproblems
28+
catalan = [0]*(n+1)
29+
30+
# Initialize first two values in table
31+
catalan[0] = 1
32+
catalan[1] = 1
33+
34+
# Fill entries in catalan[]
35+
# using recursive formula
36+
for i in range(2, n + 1):
37+
for j in range(i):
38+
catalan[i] += catalan[j] * catalan[i-j-1]
39+
40+
# Return last entry
41+
return catalan[n]
42+
43+
44+
# Driver Code
45+
print ("-- Nth Catalan Numbers using Dynamic Programming --")
46+
print ()
47+
print ("Enter the upper range : ")
48+
k = int(input())
49+
print ()
50+
print ("Printing Nth Catalan Numbers from 0 to {0}...".format(k))
51+
print ()
52+
print ("Here you go!")
53+
for i in range(k):
54+
print(catalan(i), end=" ")
55+
56+
57+
# -----------------------------------------------------------------------------------
58+
59+
# Output:
60+
# -- Nth Catalan Numbers using Dynamic Programming --
61+
62+
# Enter the upper range :
63+
# 12
64+
65+
# Printing Nth Catalan Numbers from 0 to 12...
66+
67+
# Here you go!
68+
# 1 1 2 5 14 42 132 429 1430 4862 16796 58786
69+
70+
# -----------------------------------------------------------------------------------
71+
72+
# Code Contributed by, Abhishek Sharma, 2022
73+
74+
# -----------------------------------------------------------------------------------
16.9 KB
Loading
23.1 KB
Loading

Graphs/Breadth First Search/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Overview:
2+
Two techniques are frequently used for graph traversal: These techniques are called depth-first search (DFS) and breadth-first search (BFS), although we will just talk about BFS. Breadth-first search (BFS) is used to solve many problems, including finding the shortest path in a graph or solving puzzle games such as Rubik’s Cubes.
3+
4+
## What is Breadth First Search in Python?
5+
Breadth-first search (BFS) is an algorithm for traversing graphs or trees. Breath-first search for trees and graphs are almost the same. BFS uses a queue data structure for finding the shortest path in a graph. We use queue to get the next vertex to start the search, and the visited array is used to mark all the nodes that have been visited, so when it appears again, we will perform BFS on that node.
6+
7+
## Algorithm of DFS in Python
8+
The algorithm works as follows:
9+
10+
* Create a queue and insert any one vertex of the graph at the back of the queue.
11+
* Initialize a visited array and mark that vertex as visited.
12+
* Follow the below process till the queue becomes empty:
13+
* Remove a front vertex from the queue.
14+
* Get all the adjacent vertices of the removed vertex.
15+
* If the adjacent vertex has not been visited, mark it as visited and insert it at the back of the queue.
16+
17+
## Time & Space Complexity
18+
* Time complexity is `O(V+E)`, where V denotes the number of vertices and E denotes the number of edges.
19+
* Space complexity is `O(V)`.
20+
21+
## Applications of Breadth First Search
22+
* Crawlers in Search Engines
23+
* Peer-to-Peer Networking
24+
* GPS Navigation Systems
25+
* Ford-Fulkerson algorithm
26+
27+
## Input & Output
28+
### Input:
29+
30+
<img width=50% src="../Breadth First Search/Images/input.jpg">
31+
32+
```python
33+
graph = {
34+
'A': ['B', 'C', 'D'],
35+
'B': ['A'],
36+
'C': ['A', 'D'],
37+
'D': ['A', 'C', 'E'],
38+
'E': ['D']
39+
}
40+
```
41+
### Output:
42+
<img width=50% src="../Breadth First Search/Images/output.jpg">
43+
44+
```python
45+
Breadth-first Search: A B C D E
46+
```

Graphs/Breadth First Search/bfs.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Create a graph given in the above diagram.
2+
graph = {
3+
'A': ['B', 'C', 'D'],
4+
'B': ['A'],
5+
'C': ['A', 'D'],
6+
'D': ['A', 'C', 'E'],
7+
'E': ['D']
8+
}
9+
10+
# to print a BFS of a graph
11+
def bfs(node):
12+
13+
# mark vertices as False means not visited
14+
visited = [False] * (len(graph))
15+
16+
# make a empty queue for bfs
17+
queue = []
18+
19+
# mark given node as visited and add it to the queue
20+
visited.append(node)
21+
queue.append(node)
22+
23+
while queue:
24+
# Remove the front vertex or the vertex at 0th index from the queue and print that vertex.
25+
v = queue.pop(0)
26+
print(v, end=" ")
27+
28+
for neigh in graph[v]:
29+
if neigh not in visited:
30+
visited.append(neigh)
31+
queue.append(neigh)
32+
33+
34+
# Driver Code
35+
if __name__ == "__main__":
36+
bfs('A')

Greedy/Huffman Coding/Images/hc-1.png

20.7 KB
Loading

Greedy/Huffman Coding/Images/hc-2.png

17.7 KB
Loading

0 commit comments

Comments
 (0)