cop 3530 discussion session #6 yilin shen. outline chapter 7 o q35p 250 o q39p 265 chapter 8 o...

23
COP 3530 Discussion Session #6 Yilin Shen

Upload: monica-hunt

Post on 18-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: 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

COP 3530Discussion Session #6

Yilin Shen

Page 2: 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

Outline• Chapter 7

o Q35 p 250 o Q39 p 265

• Chapter 8o Problem 8.5.3: Rearranging Railroad cars p 289o Problem 8.5.4: Switch Box Routing p 294

• Chapter 9o Problem 9.5.3: Image-Component Labeling p 341o Explain the code of Problem 9.5.3

Page 3: 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

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.

Page 4: 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

Solution• A sample antidiagonal matrix is given below: 

0 0 0 10 0 3 00 0 0 06 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. 

Page 5: 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

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.

Page 6: 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

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

Page 7: 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

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. 

Page 8: 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

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

Initial

Final

Page 9: 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

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 permittedo 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.

Page 10: 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

Solution of the Example

3 6 9H1 H2 H3

2 4 7

7 4 2 9 6 35 8 1

Input Track

Holding Track

6 5 4 3 2 19 8 7

Output Track

8

Page 11: 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

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 Complexityo O(numberOfTracks ∗ numberOfCars)o Better Analysis --- Using Binary Search Tree O(numberOfCars∗ log(numberOfTracks))

Page 12: 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

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

Page 13: 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

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

Page 14: 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

Solution Strategy• Idea: when a net is connected, the wire partitions

the routing region into two regionso 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

Page 15: 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

Cont.

• Suppose the nets for Figure 8.8(a) are (1, 5), (2, 3), (4, 7), and (6, 8), is it routable?

12345678

• Routable if stack is empty at last

• Time Complexity? O(n)Stack

Page 16: 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

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 1o A 0 pixel represents image backgroundo A 1 represents a point on an image component

• Component Pixelso Two pixels are adjacent if one is to the left, above, right, or below the othero 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

Page 17: 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

Example

A 7 x 7 grid Labeled Components

Figure:  Image-Component Labeling example

Page 18: 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

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

Page 19: 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

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(m2)

Page 20: 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

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 }

Page 21: 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

Cont.// neighbors of a pixel position int numOfNbrs = 4;

// scan all pixels labeling componentsarrayQueue<position> q;position here, nbr;int id = 1; // component idfor (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); } }

Page 22: 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

// 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

Page 23: 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

Questions?

Have a good evening!