Archive for category Programming
Maximum Cost Path down a Number Triangle
Posted by stphung in Personal, Programming on May 15, 2010
My solution of problem number 18 and 67 from projecteuler.net. I highly recommend tackling some of the problems there, they are fun and challenging.
The Problem
The problem at hand is from projecteuler.net, problem number 18 or 67 which can be found at http://projecteuler.net/index.php?section=problems&id=18 and http://projecteuler.net/index.php?section=problems&id=67. The difference between problem 18 and 67 is the input size. The problem summarized is, starting at the top of a triangle structure of numbers where a number can move to adjacent numbers on the row below, find the maximum cost path to the base. In the example given on projecteuler, this triangle made up using four rows of numbers the maximum sum is 3+7+4+9=23.
3
7 4
2 4 6
8 5 9 3
Brute Force Feasibility
The problem that is asked for us to solve is to find the maximum cost path down a triangle of 15 rows. The feasibility of testing every case for 15 rows isn't bad. By observation, the number of paths in a triangle with 1 row is 1, 2 rows is 2, 3 rows is 4, so for the general case we have for n rows 2n-1 distinct paths. For n=15 we only have 215-1=16,384 distinct path which is definitely possible to brute force but the number of distinct paths increases exponentially with n which would make for an algorithm with O(2n) runtime. Problem 67 however is not possible to brute force since there are 100 rows which equates to 2100-1 distinct paths.
Using Optimal Substructure
Looking at simpler problems where n is small such as n=1 or n=2 the problem feels like it should have some optimal substructure since the maximum path from one level can probably be used somehow to compute the maximum path to the next level.
If n=1 the maximum cost path is the cost of that single number since there is only one choice, in this case, a.
a
If n=2 there are two distinct paths down the triangle which are, choose the single number in row 1, then either choose the first number or the second number in row 2. In this particular triangle the paths would be ab or ac. The values a+b and a+c here would represent the maximum cost path down triangle such that you end up at b or c respectively. The answer to the question in this case is max(a+b, a+c).
a
b c
If n=3 there are now four distinct paths down the triangle.
a
b c
d e f
Optimal substructure becomes most apparent for this value of n in my opinion. If we had precomputed values to the n=2 case which were the maximum cost path to get to b and c then we could easily compute the maximum cost path to get to d, e, and f. The maximum cost path to get to a node k will be denoted mk. Using this structure produced by solving n=2 it is intuitive how to solve n=3.
ma
mb mc
d e f
Looking at d, there is only one option,b , which makes sense since there are no paths that can get to d that also contain c which means md = (mb+d).
Looking at e, there are complications here since it is an interior node that can be traversed to by b or c. The maximum cost path to reach e would be computed as me = max(mb+e, mc+e).
Looking at f, this is a case which is similar to computing md since f is not an interior node which can be traversed to by two nodes, mf=(mc+f).
After these solving n=3 we have the following structure.
ma
mb mc
md me mf
The solution to the problem for n=3 is max(md, me, mf) since the maximum cost path to get to the bottom of the triangle is going to be the maximum cost path to get to one of the nodes at the bottom of the triangle.
Generalizing
Computing the maximum cost paths for row n+1 given the maximum cost paths from row n is easy. The intuition here is that after computing the maximum cost paths for row n there are only a few choices for maximum cost paths for row n+1. As a generalization, the solution for a triangle of n rows is to compute the the maximum cost paths for for row k using the maximum cost paths from row k-1 starting at row 2 until k=n then taking the reduction using the max operator on the maximum cost paths in row n. Building the triangle top down we now have an algorithm which runs in O(n2/2) which is definitely faster than O(2n) for large values of n.
The approach taken here can basically be classified as modified Dijkstra which relaxes edges if the weights are higher rather than lower, the graph structure is emulated using a grid structure in my implementation.
QuickMark 3 Released
Posted by stphung in Gaming, Programming on April 25, 2010
For those who have stuck to using QuickMark, I salute you for I was stuck on how to extend this addon further before that was growing to be more and more unmaintainable as time went on. The most I have been doing as of lately has been updating the version number and ensuring nothing was broken.
QuickMark has been long overdue for a rewrite, I am now using the Ace3 Framework. The effect of this hopefully is a more maintainable addon that I can add features to easily and quickly. To start this off right, I have added a highly requested feature which has been locking and unlocking of the user interface. I am truly excited to see what direction this addon takes off in, I will sincerely consider each and every one of your feature requests in a democratic manner.
Thank you,
Steven
Using Java’s new style for-loop
Posted by stphung in Personal, Programming on September 23, 2009
I felt like making a screencast today, so I did. I will be making more of these in the future so I figured I’d talk about a topic I think I know half decent so I decided on talking about using Java’s new style for-loop. This discusses the semantics of the new style for-loop along with how to roll your own data types which are able to be used together with it.
You can find the screencast here (right-click and download).
Where’s my hashCode?
Posted by stphung in Programming on June 22, 2009
Every now and then we run into code where the what is written does not reflect the author’s intent, that’s a bad thing. Here’s an example of Java code that does not reflect the author’s intent most of the time.
pubic class Foo {
private int[] myInts;
// Constructors...
// Methods...
public int hashCode() {
return this.myInts.hashCode();
}
}
What’s the problem? The catch here is that invoking hashCode() on an array does not take into consideration the data that resides inside the array and is the same as invoking System.identityCode(…). This could lead to potential problems when using hash code dependent structures such as HashSet or HashMap. If you run the following code you will find that both printouts are identical.
public class Test {
public static void main ( String[] args ) {
int[] a = new int[1];
a[0] = 1;
System.out.println(a.hashCode()); // A value
a[0] = 5000;
System.out.println(a.hashCode()); // That value again
}
}
What do we do then? The fix for this is to use the Arrays.hashCode(…) method which is overloaded for all of the primitive arrays and Object array as well. The usage of Arrays.hashCode(…) is shown below.
public class Test {
public static void main ( String[] args ) {
int[] a = new int[1];
a[0] = 1;
System.out.println(Arrays.hashCode(a)); // A value
a[0] = 5000;
System.out.println(Arrays.hashCode(a)); // Another value
}
}
Be careful when using methods, be absolutely certain about the behavior of the methods you invoke. If you are a developer and recognize that you are doing something that might look questionable then document defensively for fear that another developer may come along and fix your “mistake” which was actually what you intended.
JavaOne 2009 Slides
Posted by stphung in Programming, Technology on June 15, 2009
JavaOne 2009 slides are available here. The videos are not yet available but should appear under this same link when they are if they are consistent with JavaOne 2008. Definitely take a look at some of these if you are a Java developer or developer of any type even.
Return from JavaOne
Posted by stphung in Personal, Programming, Technology on June 12, 2009
It’s been a week since JavaOne and I finally have the time to at least talk about what I thought about it. The core of JavaOne in my opinion is that it is an eye opening experience, there are people all over doing very cool things with Java. What you get out of going is incredible if you can process a small fraction of it, what you learn does come in useful in your day to day work. I know my coding practices will change as a result of going to JavaOne this year.
I thought it was very cool how all of the rockstar figures are just roaming about like regular people, you can essentially walk up to them and have a conversation as many did. Some of the most notable technical talks for me came from Joshua Bloch, Bill Pugh, and William Ernst.
JavaOne was very interesting and helpful, it is a must if you are a Java developer. I hope to be there again next year!
Immutability and Functional Languages
Posted by stphung in Programming on March 31, 2009
Introduction
Coming from an object oriented background, recently I was introduced to the functional paradigm. Functional languages tend to lean towards building applications using functions which produce no side effects and have objects be immutable. Producing no side effects means every function has an input, an output, and no state outside of the scope of the method is modified. We are focusing on the second aspect of functional languages which is immutability.
#define Immutability
What does it mean exactly to have objects be immutable? An immutable object is simply an object where after creation its state cannot be changed.
Point p = new Point(2,5); // Create a new Point object with coordinates (2,5).
// If the Point object is immutable, there is nothing we can do to change the state of the object that p references now!
Why immutability?
The question now is, what does immutability buy us, isn’t it merely inconvenient if I can’t modify the state of an object? First of all, something you should be asking yourself before you allow for modification of the state of an object is, do I really need to be able to change its state? If the answer is a blatant no then you should do whatever is necessary in the language you are working in to make this object immutable, why even have mutability if it is not necessary? As Josh Bloch would say, “When in doubt, leave it out”.
Aside from this practice that many developers follow there are actual benefits to immutability. With immutability, concurrent access in these areas is thread safe because nothing will ever change which simplifies your code by removing any necessary locking mechanisms you would need to put in. Also with immutability, shallow copies of an immutable object are just as good as a deep copy. An object reference to an object which cannot change is just as good as an object reference to a new object which cannot change with the same values. The amount of memory saved with using shallow copies per object with the same state is approximately the sum of the memory taken up by all member variables for the object subtracted by the memory used for an object reference.
Consequences of Immutable Objects
Now that we know what immutability is and what it buys us I can dive into some of the consequences of immutable objects. Take the following code for example assuming that Point is immutable:
Point p = new Point(3,5); // Create a new Point object with coordinates (2,5).
for(awhile) {
sleep(100);
p = new Point(p.getX()+1, p.getY()); // Move the point +1 on the x-axis.
}
Something you might be thinking is, why on earth are we making an object every time we move the Point over by +1 on the x-axis? Immutability is the reason! We can’t change the x coordinate of the Point that p references so the only way we can move the Point that p references is by creating a new Point and referencing that Point. This is unfortunate because everytime we do this we need to create a new Point object, the more we do this the more memory we will use. Fortunately it is the case that most object oriented programmers would not take the route of immutability here and simply add a setter to allow modification of the coordinates of a Point.
Functional Languages demand Immutability
Functional languages are not as flexible as object oriented languages however, meaning that you cannot just go in and modify an object since immutability is baseline. How do functional languages deal with the memory inefficiencies of creating so many objects? Take for instance the following clojure(lisp) code:
(def myList (list (range 1000000))) // Set myList to be a list of numbers from 0 to 999999.
(print (rest myList)) // Print all but the first element in myList.
In the more interesting second line we are creating an anonymous list object by using the rest function and passing that returned value into the print function. In order to do this the rest function would make a copy of all of the numbers from 1 to 999999, pack them into a list and return it. This is exactly the type of inefficiencies we discussed above that using immutable objects brings about.
Functional language implementations have indeed thought this through though and to counteract it they use something called “Persistent Data Structures”. Persistent data structures are data structures that keep track of modifications to itself, essentially a diffing mechanism for data structures. Using the example above, the implementation of the language would essentially note that the argument passed into the print function is “myList without the first element”. This save us from needing to copy the numbers from 1 to 999999 and passing that into print, instead we just remember that we are passing in “myList without the first element” which is much cheaper. This type of behavior is implemented in functional languages such as Clojure, Ocaml, and Haskell.
Conclusion
Immutability is a good thing, use it when you can. I have also shredded one of the fallacies I had about programming in a functional language which is that I would be creating so many objects everytime I make a function call that memory usage would be too high. It is probably a good idea to do a bit of reading on some of the implementation details of various languages before making any such assumptions.
One-Time Program Servers
Posted by stphung in Personal, Programming, Technology on December 4, 2008
Created a video presentation for my graduate course at North Carolina State University, CSC574 “Introduction to Computer and Network Security”. The paper of choice was “One-Time Programs” by Goldwasser, Kalai, and Rothblum. This video describes at a high level what one-time programs are, the concept of their creation, applications, and insight to the benefits and drawbacks of using them.
An idea I use the term “One-Time Program Servers” for is also presented at a high level describing a method for abstracting away the hardware component from developers and users of one-time programs. The aim of one-time program servers is to make the transition for creating and using one-time programs seamless.
QuickMark 2 Released
Posted by stphung in Gaming, Programming on October 17, 2008
If you are a World of Warcraft player, QuickMark allows you to set the raid target icon of units quickly rather than having to go through the right-click menu or create keybindings. I have released version 2 of QuickMark which complies with the World of Warcraft 3.0.2 API. This addon was submitted to curse-gaming only to find out that it needs approval now, it appears their policy of just letting people submit has changed. Pretty disappointing after working on it until 7 in the morning. I hope it gets approved but if not I won’t fret, the original reason for this addon was for my personal use, I just thought I would share.

Changes made in this version are using the new 3.0.2 API and also rewriting the user interface to be human readable and slimmer to give more screen real estate to the players. I have provided a link for anyone who would like to download this addon but does not want to wait for it to get approved(or not) at curse-gaming.