Every student must hand in his own work, but every student must list the name of the lab partner (if any) on all labs.
This lab has three parts. You and your partner(s) should switch off typing each part, as explained by your lab TA. As one person types the lab, the other should be watching over the code and offering suggestions. Each part should be in addition to the previous parts, so do not erase any previous work when you switch.
The textbook should present examples of the code necessary to complete this lab. However, collaboration is allowed. You and your lab partner may discuss the lab with other pairs in the lab. It is acceptable to write code on the white board for the benefit of other lab pairs, but you are not allowed to electronically copy and/or transfer files between groups.
Input (. for 0 for visibility) 1 . 1 . 1 1 . 1 . , . . 1 . 1 1 . . . 1 1 1 1 1 1 Colors array after 4th row: 2 . 3 . 4 2 . 3 . . . . 3 . 5 6 . . . 5 Output: final colors array: 2 . 3 . 4 2 . 3 . . . . 3 . 5 5 . . . 5 5 5 5 5 5
We use the O(1) find O(N) union algorithms described in Weiss p. 333.
Let our blobs be "four-connected", so Up, Down, Left, Right neighbors are connected but up-and-left, etc. are not (touching corners diagonally doesn't count). For a pixel x, call its upper neighbor xU, its left neighbor xL. So if x is A(i,j), xU is A(i-1,j) for instance.
Part 1:
Then a simplified algorithm skeleton is the following.
It needs checks for array bound violations (or add a top
row and left column of 0s to the input array?), and maybe other
details ;-}.
using indices i and j, scan the image rowwise from upper left to lower right: call x = A(i,j). If x == 0 continue elseif xU == 0 and xL == 0, color(x) = new(color); elseif xU == y and xL == 0, color(x) = y; elseif xL == y and xU == 0, color(x) = y; elseif xL == y and xU == z, { color(x) = y; UNION! y is equiv. to z; }
Part 2:
As Weiss points out, to UNION! color y and
color z sets, use the
brute-force approach of scanning the entire array and setting all
y's
to z or vice-versa.
Part 3: Joint Work:
Use an editor and make some nice, nasty, complete, and convincing
cases (needn't be big) for input and submit the results in your
hand-in.