Data:
8 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 1 1 0 0 0 1 0 0 0 0 1 0 0 0 1 0 1 0 0 1 0 0 0 1 0 0 1 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 1 0 1 0 0
Corresponding graph:
0 -- 2 -- 4 -- 6 1 --- 3 | \ / | | / \ | 7 --- 5
How algorithm runs:
Read in graph data: Read number of vertices (8) For row 0 For col 0 read in edge value (0) - no edge from 0 to 0 For col 1 read in edge value (0) - no edge from 0 to 1 For col 2 read in edge value (1) - EDGE from 0 to 2 For col 3 read in edge value (0) - no edge from 0 to 3 For col 4 read in edge value (0) - no edge from 0 to 4 For col 5 read in edge value (0) - no edge from 0 to 5 For col 6 read in edge value (0) - no edge from 0 to 6 For col 7 read in edge value (0) - no edge from 0 to 7 For row 1 For col 0 read in edge value (0) - no edge from 1 to 0 For col 1 read in edge value (0) - no edge from 1 to 1 For col 2 read in edge value (0) - no edge from 1 to 2 For col 3 read in edge value (1) - EDGE from 1 to 3 For col 4 read in edge value (0) - no edge from 1 to 4 For col 5 read in edge value (1) - EDGE from 1 to 5 For col 6 read in edge value (0) - no edge from 1 to 6 For col 7 read in edge value (1) - EDGE from 1 to 7 For row 2 For col 0 read in edge value (1) - EDGE from 2 to 0 For col 1 read in edge value (0) - no edge from 2 to 1 For col 2 read in edge value (0) - no edge from 2 to 2 For col 3 read in edge value (0) - no edge from 2 to 3 For col 4 read in edge value (1) - EDGE from 2 to 4 For col 5 read in edge value (0) - no edge from 2 to 5 For col 6 read in edge value (0) - no edge from 2 to 6 For col 7 read in edge value (0) - no edge from 2 to 7 For row 3 For col 0 read in edge value (0) - no edge from 3 to 0 For col 1 read in edge value (1) - EDGE from 3 to 1 For col 2 read in edge value (0) - no edge from 3 to 2 For col 3 read in edge value (0) - no edge from 3 to 3 For col 4 read in edge value (0) - no edge from 3 to 4 For col 5 read in edge value (1) - EDGE from 3 to 5 For col 6 read in edge value (0) - no edge from 3 to 6 For col 7 read in edge value (1) - EDGE from 3 to 7 For row 4 For col 0 read in edge value (0) - no edge from 4 to 0 For col 1 read in edge value (0) - no edge from 4 to 1 For col 2 read in edge value (1) - EDGE from 4 to 2 For col 3 read in edge value (0) - no edge from 4 to 3 For col 4 read in edge value (0) - no edge from 4 to 4 For col 5 read in edge value (0) - no edge from 4 to 5 For col 6 read in edge value (1) - EDGE from 4 to 6 For col 7 read in edge value (0) - no edge from 4 to 7 For row 5 For col 0 read in edge value (0) - no edge from 5 to 0 For col 1 read in edge value (1) - EDGE from 5 to 1 For col 2 read in edge value (0) - no edge from 5 to 2 For col 3 read in edge value (1) - EDGE from 5 to 3 For col 4 read in edge value (0) - no edge from 5 to 4 For col 5 read in edge value (0) - no edge from 5 to 5 For col 6 read in edge value (0) - no edge from 5 to 6 For col 7 read in edge value (1) - EDGE from 5 to 7 For row 6 For col 0 read in edge value (0) - no edge from 6 to 0 For col 1 read in edge value (0) - no edge from 6 to 1 For col 2 read in edge value (0) - no edge from 6 to 2 For col 3 read in edge value (0) - no edge from 6 to 3 For col 4 read in edge value (1) - EDGE from 6 to 4 For col 5 read in edge value (0) - no edge from 6 to 5 For col 6 read in edge value (0) - no edge from 6 to 6 For col 7 read in edge value (0) - no edge from 6 to 7 For row 7 For col 0 read in edge value (0) - no edge from 7 to 0 For col 1 read in edge value (1) - EDGE from 7 to 1 For col 2 read in edge value (0) - no edge from 7 to 2 For col 3 read in edge value (1) - EDGE from 7 to 3 For col 4 read in edge value (0) - no edge from 7 to 4 For col 5 read in edge value (1) - EDGE from 7 to 5 For col 6 read in edge value (0) - no edge from 7 to 6 For col 7 read in edge value (0) - no edge from 0 to 7 Number of components seen so far (C) = 0 Mark all vertices not visited Mark vertex 0 not visited Mark vertex 1 not visited Mark vertex 2 not visited Mark vertex 3 not visited Mark vertex 4 not visited Mark vertex 5 not visited Mark vertex 6 not visited Mark vertex 7 not visited For each vertex in graph V For V=0 is vertex V(0) not visited? (yes) increment C (to 1) Visit(V,C) (Visit(W is 0,CompNum is 1)) Mark vertex 0 as visited Set vertex 0's component number to 1 For each vertex X where there is an edge from 0 to X X=0, edge from 0 to 0? (no) X=1, edge from 0 to 1? (no) X=2, edge from 0 to 2? (yes) is vertex 2 not visited? (yes) Visit(X,CompNum) (Visit(W is 2,CompNum is 1)) Mark vertex 2 as visited Set vertex 2's component number to 1 For each vertex X where there is an edge from 2 to X X=0, edge from 2 to 0? (yes) is vertex 0 not visited? (no) X=1, edge from 2 to 1? (no) X=2, edge from 2 to 2? (no) X=3, edge from 2 to 3? (no) X=4, edge from 2 to 4? (yes) is vertex 4 not visited? (yes) Visit(X,CompNum) (Visit(W is 4,CompNum is 1)) Mark vertex 4 as visited Set vertex 4's component number to 1 For each vertex X where there is an edge from 4 to X X=0, edge from 4 to 0? (no) X=1, edge from 4 to 1? (no) X=2, edge from 4 to 2? (yes) is vertex 2 not visited? (no) X=3, edge from 4 to 3? (no) X=4, edge from 4 to 4? (no) X=5, edge from 4 to 5? (no) X=6, edge from 4 to 6? (yes) is vertex 6 not visited? (yes) Visit(X,CompNum) (Visit(W is 6,CompNum is 1)) Mark vertex 6 as visited Set vertex 6's component number to 1 For each vertex X where there is an edge from 6 to X X=0, edge from 6 to 0? (no) X=1, edge from 6 to 1? (no) X=2, edge from 6 to 2? (no) X=3, edge from 6 to 3? (no) X=4, edge from 6 to 4? (yes) is vertex 2 not visited? (no) X=5, edge from 6 to 5? (no) X=6, edge from 6 to 6? (no) X=7, edge from 6 to 7? (no) X=7, edge from 4 to 7? (no) X=5, edge from 2 to 5? (no) X=6, edge from 2 to 6? (no) X=7, edge from 2 to 7? (no) X=3, edge from 0 to 3? (no) X=4, edge from 0 to 4? (no) X=5, edge from 0 to 5? (no) X=6, edge from 0 to 6? (no) X=7, edge from 0 to 7? (no) For V=1 is vertex V(1) not visited? (yes) increment C (to 2) Visit(V,C) (Visit(W is 1,CompNum is 2)) Mark vertex 1 as visited Set vertex 1's component number to 2 For each vertex X where there is an edge from 1 to X X=0, edge from 1 to 0? (no) X=1, edge from 1 to 1? (no) X=2, edge from 1 to 2? (no) X=3, edge from 1 to 3? (yes) is vertex 3 not visited? (yes) Visit(X,CompNum) (Visit(W is 3,CompNum is 2)) Mark vertex 3 as visited Set vertex 3's component number to 2 For each vertex X where there is an edge from 3 to X X=0, edge from 3 to 0? (no) X=1, edge from 3 to 1? (yes) is vertex 1 not visited? (no) X=2, edge from 3 to 2? (no) X=3, edge from 3 to 3? (no) X=4, edge from 3 to 4? (no) X=5, edge from 3 to 5? (yes) is vertex 5 not visited? (yes) Visit(X,CompNum) (Visit(W is 5,CompNum is 2)) Mark vertex 5 as visited Set vertex 5's component number to 2 For each vertex X where there is an edge from 5 to X X=0, edge from 5 to 0? (no) X=1, edge from 5 to 1? (yes) is vertex 1 not visited? (no) X=2, edge from 5 to 2? (no) X=3, edge from 5 to 3? (yes) is vertex 3 not visited? (no) X=4, edge from 5 to 4? (no) X=5, edge from 5 to 5? (no) X=6, edge from 5 to 6? (no) X=7, edge from 5 to 7? (yes) is vertex 7 not visited? (yes) Visit(X,CompNum) (Visit(W is 7,CompNum is 2)) Mark vertex 7 as visited Set vertex 7's component number to 2 For each vertex X where there is an edge from 7 to X X=0, edge from 7 to 0? (no) X=1, edge from 7 to 1? (yes) is vertex 1 not visited? (no) X=2, edge from 7 to 2? (no) X=3, edge from 7 to 3? (yes) is vertex 3 not visited? (no) X=4, edge from 7 to 4? (no) X=5, edge from 7 to 5? (yes) is vertex 5 not visited? (no) X=6, edge from 7 to 6? (no) X=7, edge from 7 to 7? (no) X=6, edge from 3 to 6? (no) X=7, edge from 3 to 7? (yes) is vertex 7 not visited? (no) X=4, edge from 1 to 4? (no) X=5, edge from 1 to 5? (yes) is vertex 5 not visited? (no) X=6, edge from 1 to 6? (no) X=7, edge from 1 to 7? (yes) is vertex 7 not visited? (no) For V=2 is vertex V(2) not visited? (no) For V=3 is vertex V(3) not visited? (no) For V=4 is vertex V(4) not visited? (no) For V=5 is vertex V(5) not visited? (no) For V=6 is vertex V(6) not visited? (no) For V=7 is vertex V(7) not visited? (no) For each component from 1 to C(2) For component 1 Is vertex 0 part of component 1? (yes) print 0 Is vertex 1 part of component 1? (no) Is vertex 2 part of component 1? (yes) print 2 Is vertex 3 part of component 1? (no) Is vertex 4 part of component 1? (yes) print 4 Is vertex 5 part of component 1? (no) Is vertex 6 part of component 1? (yes) print 6 Is vertex 7 part of component 1? (no) For component 2 Is vertex 0 part of component 2? (no) Is vertex 1 part of component 2? (yes) print 1 Is vertex 2 part of component 2? (no) Is vertex 3 part of component 2? (yes) print 3 Is vertex 4 part of component 2? (no) Is vertex 5 part of component 2? (yes) print 5 Is vertex 6 part of component 2? (no) Is vertex 7 part of component 2? (yes) print 7