Is there any special design pattern or algorithm available for this problem?
There are multiple items (A1,A2,A3 ..An) and I want to arrange them, some of them are related to the other ones and only can comes before or after them.
For example A2 can be placed only after A4 and “An” can be placed at the end of the set. But the point is that the sequence of some items can be interchangeable and based on sequence some items should not be in the set.
For examlpe consider this scenario
There are 6 items
A1, A2, A3, A4, A5, A6
And the rules are
A1 must be at the first place (always),
A2 can be after A4,
A5 can be in the set only if A3 has been there before it
A6 comes at the end of the set and it is a mandatory member but it only can be there if all other valid items have been in the set before it !
Valid sets are like this
A1, A4, A3, A2, A5, A6
A1, A4, A2, A6
Invalid sets
A4, A3, A2, A5, A6 (A1 is missed)
A1, A4, A3, A2, A5 (A6 is missed)
A1, A3, A2, A6 (A2 only comes after A4)
Note: I have to validate the input! and input can have any order! I mean I don’t want to sort the items I want to validate an input set from the user
As a sample, based on my example above the below sets are all valid
{A1, A4, A3, A2, A5, A6}
{A1, A4, A2, A3, A5, A6}
{A1, A3, A4, A2, A5, A6}
{A1, A3, A5, A4, A2, A6}
So user might enter any of these as an input and all of them are valid based on the defined conditions!
Any idea of any special design pattern or algorithm which can be applied to this problem? Number of items or the rules might get change in the future!
“BalusC” had been removed my “Design pattern” tag! But so far I think the best way to handle this problem might be the command pattern. I mean I considered each item as a command from the user and I defined a validation process for the command (“canExecute”) I am going to code it in C# and since ICommand interface in .Net has “canExecute” method I think I will use it to validate the command based on condition. (Execute method just added the item to the result set!) I have not coded it yet so I am not sure how complicated the validation process might be. I thought maybe someone has some idea how I can combine the command pattern and a validation algorithm to achieve the goals.
I might be wrong so any idea or suggestion can be helpful. Thanks.
This should be solvable with a variation of a topological sort.
Basically, you build a directed acyclic graph where there is an edge from Ai to Aj if Ai must come before Aj in the result. The topological sort will then give you a valid order for the A's.
This will not deal with the rule that some items may be missing, but that should be simple to layer on top of this.
A1, A4, A2, A6
cannot be a valid set, because
A6 comes at the end of the set and it is a mandatory member but it
only can be there if all other valid items have been in the set before
it
Related
I want to do numerical integration in Mathematica over a large dataset like: {{x1,y1(c)}, {x2,y2(c)}, {x3,y3(c)}, {x4,y4(c)}..} where y1=(a1+c)/b1, y2= (a2+c)/b2, y3=(a3+c)/b3, y4=(a4+c)/b4.... and a1, a2, a3, a4, b1, b2, b3, b4..... are just numbers and c is a constant. After the integration, I want to plot the resulting function as a function of c. How can I do that?
Lets say I have package A version 1 and package A version 2, Will call them A1 and A2 respectively.
If I have a pool of packages: A1, A2, B1, B2, C1, C2, D1, D2
A1 depends on B1, will represent as (A1, (B1)).
Plus A1 depends on any version of package C "C1 or C2 satisfy A1", will represent as (A1, (C1, C2))
combining A1 deps together, then A1 data-structure becomes: (A1, (B1), (C1, C2))
Also B1 depends on D1: (B1, (D1))
A1 structure becomes: (A1, ((B1, (D1))), (C1, C2))
similarly A2 structure is (A2, ((B2, (D2))), (C1, C2))
My question is: How can I select best candidate of package A, where I can select based on a condition (for example, the condition is the package does not conflict with current installed packages).
by combining A1 and A2: ((A1, ((B1, (D1))), (C1, C2)), (A2, ((B2, (D2))), (C1, C2)))
How can I traverse this data structure
So start with A1, if doesn't conflict check B1, if doesn't conflict check D1, if doesn't conflict check (C1, C2), and take one only either C1 or C2.
With this I end up selecting (A1, B1, D1, C1).
In case if A1 or any of its deps did not meet the condition, (for example if B1 conflicts with installed packages), then drop A1 entirely and move to check A2. then end up with (A2, B2, D2, C1).
What kind of traversal would that be?
I have been reading about in-order, pre-order, post-order traversal, and wondering if I need to do something similar here.
Assuming you are asking traversal on a more generic problem rather than working on this instance, I don't think there exists such a traversal.
Note that in-order is only applicable to BINARY trees. Any other kind of tree does not have in-order traversal. If your generic problem has B1, B2, B3, then apparently there wouldn't be a binary tree representation.
One property about traversal, is that the tree has all the information inclusively in the itself. When you traverse over a tree you never worry about "external information". In your case, your tree is not complete in information - you need to depend on external information to see if there is a conflict. e.g. B1 is installed - this information is never in the tree.
You can use adjacency list to represent the data:
Suppose the packages are A1, A2, B1, B2, C1, C2.
And A1 depends on B1 and C2, A2 depends on B1 and C1 and C2.
The above data can be represented as
[A1] -> [B1, C2]
[A2] -> [B1, C1, C2]
Use Topological Sorting to get the order of dependencies
I have the following tables:
A has columns A1 (text) and A2 (number).
B has columns B1 (text), B2 (text), and B3 (number).
Let's say the user fills in B1 with b1 and B2 with b2. I want the value in B3 (call it b3) to be automatically calculated as follows:
Search A1 to find b1.
Get the A2 value corresponding to b1. Call this c1.
Search A1 to find b2.
4.Get the A2 value corresponding to b2. Call this c2.
b3 is min[c1, c2]
Can I do this by making B3 a calculated field, or by using a query?
In Access 2010 and later you could use a Before Change data macro on [TableB] to derive the [B3] value like this:
For more information, see
Create a data macro
square([A1, A2, A3|_], [B1, B2, B3|_], [C1, C2, C3|_]):-
all_diff([A1, A2, A3, B1, B2, B3, C1, C2, C3]).
squares([[]|_]):- !.
squares([[A1,A2,A3|A4], [B1,B2,B3|B4], [C1,C2,C3|C4], [D1,D2,D3|D4], [E1,E2,E3|E4], [F1,F2,F3|F4], [G1,G2,G3|G4], [H1,H2,H3|H4], [I1,I2,I3|I4]]):-
square([[A1,A2,A3], [B1,B2,B3], [C1,C2,C3]]),
square([[D1,D2,D3], [E1,E2,E3], [F1,F2,F3]]),
square([[G1,G2,G3], [H1,H2,H3], [I1,I2,I3]]),
squares([A4, B4, C4, D4, E4, F4, G4, H4, I4]).
I'm getting a uncaught exception: error(existence_error(procedure,square/1),squares/1) - I'm new to Prolog so I'm not quite sure what this is saying (well, where it's saying it's going wrong).
This is inside a sudoku program, by the way.
squares iterates through the 3x3 squares of the sudoku puzzle starting with the top left, middle left, and bottom left - moving to the top middle, middle middle, bottom middle, and so on.
square checks the focus square - A1, A2, A3 are the first three values of the first row
B1, B2, B3 are the first three values of the second row, and so on. It assembles them into one array and checks to make sure they are all different.
on the lines
square([[A1,A2,A3], [B1,B2,B3], [C1,C2,C3]]),
square([[D1,D2,D3], [E1,E2,E3], [F1,F2,F3]]),
square([[G1,G2,G3], [H1,H2,H3], [I1,I2,I3]]),
you'll note that the type is ([[],[],[]]), however, the function, square is ([],[],[]).
Changing the lines to
square([A1,A2,A3], [B1,B2,B3], [C1,C2,C3]),
square([D1,D2,D3], [E1,E2,E3], [F1,F2,F3]),
square([G1,G2,G3], [H1,H2,H3], [I1,I2,I3]),
fixes the problem.
I wish to create a program in Java which will ask the user a number of questions and report some results. It is pretty much like a survey. In order to explain the problem better consider the following example:
Let’s say that there are currently 4 questions available eg Qa, Qb, Qc and Qd. Each question has a number of possible options:
=> Question A has 4 possible options a1, a2, a3 and a4.
=> Question B has 3 possible options b1, b2 and b3
=> Question C has 5 possible options c1, c2, c3, c4 and c5
=> Question D has 2 possible options d1 and d2
Moreover there are some results available which will be reported based on the user’s answers in the above questions. Let’s assume that there are 5 such results called R1, R2, R3, R4 and R5. Each result has a number of characteristics. These characteristics are really answers to the above questions. More precisely:
=> The characteristics of R1 is the set of {Qa = a4, Qb = b2, Qc = c2, Qd = d1}
This says that R1 is related with Qa via the a1 option, with Qb via the b2 option and so on
=> R2: {Qa = a3, Qb = b3, Qc = c3, Qd = d2}
=> R3: {Qa = a4, Qb = b1, Qc = c1, Qd = d2}
=> R4: {Qa = a2, Qb = b2, Qc = c5, Qd = d1}
=> R5: {Qa = a1, Qb = b3, Qc = c4, Qd = d2}
Let’s say that a user U provides the following answers to the questions
{Qa = a4, Qb = b1, Qc = c1, Qd = d1}
The purpose of the program is to report the result which is closer to the user answers along with a percentage of how close it is. For instance since there is no any result which matches 100% the user answers the program should report the results which match as more answers as possible (above a certain threshold eg 50%). In that specific case the program should report the follow results:
=> R3 with 75% (since there are 3 matches on the 4 questions)
=> R1 with 50% (since there are 2 matches on the 4 questions)
Notice that R4 has one match (so 25%) whereas R2 and R5 have no matches at all (so 0%).
The main issue on implementing the above program is that there are a lot of questions (approximately 30) with a number of answers each (3-4 answers each). I am not aware of an efficient algorithm which can retrieve the results which are closer to the user answers. Notice that the way that these results are stored is not important at all. You can assume that the results are stored in a relational database and that SQL query is used to retrieve them.
The only solution I can think of is to perform an exhaustive search but this not efficient at all. In other words I am thinking to do the following:
=> First try to retrieve results which match exactly the user answers:
{Qa = a4, Qb = b1, Qc = c1, Qd = d1}
=> If no results exist then change the option of a question (eg Qa) and try again. For example try:
{Qa = a1, Qb = b1, Qc = c1, Qd = d1}
=> If there is still nothing then try the rest possible options for Qa eg a2, a3
=> If there is still nothing then give Qa the initial user answer (that is a4) and move to Qb to do something similar. For example try something like: {Qa = a4, Qb = b2, Qc = c1, Qd = d1}
=> If after trying all the possible options for all questions one by one there are any results then try changing the options of COMBINATIONS of questions. For example try change the options of two questions at the same time (eg Qa and Qb): {Qa = a1, Qb = b2, Qc = c1, Qd = d1}
=> Then try combinations of three questions and so on...
Clearly the above algorithm would be extremely slow on a large number of questions. Is there any known algorithm or heuristic which is more efficient than the above algorithm?
Thanks in advance
"Only" 30 Questions?
Then the following "stupid" algorithm will probably be faster than any highly "intelligent" and complicated algorithm.
iterate over characteristics
score = 0
iterate over questions
if questions's answer is right in current characteristic
score++
Then add a variable which keeps track of the maximum value and matching characteristic and you are set.
Runtime is size of characteristics * size of questions, whereas the algorithm you are describing can have exponential runtime, and on top of that is much more complicated both for programming and for executing (due to effects as branch misprediction)