Thursday, July 25, 2013

How to enable finder to show all hidden files Mac OS X 10.8

Type the following commands in Terminal:

defaults write com.apple.Finder AppleShowAllFiles YES
killall -HUP Finder

The first command sets a hidden preference so the finder shows all files and the second command restarts the Finder so these preferences take effect. The kill all command tells Finder to quit while the -HUP flag asks the program to restart.

The files can be hidden by typing the following into Terminal:

defaults write com.apple.Finder AppleShowAllFiles NO
killall -HUP Finder

If these commands do not work try a lower case F in the word Finder in the following command:

defaults write com.apple.Finder AppleShowAllFiles

Tuesday, July 9, 2013

Divide and Conquer Inversion Counting Algorithm vs Brute Force

First I will generate a random array of size 100,000 for both algorithms and then proceed with counting the number of inversions in the generated array according to the corresponding algorithm. Here are both algoritms and I will discuss the run times later

1. Divide and Conquer Algorithm
from random import randint


def generate_array():
    length_of_array = 100000
    i = 0
    array = []
    while i < length_of_array:
        array.append(randint(0,length_of_array))
        i += 1

    return array

def sort_count(A,B):
    global inversions
    C=[]
    lenA = len(A)
    lenB = len(B)
    i, j = 0, 0
    while i < lenA and j < lenB:
        if A[i] <= B[j]:
            C.append(A[i])
            i=i+1
        else:
            inversions= inversions +len(A)-i #the maggic happens here
            C.append(B[j])
            j=j+1
    if i == lenA:#A get to the end
        C.extend(B[j:])
    else:
        C.extend(A[i:])
    return C # sends C back to divide_array which send that back to the divide_array
             # which called it as the right variable

def divide_array(L):
    N = len(L)
    if N > 1:
        left = divide_array(L[0:N/2])
        right = divide_array(L[N/2:])
        return sort_count(left,right)
    else:
        return L
inversions = 0
array = generate_array()
sorted_array = divide_array(array)
print inversions


2. Brute Force Algorithm

from random import randint


def generate_array():
    length_of_array = 100000
    i = 0
    array = []
    while i < length_of_array:
        array.append(randint(0,length_of_array))
        i += 1

    
    return array


array = generate_array()
i = 0
inversions = 0 
length = len(array)
while i < length:
    for n in array[i+1:]:
        if n < array[i]:
            inversions = inversions + 1
    i = i+1

print inversions


Analysis of Algorithms

So both of these algorithms can complete the job of counting number of inversions however, the Brute Force Algoritm is considerably slower. 

While Divide and Conquer completed the task in 1.325s the Brute Force Algorithm completed the task in 20m35.371s on the same computer.  This is a huge difference! Which shows the power of the Divide and Conquer Algorithm. 

Divide and Conquer runs on O(n log n) time while Brute Force runs on O(n^2) time. Where n is the length of the array. 

Graphic representation of big-Oh comparison for algorithms



Here's the code for making the above graph
from pylab import *

n = arange(1,100)
f2 = n* (log(n))
f = n**2

fig = figure()
ax = fig.add_subplot(111)

ax.plot(f2)
ax.plot(f)

legend(('Divide and Conquer', 'Brute Force'), 'upper left', shadow=True)

title('Divide and Conquer Inversion Counting Algorithm vs Brute Force') 

xlabel('Size of array (n)')
ylabel('Number of interations')

show()