Skip to main content

Working Through The Efficient Three Sum Algorithm

This morning I spent an hour or so working on recreating the Three Sum algorithm in Java (I know because, for the sake of motivation, I timed myself).  I wanted to recreate the solution suggested in the class I'm following: go through all the possible pairs in the array and select, from the remaining values, all elements that sum to zero when added together with the pairs[1] -- using binary search.

That's the solution that takes you from O(n^3) to O(n^2 log n) -- if I'm getting the notation right.  Because binary search for N elements is a log N algorithm: if you look through N elements by dividing them into halves, you can do that at worst log N times.  (8 elements = 2 halves of 4 elements = 4 parts of 2 elements = 8 singletons, or 8 => 4 => 2 => 1 = 3 steps -- I guess, not including 8 itself = log 8).

Again, I had a few problems:

  • I forgot to put my midpoint value inside the loop (it was not being recalculated each time the high and low points changed).
  • I had the loop running while the low was less than the high, not less than or equal to the high.
The last part screwed up my results by quite a bit.  Where I was supposed to find 80 triples, for instance, I only found 40.

The hard thing about debugging algorithms in Java, for me, is that there's a lot more infrastructure to set up before you can actually run the algorithm.  Java still feels clunkier than JavaScript -- just because it's less permissive.  One simple example: I wanted to feed an array into a function as test data.  In JavaScript you would just write:

foo([1,2,3,4,5,6])

but after a half-hearted Google search and consultation with a friend, I learned that Java wants you to write

foo(new {1,2,3,4,5,6})

-- an anonymous object, if I remember from C#.  But I didn't think of that beforehand.  So it's one of these languages that's just picky.  And then there are different methods inside of a class that have to be called. And then there's all this data going through the methods.  So I also have a lesson for today:

If you want to debug an algorithm running on a big data set, find a representative portion of the data that you understand and work with that.

Otherwise you'll just get lost.

I'll still have to think a bit more to figure out how to debug recursive functions -- because in that case, the call-stack kind of becomes your big data set.  And it's hard to think about what things might be going wrong inside a very big call stack.

One more obvious moral for the day as a parting gift:

Don't run binary search on arrays that haven't been sorted.

LOL.

[1] Another thing I thought about was how you make sure you get all the unique triples that sum to zero.  That's why, in the algorithm I'm following, you define each inner loop over one plus the value of the outer loop.  I think a long time ago I tried to solve this problem by deleting elements from the array once the program had found mates for them -- but that couldn't have been the most efficient way to do it.  So in the final version of the algorithm, look at each element in the array and find pairs for it from all the elements following it, then accept only binary search results that are later than the pair.  And that raises another issue -- you can only run this version of the algorithm on a set.  If there are multiple items that sum to zero with a pair, binary search won't get you the right one.  That's when you would need to start thinking about deleting things from the array -- but it would screw up the rest of the book-keeping.

Comments

  1. "If you want to debug an algorithm running on a big data set, find a representative portion of the data that you understand and work with that."

    Yes that is a great lesson to learn. Always start with the most simple representation of the problem and master that before adding additional layers to it.

    ReplyDelete

Post a Comment

Popular posts from this blog

It's a Date

I guess I should really be putting these things up in GitHub.  The way I see it, the coding journal is just a place to share the code I write or study along with any notes I have about it.  It's sort of a documentation LiveJournal, if you will. Anyway, this is a "study" for my project idea: create an app that will prompt the user for two dates, then calculate the difference between them. The burden of this study is twofold: (1) convert dates in standard American form (e.g. December 15, 1993) into dates in standard American numeric form (e.g. 12/15/1993); (2) create a numerical representation of the date. To process the date, I started with a list of the months.  I then used a loop to create a dictionary that would attach a value to each month. Next I had to parse the user entry (I haven't added any debugging for incorrect entries yet). I did so by splitting the entry into "raw" data.  I used my dictionary to process the month name into a number, str...

Nailing Down The Meaning Of "This" In JavaScript

Sense-certainty itself has thus to be asked: What is the This? If we take it in the two-fold form of its existence, as the Now and as the Here, the dialectic it has in it will take a form as intelligible as the This itself. To the question, What is the Now? we reply, for example, the Now is night-time. To test the truth of this certainty of sense, a simple experiment is all we need: write that truth down. A truth cannot lose anything by being written down, and just as little by our preserving and keeping it. If we look again at the truth we have written down, look at it now, at this noon-time, we shall have to say it has turned stale and become out of date.   Hegel,  Phenomenology of Spirit  S. 95 Overview In philosophy, English words like "this" are referred to as indexicals, with the implication that (A) they are used to point at something and (B) what they point at is determined by context (just as, when I point at someone, who I am pointing at is determined ...

Compiling and Executing Java Files With -cp

I decided I was going to "man up" and figure out how to compile a java program with an external dependency from the command line instead of relying on an IDE-- the DOS command line, to be more specific. I ran into a few problems: 1.  The external dependency was given to me as a java file.  I experimented compiling it as a .jar, but I wasn't sure how to import a class from a .jar, so I ended up compiling it into a class. 2.  When I tried to run the file, I got an error saying that the class had been compiled with a different version of Java than my JRE.  The Internet told me to check my path variable for Java.  It sure looked like it was pointing to the latest JRE (and the same version of Java as my compiler).  I asked the Internet again and found the following command: for %I in (java.exe) do @echo %~$PATH:I I'm not exactly sure what the syntax of that magic command is (intuitively it's returning the path that executes when I run the "java" com...