Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3530 Discussion Session #6 Yilin Shen. Outline Chapter 7 o Q35p 250 o Q39p 265 Chapter 8 o Problem 8.5.3: Rearranging Railroad carsp 289 o Problem.

Similar presentations


Presentation on theme: "COP 3530 Discussion Session #6 Yilin Shen. Outline Chapter 7 o Q35p 250 o Q39p 265 Chapter 8 o Problem 8.5.3: Rearranging Railroad carsp 289 o Problem."— Presentation transcript:

1 COP 3530 Discussion Session #6 Yilin Shen

2 Outline Chapter 7 o Q35p 250 o Q39p 265 Chapter 8 o Problem 8.5.3: Rearranging Railroad carsp 289 o Problem 8.5.4: Switch Box Routingp 294 Chapter 9 o Problem 9.5.3: Image-Component Labelingp 341 o Explain the code of Problem 9.5.3

3 Q35 An n × n square matrix M is an antidiagonal matrix iff all entries M(i, j) with i + j ≠ n + 1 equal zero. o Give a sample of a 4 × 4 antidiagonal matrix. o Show that the antidiagonal matrix M has at most n nonzero entries. o Devise a way to represent an antidiagonal matrix in a one-dimensional array of size n. o Use the representation of (c) to arrive at the code for the C++ class antidiagonalMatrix that includes methods for the get and set operations. o What is the time complexity of your get and set codes? o Test your code.

4 Solution A sample antidiagonal matrix is given below: 0 0 0 1 0 0 3 0 0 0 0 0 6 0 0 0 In an antidiagonal matrix there can be at most one nonzero entry in each row; for row i, this nonzero entry is in column n+1-i. Therefore, an n x n antidiagonal matrix can have at most n nonzero entries. We can put the antidiagonal element M(i, n+1-j) in position i-1 of a one-dimensional array.

5 Q39 Suppose that a 500 × 500 matrix that has 2000 nonzero terms is to be represented. How much space is needed when a 500×500 two-dimensional array of type int is used? How much space is needed when sparseMatrix is used? How many nonzero elements must an m × n matrix have before the space required by sparseMatrix exceeds that required by an m × n two-dimensional array? You may assume that T is int.

6 Solution We shall ignore the space overheads of the array of arrays representation used by C++ and consider only the space for the 250000 elements in the 500 x 500 array. At 4 bytes per element, this works out to 1000000 bytes. When a sparseMatrix is used, we need 12 bytes for each nonzero element. So, the 2000 nonzero element matrix takes 24000 bytes. Using the same assumptions as in (a), 4mn bytes are taken by the two-dimensional array representation and 12p (p is the number of nonzero terms) by the sparse matrix representation. The sparse matrix representation takes more space when p > mn/3

7 Problem 8.5.3: Problem 8.5.3: Rearranging Railroad Cars A freight train has n railroad cars. o Each is to be left at a different station. o Assume that the n stations are numbered 1 through n and that the freight train visits these stations in the order n through 1. The railroad cars are labeled by their destination. To facilitate removal of the railroad cars from the train, we must reorder the cars so that they are in the order 1 through n from front to back. o When the cars are in this order, the last car is detached at each station. We rearrange the cars at a shunting yard that has an input track, an output track, and k holding tracks between the input and output tracks.

8 Example Figure 8.6(a) shows a shunting yard with k = 3 holding tracks H1, H2, and H3. The n cars of the freight train begin in the input track and are to end up in the output track in the order 1 through n from right to left. In Figure 8.6(a), n = 9; the cars are initially in the order 5, 8, 1, 7, 4, 2, 9, 6, 3 from back to front. Figure 8.6(b) shows the cars rearranged in the desired order. Figure: A three-track example InitialFinal

9 Solution Strategy To rearrange the cars, we examine the cars on the input track from front to back. o If the car being examined is the next one in the output arrangement, we move it directly to the output track. o If not, we move it to a holding track and leave it there until it is time to place it in the output track. The holding tracks operate in a LIFO manner and only the following moves are permitted o A car may be moved from the front (i.e., right end) of the input track to the top of one of the holding tracks or to the left end of the output track. o A car may be moved from the top of a holding track to the left end of the output track.

10 Solution of the Example 369 H1H1 H2H2 H3H3 247 742963581 Input Track Holding Track 654321987 Output Track 8

11 Assignment Rule The least restrictions on future car placement arise when the new car u is moved to the holding track that has at its top a car with smallest label v such that v > u Time Complexity o O(numberOfTracks ∗ numberOfCars) o Better Analysis --- Using Binary Search Tree O(numberOfCars ∗ log(numberOfTracks))

12 Problem 8.5.4: Switch Box Routing Given a rectangular routing region with pins at the periphery, determine routable switch box Pairs of pins are to be connected together by laying a metal path between the two pins. This path is confined to the routing region and is called a wire Wire intersections are forbidden Each pair of pins that is to be connected is called a net Segments that are not parallel to these axes as well as segments that are not straight lines are permissible

13 Example The wire routing of Figure 8.8(b) has a pair of intersecting wires (those for nets (1, 4) and (2, 3)) The routing of Figure 8.8(c) has no intersections. Since the four nets can be routed with no intersections routable switch box

14 Solution Strategy Idea: when a net is connected, the wire partitions the routing region into two regions o If there is now a net with one pin in one region and the other in a different region, this new net cannot be routed and the routing instance is unroutable o If there is no net with this property, then since the wires cannot cross between regions, we can attempt to determine whether each region is independently routable

15 Cont. Suppose the nets for Figure 8.8(a) are (1, 5), (2, 3), (4, 7), and (6, 8), is it routable? 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 Routable if stack is empty at last Time Complexity? O(n) Stack

16 Problem 9.5.3: Image-Component Labeling A digitized image is an m × m matrix of pixels. In a binary image each pixel is either 0 or 1 o A 0 pixel represents image background o A 1 represents a point on an image component Component Pixels o Two pixels are adjacent if one is to the left, above, right, or below the other o Two component pixels that are adjacent are pixels of the same image component The objective of component labeling is to label the component pixels so that two pixels get the same label iff they are pixels of the same image component

17 Example A 7 x 7 gridLabeled Components Figure: Image-Component Labeling example

18 Solution Strategy The components are determined by scanning the pixels by rows and within rows by columns When an unlabeled component pixel is encountered, it is given a component identifier / label. This pixel forms the seed of a new component Determine the remaining pixels in the component by identifying and labeling all component pixels that are adjacent to the seed

19 Time Complexity Θ(m) time to initialize the wall of background pixels Θ(1) time to initialize offsets For each component O(number of pixels in component) The total time spent identifying and labeling nonseed component pixels is O(number of component pixels in image) Overall Complexity O(m 2 )

20 Code // initialize offsets position offset[4]; offset[0].row = 0; offset[0].col = 1; // right offset[1].row = 1; offset[1].col = 0; // down offset[2].row = 0; offset[2].col = -1; // left offset[3].row = -1; offset[3].col = 0; // up // initialize wall of 0 pixels for (int i = 0; i <= size + 1; i++) { pixel[0][i] = pixel[size + 1][i] = 0; // bottom and top pixel[i][0] = pixel[i][size + 1] = 0; // left and right }

21 Cont. // neighbors of a pixel position int numOfNbrs = 4; // scan all pixels labeling components arrayQueue q; position here, nbr; int id = 1; // component id for (int r = 1; r <= size; r++) { for (int c = 1; c <= size; c++) { if (pixel[r][c] == 1) {// new component // get next id pixel[r][c] = ++id; here.row = r; here.col = c; while (true) {// find rest of component for (int i = 0; i < numOfNbrs; i++) {// check all neighbors of here nbr.row = here.row + offset[i].row; nbr.col = here.col + offset[i].col; if (pixel[nbr.row][nbr.col] == 1) {// pixel is part of current component pixel[nbr.row][nbr.col] = id; q.push(nbr); }

22 // any unexplored pixels in component? if (q.empty()) break; here = q.front(); // a component pixel q.pop(); } // end of if } } // end of for c, and for r

23 Questions? Have a good evening!


Download ppt "COP 3530 Discussion Session #6 Yilin Shen. Outline Chapter 7 o Q35p 250 o Q39p 265 Chapter 8 o Problem 8.5.3: Rearranging Railroad carsp 289 o Problem."

Similar presentations


Ads by Google