Skip to content

Instantly share code, notes, and snippets.

View alvgaona's full-sized avatar

Alvaro Gaona alvgaona

View GitHub Profile
@alvgaona
alvgaona / grasp.py
Created October 8, 2025 18:32
GRASP Algorithm
def grasp_max_clique(
graph: dict[int, set[int]], iterations: int = 100, alpha: float = 0.2
) -> set[int]:
"""
GRASP (Greedy Randomized Adaptive Search Procedure) for maximum clique.
Combines randomized greedy construction with local search.
Args:
graph: Dictionary representing adjacency list
iterations: Number of GRASP iterations
@alvgaona
alvgaona / cpp.py
Created October 8, 2025 18:17
Chinese Postman Problem - Standard Exact Algorithm
class StandardCPP(CPPSolver):
"""
Standard Chinese Postman Algorithm for complete graphs.
Time Complexity: O(n³) for matching, O(n²) for circuit
Space Complexity: O(n²)
This is an EXACT algorithm that always finds the optimal solution.
"""
@alvgaona
alvgaona / nearest_neighbour.py
Created October 8, 2025 16:27
Traveling Salesman Problem - Nearest Neighbour
class TSPSolver:
"""Base class for TSP solvers."""
def __init__(self, problem) -> None:
"""
Initialize the TSP solver.
Args:
problem: tsplib95 problem instance
"""
@alvgaona
alvgaona / shortest_path.py
Created October 8, 2025 16:25
Dijkstra's algorithm
import heapq
from typing import Dict, List, Tuple, Optional
def dijkstra(
graph, start_node: int, end_node: Optional[int] = None
) -> Tuple[Dict[int, float], Dict[int, Optional[int]]]:
"""
Implements Dijkstra's shortest path algorithm.
@alvgaona
alvgaona / qr.m
Created March 30, 2024 02:23
Householder QR Decomposition
A = [
-1 -1 1;
1 3 3;
-1 -1 5
];
[QQ,RR] = qr(A);
[m, n] = size(A);
@alvgaona
alvgaona / bessi.m
Created December 8, 2020 13:11
Zeroth Order Modified Bessel Function of the First Kind
x = 2;
ax = abs(x);
if (ax < 3.75)
y = x/3.75;
y = y*y;
ans = 1.0+y*(3.5156229+y*(3.0899424+y*(1.2067492 + y*(0.2659732+y*(0.360768e-1+y*0.45813e-2)))));
else
y=3.75/ax;