What does O(log n) mean exactly?

I am learning about Big O Notation running times and amortized times. I understand the notion of O(n) linear time, meaning that the size of the input affects the growth of the algorithm proportionally…and the same goes for, for example, quadratic time O(n2) etc..even algorithms, such as permutation generators, with O(n!) times, that grow by … Read more

Finding the max/min value in an array of primitives using Java

Using Commons Lang (to convert) + Collections (to min/max) import java.util.Arrays; import java.util.Collections; import org.apache.commons.lang.ArrayUtils; public class MinMaxValue { public static void main(String[] args) { char[] a = {‘3’, ‘5’, ‘1’, ‘4’, ‘2’}; List b = Arrays.asList(ArrayUtils.toObject(a)); System.out.println(Collections.min(b)); System.out.println(Collections.max(b)); } } Note that Arrays.asList() wraps the underlying array, so it should not be too memory intensive and … Read more

Find how many connected groups of nodes in a given adjacency matrix

If the input matrix is guaranteed to describe transitive connectivity, it has a peculiar form that allows for an algorithm probing only a subset of the matrix elements. Here is an example implementation in Python: def count_connected_groups(adj): n = len(adj) nodes_to_check = set([i for i in range(n)]) # [] not needed in python 3 count = 0 … Read more

Good Java graph algorithm library?

If you were using JGraph, you should give a try to JGraphT which is designed for algorithms. One of its features is visualization using the JGraph library. It’s still developed, but pretty stable. I analyzed the complexity of JGraphT algorithms some time ago. Some of them aren’t the quickest, but if you’re going to implement them on … Read more

Generating all permutations of a given string

public static void permutation(String str) { permutation(“”, str); } private static void permutation(String prefix, String str) { int n = str.length(); if (n == 0) System.out.println(prefix); else { for (int i = 0; i < n; i++) permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n)); } } (via Introduction to Programming in Java)