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
import heapq | |
def dijkstra(graph, start): | |
pq = [(0, start)] | |
heapq.heapify(pq) | |
dist = {node: float('inf') for node in graph} | |
dist[start] = 0 | |
prev = {node: None for node in graph} |
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
from collections import deque | |
from typing import Dict, List | |
def topological_sort_dfs(nodes: List[int], edges: Dict[int, List[int]]): | |
visited = set() | |
stack = [] | |
on_path = set() # To detect cycles | |
def dfs(node): |
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
class TrieNode: | |
def __init__(self): | |
self.children = {} | |
self.is_end_of_word = False | |
class Trie: | |
def __init__(self): | |
self.root = TrieNode() | |
def insert(self, word): |
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
class UnionFind: | |
def __init__(self): | |
self.parent = {} | |
self.rank = {} | |
def add(self, x): | |
""" | |
Adds a new element to the Union-Find structure. | |
Initializes the element as its own parent (singleton set). | |
""" |
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
# class TreeNode: | |
# def __init__(self, val=0, left=None, right=None): | |
# self.val = val | |
# self.left = left | |
# self.right = right | |
from collections import deque | |
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
# class TreeNode: | |
# def __init__(self, val=0, left=None, right=None): | |
# self.val = val | |
# self.left = left | |
# self.right = right | |
def iterative_pre_order_traversal(root: TreeNode): | |
stack = [root] | |
while stack: |
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
# class TreeNode: | |
# def __init__(self, val=0, left=None, right=None): | |
# self.val = val | |
# self.left = left | |
# self.right = right | |
def recursive_inorder_traversal(root: TreeNode): | |
if not root: | |
return |