Skip to content

Instantly share code, notes, and snippets.

@nhudinhtuan
nhudinhtuan / trie.py
Created April 3, 2020 13:14
Trie Data Structure
class TrieNode(object):
def __init__(self):
self.children = {}
self.is_word = False # mark the end of a word
class Trie(object):
def __init__(self):
from collections import deque
# graph: List[List[int]]
# s: start vertex
def bfs(graph, s):
# set is used to mark visited vertices
visited = set()
# create a queue for BFS
queue = deque()
# graph is represented by adjacency list: List[List[int]]
# s: start vertex
def dfs(graph, s):
# set is used to mark visited vertices
visited = set()
def recur(current_vertex):
print(current_vertex)
# mark it visited
# graph is represented by adjacency list: List[List[int]]
# s: start vertex
def dfs_iterating(graph, s):
# set is used to mark visited vertices
visited = set()
# create a stack for DFS
stack = []
# Push vertex s to the stack
@nhudinhtuan
nhudinhtuan / is_cyclic_directed_graph.py
Created April 6, 2020 02:19
Detect cycle in directed graph
# graph is represented by adjacency list: List[List[int]]
# DFS to detect cyclic
def is_cyclic_directed_graph(graph):
# set is used to mark visited vertices
visited = set()
# set is used to keep track the ancestor vertices in recursive stack.
ancestors = set()
def is_cyclic_recur(current_vertex):
# mark it visited
@nhudinhtuan
nhudinhtuan / is_cyclic_undirected_graph.py
Created April 6, 2020 02:24
Detect cycle in undirected graph
# graph is represented by adjacency list: List[List[int]]
# DFS to detect cyclic
def is_cyclic_undirected_graph(graph):
# set is used to mark visited vertices
visited = set()
def is_cyclic_recur(current_vertex, parent):
# mark it visited
visited.add(current_vertex)
@nhudinhtuan
nhudinhtuan / orange_rotting.py
Created April 6, 2020 02:40
Oranges Rotting
from collections import deque
class Solution(object):
def orangesRotting(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
# corner cases
@nhudinhtuan
nhudinhtuan / topological_sort.py
Created April 6, 2020 12:35
Topological sort
# graph is represented by adjacency list: List[List[int]]
# using DFS to find the topological sorting
def topological_sort(graph):
# using a stack to keep topological sorting
stack = []
# set is used to mark visited vertices
visited = set()
def recur(current_vertex):
@nhudinhtuan
nhudinhtuan / find_shortest_path_unweight.py
Created April 6, 2020 13:53
Find the shortest path in an unweighted graph
from collections import deque
# graph is represented by adjacency list: List[List[int]]
# s: start vertex
# d: destination vertex
# based on BFS
def find_shortest_path(graph, s, d):
# pred[i] stores predecessor of i in the path
pred = [-1] * len(graph)
# set is used to mark visited vertices
visited = set()
@nhudinhtuan
nhudinhtuan / dijkstra.py
Created April 7, 2020 03:57
Dijkstra algorithm
import heapq
# graph is represented by adjacency list: List[List[pair]]
# s is the source vertex
def dijkstra(graph, s):
# set is used to mark finalized vertices
visited = set()
# an array to keep the distance from s to this vertex.
# initialize all distances as infinite, except s
dist = [float('inf')] * len(graph)
dist[s] = 0