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 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() |
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
# 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 | |
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
# 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: |
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
# 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() |
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
# 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 |
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
""" | |
---------------------------- | |
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 |
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
""" | |
---------------------------- | |
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 |
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 = dict() | |
self.rank = dict() | |
def find(self, item) -> int: | |
origional_item = item | |
parent = self.parent[item] | |
while parent is not None: |
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 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 |
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
# 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() |