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:
clearglobal LEN G H dP=[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 dC=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); endendend
How can I change the code to make it the shortest or is there any new way to solve this problem?
Best Answer