Skip to content

Instantly share code, notes, and snippets.

@anchitarnav
anchitarnav / bridges.py
Created August 22, 2021 11:46
Find bridges in a Undirected Graph using Tarjan's aglo python implementation
from typing import List
from collections import defaultdict
from math import inf
from enum import Enum, auto
class EdgeTypes(Enum):
CROSS_EDGE = auto()
BACK_EDGE = auto()
TREE_EDGE = auto()
@anchitarnav
anchitarnav / tarjans_algo.py
Last active August 22, 2021 08:24
Tarjan’s Algo for Finding Articulation Points in a Graph and to find strongly connected components
# Tarjan's Algo for Strongly Connected Components (SCC)
# Find Articulation Points using tarjan's algo
# Python implementation
from collections import defaultdict
from math import inf
from enum import Enum, auto
from typing import Tuple, List, Set
@anchitarnav
anchitarnav / tarjans_algo_scc.py
Created August 22, 2021 06:03
Tarjan's Algorithm for Stringly Connected Components - Python Implementation
# Tarjan's Algo for Strongly Connected Components (SCC)
# Python implementation
from collections import defaultdict
from math import inf
from enum import Enum, auto
from typing import Tuple, List, Set
# Rules:
@anchitarnav
anchitarnav / travelling_salesman.py
Created July 31, 2021 11:35
Travelling Salesman Python Implementation Backtracking
# Given a starting vertex visit all vertex
from typing import Dict, Set
class Graph:
def __init__(self):
self.edges = list()
self.graph: Dict[int, Set] = dict()
self.visited = dict()
@anchitarnav
anchitarnav / hamiltonian_path.py
Last active November 19, 2023 13:41
Hamiltonian Path Python Implementation
# Get all hamiltonian paths in a graph from a given start vertex
# Python implementation
from typing import Dict, Set, List
from collections import defaultdict
class Graph:
def __init__(self, edges):
self.edges = edges
@anchitarnav
anchitarnav / n_queens_problem.py
Created July 24, 2021 20:18
N Queens Problem Python Implementation
"""
----------------------------
N - Queens Problem
----------------------------
--- Python Implementation
--- Backtracking approach
--- Description: Given a chessboard of 4x4 size, place n queens on it so that they done attack each other.
"""
from typing import Generator, List, Set, Tuple, Dict
@anchitarnav
anchitarnav / n_queens_problem.py
Created July 24, 2021 19:37
N Queens Problem Python Implementation
"""
----------------------------
N - Queens Problem
----------------------------
--- Python Implementation
--- Backtracking approach
--- Description: Given a chessboard of 4x4 size, place n queens on it so that they done attack each other.
"""
from typing import Generator, List, Set, Tuple, Dict
from copy import deepcopy
@anchitarnav
anchitarnav / union_find.py
Created July 11, 2021 08:05
Union Find Data Structure using python also called DisJoint Sets
class UnionFind:
def __init__(self):
self.parent = dict()
self.rank = dict()
def find(self, item) -> int:
origional_item = item
parent = self.parent[item]
while parent is not None:
@anchitarnav
anchitarnav / bipartite_graph.py
Created July 5, 2021 15:07
bipartite graphs using coloring technique python implementation with BFS
from typing import List
# This is an undirected graph.
# _graph[u] = list of all elements parallel to u (e.g. _graph[u] = [v])
# Also _graph[v] = [u] Since undirected graph the opposite edge is also present
def isBipartite(_graph: List[List[int]]) -> bool:
# we are already given the adjacency matrix, so we can start BFS
@anchitarnav
anchitarnav / dijkstra.py
Created July 4, 2021 10:09
Python Implementation of Dijkstra with python heapq (priority Queue)
# Dijkstra Algo with no destination.
# Calculate -> Shortest path from start to all nodes
import math
import heapq
from typing import Dict, List, Tuple
# Maintains neighbours and weights
graph: Dict[int, List[Tuple[int, int]]] = dict()