How to design/word Dynamic Programming sub problems? [closed] - algorithm

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying out the bottom up dynamic programming method and I have a few problems with it.
I have learnt to arrive at the required solution by storing previously computed values in either a 1D or 2D array and referring to them when necessary. The problem is I am not able to backtrack using the values stored in my array.
For example, if the problem is the classic 'Longest Subsequence' problem, I can arrive at the value of the longest subsequence, but I am not able to backtrack through the stored values and find what letters/digits appear in the subsequence.
I have gone through a lot of university course tutorials and youtube tutorials, but nobody seems to explain how a person can 'word' the subproblem correctly.
Does anybody have tips on how to craft the subproblems and maintain array values so that backtracking possible and easy?

A simple solution is to keep a second array with the same dimensions as the first, and call it your index array, and use it to track the location of the element that contributed to your choice.
So in a 2d example:
LetA be the standard dynamic programming array
Let I be the index array
If the value A[x,y] is decided by A[x0,y0], then I[x,y]=(x0,y0).
When trying to backtrack from A[i,j], access I[i,j] to find the next element in the backtrack chain.
You can use default values for the array I so you know when you have reached the end of the chain.

Related

Data structure to work with matrix-based problems [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I often find myself unsure about what data structure is better for matrix-based algorithms.
By "matrix-based algoritm" I mean algorithms like Needleman-Wunsh alignment. There are many algorithms that are visually represented with a matrix.
I wonder what should I choose:
Array of arrays
Linked-list of linked-lists
Hash table where Key is a tuple like (line, column)
etc
What do I have to consider when facing this impasse?
Obs: My question is "language-open". You can use any programming language in your answer.
What data structure to use depends on your algorithm and how you will access that matrix. For example, If size is fixed and there is a need for fast access, it is better to use 2 dimensional array, because no matter what you use, you will have to allocate that space anyway. If the size of matrix is determined dynamically, then probably vector of vector (or similar data structure depending on language).
Another question is if your matrix is sparse and extremely big(like in digital geometry algorithms) and you have to do arithmetic operations on that matrix very often, then triple format data structures could be useful , for example compressed row storage that could be created using 3 vectors. You can read more in this link https://de.wikipedia.org/wiki/Compressed_Row_Storage
Hope it helps

Python: Fit geometric forms into a board matrix? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
In the past few days i was thinking of a solution for an AI problem. The problem sounds like this:
I want to determine an arrangement for a few given geometric forms(that do not exceed the given board size) on a square board of given size, in such a way that the board will be uniform covered and the forms will
not overlap.
I want to apply Depth first search / Greedy best first search, but it feels difficult to find a proper representation of the forms and the actual board in order to traverse it. I'm new to python so that makes it a bit more difficult. Any suggestions?
Visual example:
What you are describing is a variation on rectangle/square fitting. Versions of the problem exist where unused cells have to be minimised for an optimal placement of the figures, whereas other versions, like the one you are describing, require for the whole board to be covered uniformly. These are called 'perfect square/rectangle placement' problems.
Typical ways to solve these problems involve the usage of finite integer domains representing the variables of the rectangles and a set of constraints making sure the geometrical placements are valid ones (i.e. don't cross the board borders, don't overlap with each other mutually, ..).

Algorithm for finding similar words [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In order to support users learning English, I want to make a multiple-choice quiz using the vocabulary that the user is studying.
For example, if the user is learning "angel" then I need an algorithm to produce some similar words such as "angle" and "angled"
Another example, if the user is learning "accountant" then I need an algorithm to produce some similar words such as "accounttant" and "acountant", "acounttant"
You could compute the Levenshtein Distance from the starting word to each word in your vocabulary and pick the 2 or 3 shortest ones.
Depending on how many words are in your dictionary this might take a long time though, so I would recommend bailing out after a certain (small) number of steps - i.e. if you have made 3 mutations and still haven't arrived at your target word then stop and move on to the next one.

Ruby Object manipulation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
We have an algorithm that compare ruby objects coming from MongoDB. The majority of the time spent, it taking the results (~1000), assigning a weight to them, and comparing them to a base object. This process takes ~2 sec for 1000 objects. Afterwards, we order the objects by the weight, and take the top 10.
Given that the number of initial matches will continue to grow, I'm looking for more efficient ways to compare and sort matches in Ruby.
I know this is kind of vague, but let's assume they are User objects that have arrays of data about the person and we're comparing them to a single user to find the best match for that user.
Have you considered storing/caching the weight? This works well if the weight depends only on the attributes of each user and not on values external to that user.
Also, how complex is the calculation involving the weight associated with a user and the "base" user? If it's complex you may want to consider using a graph database, which can store data that is specific to the relation between 2 nodes/objects.

How to start reverse engineering a algorithm by key [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I have a set of keys e.g (IENDKCAGI, RZRLBYBFH) and now I want to find the algorithm of it. I allready know the basics of cryptography but I don't know how to start reverse engeneering the algorithm.
I don't search a certain algorithm, I'm just interested how the process would look like.
cheers
EDIT: I don't want to crack any key!!! I buy the software I need!
I'm just interested in the approach of reengeneering a checksum from the result, thats my conceptual formulation, without knowing the algorythm. This topic is more theorethical, but in my opinion it has a certain relevancy also for stackoverflow
You can analyze it to some degree, at least enough to rule out several possibilities. You say you have a set of keys, and I'm not sure what you mean by that, so pretend for discussion that the left value is the plaintext and the right value is the encrypted equivalent.
You can determine that the left value has only one repeating character, "I", and that the right value has two, "R" and "B". From that you can rule out a simple substitution cipher, even one with characters rearranged.
Both values appear to have only characters in the range [A-Z] (a larger sample would help confirm), so you can rule out encryption techniques that yield binary results, like most block and stream ciphers. In fact, use of such a limited character set implies that it was designed for use by people rather than machines. That would imply a relatively simple cipher technique, but may also involve an additional key to which you do not have access.

Resources