How to eliminate subset of paths?

I have a cell array of paths stored as a variable:
[1,2,3,6,8,10]
[1,2,4,6,8,10]
[1,2,3,6,8,10,11]
[1,2,4,6,8,10,11]
[1,2,4,12]
[1,2,3,6,8,10,13]
[1,2,3,6,8,10,11,13]
[1,2,3,6,8,10,14]
[1,2,4,6,8,10,14]
[1,2,3,6,15]
[1,2,3,6,8,15]
[2,1]
[2,3]
[2,4]
[2,5]
[2,4,5]
[2,3,6]
Is there a way to remove those paths which are subsets of other paths? For eg: In this case [1,2,3,6,8,10] is a subset of [1,2,3,6,8,10,11] and can hence be removed. Similarly [1,2,4,6,8,10] can be removed. But [1,2,3,6,15] is not a subset of [1,2,3,6,8,15]. So the matlab functions like 'ismember' cannot be used. The end result should be:
[1,2,4,6,8,10,11]
[1,2,4,12]
[1,2,3,6,8,10,13]
[1,2,3,6,8,10,11,13]
[1,2,3,6,8,10,14]
[1,2,4,6,8,10,14]
[1,2,3,6,15]
[1,2,3,6,8,15]
[2,1]
[2,5]
[2,4,5]
[2,3,6]
Thank you for your time and help.

 Accepted Answer

p = {[1,2,3,6,8,10]
[1,2,4,6,8,10]
[1,2,3,6,8,10,11]
[1,2,4,6,8,10,11]
[1,2,4,12]
[1,2,3,6,8,10,13]
[1,2,3,6,8,10,11,13]
[1,2,3,6,8,10,14]
[1,2,4,6,8,10,14]
[1,2,3,6,15]
[1,2,3,6,8,15]
[2,1]
[2,3]
[2,4]
[2,5]
[2,4,5]
[2,3,6]};
%
k = [] ;
for i = 1:length(p)
for j = 1:length(p)
if i ~= j
if all(ismember(p{i},p{j})) ;
% p(i) = [] ;
k = [k i];
break
end
end
end
end
pos = 1:length(p) ;
idx = setdiff(pos,k) ;
iwant = p(idx) ;

7 Comments

Thanks for the code. The output I got for this was:
[1,2,4,6,8,10,11]
[1,2,4,12]
[1,2,3,6,8,10,11,13]
[1,2,3,6,8,10,14]
[1,2,4,6,8,10,14]
[1,2,3,6,8,15]
[2,4,5]
Please note that while eliminating paths the position of nodes is also to be considered i.e. [1,2,3,6,15] is not considered a subset of [1,2,3,6,8,15] due to the presence of 8.
How about this?
k = [] ;
for i = 1:length(p)
for j = 1:length(p)
if i ~= j
[temp1,temp2] = (ismember(p{i},p{j})) ;
if all(temp1)
k = [k j];
break
end
end
end
end
pos = 1:length(p) ;
idx = setdiff(pos,k) ;
iwant = p(idx) ;
Still it is not working. Are you sure 'ismember' function can be used here? Since it does not consider the position of elements and only checks for the inclusion of an element in the superset, will it work in this context?
Check temp1, temp2 picking different values. If they follow order, you get all 1's, if they dont follow order there will be one zero.
What i found is that in case A is a subset of B, then temp2 shows consecutive numbers like 1 2 3 4. So I tried modifying the program to eliminate a subset if temp2 shows consecutive numbers 'in order'. I this method right? But still it is not giving correct results. Can you help me finding the error in this
no_of_paths = size(p);
for iv = 1:no_of_paths
for v = 1:no_of_paths
if iv~=v
if length(p{v})>length(p{iv})
[temp1 temp2] = ismember(p{iv},p{v});
for len = 1:(length(p{iv})-1)
if temp2(1,len+1) == ((temp2(1,len))+1)
p{iv}=[];
end
end
else
[temp1 temp2] = ismember(p{v},p{iv});
for len = 1:(length(p{v})-1)
if temp2(1,len+1) == (temp2(1,len))+1
p{v}=[];
end
end
end
end
end
end
p2= p(~cellfun(@isempty, p));
This shall work:
k = [] ;
for i = 1:length(p)
for j = 1:length(p)
if i ~= j
[temp1,temp2] = (ismember(p{i},p{j})) ;
if diff(temp2)==1
k = [k i];
break
end
end
end
end
pos = 1:length(p) ;
idx = setdiff(pos,k) ;
iwant = p(idx) ;
Note that [2,3,6] is there in the first path. So this is not recognized. It should be eliminated right?
Yes. It works. Thanks alot :)

Sign in to comment.

More Answers (0)

Asked:

on 27 Oct 2016

Commented:

on 31 Oct 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!