-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/codebasics/data-structure…
…s-algorithms-python into master
- Loading branch information
Showing
21 changed files
with
213 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,3 +127,6 @@ dmypy.json | |
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# Editor settings | ||
.vscode/settings.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# function for depth first search | ||
def dfs(data, start, visited=set()): | ||
|
||
# if not visited print it | ||
if start not in visited: | ||
print(start, end=" ") | ||
|
||
visited.add(start) | ||
|
||
for i in data[start] - visited: | ||
|
||
# if not visited gi in depth of it | ||
dfs(data, i, visited) | ||
return | ||
|
||
|
||
# sample data in dictionary form | ||
data = { | ||
'A': {'B'}, | ||
'B': {'A', 'C', 'D'}, | ||
'C': {'B', 'E'}, | ||
'D': {'B', 'E'}, | ||
'E': {'C', 'D', 'F'}, | ||
'F': {'E'} | ||
} | ||
|
||
|
||
if __name__ == '__main__': | ||
|
||
dfs(data, 'A') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# function for depth first search | ||
def find_employees(data, start, employee, visited=set()): | ||
# if not visited print it | ||
if start not in visited: | ||
print(start, end=" ") | ||
if start == employee: | ||
print(":", end=" ") | ||
visited.add(start) | ||
|
||
for i in data[start] - visited: | ||
# if not visited go in depth of it | ||
find_employees(data, i, visited) | ||
return | ||
|
||
|
||
# sample data in dictionary form | ||
data = { | ||
"karan": {"darshan", "nikhil"}, | ||
"darshan": {"khantil", "tanuj"}, | ||
"tanuj": {"nikhil"}, | ||
"krinish": {"hetul"}, | ||
"khantil": set(), | ||
"nikhil": set() | ||
} | ||
|
||
|
||
if __name__ == '__main__': | ||
|
||
find_employees(data, "karan", "karan") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# DFS Exercise | ||
|
||
![Overview : Employee hierarchy](https://github.com/beladiyadarshan/DFS/blob/main/emp.png?raw=true) | ||
|
||
Given a graph containing managers and their employees as a dictionary of sets, print all employees reporting to a given manager. | ||
|
||
``` | ||
data = { | ||
"karan": {"darshan","nikhil"}, | ||
"darshan": {"khantil", "tanuj"}, | ||
"tanuj": {"nikhil"}, | ||
"krinish": {"hetul"}, | ||
"khantil" : set(), | ||
"nikhil" : set() | ||
} | ||
``` | ||
|
||
Example: Darshan and Nikhil are reporting to Karan. Khantil and Tanuj are reporting to Darshan. | ||
|
||
``` | ||
Q. Find all employees who are reporting to Karan. | ||
``` | ||
|
||
**Explanation:** | ||
|
||
-So here, we want to find all the children nodes of Karan. | ||
|
||
-We will perform DFS starting on Karan and then traverse all the children of Karan which are unvisited. | ||
|
||
**Output:** | ||
|
||
karan : nikhil darshan tanuj khantil | ||
|
||
[Solution](https://github.com/beladiyadarshan/DFS/blob/main/dfs_exercise.py) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
def bfs(data, start, visited=set()): | ||
|
||
queue = [start] | ||
|
||
while queue: | ||
current_node = queue.pop(0) | ||
if current_node not in visited: | ||
print(current_node, end=" ") | ||
visited.add(current_node) | ||
|
||
for i in data[current_node] - visited: | ||
queue.append(i) | ||
return | ||
|
||
|
||
if __name__ == '__main__': | ||
|
||
data = { | ||
'A': {'B'}, 'B': {'A', 'C', 'D'}, 'C': {'B', 'E'}, 'D': {'B', 'E'}, | ||
'E': {'C', 'D', 'F'}, 'F': {'E'} | ||
} | ||
|
||
bfs(data, 'A') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# BFS_Traversal | ||
BFS Traversal of a graph in python using graph given in dictionary | ||
|
||
Implement a function to find whether a path exists for a given set of airline routes. The routes are depicted using graphs as a dictionary of sets, where keys represent as source and elements of sets represent destinations. Print the path if it exists. | ||
|
||
Example: A, B, C, D, E, F are the nodes of the graph. | ||
|
||
|
||
For example, if you are given following data: | ||
|
||
``` | ||
data = { | ||
'A': {'B'}, | ||
'B': {'A', 'C', 'D'}, | ||
'C': {'B', 'E'}, | ||
'D': {'B', 'E'}, | ||
'E': {'C', 'D', 'F'}, | ||
'F': {'E'} | ||
} | ||
``` | ||
|
||
The resultant graph will be: | ||
|
||
![alt text](https://github.com/nikhilailani/data-structures-algorithms-python/blob/master/algorithms/9_BreadthFirstSearch/DFS_BFS_Graph.png) | ||
|
||
Example: Source is A, Destination is D. | ||
|
||
Expected Output: | ||
|
||
``` | ||
Path : A->B->D | ||
``` | ||
|
||
[Solution](https://github.com/nikhilailani/data-structures-algorithms-python/blob/master/algorithms/9_BreadthFirstSearch/bfs_exercise_solution.py) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
def bfs(data, start, end, visited=[]): | ||
queue = [start] | ||
|
||
while queue: | ||
current_node = queue.pop(0) | ||
if current_node==end: | ||
print("Path: " + "->".join(visited) + "->" + end) | ||
return | ||
visited.append(current_node) | ||
|
||
for i in data[current_node] - set(visited): | ||
queue.append(i) | ||
print("Path does not exist!") | ||
return | ||
|
||
|
||
if __name__ == '__main__': | ||
data = { | ||
'A': {'B'}, | ||
'B': {'C', 'D'}, | ||
'C': {'E'}, | ||
'D': {'E'}, | ||
'E': {'F'}, | ||
'F': set() | ||
} | ||
bfs(data, 'A', 'D') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 7 additions & 7 deletions
14
data_structures/4_HashTable_2_Collisions/4_hash_table_exercise.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
# Exercise: Hash Table | ||
|
||
1. [nyc_weather.csv](https://github.com/codebasics/py/blob/master/DataStructures/4_HashTable_2_Collisions/Solution/nyc_weather.csv) contains new york city weather for first few days in the month of January. Write a program that can answer following, | ||
1. [nyc_weather.csv](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/4_HashTable_2_Collisions/Solution/nyc_weather.csv) contains new york city weather for first few days in the month of January. Write a program that can answer following, | ||
1. What was the average temperature in first week of Jan | ||
1. What was the maximum temperature in first 10 days of Jan | ||
|
||
Figure out data structure that is best for this problem | ||
|
||
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/4_HashTable_2_Collisions/Solution/weather_analysis.ipynb) | ||
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/4_HashTable_2_Collisions/Solution/weather_analysis.ipynb) | ||
|
||
2. [nyc_weather.csv](https://github.com/codebasics/py/blob/master/DataStructures/4_HashTable_2_Collisions/Solution/nyc_weather.csv) contains new york city weather for first few days in the month of January. Write a program that can answer following, | ||
2. [nyc_weather.csv](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/4_HashTable_2_Collisions/Solution/nyc_weather.csv) contains new york city weather for first few days in the month of January. Write a program that can answer following, | ||
1. What was the temperature on Jan 9? | ||
1. What was the temperature on Jan 4? | ||
|
||
Figure out data structure that is best for this problem | ||
|
||
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/4_HashTable_2_Collisions/Solution/weather_analysis.ipynb) | ||
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/4_HashTable_2_Collisions/Solution/weather_analysis.ipynb) | ||
|
||
3. [poem.txt](https://github.com/codebasics/py/blob/master/DataStructures/4_HashTable_2_Collisions/Solution/poem.txt) Contains famous poem "Road not taken" by poet Robert Frost. You have to read this file in python and print every word and its count as show below. Think about the best data structure that you can use to solve this problem and figure out why you selected that specific data structure. | ||
3. [poem.txt](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/4_HashTable_2_Collisions/Solution/poem.txt) Contains famous poem "Road not taken" by poet Robert Frost. You have to read this file in python and print every word and its count as show below. Think about the best data structure that you can use to solve this problem and figure out why you selected that specific data structure. | ||
``` | ||
'diverged': 2, | ||
'in': 3, | ||
'I': 8 | ||
``` | ||
|
||
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/4_HashTable_2_Collisions/Solution/exercise_poem_find_word_occurances.ipynb) | ||
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/4_HashTable_2_Collisions/Solution/exercise_poem_find_word_occurances.ipynb) | ||
|
||
4. Implement hash table where collisions are handled using linear probing. We learnt about linear probing in the video tutorial. Take the hash table implementation that uses chaining and modify methods to use **linear probing**. Keep MAX size of arr in hashtable as 10. | ||
|
||
[Solution](https://github.com/codebasics/py/blob/master/DataStructures/4_HashTable_2_Collisions/Solution/exercise_hash_table_linear_probing.ipynb) | ||
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/4_HashTable_2_Collisions/Solution/exercise_hash_table_linear_probing.ipynb) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.