Skip to content

arnauddesombre/Python-Utilities

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python utilities

My Python utility programs.

Each program in this repository is a stand-alone file, and works in Python 2 and 3. These can be useful for competitive programming for example.

Tip to import a homemade method in another python program:

# import function bellman_ford() from C:/temp/bellman_ford.py

import imp
BF = imp.load_source("bellman_ford", "C:/temp/bellman_ford.py")
...
x = BF.bellman_ford(graph, node)

alignment.py
alignment v2.py
Sequence alignment (align 2 strings minimizing substitution and insertion cost).
Optional parameters:

  • penalty for substitution
  • penalty for gap insertion

Return:

  • The first string with gap inserted
  • The second string with gap inserted
  • The total substitution and insertion cost

arithmetic.py
Contain the following arithmetic functions:

  • is_prime(n): return Boolean indicating if n is prime
  • gcd(a, b): return greatest common divisor of a and b
  • gcdm(*args): return gcd of args
  • lcm(a, b): return lowest common multiple of a and b
  • lcmm(*args): return lcm of args
  • prime_decomposition(n): return all the prime factors of n in a list
  • factors(n): return all the factors (divisors) of n in a list (brute-force from 1 to sqrt(n))
  • divisors(n): same as factors(n), using combination of prime factors (much more efficient)

ASCII.py
ASCII codes using Unicode strings (for example print(u'\u2588') will print ASCII character 0xDB (FULL BLOCK))


a-star - show path.py
Compute the shortest distance from a source to a destination in a directed graph where some crow distance (Euclid or haversine can be defined as well as the shortest corresponding path.
/!\ all distances must be >= 0

Return:

  • shortest distance
  • shortest path from source to destination

Note: this program implements A* starting from the Dijkstra implementation of "dijkstra - show path.py"


a-star.py
Compute the shortest distance from a source to a destination in a directed graph where some crow distance (Euclid or haversine can be defined.
/!\ all distances must be >= 0

Return:

  • shortest distance

Note: this program implements A* starting from the Dijkstra implementation of "dijkstra.py"


bellman_ford.py
Compute all shortest distances from a source to all points in a directed graph as well as the shortest corresponding paths using the Bellman-Ford algorithm.
/!\ distances can be < 0
/!\ no negative cycle can exist in the tree

Return:

  • shortest distances
  • shortest path parents dictionary
  • Boolean indicating if a negative cycle exists

negative_cycle(graph, source) will return a list containing one negative cycle (note there can be multiple negative cycles). The first and last element of the list are the same ([A, B, ..., A])

bellman_ford_path(parents, source, destination) will return the path (a list) from 'source' to 'destination'. Note that if there is a negative loop, this function will run indefinitely


bfs.py
Explore a directed graph using Breadth First Search (BFS).

Return:

  • a dictionary containing the visiting order for each node starting at 1

bidirectional a-star - show path.py
Compute the shortest distance from a source to a destination in a directed graph where some crow distance (Euclid or haversine can be defined as well as the shortest corresponding path using bi-directional A* algorithm.
/!\ all distances must be >= 0

Return:

  • shortest distance
  • shortest path from source to destination

bidirectional dijkstra - show path.py
Compute the shortest distance from a source to a destination in a directed graph as well as the shortest corresponding path using bi-directional Dijkstra algorithm.
/!\ all distances must be >= 0

Return:

  • shortest distance
  • shortest path from source to destination

binaryIndexedTree.py
Implementation of Binary Indexed Trees (Fenwick Tree).


centroid.py
Implementation of the Centroid Decomposition of a Tree.
This program should be provided the text file centroid.txt as input:
python centroid.py < centroid.txt


clustering.py
Compute k clusters from an undirected graph, using Kruskal's MST (minimum spanning tree) algorithm, so that the minimum distance between 2 nodes in different clusters is maximized.

Return:

  • a dictionary of integer keys 1 to k containing the list of nodes of each cluster
  • the minimum distance between the k clusters

combinations.py
Miscellaneous implementations of nCr(), the “n choose r” function, with some optimizations for competitive programming.


compare a-star & landmark [roadmap]
Compare the performance of A* and Landmarks algorithm using real world maps (links and instructions are in the file, or also available on this page: USA-road-d.NY.co and USA-road-d.NY.gr).
v1: The landmarks used are the nodes closest to the 4 corners of the square and the 4 centers of each side of the square of the square that contains the map.
v2: The landmarks used are the nodes closest to the 4 corners of the square, the 4 centers of each side of the square, and the 8 quarters (2 per side) of the square that contains the map. Only the 4 best landmarks among the 16 are used for each calculation.

compare dijkstra & a-star.py
Compare the performance of Dijkstra, bidirectional Dijkstra, A* algorithm, and Bellman-Ford, using a N * N square where roads are all vertical and all horizontal lines.

compare dijkstra & a-star [roadmap].py
Compare the performance of Dijkstra, bidirectional Dijkstra, and A* algorithm, using real world maps (links and instructions are in the file).

These are not a "utility" files, just performance and calibration tools. The conclusion is similar to what's described in:
http://www.cs.princeton.edu/courses/archive/spr06/cos423/Handouts/EPP%20shortest%20path%20algorithms.pdf

  • in a graph where no crow distance can be defined (social network for example), bi-directional Dijkstra is best
  • if a crow distance can be defined, A* is best (bi-directional A* is not as good - but still better than bi-directional Dijkstra)
  • using landmarks requires more memory (storage) and some pre-processing. Landmark v1 is faster than A* by by 25-30%, Landmark v2 is faster by 35-40%

convex hull.py
Determine the convex hull of a set of points using Graham scan algorithm.
This program should be provided the text file convex_hall.txt as input:
python "convex hall.py" < convex_hall.txt


dfs.py
Explore a directed graph using Depth First Search (DFS).
Implements:

  • DFS_recursive(graph, source): a recursive algorithm
  • DFS_linear(graph, source): a linear algorithm
  • DFS(graph, source, recursive=False): which calls one or the other

Return:

  • 2 dictionaries containing the visiting orders (pre_visit & post_visit) for each node starting at 1

dijkstra - show path.py
Compute all shortest distances from a source to all nodes in a directed graph as well as the shortest corresponding paths using Dijkstra's algorithm.
/!\ all distances must be >= 0

Return:

  • shortest distances
  • shortest path parents dictionary

(if a destination is specified, the algorithm stops when 'destination' is found and only returns the distance from source to destination as well as the corresponding shortest path.)


dijkstra.py
Compute all shortest distances from a source to all nodes in a directed graph using Dijkstra's algorithm.
/!\ all distances must be >= 0

Return:

  • shortest distances

(if a destination is specified, the algorithm stops when 'destination' is found and only returns the distance from source to destination.)


fibonacci.py
Calculate the Fibonacci numbers using Matrix exponentiation.
This method has an O(Log(N)) time complexity, much better than the brute-force method calculating all Fibonacci numbers from 1 to n using using F(n) = F(n-1) + F(n-2) in O(N) time.


find_all_paths.py
Find all simple paths (no loop) in a directed graph starting from a specified node by brute recursive enumeration.
Variant implemented: find only one.


floyd-warshall.py
Implementation of the Floyd-Warshall algorithm (all-pairs shortest paths).


kadane.py
Implementation of the Kadane algorithm (Maximum subarray problem).


kargerMinCut.py
Compute the minimum cut of an undirected graph using Karger's algorithm.


knapsack.py
Knapsack algorithm (Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible).

  • Value = non-negative
  • Weight = non-negative and integral
  • Total weight limit = a non-negative integer

2 algorithm implemented:
1/ recursive algorithm, return sum of values only, but list of item can be retrieved
2/ dynamic programming algorithm, return sum of values & list of items


kruskal.py
Compute Kruskal's minimum spanning tree (MST) algorithm of an undirected graph (edge's costs can be negative).


landmark - show path.py
Compute the shortest distance from a source to a destination in a directed graph where some crow distance (Euclid) can be defined, and where distances from some landmarks are known, as well as the shortest corresponding path.
/!\ all distances must be >= 0

Return:

  • shortest distance
  • shortest path from source to destination

lca.py
Miscellaneous implementation of Lowest Common Ancestor (LCA) algorithms, with some optimizations for competitive programming.
This program should be provided the text file lca.txt as input:
python lca.py < lca.txt


longest common sub-sequence.py
longest common sub-string.py
Determine the longest sub-sequence / sub-string between two sequences (strings).
This program should be provided the text file "longest common sub.txt" as input:
python "longest common sub-sequence.py" < "longest common sub.txt"
python "longest common sub-string.py" < "longest common sub.txt"

Note:

  • a sub-string is a contiguous part of a string (AB is a sub-string of ABABA).
  • a sub-sequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements (AAA is a sub-string of ABABA).

palindrome.py
Find the longest palindrome within a string, using Manacher's algorithm.


parallel_cpu_1.py
parallel_cpu_2.py
Template for parallel processing in Python.

1: Parallel execution using multiprocessing/map
from multiprocessing.dummy import Pool as ThreadPool

2: Parallel execution using pathos/map
from pathos.multiprocessing import cpu_count
from pathos.pools import ParallelPool


prim.py
Compute Prim's minimum spanning tree (MST) algorithm of an undirected graph (edge's costs can be negative).


rank_matrix.py
Find the rank of a matrix (not necessarily square).


running_median.py
Compute the running median of a series of numbers in O(Log(n)) time, which is the most efficient algorithm.


scc.py
Strongly Connected Components (SCC)
Compute the SCC of a directed graph using the Kosaraju's algorithm

return:

  • the number of SCC
  • a dictionary SCC1, SCC1[n] = list of all nodes in SCC number 'n'
  • a dictionary SCC2, SCC2[node] = number of the SCC of which 'node' is a member

segment_tree.py
Implementation of Segment Tree.


traveling_salesman.py
Compute the shortest path between all nodes of a connected graph starting at one particular node and returning at that same node (connected = there is a path between every pair of nodes).
(implementation with dynamic programming algorithm)


trie.py
Implementation of a TRIE (prefix tree)


weekday.py

  • isleap(year) return a Boolean indicating if the year is leap
  • weekday(day, month, year) return the day of the week for any date

Note that the periodicity of weekday is 2800 years. i.e:
weekday(day, month, year) == weekday(day, month, year + 2800) for any date


About

Python utility programs

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages