Wednesday, January 14, 2009

Structure or no structure

I was in the advance algorithm class this morning, and there was something came across my mind.
I was shown a basic solution of using dynamic programming in searching for Longest Common Subsequence (LCS) between two sequences / strings. (well this out of context). What's make me think about struct/no struct, is because this solution provides two matrices in the solution, which contains two different types of values, but structure is identical, e.g. 2 mxn/ 2-dimensional arrays. We may assume this subject intentionally use naive approach to explain about more complex thing (about advance algorithm), so it may use simple representation (2 matrices) instead of array of structures.

But in reality which is the best?
For the above example, we may use array of structure (which contains both values) instead of using 2 separate but identical arrays to represent 2 different types of values. I originally advocates that (structure instead of different arrays). But, after seeing the example, i changed my mind. Why? because this approach can also provide several advantages:
  1. it is faster
  2. it is simpler, (that's why it is fast).
Array is simpler than structure, because underlying beneath, compiler construct the housekeeping information for arrays simpler than for structure. Where as for arrays, only 2 info stored ; (1) the types, (2) the length/size. But more information needed to maintain structures i.e. structure description (complex). This also means increased access time (ignorantly the compile time as well, maybe!).

Basically, what i think best is both approaches (structure or multiple arrays) should be considered, regardless what type of information we need to store. E.g. personal information (name, age, address), we may have to considered both approaches - either structure or 3 arrays, instead bluntly approaching towards structure only.

No comments:

Post a Comment