The shortest way to find the sequence of all 1s to all 0s
Show older comments
The sequence P has a total of N bits, given the initial state (0 or 1). The given number d is the number of changes each time. Find the least number of changes method process (including the process) from A to all 0 sequence.
For example: P=[1 1 1 1], d=2, then the shortest change sequence is [0 0 1 1]->[0 0 0 0].
Of course, there are also sequences that can never be changed successfully.
The code I wrote, it can only give all the results that can be changed, but not necessarily the least number of times. For example, A=[1 1 1 1 1 1 1 1 1 1 1 1], D=9. The correct result should be [0 0 0 0 0 0 0 0 0 1 1 1]->[1 1 1 1 1 1 0 0 0 0 0 0]->[0 0 0 1 1 1 1 1 1 1 1 1 1] ->[0 0 0 0 0 0 0 0 0 0 0 0].
This is the code I wrote:
clear
global LEN G H d
P=[1 1 1 1 1 1 1 1 1 1 1 1];
d=6;
LEN=length(P);
H=length(find(P==0));
G=graph();
G=G.addnode(num2str(P));
dd(P);
p=plot(G);
function dd(P)
global LEN G H d
C=nchoosek(1:LEN,d);
for i=1:length(C)
D=P;
D(C(i,:))=~D(C(i,:));
L=length(find(D==0));
if ~any(H==L)
H=[H L];
G=G.addnode(num2str(D));
G=G.addedge(num2str(P),num2str(D));
dd(D);
end
end
end
How can I change the code to make it the shortest or is there any new way to solve this problem?
3 Comments
Walter Roberson
on 24 Jul 2020
What is a valid change? You say that d is the number of changes each time: does d give the number of bits that are flipped ?
Bruno Luong
on 24 Jul 2020
Walter, look at his first question d is what he calls "number of flips", so yes the number of bits that change values.
Yang Metin
on 25 Jul 2020
Accepted Answer
More Answers (1)
Bruno Luong
on 24 Jul 2020
Edited: Bruno Luong
on 24 Jul 2020
0 votes
For the shortest, you would probably want to take a look of dynamic programming.
A standard problem quite similar to yours is the Coin Change problem: how to pay a given an amount from a set of coins. Here is nice tutorial of Coin Change
In your case the number of 0s is 0 at the start, and you want it to reach N - the length of the array - in mimum number of steps.
Everytime you flip d elements, you change the number of 0s by d, d-2, d-4, ..., 0/1 (depending on d is odd or even), and might be a subset of the above.
It looks pretty much to me you might be able to use the coin change technique with few modifications.
Before you ask, no I won't give you the code. Let you rather study the dynamic programming technique.
3 Comments
Yang Metin
on 25 Jul 2020
Edited: Yang Metin
on 25 Jul 2020
Bruno Luong
on 25 Jul 2020
If you decide to compute the whole graph, which is awfully costly IMO, then you can find shorttest path simply by calling
P = shortestpath(G, s, t)
where s is your initial node having all 1s and t any node where you reach the target array having all 0s.
Yang Metin
on 25 Jul 2020
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!