This file contains hidden or 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
def bellman_ford(n, edges, src): | |
dist = [float("inf")] * n | |
dist[src] = 0 | |
for _ in range(n - 1): | |
for u, v, weight in edges: | |
if dist[u] != float("inf") and dist[u] + weight < dist[v]: | |
dist[v] = dist[u] + weight | |
for u, v, weight in edges: |
This file contains hidden or 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 iterative_minimum_spanning_tree(adj, start_node=0): | |
mst = [] | |
visited = set() | |
min_heap = [(0, start_node, -1)] # (weight, current_node, parent_node) | |
while min_heap: | |
weight, node, parent = heapq.heappop(min_heap) |
This file contains hidden or 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
def recursive_eulerian_path(adj, start_node): | |
temp_adj = {v: list(adj[v]) for v in adj} | |
def dfs(start): | |
stack = [start] | |
path = [] | |
while stack: | |
while temp_adj[stack[-1]]: | |
next_node = temp_adj[stack[-1]].pop() | |
stack.append(next_node) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |