Cody

# Problem 2730. Graph Algorithms 3: Number of Connected Components

Solution 2211373

Submitted on 14 Apr 2020 by Asif Newaz
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
a = ones(3) - eye(3); b = zeros(3); mat = [a b; b a]; n = 2; assert(isequal(cComponents(mat),n))

a = digraph with properties: Edges: [12×2 table] Nodes: [6×0 table] b = 2 2 2 1 1 1 y = 2

2   Pass
a = randi(20); mat = ones(a) - eye(a); n = 1; assert(isequal(cComponents(mat),n))

a = digraph with properties: Edges: [342×2 table] Nodes: [19×0 table] b = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 y = 1

3   Pass
mat = [0 1 0 0 1; 1 0 1 0 0; 0 1 0 1 0; 0 0 1 0 1; 1 0 0 1 0]; assert(isequal(cComponents(mat),1))

a = digraph with properties: Edges: [10×2 table] Nodes: [5×0 table] b = 1 1 1 1 1 y = 1

4   Pass
a = ones(3) - eye(3); b = zeros(3); mat = [a b b; b a b; b b a]; n = 3; assert(isequal(cComponents(mat),n))

a = digraph with properties: Edges: [18×2 table] Nodes: [9×0 table] b = 3 3 3 2 2 2 1 1 1 y = 3

5   Pass
a = ones(3) - eye(3); b = zeros(3); p = floor((randi(20)+3)/3)*3; mat = []; for i= 1:p c = [repmat(b,1,i-1) a repmat(b,1,p-i)]; mat = [mat;c]; end assert(isequal(cComponents(mat),p))

a = digraph with properties: Edges: [126×2 table] Nodes: [63×0 table] b = Columns 1 through 30 21 21 21 20 20 20 19 19 19 18 18 18 17 17 17 16 16 16 15 15 15 14 14 14 13 13 13 12 12 12 Columns 31 through 60 11 11 11 10 10 10 9 9 9 8 8 8 7 7 7 6 6 6 5 5 5 4 4 4 3 3 3 2 2 2 Columns 61 through 63 1 1 1 y = 21