Skip to content

Instantly share code, notes, and snippets.

View GEEGABYTE1's full-sized avatar
🍵
Drinking Passionfruit and Hibiscus Tea

Jaival Patel GEEGABYTE1

🍵
Drinking Passionfruit and Hibiscus Tea
View GitHub Profile
@GEEGABYTE1
GEEGABYTE1 / dfs.py
Created July 21, 2021 03:03
Max-Depth Solution
from tree import TreeNode
def dfs(root_node, goal, path=(), layers_lst=[], root=None):
if root == None:
root = root_node
path = path + (root_node,)
current_level_lst=[]
for i in path:
if not i.value in current_level_lst:
@GEEGABYTE1
GEEGABYTE1 / bfs.py
Created July 21, 2021 03:43
Max-Depth Solution
from Tree import Tree
from collections import deque
def bfs(root, count=0):
path_queue = deque()
initial_path = [root]
path_queue.appendleft(initial_path)
while path_queue:
if count == 0 or len(path_queue) == 2:
@GEEGABYTE1
GEEGABYTE1 / graph_search.py
Created August 2, 2021 16:03
Graph Search Algorithms
def dfs(graph, current_vertex, target, visited=None):
if visited == None:
visited = []
visited.append(current_vertex)
if current_vertex == target:
return visited
else:
for neighbour in graph[current_vertex]:
if not neighbour in visited:
path = dfs(graph, neighbour, target, visited)
@GEEGABYTE1
GEEGABYTE1 / Traversal.py
Created August 2, 2021 16:59
Graph Search Traversal Algorithms
def bfs(graph, start_vertex, target):
path = [start_vertex]
vertex_and_path = [start_vertex, path]
bfs_queue = [vertex_and_path]
visited = set()
paths = {}
counter = 0
while bfs_queue:
print('-'*24)
@GEEGABYTE1
GEEGABYTE1 / test.py
Created August 2, 2021 17:18
Depth-First and BFS traversal
## Our graph has been converted into a dictionary in the form of {vertex: [edge1, edge2, edge3, ..., edge n]} ##
### BFS Traversal ###
first_element = list(test_graph.graph_dict.keys())[0]
last_element = list(test_graph.graph_dict.keys())[-1]
lst = list(test_graph.graph_dict[first_element].edges.keys())
for i in range(len(lst)):
first_edge = lst[0]
@GEEGABYTE1
GEEGABYTE1 / eulerian.py
Created August 13, 2021 03:12
Eulerian Path and Cycle Traversal Algorithm
from dfs import dfs
from inputs import g1, g2
paths = []
def eulerian(*args):
graphs = args
for graph in graphs:
print("-"*24 + "\n")
@GEEGABYTE1
GEEGABYTE1 / dfs.py
Last active August 13, 2021 14:55
DFS algorithm for Eulerian Path
def dfs(graph, current_vertex, edges, last_edge, last_vertex, prev_vertex=None, visited=None):
edges_vertices = (list(graph.values()))
lst_values = []
for lst in edges_vertices:
lst_values.append(len(lst))
biggest_lst = max(lst_values)
biggest_edge = None
@GEEGABYTE1
GEEGABYTE1 / travelingsalesman.py
Created August 13, 2021 16:10
Traveling Salesman Algorithm
def visit_checker(graph):
for status in list(graph.values()):
if status == "unvisited":
return False
else:
continue
return True
def traveling_salesperson(graph):
@GEEGABYTE1
GEEGABYTE1 / script.py
Created August 30, 2021 20:53
Sieve of Eratosthenes - Base Implementation
def sieve_of_eratosthenes(limit):
true_indices = []
array = [i for i in range(2, limit + 1)]
dictionary = {}
for number in array:
dictionary[number] = True
for key, value in dictionary.items():
if value == False:
continue
@GEEGABYTE1
GEEGABYTE1 / optimized.py
Created August 30, 2021 21:12
Optimization of Sieve of Eratosthenes
import math
def sieve_of_eratosthenes (limit):
if (limit <= 1):
return []
output = [True] * (limit+1)
output[0] = False