开发者

Find the longest increasing subsequence in a 2-D array

开发者 https://www.devze.com 2023-02-19 01:25 出处:网络
I have this problem as homework and I really just have no idea where to begin.I have implemented the solution using a recursive algorithm (#1), but I just cannot figure out how to solve the problem us

I have this problem as homework and I really just have no idea where to begin. I have implemented the solution using a recursive algorithm (#1), but I just cannot figure out how to solve the problem using a stack... any assistance would be great.

Find the longest increasing sequence of numbers in a 15 x 15 array. For example, if the array, 4x4, contains

97  47  56  36
35  57  41  13
89  36  98  75
25  45  26  17

then the longest increasing sequence of numbers is the sequence of length eight consisting of 17, 26, 36, 41, 47, 56, 57, 97. Note that开发者_开发百科 there are no duplicates in the increasing sequence.

  1. Design a recursive algorithm to solve this problem and implement it in Java.

  2. Design a non-recursive algorithm to solve the same problem using a stack.


Because this is homework, here is a hint: You can convert your array of numbers to a directed acyclic graph. (It is acyclic because there are no duplicates allowed in the sequence.) After that you can use an algorithm to solve the longest path problem, to find a simple path of maximum length in your graph.

0

精彩评论

暂无评论...
验证码 换一张
取 消