Recursive array analysis (math program) - php

I'm making a PHP trigonometry tool using PHP that can analyze how best to solve a given problem.
For instance, I know angle A and sides b and c, and I need the computer to calculate which formulas to use in which order to make the best mathematical solution to finding an unknown value.
Right now, I have created an array with numerous options on how to find the unknown value:
Image of array
The array is made like this:[formula: xxx] is a suggestion on what formula to use to find a previous value.
[target: xxx] is the name of the value we're looking for in order to satisfy the need of the previous formula.
There might be more than one target attached to a formula, because a formula might need more than one information in order to be complete.
At the end of each path there is an array showing a mathematical function which can be solved with the information we have, and if you track backwards from there you have enough information to solve the task which was initially given (which was angle B (cannot be seen on the image, but it is))
So, out of all of these solution options I need to find the shortest solution, the solution which requires the fewest steps.
Keep in mind that a formula might have two unknown variables, which means that we need to calculate the total sum of the shortest path and end up with the an array containing the optimal path.
Bonus question: I'm aware that this only solves the problem using the information provided by the user, maybe at some point along the way we find a variable, which is needed at some other point. Maybe if a function needs two variables, it is faster to find A before B before A calculates a "target" which might be needed in B.
I would really like to have a solution for the first part, but I will need to solve the "bonus question" later on.

Related

How can I identify certain symbols on a scanned form?

I'm building a web application in PHP, and part of the requirement is that I need to be able to quickly process data on a scanned copy of a fairly simple form, and save it to a database for later retrieval.
Given the following image
how can I identify and assign a database field a value of either true or false (true when it sees a tick, and false otherwise)?
I'm thinking along the following line of implementation:
I will keep two copies of the above image - the first will have ticks shown (as above), and the second will be a "clean" copy of the image with the borders left behind. Comparing between the two images will yield a difference; the difference will return either a value of true or false.
There are drawbacks as far as I can observe of the above implementation. What happens if the user scribbles something in it (as seen above) but it does not mean anything? How do I even ensure that the returned values of true or false are assigned to the appropriate columns in the database?
I don't have any code implementation at this point in time, and I'm not asking for it. Rather, I'm asking for guidance on where to look and how I can efficiently do this.
You may try using OpenCV framework for PHP (https://github.com/mgdm/OpenCV-for-PHP, http://mgdm.net/talks/confoo11/making-php-see.pdf) and use contour detection (or any other classificators) to find signs like "V" and skip false-positives.
You might want to use a PHP OCR library.
I will do thin in a following way: I will divide image into 2x6 grid and count black pixels in each row. If the number n contains in <A;B> then we can assume that row is checked. If someone scratch off an answer then n is larger than B.
So if n is in <A;B> range we can check its pattern - for example common part of the all marked rows because of user's handwriting.

Find nearest neighbour points in 2D Array

I have a 2D array with various entries at different positions. However some positions have same value (say 5). I need to find the nearest block with value 5 from any other positioned element.!
Image is in this link :)
This is the image to understand the problem better
In this pic above. We can use the concept of Digital Image Processing to find the m-distance between each blocks. But if the problem space is too big ( suppose an array of 100X100 or 200X200) then the solution in this way will be time taking.
In way to solution I found out these links.
Wikipedia Link for Nearest Neighbour
Apart from this how to map this whole thing in programming...?
You can try any PL/SQL code for this, then you can get the nearest point from there.
The simplest (maybe not most efficient) way is if you use the Wikipedia method #1 which is as follows:
Loop though all the coordinate pairs, finding the distance between them. Formula: sqrt((x2-x1)^2+(y2-y1)^2)
Keep track of which pair are closest to the point you are testing, and the closest distance.
After each calculation, test if the distance is shorter; if so, then overwrite the distance and closest-pair variables.
I can expand this if you like.

All possible combinations from sets

I have a set of numbers:
1,22
1,46
32,1
1,9
32,22
1,14
1,45
1,33
33,22
45,22
32,46
32,9
3,1
3,9
3,22
3,32
3,46
9,22
46,22
46,45
46,33
15,1
15,46
15,6
15,22
15,3
15,9
15,45
15,33
15,32
15,14
I need to get combinations from them with a rule that each new pair can only be appended if the latter number is the same as the first in the pair.
For example if I have a pair {15,1}, the next on can be only {1,46} and the next {46,45}, and the final pair must end with the first number of the whole set. In this case it could be for example {45,1}.
So the end result of sets with 4 set limit would be
{15,1,1,46,46,45,45,1}
I can do basic power sets and generate all possible combinations from set of numbers but this seems to be too advanced for me.
I can do C, Javascript or PHP so all the help or solutions to this are highly appreciated. And for clarification, this is not a homework, this is just something I would like to learn and understand.
This looks as if some graph data structure, and some graph algorithms, would be appropriate. Your graph would comprise nodes (each of which is a number) and edges (each of which represents one of your pairs). Then write the appropriate routine for walking round the graph. It's not entirely clear from your question what the rules for the walk are, but I guess you know.
EDIT
Of course, I should point out that what you have is already a graph data structure, it's called an adjacency list. Google around for algorithms and representations.

Pattern matching for people who dont know algorithms - finding adjacent X's in a grid

I'm wondering what the best method would be for me to approach a problem where I need to find adjacent (horizontal, vertical, diagonal) X's in a grid which is provided.
I wanted to know what the recursive way, and the nonrecursive way would be. I tried a recursive method of checking each column, and then iterating rows - that gives me X's in one direction - should I write seperate recursive functions for the other directions?
Example grid:
XXX0X
0000X
00X00
XXXX0
0000X
output should be :
(0,0),(1,0),(2,0)
(4,0),(4,1)
(2,2),(0,3),(1,3),(2,3)(3,3)
You may want to check out the Flood Fill algorithm. You can find it on Wikipedia.
I think what you're describing is more or less it. What you do is basically:
For a given position:
If it is of the desired color (in your case 'O'):
mark it (say, re-color it to a color 'M'),
recurse on all desirable directions (run the same algorithm
on new positions, which are +/-1 away);
else
do nothing.
In your case, the result are the positions marked 'M'. If you want to find additional adjacencies, you can always reset the ones marked 'M' and start the algorithm on a different position.
EDIT: According to your examples, it seems you're looking for adjacent 'X's. :)

Need an algorithm to find near-duplicate text values

I run a photo website where users are free to enter any tag they like, even tags not used before. As a result, a photo of a tag may sometimes be tagged as "insect" whilst somebody else tags it as "insects".
I'd like to keep the free-tagging capability, yet would like to have a way to filter out such near-duplicates. The total collection of tags is currently at 1,500. My idea is to read all of them from the DB into mem and then run an alghoritm on it that displays "suspects".
My idea of a suspect is that x% of the characters in the string are the same (same char and order), where x is configurable. I could probably code a really inefficient way to do this but I was wondering if there is an existing solution to this problem?
Edit: Forgot to mention: just sorting the tags isn't enough, as that would require me to go through the entire set to find dupes.
There are some flaws in your logic. For example, what happens when the plural of an object is different from the singular (i.e. person vs. people or even candy vs. candies).
If English is the primary language, check out Soundex which allows phonetic matches. Also consider using a crowd-sourced synonym model where users can create links to existing tags.
Maybe the algorithm you are looking for is approximate string matching.
http://en.wikipedia.org/wiki/Approximate_string_matching.
by a given word you can match it to list of words and if the 'distance' is close add it to suspects.
A fast implementation is to use dynamic programming like the Needleman–Wunsch algorithm.
I have made a blog example of this in C# where you can configure the 'distance' using a matrix character lookup file.
http://kunuk.wordpress.com/2010/10/17/dynamic-programming-example-with-c-using-needleman-wunsch-algorithm/
Is "either contains either" fine? You could do a SQL query something like this, if your images are in a database (which would only make sense):
SELECT * FROM ImageTags WHERE INSTR('theNewTag', TagName) > 0 OR INSTR(TagName, 'theNewTag') > 0 LIMIT 1;
If you really want to do this efficiently I would suggest some sort of JavaScript implementation that displays possibilities as the user is typing in a tag that they want. Not only will it save the user time to happily see 5 suggestions as they type. It will automatically stop them from typing "suspects" when "suspect" shows up as a suggestion. That is, of course, unless they really want "suspects" as a point of urgency.
You could load a huge list of words and as the user types narrow them down. I get the feeling that this could be very simplistic esp if you want to anticipate correctly spelled words. If someone misses a letter, they'll probably go back to fix it when they see a list of suggestions that isn't at all what they meant to type. And when they do correctly type a word it'll pop up in the suggestions.

Categories