-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhw6.tex
306 lines (280 loc) · 11.7 KB
/
hw6.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
\documentclass[12pt, a4paper]{report}
\usepackage{tikz}
\usetikzlibrary{positioning,shadows,arrows}
\begin{document}
\title{Homework 6}
\author{Chang Wang}
\date{\today}
\maketitle
\section{Shortest path problem.}
\subsection{Give a reasonably efficient dynamic programming algorithm for finding a shortest path between p1 and p2.}
Here I use $R_{B_{i}^{u}}^{B_{j}^{v}} (1 \leq i < j \leq k , 1 \leq u < v \leq n)$ denotes the shortest path from aperture $u$ on Barrier $i$ to aperture $v$ on Barrier $j$. \\
First, I introduce two virtual Barriers 0 and (k+1) which go through point $p_{1}$ and $p_{2}$ respectively, and are parallel to other Barriers. Then, the dynamic programming fomulation is:
\begin{center}
$R_{B_{0}^{p_{1}}}^{B_{k+1}^{p_{2}}} = min \{R_{B_{0}^{p_{1}}}^{B_{i}^{u}} + R_{B_{i}^{u}}^{B_{j}^{v}} + R_{B_{j}^{v}}^{B_{k+1}^{p_{2}}}\}$
\end{center}
This algorithm requires to compare each two apertures on two barriers, which time complexity is $O(n^{2})$. Then it computes the minimum one, the time complexity is $O(k)$. Finally the complexity is $O(n^{2})$.
\subsection{divide-and-conquer algorithm.}
I'm sorry Dr. Gupta, I could not solve this question. Could you please give me some explanation or clue to help me figure it out. Thanks!
\section{Consider the problem of merging two sorted list A(1..k) and B(1..m).}
\subsection{Give a decision tree for merging A and B in the case that k=7 and m=2.}
Due to the page layout, I could not exhibit the whole figure in one page, so I divided it into separated parts. In the following figures, which stand for the decision tree. Each circle denotes a comparison, and the leave are in the ellipses.
\\
There should be {\bf 36 leaves} in the decision tree.
\begin{center}
\begin{tikzpicture}[
state/.style={circle, draw=none, text centered, anchor=north, text=black, circular drop shadow},
leaf/.style={rectangle, draw=none, circular drop shadow,
text centered, anchor=north, text=white},
level distance=1cm, sibling distance=15cm
]
\node [state] (Root) {$a_{1}, b_{1}$} [->]
child{ [sibling distance=12cm]
node [state] {$a_{2}, b_{1}$}
child {
node [state] {$a_{3}, b_{1}$}
child {
node [state] {$a_{4}, b_{1}$}
child {
node [state] {$a_{5}, b_{1}$}
child { [sibling distance=10.5cm]
node [state] {$a_{6}, b_{1}$}
child { [sibling distance=3cm]
node [state] {$a_{7}, b_{1}$}
child { [sibling distance=4cm]
node [leaf] {$a_{1}a_{2}a_{3}a_{4}a_{5}a_{6}a_{7}b_{1}b_{2}$}
}
child { [sibling distance=4cm]
node [state] {$a_{7}, b_{2}$}
child { node [leaf] {$a_{1}a_{2}a_{3}a_{4}a_{5}a_{6}b_{1}a_{7}b_{2}$} }
child { node [leaf] {$a_{1}a_{2}a_{3}a_{4}a_{5}a_{6}b_{1}b_{2}a_{7}$} }
}
}
child { [sibling distance=3cm]
node [state] {$a_{6}, b_{2}$}
child { [sibling distance=4cm]
node [state] {$a_{7}, b_{2}$}
child { node [leaf] {$a_{1}a_{2}a_{3}a_{4}a_{5}b_{1}a_{6}a_{7}b_{2}$} }
child { node [leaf] {$a_{1}a_{2}a_{3}a_{4}a_{5}b_{1}a_{6}b_{2}a_{7}$} }
}
child { node [leaf] {$a_{1}a_{2}a_{3}a_{4}a_{5}b_{1}b_{2}a_{6}a_{7}$} }
}
}
}
}
}
}
;
\end{tikzpicture}
\end{center}
\begin{tikzpicture}[
state/.style={circle, draw=none, text centered, anchor=north, text=black, circular drop shadow},
leaf/.style={rectangle, draw=none, circular drop shadow,
text centered, anchor=north, text=white},
level distance=1cm, sibling distance=15cm
]
\node [state] (Root) {$a_{1}, b_{1}$} [->]
child{ [sibling distance=12cm]
node [state] {$a_{2}, b_{1}$}
child { [sibling distance=5cm]
node [state] {$a_{3}, b_{1}$}
child { [sibling distance=3cm]
node [state] {$a_{4}, b_{1}$}
child { [sibling distance=3cm]
node [state] {$a_{5}, b_{1}$}
child { [sibling distance=3cm]
node [state] {$a_{5}, b_{2}$}
child { [sibling distance=3cm]
node [state] {$a_{6}, b_{2}$}
child { [sibling distance=5cm]
node [state] {$a_{7}, b_{2}$}
child { node [leaf] {$a_{1}a_{2}a_{3}a_{4}b_{1}a_{5}a_{6}a_{7}b_{2}$} }
child { node [leaf] {$a_{1}a_{2}a_{3}a_{4}b_{1}a_{5}a_{6}b_{2}a_{7}$} }
}
child { node [leaf] {$a_{1}a_{2}a_{3}a_{4}b_{1}a_{5}b_{2}a_{6}a_{7}$} }
}
child { node [leaf] {$a_{1}a_{2}a_{3}a_{4}b_{1}b_{2}a_{5}a_{6}a_{7}$} }
}
}
}
}
}
;
\end{tikzpicture}
\begin{tikzpicture}[
state/.style={circle, draw=none, text centered, anchor=north, text=black, circular drop shadow},
leaf/.style={rectangle, draw=none, circular drop shadow,
text centered, anchor=north, text=white},
level distance=1cm, sibling distance=15cm
]
\node [state] (Root) {$a_{1}, b_{1}$} [->]
child{
node [state] {$a_{2}, b_{1}$}
child {
node [state] {$a_{3}, b_{1}$}
child {
node [state] {$a_{4}, b_{1}$}
child { [sibling distance=5cm]
node [state] {$a_{4}, b_{2}$}
child { [sibling distance=5cm]
node [state] {$a_{5}, b_{2}$}
child { [sibling distance=3cm]
node [state] {$a_{6}, b_{2}$}
child { [sibling distance=4cm]
node [state] {$a_{7}, b_{2}$}
child { node [leaf] {$a_{1}a_{2}a_{3}b_{1}a_{4}a_{5}a_{6}a_{7}b_{2}$} }
child { node [leaf] {$a_{1}a_{2}a_{3}b_{1}a_{4}a_{5}a_{6}b_{2}a_{7}$} }
}
child { node [leaf] {$a_{1}a_{2}a_{3}b_{1}a_{4}a_{5}b_{2}a_{6}a_{7}$} }
}
child { node [leaf] { $a_{1}a_{2}a_{3}b_{1}a_{4}b_{2}a_{5}a_{6}a_{7}$} }
}
child { node [leaf] { $a_{1}a_{2}a_{3}b_{1}b_{2}a_{4}a_{5}a_{6}a_{7}$} }
}
}
}
}
;
\end{tikzpicture}
\begin{tikzpicture}[
state/.style={circle, draw=none, text centered, anchor=north, text=black, circular drop shadow},
leaf/.style={rectangle, draw=none, circular drop shadow,
text centered, anchor=north, text=white},
level distance=1cm, sibling distance=15cm
]
\node [state] (Root) {$a_{1}, b_{1}$} [->]
child{ [sibling distance=12cm]
node [state] {$a_{2}, b_{1}$}
child { [sibling distance=5cm]
node [state] {$a_{3}, b_{1}$}
child { [sibling distance=3cm]
node [state] {$a_{3}, b_{2}$}
child { [sibling distance=3cm]
node [state] {$a_{4}, b_{2}$}
child { [sibling distance=3cm]
node [state] {$a_{5}, b_{2}$}
child { [sibling distance=3cm]
node [state] {$a_{6}, b_{2}$}
child { [sibling distance=5cm]
node [state] {$a_{7}, b_{2}$}
child { node [leaf] {$a_{1}a_{2}b_{1}a_{3}a_{4}a_{5}a_{6}a_{7}b_{2}$} }
child { node [leaf] {$a_{1}a_{2}b_{1}a_{3}a_{4}a_{5}a_{6}b_{2}a_{7}$} }
}
child { node [leaf] {$a_{1}a_{2}b_{1}a_{3}a_{4}a_{5}b_{2}a_{6}a_{7}$} }
}
child { node [leaf] {$a_{1}a_{2}b_{1}a_{3}a_{4}b_{2}a_{5}a_{6}a_{7}$} }
}
child { node [leaf] {$a_{1}a_{2}b_{1}a_{3}b_{2}a_{4}a_{5}a_{6}a_{7}$} }
}
child { node [leaf] {$a_{1}a_{2}b_{1}b_{2}a_{3}a_{4}a_{5}a_{6}a_{7}$} }
}
}
}
;
\end{tikzpicture}
\begin{tikzpicture}[
state/.style={circle, draw=none, text centered, anchor=north, text=black, circular drop shadow},
leaf/.style={rectangle, draw=none, circular drop shadow,
text centered, anchor=north, text=white},
level distance=1cm, sibling distance=15cm
]
\node [state] (Root) {$a_{1}, b_{1}$} [->]
child{ [sibling distance=12cm]
node [state] {$a_{2}, b_{1}$}
child { [sibling distance=3cm]
node [state] {$a_{2}, b_{2}$}
child {
node [state] {$a_{3}, b_{2}$}
child { [sibling distance=3cm]
node [state] {$a_{4}, b_{2}$}
child { [sibling distance=3cm]
node [state] {$a_{5}, b_{2}$}
child {
node [state] {$a_{6}, b_{2}$}
child { [sibling distance=5cm]
node [state] {$a_{7}, b_{2}$}
child { node [leaf] {$a_{1}b_{1}a_{2}a_{3}a_{4}a_{5}a_{6}a_{7}b_{2}$} }
child { node [leaf] {$a_{1}b_{1}a_{2}a_{3}a_{4}a_{5}a_{6}b_{2}a_{7}$} }
}
child { node [leaf] {$a_{1}b_{1}a_{2}a_{3}a_{4}a_{5}b_{2}a_{6}a_{7}$} }
}
child { node [leaf] {$a_{1}b_{1}a_{2}a_{3}a_{4}b_{2}a_{5}a_{6}a_{7}$} }
}
child { [sibling distance=4cm]
node [leaf] {$a_{1}b_{1}a_{2}a_{3}b_{2}a_{4}a_{5}a_{6}a_{7}$}
}
}
child { [sibling distance=4cm]
node [leaf] {$a_{1}b_{1}a_{2}b_{2}a_{3}a_{4}a_{5}a_{6}a_{7}$}
}
}
child { [sibling distance=4cm]
node [leaf] {$a_{1}b_{1}b_{2}a_{2}a_{3}a_{4}a_{5}a_{6}a_{7}$}
}
}
}
;
\end{tikzpicture}
\begin{tikzpicture}[
state/.style={circle, draw=none, text centered, anchor=north, text=black, circular drop shadow},
leaf/.style={rectangle, draw=none, circular drop shadow,
text centered, anchor=north, text=white},
level distance=1cm, sibling distance=12cm
]
\node [state] (Root) {$a_{1}, b_{1}$} [->]
child{ [sibling distance=3cm]
node [state] {$a_{1}, b_{2}$}
child { [sibling distance=3cm]
node [state] {$a_{2}, b_{2}$}
child { [sibling distance=3cm]
node [state] {$a_{3}, b_{2}$}
child { [sibling distance=3cm]
node [state] {$a_{4}, b_{2}$}
child { [sibling distance=3cm]
node [state] {$a_{5}, b_{2}$}
child { [sibling distance=3cm]
node [state] {$a_{6}, b_{2}$}
child { [sibling distance=5cm]
node [state] {$a_{7}, b_{2}$}
child { node [leaf] {$b_{1}a_{1}a_{2}a_{3}a_{4}a_{5}a_{6}a_{7}b_{2}$} }
child { node [leaf] {$b_{1}a_{1}a_{2}a_{3}a_{4}a_{5}a_{6}b_{2}a_{7}$} }
}
child {
node [leaf] {$b_{1}a_{1}a_{2}a_{3}a_{4}a_{5}b_{2}a_{6}a_{7}$}
}
}
child {
node [leaf] {$b_{1}a_{1}a_{2}a_{3}a_{4}b_{2}a_{5}a_{6}a_{7}$}
}
}
child {
node [leaf] {$b_{1}a_{1}a_{2}a_{3}b_{2}a_{4}a_{5}a_{6}a_{7}$}
}
}
child { node [leaf] {$b_{1}a_{1}a_{2}b_{2}a_{3}a_{4}a_{5}a_{6}a_{7}$}
}
}
child { node [leaf] {$b_{1}a_{1}b_{2}a_{3}a_{4}a_{5}a_{6}a_{7}$}
}
}
child { node [leaf] {$b_{1}b_{2}a_{1}a_{2}a_{3}a_{4}a_{5}a_{6}a_{7}$}
}
}
;
\end{tikzpicture}
\\[1cm]
\subsection{Find the number of leaves in a decision tree for merging A and B, assuming $k \geq m$.}
There are (k+m) elements in the final merged list, so it should have $(k+m)!$ possible permutations. We divide it into two lists of length k and m respectively, then for each of the lists there are $k!$ and $m!$ possible permutations. Eventually, there are $k! \times m!$ total possible permutations. To find how many ways there are to divide $(k+m)$ numbers into 2 lists each size of n, divide total number of possible permutations of $(k+m)$ by total number of possible permutations of two lists of size k and m at the same time.
\\
\begin{center}
$\frac{(k+m)!}{k!m!}$
\end{center}
And it also is the leaves in the decision tree for merging A and B.
\subsection{Using decision trees, give a lower bound on merging A and B, assuming $k \geq m$.}
Since each node in decision tree represents one comparison operation, the worst-case number of comparison for a merging is determined by the height of the tree. Lower bound on the tree height is the lower bound on the number of comparisons. For the simple sake, assume that all of the input elements are distinct, so we have a binary tree. Suppose the height of the tree is h, so this tree has no more than $2^{h}$ leaves. And we could get $2^{h} \geq \frac{(k+m)!}{k!m!}$, then we have $h \geq \log \frac{(k+m)!}{k!m!}$.\\[0.5cm]
$\frac{(k+m)!}{k!m!} = \frac{(k+m)(k+m-1)...(k+1)(k!)}{k!m!} = \frac{(k+m)(k+m-1)...(k+1)}{m(m-1)...1}$ \\[0.5cm]
Due to $k \geq m$, we could have $\frac{k+m}{m} \geq 2$. \\
So, above formula could be changed to $(\frac{k+m}{m}) (\frac{k+m-1}{m-1}) ... (\frac{k+1}{1}) \geq 2^{m}$ \\
Finally, $h \geq \log \frac{(k+m)!}{k!m!} \geq \log (2^{m}) \geq \Omega (m)$ \\
The lower bound on merging A and B is: $\Omega (m)$.
\end{document}