Skip to content

Instantly share code, notes, and snippets.

@jrebrown70
jrebrown70 / flask_setup
Last active July 31, 2020 13:04
[Flask Setup] Setting up of flask. #flask #flasksetup
#From shell
virtualenv flaskenv -p python3
ls flaskenv
ls flaskenv/bin/
#Activate virtual environment
. flaskenv/bin/activate
pip install Flask
pip install flask-wtf
pip install email_validator
@jrebrown70
jrebrown70 / topological_sort.py
Created July 6, 2020 09:00
[Topological Search] General graph topological sort. #topologicalsort #graphtheory
def topological_sort(graph):
queue = Queue()
indegree_map = {}
for i in range(graph.num_vertices):
indegree_map[i] = graph.get_indegree(i)
#Add root node to the queue
if indegree_map[i] == 0:
@jrebrown70
jrebrown70 / breadth_first_search.py
Created July 6, 2020 08:59
[Breadth First Search] General graph breadth first search. #bfs #graphtheory
def breadth_first(graph, start=0):
queue = Queue()
queue.put(start)
#Return an array of shape (graph.num_vertices, ), boolean values initialised as false
visited = np.full((graph.num_vertices, ), False, dtype = bool)
while not queue.empty():
vertex = queue.get()
@jrebrown70
jrebrown70 / depth_first_search.py
Created July 6, 2020 08:57
[Depth First Search] General graph implementation of depth first search. #dfs #graphtheory
def depth_first(graph,visited,current = 0):
if visited[current] :
return
visited[current] = True
print("Visited: ",current)
for vertex in graph.get_adjacent_vertices(current):
@jrebrown70
jrebrown70 / graph.py
Last active July 6, 2020 06:29
[Graph] Graph representation #graphtheory #graph #adjacencyrepresentation #python #adjacencymatrix
import abc
import numpy as np
class Graph(abc.ABC):
def __init__(self, num_vertices, directed = False):
self.num_vertices = num_vertices
self.directed = directed
@abc.abstractmethod
@jrebrown70
jrebrown70 / bst_depth_first_search.py
Last active July 6, 2020 05:58
[BST Depth First Search] Implementations of depth first search on a binary search tree. #dfs #bst #depthfirstsearch #graphtheory #python
#Pre-Order Depth First Search
def pre_order(node):
path = []
if node:
path.append(node.data)
path = path + pre_order(node.get_left_child())
path = path + pre_order(node.get_right_child())
@jrebrown70
jrebrown70 / bst_breadth_first_search.py
Last active July 6, 2020 05:57
[BST Breadth First Search] Implementation of a Breadth First Search on a Binary Search Tree #bst #bfs #breadthfirstsearch #python #graph #graphtheory
#Breadth First Search
class MyQueue:
def __init__(self):
"""Creat a new queue"""
self.items = []
def is_empty(self):
@jrebrown70
jrebrown70 / bst_implementation.py
Last active July 6, 2020 03:36
[Binary Search Tree] Implementation of a binary search tree #bst #graphtheory #python
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
def get_left_child(self):
return self.left