You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MATLAB中含公共值的网络分组问题求解

Hey there! Let's work through this network grouping problem together. First, let's break down what we're really dealing with: this is essentially a graph coloring problem where each network is a node, and we draw an edge between two nodes if they share common vehicle IDs (meaning they can't be in the same group). Our goal is to color these nodes so adjacent nodes have different colors, while keeping the group sizes as balanced as possible.

First, Fix the Conflict Detection

Your current code only checks the last column against previous ones, but we need a full "conflict matrix" that tracks which networks can't be grouped together. Let's rewrite that part to capture all pairwise conflicts:

a = [
1 2 2 1;
2 3 5 12;
0 4 6 13;
0 5 7 14;
0 6 8 15;
0 0 9 16;
0 0 0 17;
0 0 0 18;
0 0 0 19;
0 0 0 20;
0 0 0 21;
0 0 0 22
];

n = size(a, 2); % Total number of networks
conflict = zeros(n); % n×n matrix: conflict(i,j)=1 if networks i&j share values

% Fill the conflict matrix
for i = 1:n
    for j = i+1:n % Avoid duplicate checks (i,j and j,i are the same)
        common_vals = intersect(a(:,i), a(:,j));
        if ~isempty(common_vals)
            conflict(i,j) = 1;
            conflict(j,i) = 1; % Conflict is mutual
        end
    end
end

This matrix will tell us exactly which networks are incompatible with each other. For your example, it'll show:

  • Network 1 conflicts with 2 and 3
  • Network 2 conflicts with 1 and 3
  • Network 3 conflicts with 1 and 2
  • Network 4 has no conflicts with anyone

Now, Group the Networks with Balance in Mind

We'll use a greedy approach first to assign groups, then add a quick step to balance group sizes:

groups = cell(n, 1); % Stores the networks in each group
group_id = zeros(n, 1); % Tracks which group each network belongs to
current_group_count = 0;

% Step 1: Assign initial groups greedily
for net = 1:n
    % Find all groups that conflict with the current network
    conflicting_groups = unique(group_id(conflict(net,:) == 1));
    conflicting_groups = conflicting_groups(conflicting_groups ~= 0); % Ignore unassigned networks

    % Pick the first available group that doesn't conflict
    assigned_group = 1;
    while ismember(assigned_group, conflicting_groups)
        assigned_group = assigned_group + 1;
    end

    % Create a new group if needed
    if assigned_group > current_group_count
        current_group_count = assigned_group;
        groups{assigned_group} = [];
    end

    % Add the network to its group
    group_id(net) = assigned_group;
    groups{assigned_group} = [groups{assigned_group}, net];
end

% Step 2: Balance group sizes (optional but helpful)
% Move networks from larger groups to smaller compatible groups
for net = 1:n
    current_g = group_id(net);
    % Look for smaller groups that don't conflict with this network
    possible_groups = [];
    for g = 1:current_group_count
        if g == current_g
            continue;
        end
        % Check if the network can join group g (no conflicts with any member)
        can_join = true;
        for net_in_g = groups{g}
            if conflict(net, net_in_g) == 1
                can_join = false;
                break;
            end
        end
        if can_join && length(groups{g}) < length(groups{current_g})
            possible_groups = [possible_groups, g];
        end
    end
    % If a valid smaller group exists, move the network there
    if ~isempty(possible_groups)
        target_g = possible_groups(1); % Pick the smallest valid group
        groups{current_g} = groups{current_g}(groups{current_g} ~= net);
        groups{target_g} = [groups{target_g}, net];
        group_id(net) = target_g;
    end
end

% Print the final result
for g = 1:current_group_count
    fprintf('group%d包含network%s\n', g, num2str(groups{g}));
end

What This Does for Your Example

Running this code on your matrix will produce exactly the result you want:

  • group1包含network1
  • group2包含network2 4
  • group3包含network3

The greedy step first assigns network 1 to group 1, network 2 to group 2 (since it conflicts with 1), network 3 to group 3 (conflicts with 1 and 2), then network 4 can join group 2 (no conflicts with network 2). The balance step keeps things evenly sized since group 2 only has two networks, which is as balanced as possible given the constraints.

内容的提问来源于stack exchange,提问作者Kaan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:36:12