Skip to content

RussellDash332/pytils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pytils

Not to be confused with the pytils library for Russian-specific string utilities.

My collection of Python utility codes primarily designed for solving Kattis and Advent of Code (AoC) problems. It is intended to be as fast as possible, if not as short as possible. Feel free to use and customize them for your own use as you see fit.

Treasure List

Classification taken from cp-algorithms.

Algebra and Number Theory

Prime Numbers

Modular Arithmetic

Miscellaneous

Data Structures

Lists

Trees

  • avl_tree

    AVL tree implementation, including the self-rebalancing part. All queries are done in $O(\log n)$ time, except inorder traversal that takes $O(n)$ time.

    Kattis problem(s) to try on: hiredhelp, gcpc, physicalmusic, distributingseats, excellentengineers

  • fenwick_tree_rsq

    Classic Fenwick tree to answer range sum queries (RSQ). Both PURQ (point update, range query) and RURQ (range update, range query) versions are available. Updates and queries all run in $O(\log n)$ time.

    Kattis problem(s) to try on: fenwick, supercomputer, taxtherich, hringvegurinn, tonlistarlisti

  • fenwick_tree_rmq

    Binary-indexed tree but to answer range minimum/maximum queries (RMQ). Updates and queries all run in $O(\log n)$ time.

  • segment_tree_general

    General recursive implementation of segment tree, which can be customized to handle different aggregate functions. Updates and queries all run in $O(\log n)$ time.

    Kattis problem(s) to try on: stigavordur

  • segment_tree_dynamic

    Recursive implementation of the dynamic segment tree, which is currently optimized only for summation as aggregate. Updates and queries all run in $O(\log n)$ time.

    Kattis problem(s) to try on: japaneselottery, queryonarray

  • treap

    Quick implementation of a treap data structure.

  • wavelet_tree

    Incomplete implementation of the wavelet tree.

Miscellaneous

Dynamic Programming

Knapsack

Convex Hull Trick

  • convex_hull_trick

    Convex hull optimization to optimize a specific $O(n^2)$ DP problem into an $O(n\log n)$ one. Here, we assume that the queries and slopes are sorted, thus reducing the problem into a sliding window after sorting.

    Kattis problem(s) to try on: coveredwalkway

  • dynamic_convex_hull_trick_line_container

    Dynamic convex hull optimization, also known as the line container, solves a more general problem where lines and queries are not necessarily given in sorted order. Due to list insertion and deletion being $O(n)$, each operation should run in $O(n)$ time instead of $O(\log n)$, but should still be fast enough in most cases.

    Kattis problem(s) to try on: quintessentialbirthdaysurprise, yatp

Miscellaneous

String Processing

Suffix Array

String Matching

Miscellaneous

Linear Algebra

Numerical Methods

Geometry

2D

3D

  • compgeo_3d_projection_intersection

    Solves different simple queries in 3D coordinate system:

    • point-to-segment projection
    • point-to-plane projection
    • point-line distance
    • point-plane distance
    • line-plane intersection
  • compgeo_3d_convex_hull

    Given $n$ 3D-coordinates where no four points lie on the same plane, compute its convex hull (smallest polyhedron that contains all points). Runs in $O(n^2)$ time.

    Kattis problem(s) to try on: worminapple

  • compgeo_3d_minimum_enclosing_circle

    Smallest circle/sphere (2D/3D) that covers all the given points. Outputs the center and the radius.

    Kattis problem(s) to try on: starsinacan

Graph Algorithms

Graph Representation

Graph Traversal

  • traversal_bfs

    Simple BFS implementation. Runs in $O(V+E)$ time.

  • traversal_dfs

    Simple iterative DFS implementation using stacks (because recursion is slow in Python). Runs in $O(V+E)$ time.

Connected Components & Graph Properties

Minimum Spanning Trees

Shortest Paths

Lowest Common Ancestor

Network Flows

Matchings

Miscellaneous

  • 2sat

    Check for 2-satisfiability of conjunctive normal forms (CNF). On top of checking if it's satisfiable, also finds such boolean assignment if any. Makes use of Kosaraju's algorithm.

    Kattis problem(s) to try on: palindromicdna

  • chinese_postman_problem

    Shortest circuit on a weighted undirected graph that visits every edge at least once (as opposed to TSP that requires visiting every vertex other than starting node exactly once). Makes use of Floyd-Warshall's APSP algorithm.

    Kattis problem(s) to try on: joggingtrails

  • hierholzer_eulerian_cycle

    Prints the Eulerian circuit given a directed Eulerian graph (visits every edge exactly once).

    Kattis problem(s) to try on: eulerianpath

  • min_path_cover_dag

    You can use Hopcroft Karp's algorithm to find the minimum number of paths needed to cover the edges of a directed acyclic graph (DAG).

  • maximum_clique_bron_kerbosch

    Finds the maximum number such that exists a complete subgraph (clique) consisting of this many vertices using Bron-Kerbosch's algorithm, which is supposes to run in $O(3^\frac{V}{3})$ time.

    Kattis problem(s) to try on: maxclique, letterballoons

  • minimum_vertex_cover_bipartite

    Minimum number of vertices needed to cover all edges of the graph using Kőnig's theorem.

    Kattis problem(s) to try on: bilateral

  • min_clique_cover_backtracking

    Minimum number of cliques needed to cover the entire graph, simply using the plain old backtracking.

    Kattis problem(s) to try on: busplanning, committeeassignment

Advent of Code

Miscellaneous