Fractal Rendering, Digital Circuit Simulation assignment with solutions, Exercises of Algorithms and Programming

Problem set with solutions on Fractal Rendering, Digital Circuit Simulation

Typology: Exercises

2019/2020

Uploaded on 04/30/2020

tiuw
tiuw 🇺🇸

4.7

(18)

286 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Algorithms: 6.006
Massachusetts Institute of Technology September 15, 2011
Professors Erik Demaine and Srini Devadas Problem Set 2
Problem Set 2
Both theory and programming questions are due Tuesday, September 27 at 11:59PM.
Please download the .zip archive for this problem set, and refer to the README.txt file for
instructions on preparing your solutions.
Remember, your goal is to communicate. Full credit will be given only to a correct solution
which is described clearly. Convoluted and obtuse descriptions might receive low marks, even
when they are correct. Also, aim for concise solutions, as it will save you time spent on write-ups,
and also help you conceptualize the key idea of the problem.
We will provide the solutions to the problem set 10 hours after the problem set is due, which
you will use to find any errors in the proof that you submitted. You will need to submit a critique of
your solutions by Thursday, September 29th, 11:59PM. Your grade will be based on both your
solutions and your critique of the solutions.
Problem 2-1. [40 points] Fractal Rendering
You landed a consulting gig with Gopple, who is about to introduce a new line of mobile phones
with Retina HD displays, which are based on unicorn e-ink and thus have infinite resolution. The
high-level executives heard that fractals have infinite levels of detail, and decreed that the new
phones’ background will be the Koch snowflake (Figure 1).
Figure 1: The Koch snowflake fractal, rendered at Level of Detail (LoD) 0 through 5.
Unfortunately, the phone’s processor (CPU) and the graphics chip (GPU) powering the display do
not have infinite processing power, so the Koch fractal cannot be rendered in infinite detail. Gopple
engineers will stop the recursion at a fixed depth nin order to cap the processing requirement. For
example, at n= 0, the fractal is just a triangle. Because higher depths result in more detailed
drawing, this depth is usually called the Level of Detail (LoD).
The Koch snowflake at LoD ncan be drawn using an algorithm following the sketch below:
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Fractal Rendering, Digital Circuit Simulation assignment with solutions and more Exercises Algorithms and Programming in PDF only on Docsity!

Introduction to Algorithms: 6. Massachusetts Institute of Technology September 15, 2011 Professors Erik Demaine and Srini Devadas Problem Set 2

Problem Set 2

Both theory and programming questions are due Tuesday, September 27 at 11:59PM. Please download the .zip archive for this problem set, and refer to the README.txt file for instructions on preparing your solutions. Remember, your goal is to communicate. Full credit will be given only to a correct solution which is described clearly. Convoluted and obtuse descriptions might receive low marks, even when they are correct. Also, aim for concise solutions, as it will save you time spent on write-ups, and also help you conceptualize the key idea of the problem. We will provide the solutions to the problem set 10 hours after the problem set is due, which you will use to find any errors in the proof that you submitted. You will need to submit a critique of your solutions by Thursday, September 29th, 11:59PM. Your grade will be based on both your solutions and your critique of the solutions.

Problem 2-1. [40 points] Fractal Rendering

You landed a consulting gig with Gopple, who is about to introduce a new line of mobile phones with Retina HD displays, which are based on unicorn e-ink and thus have infinite resolution. The high-level executives heard that fractals have infinite levels of detail, and decreed that the new phones’ background will be the Koch snowflake (Figure 1).

Figure 1: The Koch snowflake fractal, rendered at Level of Detail (LoD) 0 through 5.

Unfortunately, the phone’s processor (CPU) and the graphics chip (GPU) powering the display do not have infinite processing power, so the Koch fractal cannot be rendered in infinite detail. Gopple engineers will stop the recursion at a fixed depth n in order to cap the processing requirement. For example, at n = 0, the fractal is just a triangle. Because higher depths result in more detailed drawing, this depth is usually called the Level of Detail (LoD).

The Koch snowflake at LoD n can be drawn using an algorithm following the sketch below:

SNOWFLAKE(n)

1 e 1 , e 2 , e 3 = edges of an equilateral triangle with side length 1 2 SNOWFLAKE-EDGE(e 1 , n) 3 SNOWFLAKE-EDGE(e 2 , n) 4 SNOWFLAKE-EDGE(e 3 , n)

SNOWFLAKE-EDGE(edge, n)

1 if n = = 0 2 edge is an edge on the snowflake 3 else 4 e 1 , e 2 , e 3 = split edge in 3 equal parts 5 SNOWFLAKE-EDGE(e 1 , n − 1) 6 f 2 , g 2 = edges of an equilateral triangle whose 3rd edge is e 2 , pointing outside the snowflake 7 ∆(f 2 , g 2 , e 2 ) is a triangle on the snowflake’s surface 8 SNOWFLAKE-EDGE(f 2 , n − 1) 9 SNOWFLAKE-EDGE(g 2 , n − 1) 10 SNOWFLAKE-EDGE(e 3 , n − 1)

The sketch above should be sufficient for solving this problem. If you are curious about the missing details, you may download and unpack the problem set’s .zip archive, and read the CoffeeScript implementation in fractal/src/fractal.coffee.

In this problem, you will explore the computational requirements of four different methods for rendering the fractal, as a function of the LoD n. For the purpose of the analysis, consider the recursive calls to SNOWFLAKE-EDGE; do not count the main call to SNOWFLAKE as part of the recursion tree. (You can think of it as a super-root node at a special level -1, but it behaves differ- ently from all other levels, so we do not include it in the tree.) Thus, the recursion tree is actually a forest of trees, though we still refer to the entire forest as the “recursion tree”. The root calls to SNOWFLAKE-EDGE are all at level 0.

Gopple’s engineers have prepared a prototype of the Koch fractal drawing software, which you can use to gain a better understanding of the problem. To use the prototype, download and unpack the problem set’s .zip archive, and use Google Chrome to open fractal/bin/fractal.html.

First, in 3D hardware-accelerated rendering (e.g., iPhone), surfaces are broken down into triangles (Figure 2). The CPU compiles a list of coordinates for the triangles’ vertices, and the GPU is responsible for producing the final image. So, from the CPU’s perspective, rendering a triangle costs the same, no matter what its surface area is, and the time for rendering the snowflake fractal is proportional to the number of triangles in its decomposition.

(a) [1 point] What is the height of the recursion tree for rendering a snowflake of LoD n?

  1. log n
  2. n

(c) [1 point] What is the asymptotic rendering time (triangle count) for a node in the recursion tree at level i, for 0 ≤ i < n?

  1. 0
  2. Θ(1)
  3. Θ(^1 i 9 )
  4. Θ( 13 i)

Solution: Levels 0 through n − 1 draw one triangle (spike) for each side that is split into 4 line segments. So the cost is always Θ(1) / node. (d) [1 point] What is the asymptotic rendering time (triangle count) at each level i of the recursion tree, for 0 ≤ i < n?

  1. 0
  2. Θ(^4 i 9 )
  3. Θ(3i)
  4. Θ(4i)

Solution: Θ(4i), obtained by multiplying the previous two answers and reducing the result to the simplest form. (e) [2 points] What is the total asymptotic cost for the CPU, when rendering a snowflake with LoD n using 3D hardware-accelerated rendering?

  1. Θ(1)
  2. Θ(n)
  3. Θ(^43 n)
  4. Θ(4n)

Solution: At level n (SNOWFLAKE-EDGE has argument 0), the termination condition is triggered, and no triangle is drawn. Summing up over all the other levels of the recursion tree, we obtain n− 1 T (n) =

Θ(4i) = Θ(4n) i= The recursion for one side of the snowflake can be written as , T n) =

Θ(1) if n = 0, ( 4 T (n − 1) + Θ(1), if n ≥ 1.

Second, when using 2D hardware-accelerated rendering, the surfaces’ outlines are broken down into open or closed paths (list of connected line segments). For example, our snowflake is one closed path composed of straight lines. The CPU compiles the list of cooordinates in each path to be drawn, and sends it to the GPU, which renders the final image. This approach is also used for talking to high-end toys such as laser cutters and plotters.

(f) [1 point] What is the height of the recursion tree for rendering a snowflake of LoD n using 2D hardware-accelerated rendering?

  1. log n
  2. n
  3. 3 n
  4. 4 n

Solution: n, because the recursion tree is the same as in the previous part. (g) [1 point] How many nodes are there in the recursion tree at level i, for 0 ≤ i ≤ n?

  1. 3 i
  2. 4 i
  3. 4 i+
  4. 3 · 4 i

Solution: 3 · 4 i, because the recursion tree is the same as in the previous part.

(h) [1 point] What is the asymptotic rendering time (line segment count) for a node in the recursion tree at level i, for 0 ≤ i < n?

  1. 0
  2. Θ(1)
  3. Θ(^19 i)
  4. Θ(^1 i 3 ) Solution: Line segments are only rendered at the last level, so the cost is 0 for levels 0 ≤ i < n. (i) [1 point] What is the asymptotic rendering time (line segment count) for a node in the last level n of the recursion tree?
  5. 0
  6. Θ(1)

n Θ(^19 )

  1. Θ(^1 n 3 ) Solution: At the last level, each side is turned into one straight line, so the cost is Θ(1) / node. (j) [1 point] What is the asymptotic rendering time (line segment count) at each level i of the recursion tree, for 0 ≤ i < n?
  2. 0
  3. Θ(^4 i 9 )
  4. Θ(3i)

Solution: n, because the recursion tree is the same as in the previous part.

(n) [1 point] How many nodes are there in the recursion tree at level i, for 0 ≤ i ≤ n?

  1. 3 i
  2. 4 i
  3. 4 i+
  4. 3 · 4 i

Solution: 3 · 4 i, because the recursion tree is the same as in the previous part. (o) [1 point] What is the asymptotic rendering time (line segment length) for a node in the recursion tree at level i, for 0 ≤ i < n? Assume that the sides of the initial triangle have length 1.

  1. 0
  2. Θ(1)
  3. Θ(^1 i 9 )
  4. Θ( 13 i)

Solution: Line segments are only rendered at the last level, so the cost is 0 for levels 0 ≤ i < n

(p) [1 point] What is the asymptotic rendering time (line segment length) for a node in the last level n of the recursion tree?

  1. 0
  2. Θ(1)
  3. Θ(^19 n)
  4. Θ( 13 n)

Solution: At level 0, each side has length 1. Levels 0 through n − 1 split a length-l side into 4 length- l 3 segments. So, at each level 0 ≤ i ≤ n, the length of a side is 1 i

Specifically, at level n, each segment has length 1 n n 3 = Θ(^13 ). (q) [1 point] What is the asymptotic rendering time (line segment length) at each level i of the recursion tree, for 0 ≤ i < n?

  1. 0
  2. Θ(^4 i 9 )
  3. Θ(3i)
  4. Θ(4i)

Solution: 0, because all the line segments are rendered at the last level. (r) [1 point] What is the asymptotic rendering time (line segment length) at the last level n in the recursion tree?

  1. Θ(n)
  2. Θ(^4 n 3 )
  3. Θ(4n)

Solution: We multiply two previous answers and reduce the result to the simplest form. 3 · 4 n^

1 n^4 n · Θ( ) = Θ( ) 3 3 (s) [1 point] What is the total asymptotic cost for the CPU, when rendering a snowflake with LoD n using 2D software (not hardware-accelerated) rendering?

  1. Θ(1)
  2. Θ(n)
  3. Θ(^43 n)
  4. Θ(4n)

Solution: The sum of the previous two answers. T (n) = Θ(^4 n 3 )

The fourth and last case we consider is 3D rendering without hardware acceleration. In this case, the CPU compiles a list of triangles, and then rasterizes each triangle. We know an algorithm to rasterize a triangle that runs in time proportional to the triangle’s surface area. This algorithm is optimal, because the number of pixels inside a triangle is proportional to the triangle’s area. For the purpose of this problem, you can assume that the area of a triangle with side length l is Θ(l^2 ). We also assume that the cost of rasterizing is greater than the cost of compiling the line segments.

(t) [4 points] What is the total asymptotic cost of rendering a snowflake with LoD n? Assume that initial triangle’s side length is 1.

  1. Θ(1)
  2. Θ(n)
  3. Θ(^4 n 3 )
  4. Θ(4n)

Solution: Θ(1). See the proof below. Also, intuitively, the snowflake does not overflow the rectangle in the visualizer (or the phone’s screen), so its area must be bounded by a constant. The triangles are adjacent and do not overlap, so the sum of their areas equals the snowflake’s area. (u) [15 points] Write a succinct proof for your answer using the recursion-tree method.

Solution: The proof follows the structure suggested by the previous parts of the problem. In the first part (3D hardware rendering), we argued that there are n levels in the recursion tree, and for 0 ≤ i ≤ n, level i has 3 · 4 i^ nodes. Each node at level

A

B

O

Figure 3: 2-input XOR gate; A and B supply the inputs, and O receives the output.

This problem will walk you through the best known approach for fixing performance issues in a system. You will profile the code, find the performance bottleneck, understand the reason behind it, and remove the bottleneck by optimizing the code.

To start working with AMDtel’s circuit simulation source code, download and unpack the problem set’s .zip archive, and go to the circuit/ directory.

The circuit simulator is in circuit.py. The AMDtel engineers pointed out that the simulation input in tests/5devadas13.in takes too long to run. We have also provided an automated test suite at test-circuit.py, together with other simulation inputs. You can ignore these files until you get to the last part of the problem set.

(a) [8 points] Run the code under the python profiler with the command below, and identify the method that takes up most of the CPU time. If two methods have similar CPU usage times, ignore the simpler one. python -m cProfile -s time circuit.py < tests/5devadas13.in Warning: the command above can take 15-30 minutes to complete, and bring the CPU usage to 100% on one of your cores. Plan accordingly. What is the name of the method with the highest CPU usage?

Solution: The first line in the profiler output identifies find min as the method with the largest CPU usage. (b) [6 points] How many times is the method called?

Solution: The first line in the profiler output indicates that find min is called 259964 times. (c) [8 points] The class containing the troublesome method is implementing a familiar data structure. What is the tightest asymptotic bound for the worst-case running time of the method that contains the bottleneck? Express your answer in terms of n, the number of elements in the data structure.

  1. O(1).
  2. O(log n).
  3. O(n).
  4. O(n log n).
  5. O(n log^2 n).
  6. O(n^2 ).

Solution: find min belongs to the PriorityQueue class, which contains an array-based priority queue implementation. Finding the minimum element in an array takes Θ(n) time.

(d) [8 points] If the data structure were implemented using the most efficient method we learned in class, what would be the tightest asymptotic bound for the worst-case running time of the method discussed in the questions above?

  1. O(1).
  2. O(log n).
  3. O(n).
  4. O(n log n).
  5. O(n log^2 n).
  6. O(n^2 ).

Solution: A priority queue implementation based on a binary min-heap takes Θ(1) time to find the minimum element, since it’s at the top of the heap. (e) [30 points] Rewrite the data structure class using the most efficient method we learned in class. Please note that you are not allowed to import any additional Python libraries and our test will check this. We have provided a few tests to help you check your code’s correctness and speed. The test cases are in the tests/ directory. tests/README.txt explains the syntax of the simulator input files. You can use the following command to run all the tests. python circuit test.py To work on a single test case, run the simulator on the test case with the following command. python circuit.py < tests/1gate.in > out Then compare your output with the correct output for the test case. diff out tests/1gate.gold For Windows, use fc to compare files. fc out tests/1gate.gold We have implemented a visualizer for your output, to help you debug your code. To use the visualizer, first produce a simulation trace. TRACE=jsonp python circuit.py < tests/1gate.in > circuit.jsonp On Windows, use the following command instead. circuit jsonp.bat < tests/1gate.in > circuit.jsonp Then use Google Chrome to open visualizer/bin/visualizer.html We recommend using the small test cases numbered 1 through 4 to check your imple- mentation’s correctness, and then use test case 5 to check the code’s speed.

MIT OpenCourseWare http://ocw.mit.edu

6.00 6 Introduction to Algorithms Fall 2011

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.