2017-03-03 85 views
1

我有一个由三个数字向量组成的矩阵data=[kids,mothers,fathers],其中每个向量包含孩子及其母亲和父亲的ID。孩子的ID是唯一的,但父亲和母亲的身份不是唯一的(兄弟姐妹和同胞兄弟姐妹都存在)。我希望将孩子向量分成两个大小相同的向量,跨越这些向量没有家庭关系。我希望这些载体尽可能大,但可能会出现一些孩子需要丢弃以确保同等大小的载体。使用两个标签向量的向量的等分

我目前的做法是计算每个“家庭组”的大小(通过父母中的任何一个相关的孩子),然后根据每个家庭的规模建立相同大小的组。然后,我设法计算孩子们,每个母亲或父亲都与:

mothersKids = arrayfun(@(x)nnz(mothers==x), mothers); 
mothersKids = unique([mothers,mothersKids],'rows'); 
fathersKids = arrayfun(@(x)nnz(fathers==x), fathers); 
fathersKids = unique([fathers,fathersKids],'rows'); 

这告诉我有多少孩子与单亲父母有关。使用家长的ID,我可以找出哪些孩子是相关的,并基于此建立组。但是,我无法弄清楚如何结合父母双方的信息来创建'家庭团体'。作为一个侧面提示:如果小孩A通过一个父母与孩子B相关并且孩子B通过一个父母与孩子C相关,但小孩A和孩子C之间不存在关系,则为了简单起见如果孩子A和孩子C被安置在同一个家庭组中,我会接受。

EDIT ::

最少例如:

输入:

data = [1,2,3,4,5,6; 11,11,12,12,13,14; 21, 22, 23, 23, 24, 25]; % = [kids,mothers,fathers] 

输出:

kidsInSameFamily = {[1,2],[3,4],[5],[6]}; 
groupOne = [1,2,5]; 
groupTwo = [3,4,6]; 
+0

小例子,... – bla

回答

0

通过构建邻接矩阵,并使用该查找子图解决这一点。同等大小的建筑群似乎是对分区的问题(Partition Problems Brute Force Algorithm)的变化,为此,MATLAB脚本,我可能需要修改我的需要存在于https://people.sc.fsu.edu/~jburkardt/m_src/partition_problem/partition_problem.html

% Get kids associated through their mother. 
storepairs =[]; 
for mom = unique(mothers)' 
    idx = find(mothers==mom); 
    if numel(idx) > 1 
     pairs = nchoosek(idx,2); 
     storepairs = [storepairs; pairs]; 
    end 
end 
% Get kids associated through their father. 
for dad = unique(fathers)' 
    idx = find(fathers==dad); 
    if numel(idx) > 1 
     pairs = nchoosek(idx,2); 
     storepairs = [storepairs; pairs]; 
    end 
end 

% Make pairs symmetric 
storepairs = [storepairs; fliplr(storepairs)]; 
% Add diagonal 
storepairs = [storepairs; [1:size(data,1); 1:size(data,1)]']; 
% Remove non-unique pairs 
storepairs = unique(storepairs,'rows'); 
% Build adjacency matrix 
A = sparse(storepairs(:,1),storepairs(:,2),1); 
% Create a graph object 
G = graph(A); 
% Get sub-graphs within graph 
SG = conncomp(G); 

% Kids in same family 
kidsInSameFamily={}; 
for ii = 1:max(SG) 
    kidsInSameFamily{ii} = kids(SG==ii); 
end