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:
- it is faster
- it is simpler, (that's why it is fast).
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