Replace list of list with “condensed” list of list while maintaining order

Here’s a brute-force approach (it might be easier to understand): from itertools import chain def condense(*lists): # remember original positions positions = {} for pos, item in enumerate(chain(*lists)): if item not in positions: positions[item] = pos # condense disregarding order sets = condense_sets(map(set, lists)) # restore order result = [sorted(s, key=positions.get) for s in sets] … Read more

Order Statistic Tree in C++

Here is the example of GNU Policy-Based STL MAP implemented as order statistic tree (tested on Linux gcc 4.6.1): #include <iostream> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef tree< int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> map_t; int main() { map_t s; s.insert(make_pair(12, 1012)); s.insert(make_pair(505, 1505)); s.insert(make_pair(30, 1030)); cout << s.find_by_order(1)->second << ‘\n’; … Read more

Find cycle of shortest length in a directed graph with positive weights

You can easily modify Floyd-Warshall algorithm. (If you’re not familiar with graph theory at all, I suggest checking it out, e.g. getting a copy of Introduction to Algorithms). Traditionally, you start path[i][i] = 0 for each i. But you can instead start from path[i][i] = INFINITY. It won’t affect algorithm itself, as those zeroes weren’t … Read more

All possible combinations of an array

EDIT: As FearUs pointed out, a better solution is to use Guava’s Sets.powerset(Set set). EDIT 2: Updated links. Quick and dirty translation of this solution: public static void main(String[] args) { List<List<String>> powerSet = new LinkedList<List<String>>(); for (int i = 1; i <= args.length; i++) powerSet.addAll(combination(Arrays.asList(args), i)); System.out.println(powerSet); } public static <T> List<List<T>> combination(List<T> values, … Read more

Sieve of Eratosthenes algorithm in JavaScript running endless for large number

You are making the Sieve of Eratosthenes much slower by making use of array manipulation functions such as Array#indexOf and Array#splice which runs in linear time. When you can have O(1) for both operations involved. Below is the Sieve of Eratosthenes following conventional programming practices: var eratosthenes = function(n) { // Eratosthenes algorithm to find … Read more

Iterating over a Binary Tree with O(1) Auxiliary Space

Geez, I’ll have to actually type it up from Knuth. This solution is by Joseph M. Morris [Inf. Proc. Letters 9 (1979), 197-200]. As far as I can tell, it runs in O(NlogN) time. static void VisitInO1Memory (Node root, Action<Node> preorderVisitor) { Node parent = root ; Node right = null ; Node curr ; while (parent != null) { curr … Read more

Color Logic Algorithm

Here is a theoretical explanation And the algo in C: typedef struct { unsigned char r, g, b; } RGB; double ColourDistance(RGB e1, RGB e2) { long rmean = ( (long)e1.r + (long)e2.r ) / 2; long r = (long)e1.r – (long)e2.r; long g = (long)e1.g – (long)e2.g; long b = (long)e1.b – (long)e2.b; return … Read more