about states of finite state machine - fsm

the question is let sigma =(1,2,3,$). I need to draw its diagram which outputs 1 when sum is >_5. if it is over 5, the amount will be carried over.
Im wondering what would be the state in this case. Can I let A = ive seen 1 B=ive seen 2, C=ive see 3 and D = accepted state?

No, you cannot use just three states. Your FSM must, somehow, keep counting the current sum of the input values so far. The problem is that a FSM has no memory (that would be another kind of automata) so it has to be one state for every possible combination of numbers that evaluate to all possible sums.
That is: you need a state that means ("the current sum is 0").Let that state be state 0. That would be your initial state. Other states would be "the current sum is 1" (state 1), "the current sum is 2" (state 2), .... , "the current sum is 5" (state 5), "the current sum is 6" (state 6), etc (no, you won't need an inifinite number of states, as for example, from state 5, a transition with value 1 leads you again to the state 1.
From state 0, a transition with 1 leads you to state 1. Transition with 2, to state 2 and transition with 3 to state 3. Easy, isn't it?
From state 3, for example, a transition with 1 leads you to transition 4, Transition with 2 to state 5, which is an accept state, and transition with 3 to state 6, which is also an accepted state.
To put another example: from state 6, a transition with 1 leads you to... state 2. That's right: state 6 means that the current sum is 6, which exceeds 5 in 1. So this is like transition 1, but it is an accepted state, so transitions from state 6 go to the same places as transitions from state 1 would go. This will help you building your FSM.
In fact, the larger state number is determined by the largest value you can sum with 1,2 and 3 that overflows 5 for the first time. That would be 1+1+1+1+3 = 7. So you need to define from state 0 to state 7, and of course, a final state when $ is detected.

You don't need to keep track of the current sum. You just need to keep track of the total sum modulo 5.
One way of doing this is to let your state space be A0, A1, A2, A3, A4, and B0, B1, B2. If you enter an "A" state, you output 0, and if you enter a "B" state, you output 1.
Your state transitions depend on the next digit that you see. For example:
If you are in A1, and the next digit you see is 2, you transition to A3, and output 0.
If you are in A4, and the next digit you see is 1, you transition to B0, and output 1.
If you are in B2, and the next digit you see is 1, you enter A3, and output 0.
If you are in B2, and the next digit you see is 3, you enter B0, and output 1.
In general, suppose you are in state XY, where X is either A or B, and Y is 0, 1, 2, 3, or 4. Let D be the next digit you see. The next state you will enter will be X'Y', where:
X' is A if D + Y < 5.
X' is B if D + Y >= 5.
Y' is D + Y modulo 5.

Related

Knapsack problem in R: How to use loop in R to check for each item instead of whole column

I've been taking a "Simulation" course in my uni and now have a task to solve a knapsack problem.
Basically I have 3 transporters (g1, g2, g3) which each can carry different weights, on top of that you can put each item 5 times in a transporter. My idea is to use a while loop so it would run as long as the total weight is below the maximum weight. And that the program would pick the item with the highest relation first as long as their pick index doesnt equal 0.
But here I got stuck, it seems that it takes all items of the column at once per loop and also doesnt stop when the value exceeds the maximum (it returns weight = 748 at the end).
Hence: how do I get the loop to just check per item and not the whole column and how can I ensure that it doesnt exceed the weight limit?
Thanks a lot for your help in advance!!
WeightIndex<- c(2, 3, 4, 1, 7, 5, 8, 15, 9, 11) #indexing the weight of each item
WorthIndex <-c(3, 4, 5, 1, 3, 7, 3, 21, 11, 10)#indexing the worth of each item
Relation <- WorthIndex/WeightIndex #creates a relation between weight/worth of the item
PickIndex <- rep(5,10) #each item can be picked 5 times
Items <-cbind(ItemIndex, WeightIndex, WorthIndex, Relation, PickIndex) #binding the indexes to a matrix
g1 <- 300 #Max weight of carrier 1
g2 <- 250 #max weight of carrier 2
g3 <- 200 #max weight of carrier 3
ItemsSorted<-Items[order(Items[,4],decreasing=TRUE),] #Sorts the items after the highest Relation of weight to worth
ItemsSorted1 <- ItemsSorted #creates a copied matrix for the first loop
TWE1 <-0#Total Weight of carrier 1
TWO1 <-0 #total worth of carrier 1
print (ItemsSorted1) #bug check
while (TWE1 <= g1){ #while the condition is true, the program should execute a for loop
if(ItemsSorted1[,5]!= 0) #in theory the loop should go bottom down and take the items with the highest price/weight relation first and it should only take items whose pick-index doesnt equal 0
{
TWE1<-TWE1+ItemsSorted1[,2] #the weight of the taken item gets appended to the list of the total weight
TWO1 <-TWO1 + ItemsSorted1[,3] #the worth of the taken item gets appended to the list of total worth
ItemsSorted1[,5] <- ItemsSorted1[,5] - 1 #deducts 1 from the pick- Index as the item has been taken
TWE1 <- sum(TWE1) #turns the list into one number
TWO1 <- sum(TWO1) #turns the list into one number
print(TWE1) #bug check
}
}
print(paste("Total weight C1",TWE1))
print(paste("Total worth C1",TWO1))
print(ItemsSorted1)```
Skimming your code, it looks like you're missing some way to index which number you're referencing in the ItemsSorted1 matrix. Your comment after the while statement says you're expecting the code to run a for-loop, but as written, it doesn't have a variable to increment.
You might be able to do something like the following to get at your expected behavior:
i <- 1 # create row-id variable to keep track of position in ItemsSorted1
while (TWE1 <= g1){
if(ItemsSorted1[i,5]!= 0){ # go item by item through the sorted list;
TWE1<-TWE1+ItemsSorted1[i,2] #the weight of the taken item gets appended to the list of the total weight
TWO1 <-TWO1 + ItemsSorted1[i,3] #the worth of the taken item gets appended to the list of total worth
ItemsSorted1[i,5] <- ItemsSorted1[i,5] - 1 #deducts 1 from the pick- Index as the item has been taken
TWE1 <- sum(TWE1) #turns the list into one number
TWO1 <- sum(TWO1) #turns the list into one number
print(TWE1) #bug check
}
else if (i<nrow(ItemsSorted1){
i <- i+1
}else{
break
}
}

State diagram, calculate the value after these functions

I have this picture of a state diagram and have to calculate the value of x after a few events. The events are e1-e2-e2-e2-e2
The x would be 2 in the beginning.
First event is e1, so I think it would become 4 after that event.
Next is e2 and I was wondering because the exit is x=x-1, so would it go to state B, because it is less that 4, or C because it was 4, but became 3 in the exit?
And lets suppose it goes to B, and becomes 5, and we do e2 again. Would nothing happen because the only possibility is x>5 and it is equal to 5?
Assuming that the guard between A and C is x>=4 (since there is no e defined) I made a small transition table:
So the final state should be B and X is 11.
In UML state machines, the guards are evaluated, when beeing in the original state. I.e., when receiving e2 the first time, x is 4 and thus you take the transition to C, unser the assumption that e is x (otherwise it doesnt make sense) . After you decided going to C, and thus leave A, you aubstract 1 from x due to the exit ocndition. When beeing in C, you can change B by trigger of e2, which is unguarded (the guard x>5 belongs to the transition from B to C). Now x is 6, as you add 3 due to the entry condition. Then you receive the next e2 and transide to B, where you add 1, so x is now 7. When receiving the next e2, you check the guard on the transition to C, which demands that x is greater 5, which holds. So lets go to C and execute the entry action once again. So x is now 10. Then you get one more e2, so the state changes to C and its entry action is executed, thus x is 11.
So after the execution of the given events, x is 11 and the statemachine is in state B.

Playing with an Array [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 5 years ago.
Improve this question
Hi cant figure out solution for below problem (here is a link to the original problem: Sumita and equal array
). please help?
Sumita is playing with a Array A of size N. she wants to make all the values of A to be equal. she can multiply any number of the values in the array any number of times by X, Y and Z. Your task is to tell her whether she can do it or not. Print "She can" if she can do it else print "She can't" without ""
Input :
First line of the input will contain T (No. of test cases).
For each test case, first line will contain four space separated integers denoting N, X, Y and Z.
Then next line will contain N space separated integers of A
Output :
For every test case, print the required answer in a new line.
Constraints :
1 ≤ T ≤ 5
2 ≤ N ≤ 10^5
X, Y, Z ∈ {2, 3, 5, 7}
1 ≤ Ai ≤ 10^9
SAMPLE INPUT:
2
2 2 2 2
2 4
3 2 3 2
2 6 7
SAMPLE OUTPUT:
She can
She can't
Explanation:
Test case #1: Multiply first value by 2.
Test case #2: Not possible.
My work so far: I find out lcm of X,Y,Z if each element inside that array is divided or can divide by lcm then ans is she can else she can't
Since you give ideas of your own but no code, I will do the same: give some ideas but no code. If you try coding this idea and get stuck, edit your question by showing your code and ask for more help.
You have the right idea of finding the Least Common Multiple (LCM) of the numbers in the given array. For each element of the array, you then can calculate the quotient of the LCM and the element--this quotient is guaranteed to be a positive integer. Then see if there is any prime divisor of that quotient that is not X, Y, or Z. If there is such a divisor, then your task is not possible: "She can't". If there is no such divisor, then this element passes the test. If all elements pass this test, the task is possible: "She can".
For your first array [2, 4] and X, Y, Z values [2, 2, 2], the array's LCM is 4. The first quotient is 4 // 2 = 2, the only prime divisor of that quotient is 2, and that is in the list of X, Y, Z. The second quotient is 4 // 4 = 1, which has no prime divisors, so that element also passes the test. All elements pass the test. so the task is possible.
For your second array [2, 6, 7] and X, Y, Z values [2, 3, 2], the array's LCM is 42. The first quotient is 42 // 2 = 21, which has the prime divisors 3 and 7. The first one (3) is in your list of X, Y, Z, but the last (7) is not, so the task is impossible. There is no need to check the quotients for the other elements of the array.
Can you program that? Note that I used Python's lists rather than arrays. Also note that this algorithm depends on the possible values of X, Y, Z being prime numbers--if they could be composite numbers, the algorithm would need to be adjusted and would be more complicated. To be more precise, the algorithm would need to be changed if any two possible values of X, Y, Z were distinct and were not relatively prime (had a Greatest Common Divisor greater than one).

Why my function is looping until Prolog crashes

I can't seem to understand why my function is looping until Prolog crashes:
isTerminalRow(_,_,_,Count,10):-
Count > 4.
isTerminalRow(B,A,Index,Count,Move):-
checkValue(B,Index,A,V),
C2 is V + Count,
I2 is Index + 1,
Move1 is Move + 1,
isTerminalRow(B,A,I2,C2,Move1).
checkValue(B,Index,A,V):-
getE(Index,B,Value),
Value = A, V is 1
; V is 0.
getE(1,[H|_],H). % get nth element
getE(I,[_|T],L):-
I1 is I - 1,
getE(I1,T,L).
The call is
?- isTerminalRow([w,w,w,w,w,e,e,e,e,e],w,1,0,10).
From your is uses, Move and Count are ground when you call isTerminalRow. For your first clause to fire, when Count becomes larger than 4, Move must be 10.
If not, the first clause does not fire; it doesn't even get a chance to consider the value of Count, and the execution continues with the second clause, which just loops (if checkValue/4 doesn't fail, that is).
Your termination conditions are too specific. Chances are, they are never met.
update: from your comments, Move is already 10 in your query, and Count is 0, so the first clause fails. After that, Move is always greater than 10 because you increment it with Move1 is Move + 1, and there's no chance for Count > 4 to even be tested, ever.

Write a DFA to recognize the following language

The question is:
Write a DFA to recognize the regular language L1 = {w ={1,2,3} | the sum of the digits in w is divisible by 5}
More so, based on the input 1 , 2 , 3 the remainder of the sum should be 0 when dividing by 5. I am almost done this question but I can't seem to understand how to find the correct remainder when the input is 3. Since I have done most of the work I have a picture that I will link so you can understand where I am stuck.
Start State: q0
Accept State: q0
My problem is how to control the input 3 so the choices for it will lead to a remainder of 0 when dividing by 5.
Here's some hints:
Have one state for each possible remainder modulo 5.
Given state x and character c, have the transition take you to state (x + c) modulo 5.
Think about what your accept state would be given the meaning of your states.
Hope this helps!

Resources