FQL allows you to pass a SQL-like query to facebook in the querystring and it returns you an xml document with your data:

SELECT metric, value FROM insights WHERE object_id=PAGE OR APP YOU WANT DATA ABOUT AND metric='page_impressions' AND end_time=end_time_date('2011-06-26') AND period=period('month')

Object_Id represents the page or app you are getting data for. This ID will be in the URL of the page when you visit it, if you are having trouble finding it.

This will query will return an XML document:

<fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true"> <insights> <metric>page_impressions</metric> <value>76</value> </insights> </fql_query_response>

But before you can run any queries for that page, you must be granted permission by the page’s owner. To do this, you must have create a facebook app on https://developers.facebook.com/apps. After doing so, you will be assigned an App Id and an App Secret token, which you will need later.

Then, you will need the admin user of the page or app (this might be you) to visit:

https://www.facebook.com/dialog/oauth?client_id=YOUR CLIENT ID&redirect_uri=YOUR WEBSITE URL&scope=read_insights&state=new

The scope param allows you to specify which permissions you need for your app. Right now all I care about is reading insights, but you might want to post to the users wall, or read the friend lists, etc.

When the user visits the above URL, they will be prompted to log in to Facebook if they are not already, and then they will be prompted to allow your app to access the data we are requesting. Then they will be redirected to the page you specified in the querystring. When they are redirected, there will be a new CODE param in the querystring. You need to extract that param and pass it to the below URL to complete authentication:

https://graph.facebook.com/oauth/access_token?client_id=YOUR APP ID&redirect_uri=SAME URL AS ABOVE&client_secret=YOUR APP SECRET&code=THE CODE YOU JUST RECEIVED

Note that you must supply the exact same URL in the second request that you did in the first. If all goes well, this second request will return:

access_token=XXXXXXXXXX&expires=12345.

Then making FQL queries is easy. You need to pass this as a parameter along with your query:

https://api.facebook.com/method/fql.query?access_token=ACCESS TOKEN YOU RECEIVED

&query=SELECT%20metric%2C%20value%20FROM%20insights%20WHERE%20object_id%%20AND%20metric%3D’page_impressions’%20AND%20end_time%3Dend_time_date(’2012-05-26′)%20AND%20period%3Dperiod(‘days_28′).

This will return the xml document above. You can use different periods, different end times, and different metrics to get all of the data you require. For a full list of metrics available (there are a lot), check out https://developers.facebook.com/docs/reference/fql/insights/.

]]>Not only is this sentence rather paradoxical in meaning (if it’s false, it’s true; it it’s true, it’s false), but it is also self-referential. This is rather an odd thing for a sentence to be.

Mathematics is not about numbers, not really. Math is about logic and objectivity. It is about creating a system that is useful for solving real world problems. Such systems are based on axioms, assumptions. These axioms are like the foundation of a house, they are not proven, they are just accepted. Axioms are like primary colors, if we start with ‘axioms’ red, and blue, we can mix a multitude of various shades by combining these colors. These combinations ‘purple’, are like theorems. Note that if we start with different primary colors – perhaps blue and yellow, we get an entirely different set of color-theorems. It is the same way with math; different systems develop depending on which axioms we assume up front. Systems like “Natural Numbers”, “Integers”, “Real Numbers”, all have different axioms at their origin. But they are all useful, depending on the situation.

The interesting question to ask is, are any of these systems “perfect”. Perfection in this sense would mean two things: every true statement about numbers could be proven with the system (every true statement is a theorem), and nothing could be proved with the system that is false. Up until Kurt Godel, it was generally believed that there was such a system – a perfect, mathematical theory of everything.

In 1931, Kurt Godel proved this theory incorrect. He proved that any formal system is fundamentally incomplete (hey, the title!). The way he did it was incredibly clever, and cemented his mathematical legacy at the age of 25. The method of the proof was to assume that the formal system WAS complete and then discover a logical inconsistency that derives from this assumption, thereby proving that the assumption of completeness was false.

The first step of his proof was to create a translation of a formal statement into numbers. For each symbol of the system, he created a mapping to a number. For example, we could assign “123″ to the symbol “0″ and “111″ to the equals sign. The statement “0=0″ becomes the number “123111123″. Whereas the former is a statement about numbers, the latter is a number about numbers. They both mean the same thing, that zero equals zero, but the latter is a number. 123111123 is a theorem of the formal system. And now the previous sentence is a new theorem of the formal system. This mapping has created a new property of numbers – “thoeremhood”. This property is just like any other property of numbers like, “perfect square”, “prime”. Some numbers will have it, others will not. Since our formal system is capable of expressing all properties of numbers, we can state this theorem in the formal system itself. What this means is that a theorem can assert the truth of another theorem.

Let’s go back to the sentence that I started with “I am not a true statement”. If we could express that theorem in the formal system, we would have discovered an inconsistency. We saw how to express the “am not a true statement” above – this is the property of theoremhood. We need to figure out the statement in the formal system that equates to “I”, and encode it with the number trick, and then we can assert that this number does NOT have theoremhood.

To do so, we need one more piece of cleverness. We can take the statement of the variable (in the sentence, this is the subject, the word “I”), and replace it with the entire sentence. That is “I am not a true statement” becomes “I am not a true statement is not a true statement” This allows the self referentiality we are looking for. We can call this trick “Arithmoquining” after Douglas Hofstadter. When you arithmoquine a statement, the statement makes a statement about itself. We can perform the same trick with a number-encoded theorem. We can start with the theorem “the arithmoquine of this statement is not a valid theorem-number”, and then arithmoquine it to be:

The arithmoquine of “the arithmoquine of this statement is not a valid theorem-number” is not a valid theorem-number. And voila – the formal system is defeated. This can be expressed in the formal system itself, so the formal system is fundamentally incomplete.

This has interesting implications for artificial intelligence. Computer programs are, after all, formal systems. If we want a computer to be able to make new discoveries, we must first provide it with the basics of the problem (axioms), and then rules to operate on these axioms to derive new observations (theorems). In this light, the question is, is there anything that is true, but that a machine could never discover? Further, is there anything that a machine will believe to be true, but in fact not be? Many have argued that Godel has proven the impossibility of machine intelligence, at least that of rivaling human intelligence, saying that there are always things a human can discover that a machine cannot, as a machine is bound by this fundamental incompleteness and a human is not.

I will not posit a definitive answer to this question, but I think it is important to consider that creating intelligence does not necessarily equate to creating human intelligence. There are likely many scenarios where a non-human intelligence is better suited to a specific problem, and who is to say which is superior? What would it even mean to call an intelligence “superior”?

One final thought, while it is possible that computer intelligences may in fact be susceptible to believing in things that are false, isn’t that just the human notion of an “opinion”?

For more on this, I highly recommend the book Godel Escher Bach by Douglas Hofstadter.

]]>To figure it out, I loop through the first thousand unicode characters, create a string by appending a character in front and behind, and then pass it to the parser engine with sys.dm_fts_parser. If two results are returned, the character broke the word, if one, it didn’t.

You could change the language by updating the 1033 parameter to whatever you need it to be.

--unicode goes from 0 to 65535 --most used values < 1000 --can get the unicode value of a char with function UNICODE('x') -- get the char from an int with NCHAR(x) DECLARE @results TABLE(Breaker nchar, IntValue int) DECLARE @i int; SET @i = 0; WHILE @i < 1000 BEGIN DECLARE @sql NVARCHAR(600); SET @sql = 'a' + NCHAR(@i) + 'b'; DECLARE @ret int; -- 1033 = english, first zero = system stoplist, second zero = no accent sensitivity Select @ret = count(*) from sys.dm_fts_parser(@sql,1033,0,0) if @ret > 1 BEGIN INSERT INTO @results (Breaker, INtValue) values (NCHAR(@i), @i); END SET @i = @i + 1 END SELECT distinct Breaker from @results;]]>

private double calculateArea(List<Point> orderedPoints) { //multiply each x coordinate by the following y int positiveDiagonalSum = 0; for (int i = 0; i<orderedPoints.size(); i++){ if (i == orderedPoints.size() - 1){ positiveDiagonalSum += orderedPoints.get(i).x * orderedPoints.get(0).y; } else { positiveDiagonalSum += orderedPoints.get(i).x * orderedPoints.get(i+1).y; } } //multiply each y coordinate by the following x int negativeDiaglonalSum = 0; for (int i = 0; i<orderedPoints.size(); i++){ if (i == orderedPoints.size() - 1){ negativeDiaglonalSum += orderedPoints.get(i).y * orderedPoints.get(0).x; } else { negativeDiaglonalSum += orderedPoints.get(i).y * orderedPoints.get(i+1).x; } } //subtract second sum from first, divide by 2 double area = Math.abs((positiveDiagonalSum - negativeDiaglonalSum) / 2.0); return area; }]]>

That said, there have been efforts to specifically create intelligent programs. Alan Turing hypothesized about intelligent machines long before they were a realistic possibility. The “Turing test” has been an industry standard, and the Loebner prize competition is held yearly to test the latest creations of AI researchers for intelligence. In this test a human judge converses with a human and with a computer and attempts to determine which is which. If the judge cannot reliably identify the computer, the computer passes the Turing test. In my first considerations of AI, all I could think about was language, about beating the Turing test. Language is certainly an intelligent behavior, but it’s not the only such. And it’s probably one of the most complicated. Not a good candidate for the first truly intelligent algorithm. Consider a plant growing toward the sun, a bird building a nest, a human infant recognizing and reaching for her mother. There are intelligent behaviors that do not require language. So what exactly is intelligence?

Intelligence requires two associated abilities:

1- Store input data in a hierarchical model of the outside world (memory)

2- Use this model to make decisions based on projections of the result of those decisions (prediction).

This is an idea proposed by Jeff Hawkins in the book On Intelligence

Intelligence is the ability to represent past input in a manner that allows you to make useful predictions based on new input as it is received. You have been widely exposed to computers, so you have a very strong internal model of their various interactions. You are very confident in what will happen if you move the mouse left or right, or if you click one of the mouse buttons on an underlined word or button, or if you press the down arrow on your keyboard. These sub-conscious realizations are projections you make based on past interactions with computers. When learning a new skill, you don’t have many projections available, this is another way of saying you don’t know what will happen. As you practice the new skill, you see a wide range of inputs based on your actions, this allows you to build a model of behavior and consequences that you can use going forward.

When you learn to cook, you must start with easy recipes with few, obvious steps. You learn to make spaghetti, toast, maybe macaroni and cheese out of a box. The first time you make spaghetti, you have to read the instructions on the box, it isn’t clear to you what will happen, you set a timer. The second time, you probably don’t read the box as many times, the fifth, you don’t need the instructions at all. You’ve created an internal model for how spaghetti noodles cook, you can reliably project that boiling water for 7-9 minutes will properly soften the noodles.

Then you can apply this model to other contexts. When you want to make lasagna, you can use your “pasta” model to make the noodles here as well. When you want to make rice a few days later, you can reuse a bit of the spaghetti noodle model. You can make basic projections about what will happen when you put dry rice into the hot water for a period of time. This is due to the hierarchical nature of intelligence. By storing data hierarchically, we can make projections for situations we have never been in before. We can make complicated calculations similar to “when I put the spaghetti in boiling water it turned al dente, these penne noodles are very similar to spaghetti, I can be reasonably certain what will happen when I put the penne noodles in boiling water”. This ability is the core of intelligence.

]]>There are N people going to a restaurant, entering one at a time. Each person is unhappy upon sitting down if the total bill does not equal a multiple of that person’s integer identifier. So, person “3″ will not be happy unless the bill is 3, 6, 9, 12, etc. Upon entering the restaurant, if a person is not immediately happy, they will order something to make them happy. This could, unfortunately, make someone else unhappy, so they would order something else . This continues until all people are happy. When more than one person is unhappy, any of them could be the first to order. The restaurant sells food at any price.

As the dinning guests could arrive at any order (for 4 people, they could arrive 1,2,3,4 or 3,2,4,1 or 2,1,3,4, etc), different orders could result in a different number of trips needed by the waiter. The waiter only comes to the table when someone is unhappy, stays until everyone is happy, and then departs. Calculate the spread of trips the waiter will take based on the order of N people entering.

For example, if there are 4 people:

If they enter 1,2,3,4

Person 1 orders something for $1, and is happy

Person 2 orders something for $1 (total $2), and is happy

Person 3 orders something for $1 (total $3) and is happy, person 2 orders something for $3 (total $6) and all are happy

Person 4 orders something for $2 (total $8), Person 3 orders something for $1 (total 9), person 2 orders something for $3 (total $12), and all are happy.

It took 4 trips for the waiter, because each person was initially unhappy.

If they enter 4,2,1,3

Person 4 orders something for $4

Person 2 is immediately happy

Person 1 is immediately happy

Person 3 calls the waiter, bill gets bumped to $12

The waiter is called twice, so the spread for 4 people is 2.

**Musings**

Upon the waiter leaving the table, the bill will be the least common multiple of the integers represented. This is true regardless of the order in which the seated guests order additional food. Each one of them is always ordering the cheapest thing to make the total bill a multiple of their number, and eventually that number becomes a multiple of all.

To find the minimum above, I front loaded the array with the highest number, and then followed it with that numbers factors. If 16 is first, 2,4,8 and 1 can all sit down without needing to call the waiter. Then 15 could come in, and the least common multiple is 240. So at that point 10, 12, 6, 5, 3 can also sit down. Then enters 14, the LCM is 1680, so 7 can enter without calling the waiter. 13 enters and the bill gets bumped to 21840, and then finally 9 enters, making the final bill 65520, and the waiter was called 6 times

Enters | Can also sit | LCM |

16 | 2,4,8,1 | $16 |

15 | 10,12,5,3,6 | $240 |

14 | 7 | 1680 |

13 | 21840 | |

11 | 240240 | |

9 | 720720 |

What if they came in increasing order?

Enters | Can Also Sit | LCM |

1 | $1 | |

2 | $2 | |

3 | 6 | $6 |

4 | 12 | $12 |

5 | 10,15 | $60 |

7 | 14 | $420 |

8 | $840 | |

9 | $2520 | |

11 | $27720 | |

13 | $360360 | |

16 | $720720 |

Here we have 11 trips, and in the least case we had 6, so the spread for 16 people is 5.

The question we are trying to answer is “what is the spread of trips the waiter must take”. There are other questions lurking that may try to distract from this core issue.

A person need not call the waiter if their number (n) is a factor of the current bill. But if we have a thousand people coming, the dinner bill (lcm) will be greater than we can track with 64 bit integers. So it is not practical to track the bill, so what else do we have to go on? In the second example above, how could we tell that 6 will not need to call the waiter? When 6 comes in, the LCM(1,2,3,4,5) = 60, a multiple of 6. How could we determine this without calculating the LCM?

My first thought is to determine if numbers have already “sat down” that multiply to equal the number in question. That is, 6 can sit down because 2 and 3 have already sat, but this doesn’t hold true for 8. 8 has to call the waiter, even though 4 and 2 are already sitting.

One way to calculate the least common multiple of numbers is to break the number into its prime factors. That is, 6 becomes 3^1 * 2^1, and 9 becomes 3^2. To find the LCM, we combine these two lists. When the two lists have a term in common (3 in this example), we drop which ever has a lower exponent. So the LCM of 6 and 9 is (3^2 * 2*1) = 18.

Or consider 16, 15, and 14:

16 | 2^4 |

15 | 5^1 * 3^1 |

14 | 7^1 * 2^1 |

The only common prime factor is 2 between 16 and 14, so the 14′s 2^1 is dropped, and our final LCM is:

2^4 * 5^1 * 3^1 * 7^1 = 1,680

With this formula, we can look at the question in a new way. When someone enters the room that has a prime factor who’s power is greater than the power of that prime already (compared to those already sitting), the waiter needs to come.

Going back to 1 through 16

1 | 1^1 |

2 | 2^1 |

3 | 3^1 |

4 | 2^2 |

5 | 5^1 |

6 | 2^1*3^1 |

7 | 7^1 |

8 | 2^3 |

9 | 3^2 |

10 | 2^1*5^1 |

11 | 11^1 |

12 | 2^2 * 3^1 |

13 | 13^1 |

14 | 7^1 * 2^1 |

15 | 5^1 * 3^1 |

16 | 2^4 |

The highest powers of each prime are:

2^4

3^2

5^1

7^1

11^1

13^1

We are ultimately calculating the difference between the maximum and the minimum waiter trips. The *maximum *trips will occur when the guests enter in such a way as the lowest prime exponents come first. That is, the number with factor 3^1 comes before that with 3^2, and 2^1 comes before any other 2s. Each exponent increase will cause a trip, and the total trips is the sum of the exponents. All numbers have a 1 factor as well, omitted for clarity above, but in the maximum scenario, person 1 would enter first, causing an additional trip: 1+ 4 +2 +1 +1 +1 +1 = 11. The *minimum *occurs when the guests with the highest power of each prime enter first. 2^4 comes in before 2^3. In this situation, we have as many trips as we have prime factors, in this case, 6. So the spread is 5.

This has shifted the problem to one of factorization. It is possible to brute force smaller inputs. That is, calculate the prime exponents with something like this:

HashMap<Long, Integer> factors = new HashMap<Long, Integer>(); factors.put(1l, 1); Long number = i; outer: while (number > 1){ for (Long prime : primes){ if (number % prime == 0){ int newUsages = 1; if (factors.containsKey(prime)){ newUsages += factors.get(prime); } factors.put(prime, newUsages); number = number / prime; continue outer; } savedFactors.put(i, factors);

We divide the number by the first found prime factor, and add an exponent to the prime factor by which we divided.

So if we have the number 36:

36 /2 = 18 | 2^1 |

18/2 = 9 | 2^2 |

9/3 = 3 | 2^2 * 3^1 |

3/3 = 1 | 2^2 * 3^2 |

But this method will not work for larger primes. The larger input will contain guest counts up to 10^12. So we need to go back to the formulas one more time and look for another shortcut. The number that we want to calculate is the difference between maximum and minimum trips. The maximum is the sum of the exponents, and the minimum is the count of the prime bases. When would a number count towards the maximum, but not the minimum?

The difference will always be the number of integers that have a single prime factor with a power other than 1. For N = 16, we have

1^0

2^2

2^3

2^4

3^2

Each of these terms count toward the sum of exponents, but not the count, as prime base is already present. 2^2 causes the sum to go up, but not the count, as we already had a 2 factor. 2^3 does the same thing.

**Solution**

The number of waiter trips is equal to the number of exponents greater than 1 of prime numbers less than the square root of the number of guests that are less than or equal to the number of guests. That is, the number of times you can multiply a prime number by itself and have a sum less than the guest count. IF we have 16 guests, we iterate through prime numbers starting with 2. 2^2, 2^3, 2^4 are all less than or equal to 16. Then we move on to 3, 3^2 is less than 16. We need to add one more to account for the prime ^ 0 factor. We don’t need to test any primes greater than the square root of 16, because the square of this term would be greater than 16, and it wouldn’t be counted.

For the large input, we have guest counts up to 10^12, so we need to enumerate through all of the primes less than 10^6. One of the fastest ways to do this is to use the Sieve of Eratosthenes. We start with 2, and eliminate all multiples thereof from the prime list. Then we go to 3, and do the same. Once we have all the primes, we check how many powers of that prime fit under each N, and this produces the final result.

boolean[] prime = new boolean[1000001]; Arrays.fill(prime, true); for (int i = 2; i<prime.length; i++){ if (prime[i]){ for (int j = 2*i; j<1000001; j+=i){ //eliminate all numbers with this as a factor prime[j] = false; } } } for (int i = 0; i < numTests; i++){ Long numberOfGuests = Long.parseLong(reader.readLine()); long count = numberOfGuests == 1 ? 0 : 1; for (int j = 2; j 0){ thisCount++; sum *= j; } count += thisCount -1; } } results.add(count);]]>

For example, we could perform an analysis of the sentence “The cat and the dog like cat food”

We go through and examine each word, and note the word that follows it

The -> Cat

Cat -> and

And -> the

The -> dog

Dog -> like

Like -> cat

Cat -> food

Then we can pile these up:

The -> Cat, Dog

Cat -> and, food

And -> the

Dog -> like

Like -> cat

Using these observations, we can build a probability distribution for each word:

“the” has a 50% chance to be followed with “cat”, and 50% to be followed with “dog”.

With these distributions, we can create a chain of words based on the first but not exactly it. Picking a starting word “cat”, flip a coin for the next word – I got “and”. Then we only have one option from “and”, so we go to “the”, flipping a coin, I once again got “Cat”, and one more flip yields “food”. We don’t have any words after food, so we are done.

Cat and the Cat Food

Markov chains can be more complicated than just one ancestor as well. We could observe the output after two words:

The Cat -> and

Cat and -> the

And the -> dog

The dog -> like

Etc.

This creates output more resembling the original input. It also requires more input data to produce interesting variety in a “new” sentence composed with its distribution.

This is a trivial example, but this technique can be very powerful. We could perform a markov analysis of every Bach composition, creating a distribution of notes that follow a given note. With that, we could create a new composition much in the style of Bach. This is exactly what Steve Larson did, creating an algorithm “EMI” pronounced “Emmy”.

Although powerful, there is nothing “intelligent” about markov sequences. They cannot really be creative, only mimick another’s creativity. With a large enough database, Markov chains could be used to diagnose sickness (what condition follows these symptoms most often?), detect fraud (what is the most likely tax return amount given this person’s previous tax filings?), and many others. But to inject true intelligence into algorithms, we need to look elsewhere.

]]>You are in charge of a prison with P number of cells in a straight line. Each cell has a window on the walls it shares with the neighbor cells. You need to release a certain number of prisoners, but when you do, word of the release spreads and the prisoners riot and cause a ruckus. But they only can do so if they hear about the release, and they only hear about the release if word gets passed to them from cell to cell. When a prisoner riots you have to bribe them with a gold coin.

So, given a total number of cells, and a list of prisoners to release (represented by their cell number), what is the minimum gold you can spend?

**Musings:**

News of the first release will pass through each cell in the prison, as there are no empty cells to arrest it. We don’t have to pay the released prisoner, so he first release will always cost P – 1 gold. The second will vary depending on how many cells the first release has cut off. In general, we want to cut off as many cells as possible, yet this is not an easy thing to determine with an algorithm.

If we have 20 cells and we need to release 4 prisoners: [1, 4, 8, 11]: We should release prisoner 11 first, as that will cut the cost of subsequent releases by the most. It is tempting to deduce that the prisoner closest to the center will always be the best choice, but this is not the case. If we instead had [1,4,11,12], 11 is still closest to the center, but 12 cuts off much more of the prison. So we can’t easily determine which prisoner will be the cheapest to release without considering the cost of each subsequent release.

Each time we release a prisoner, it creates a sub problem that looks exactly like the original problem. If the example above, after we release prisoner 11, we have a prison of 10 cells, and three prisoners to release from it. With that observation, along with realizing that releasing one prisoner from a prison of size n will always cost n – 1 gold, we can create a recursive algorithm to solve the problem.

For each prisoner we need to release:

Cost =

Number of cells – 1 + (cost to release current prisoner)

+ Cost of releasing all prisoners to the right (recursive call)

+ Cost of releasing all prisoners to the left (recursive call)

This will work for small inputs, but as the input grows in size, it takes much too long to be practical. Luckily there is a simple programming trick that lets us solve much larger problems.

Consider a prison of size 100 with 40 prisoners to release: [2,4,6,12,15,19,24,34, etc…]

We start by calculating the cost of releasing prisoner 2. To do this, we need to determine the cheapest way to release 39 prisoners from [4, 6, 12, etc]. After testing the cost to release prisoner 4, we will calculate the cheapest way to release 38 prisoners [6, 12, 15, 19, 24, 34, etc]. Eventually we will solve that problem (though it will take a lot of recursion to do so). Then we will start again on the outside most loop, and consider what happens when we first release prisoner 4. Our first step will be to look at the left, which is easy as it is just one prisoner. Then we consider what it will cost to release 38 prisoners [6, 12, 15, 19, 24, 34]. Look familiar? We’ve already solved that problem, so if we had stashed that answer somewhere we could just look it up and move on. This situation will come up thousands of times in a large input like this, and solving the problem for a large input becomes possible. This is a program technique called memorization, and the problem at large is one of Dynamic Programming.

**Solution**

Here is a solution to the problem implemented in java. I use a hashmap to store the already completed operations, using-: as the memory key. Note that you don’t need to clear the memory for subsequent data inputs.

private Integer solve(int leftBorder, int rightBorder, List<iReleases>) { String memoryKey = String.valueOf(leftBorder) + "-" + String.valueOf(rightBorder)+":" + Arrays.deepToString(iReleases.toArray()); if (memory.containsKey(memoryKey)){ return memory.get(memoryKey); } int numCells = rightBorder - leftBorder; if (iReleases.size() == 1){ return numCells - 1; } if (iReleases.size() == 0){ return 0; } //try every prisoner int cost = Integer.MAX_VALUE; for (int i = 0; i < iReleases.Size(); i++ { ArrayList<Integer> left = new ArrayList(); left.addAll(iReleases.subList(0, i)); ArrayList<Integer> right = new ArrayList<Integer>(); right.addAll(iReleases.subList(i + 1, iReleases.size())); int thisCost =( numCells - 1) + solve(leftBorder, iReleases.get(i) - 1, left) + solve(iReleases.get(i), rightBorder, right); if (thisCost < cost){ cost = thisCost; } } memory.put(memoryKey, cost); return cost; }]]>

For example, if we have an array of numbers [23, 4, 10, 9, 6, 12, 17]:

Using 10 as the pivot value, we sort the arrays into two groups (with the pivot value in the middle)

[ 4, 9, 6] [10] [23, 12, 17]

Then we apply the same process to the left group pivoting around 6:

[4] [6] [9]

and around 17 in the right group:

[12] [17] [23]

Putting it all together, the array is sorted.

The question in implementation comes in deciding which value within the array to use as the pivot. The ideal scenario is choosing the number in the middle of the array (as I did above), but until the array is sorted, we can’t know which number is in the middle (consider an array of 10,000 numbers instead of 7).

Choosing the leftmost element in the array seems as good as any, but what would happen if the array was already sorted, or at least mostly sorted?

Starting with 4,6,8,7,10,12,11

Pivoting around 4, there are no elements in the left array:

[4] [6,7,8,10,11, 12,14]

Then pivoting around 6 does no better.

[4] [6] [8,7,10,11,12,14]

So in this case, choosing the leftmost value as the pivot leads to the worst case for this algorithm, O(n^2). It takes O(n) time to compare each element to the pivot, and we have to perform this algorithm n times (once for each item in the array).

As we increase the number of elements in the array, the amount of time it takes to sort increases quadratically. That is not good, so if you expect your arrays to be mostly sorted, quick sort is probably not for you.

In the best case scenario, when we choose the middle element as the pivot for each operation each partition operation still takes O(n). To see how many sorts we will need, consider that each sublist is half the size of the main list. We are only sorting until the size of the sublists get to 1. So how many times must you divide a number by 2 until it gets to 1? Put another way, how many times must we double the number 1 until we exceed the number of elements in the array? That is the definition of a logarithm,( the exponent of 2 that will equal a given number. Log(8) = 3 because 2^3 = 8 ). So we need to perform log(n) recursions, and the total efficiency of the algorithm turns out to be O(nlog(n))

There are many variations of this algorithm, for example, partitioning the array in place instead of in new arrays, reducing the memory requirements. Others have to do with how you choose the pivot element. For example, you could choose a small number of items from the list and take the median of this selection as the pivot. Understanding the data you are trying to sort – what will it likely look like when you want to sort it, will help you implement a quicksort in the most efficient manner.

Below is a basic implementation in java, using the value in the center of the array as the pivot:

/** * Sorts the input array list from lowest to highest using the quick sort algorithm * Written By Aaron Zimmerman * http://www.algorithmatica.com * @param the list to be sorted * @return the sorted array */ public static ArrayList<Integer> quicksort(ArrayList<Integer> data){ if (data.size() <= 1){ return data; //array is sorted } int pivot = data.size() / 2; Integer pivotValue = data.remove(pivot); ArrayList<Integer> less = new ArrayList<Integer>(); ArrayList<Integer> more = new ArrayList<Integer>(); for(Integer item : data){ if (item <= pivotValue){ less.add(item); } else { more.add(item); } } ArrayList<Integer> returnList = new ArrayList<Integer>(); returnList.addAll(quicksort(less)); returnList.add(pivotValue); returnList.addAll(quicksort(more)); return returnList; }]]>

To use, just change the @dbName variable to be whatever your database is named.

Credit to Pinal Dave, I’m sure parts of this came from his blog, though I can’t say for sure which. His blog is a great source for SQL snippets, one of the places I always tend to find what I’m looking for. http://blog.sqlauthority.com/

DECLARE @dbName NVARCHAR(100) SET @dbName = 'dbname'; WITH physical AS ( SELECT avg_fragmentation_in_percent , fragment_count , page_count , avg_page_space_used_in_percent , record_count , avg_record_size_in_bytes , object_id , index_id FROM sys.dm_db_index_physical_stats(DB_ID(@dbName), null, null, null, 'Sampled') ) SELECT o.name AS 'Table' ,i.name AS 'Index Name' ,i.Type_Desc AS 'Index Type' ,user_seeks AS 'Seeks' ,user_scans AS 'Scans' ,user_lookups AS 'Lookups' ,user_seeks + user_scans + user_lookups as 'Total Reads' ,user_updates AS 'Updates' ,CASE WHEN user_updates = 0 THEN null ELSE ((user_seeks + user_scans + user_lookups) / CAST(user_updates AS FLOAT)) END AS 'ReadToWriteRatio' ,IndexColumns.IndexedColumns AS 'Indexed Columns' ,Included.IncludedColumns AS 'Included Columns' ,i.filter_definition AS 'Filter Expression' ,IndexSize.IndexSizeInKB AS 'Index Size kb' ,physical.avg_fragmentation_in_percent AS 'Fragmentation Percent' ,physical.fragment_count AS 'Fragment Count' ,physical.page_count AS 'Page Count' ,physical.avg_page_space_used_in_percent AS 'average page space used percent' ,physical.record_count AS 'Record Count' ,physical.avg_record_size_in_bytes * 8 AS 'Avg Record Size Kb' FROM sys.objects o JOIN sys.indexes i ON o.object_id = i.object_id LEFT OUTER JOIN sys.dm_db_index_usage_stats s ON i.object_id = s.object_id AND i.index_id = s.index_id LEFT OUTER JOIN physical ON physical.object_id = o.object_id AND physical.index_id = i.index_id OUTER APPLY ( SELECT SUBSTRING ( ( SELECT ',' + col.NAME + CASE WHEN ic.is_descending_key = 1 THEN ' DESC ' ELSE '' END FROM sys.Indexes ix JOIN sys.Index_Columns ic on ix.Index_ID = ic.Index_Id and ix.object_id = ic.object_id INNER JOIN sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id and ic.is_included_column = 0 WHERE ix.Object_ID = o.object_id and ix.Index_ID = i.Index_id ORDER BY ic.index_column_id ASC FOR XML PATH('') ),2,200000 ) as 'IndexedColumns' ) IndexColumns OUTER APPLY ( SELECT SUBSTRING ( ( SELECT ',' + col.NAME FROM sys.Indexes ix JOIN sys.Index_Columns ic ON ix.Index_ID = ic.Index_Id AND ix.object_id = ic.object_id INNER JOIN sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id AND ic.is_included_column = 1 WHERE ix.Object_ID = o.object_id AND ix.Index_id = i.index_id FOR XML PATH('') ),2,200000 ) AS 'IncludedColumns' ) Included OUTER APPLY ( SELECT 8 * SUM(a.used_pages) AS 'IndexSizeInKB' FROM sys.partitions AS part JOIN sys.allocation_units AS a ON a.container_id = part.partition_id WHERE part.Index_id = i.index_id and part.object_id = i.object_id GROUP BY part.OBJECT_ID, part.Index_ID ) IndexSize WHERE o.type = 'u'AND i.type IN (1, 2) -- Clustered and Non-Clustered indexes AND s.database_id = DB_ID(@dbName) AND OBJECTPROPERTY(S.[OBJECT_ID],'IsUserTable') = 1 ORDER BY o.name]]>