$34.99
Answer the questions in the boxes provided on the question sheets. If you run out of room for an answer, add a page to the end of the document.
Name: Imogen Xu Wisc id:
Asymptotic Analysis
1. Kleinberg, Jon. Algorithm Design (p. 67, q. 3, 4). Take the following list of functions and arrange them in ascending order of growth rate. That is, if function g(n) immediately follows function f(n) in your list, then it should be the case that f(n) is O(g(n)).
nhgn.int9 ⼏9的⼏上 jiiii 点上guyǙ
灬 9灬 州
2. Kleinberg, Jon. Algorithm Design (p. 68, q. 5). Assume you have positive functions f and g such that f(n) is O(g(n)). For each of the following statements, decide whether you think it is true or false and give a proof or counterexample.
(a) log2 f(n) is O(log2 g(n)) we ⺕ c.N ntfmicgmasmNfrm.N.ve he hg.fmEhgcthg.gr
then hg.fm 0lhg.gr I
(b) 2f(n) is O(2g(n)) true
⺕ C.N ato.tn
Ecgmashavem.Nfrm.N.vefins 4叫 then fni 049川
(c) f(n)2 is O(g(n)2) true
⺕ C.N.at o.tn
Ecgmashem.Nfrm.N.ft Ive then fi Og
3. Kleinberg, Jon. Algorithm Design (p. 68, q. 6). You’re given an array A consisting of n integers. You’d like to output a two-dimensional n-by-n array B in which B[i,j] (for i < j) contains the sum of array entries A[i] through A[j] — that is, the sum A[i] + A[i + 1] + ... + A[j]. (Whenever i j, it doesn’t matter what is output for B[i,j].) Here’s a simple algorithm to solve this problem. for i = 1 to n c.ru
foradd up arrayj = i + 1 to nentries A[ i ] through A[ j ] a Ěti endforstore the result in B[ i , j ] 4 ÈtiÈti
endfor (a) For some function f that you should choose, give a bound of the form O(f(n)) on the running time of this algorithm on an input of size n (i.e., a bound on the number of operations performed by the algorithm).
letTm bethetime wpwxiy.tn 04
THE Cinta Ètits Èti 4Èti Ec.nl to 们
(b) For this same function f, show that the running time of the algorithm on an input of size n is also ⌦(f(n)). (This shows an asymptotically tight bound of ⇥(f(n)) on the running time.) 不 Cin t Cr 志⼗⻔ t s Èti 1 4 Èltiy in my_ n ⼼
fu E di thenwe have fun E Oli
(c) Although the algorithm provided is the most natural way to solve the problem, it contains some highly unnecessary sources of inefficiency. Give a different algorithm to solve this problem, with an asymptotically better running time. In other words, you should design an algorithm with running time O(g(n)), where .
merge Li hi lineage 2 sorted army mergeSortL
我 1,6 indenlioii.edu bi inill.hgthH.es
D i addrifle h L 0 bin
if indent三hlength idenzih.bg hi LIbin LlengthI whilerettruruneresi hi
lih.inergesrniifihliaddz.hithn.nerge.GL
h.bgth I return
add2⼆ mgelh.li
tmeieheifindenzl h.bgth hlindenDE h index2
addzitmei thecmfnyof if add2 mergeSon is
Page 3 of 7 olnhgDL
olilres.appendlhlindenlyiindenlttiehe.it
res append hlinden2
index2 t i
Graphs
4. Given the following graph, list a possible order of traversal of nodes by breadth-first search and by depth-first search. Consider node 1 to be the starting node.
8
5. Kleinberg, Jon. Algorithm Design (p. 108, q. 5). A binary tree is a rooted tree in which each node has at most two children. Show by induction that in any binary tree the number of nodes with two children
is exactly one less than the number of leaves.ooh
n denote the numberof nodes with 2 children o
m denote thenumberofleaves 666
if mo.mil then consider a binarytree T with inn.ms 2
0 at least one partof a lanehas 2 children specialcasefull binarytee thenwe choose anyone of suchparents consider it'snodes
then we trim C we get T with n Em nl m z.tl n't
we here n m 1 i n 1 intl ⼝ m n t l m m
all parents of leaves has only one children which is leaf
then we trim all those parents got T with n m we have n_n mm
we do this repay until we have condition 0 四
6. Kleinberg, Jon. Algorithm Design (p. 108, q. 7). Some friends of yours work on wireless networks, and they’re currently studying the properties of a network of n mobile devices. As the devices move around, they define a graph at any point in time as follows:
There is a node representing each of the n devices, and there is an edge between device i and device j if the physical locations of i and j are no more than 500 meters apart. (If so, we say that i and j are “in range” of each other.)
They’d like it to be the case that the network of devices is connected at all times, and so they’ve constrained the motion of the devices to satisfy the following property: at all times, each device i is within 500 meters of at least of the other devices. (We’ll assume n is an even number.) What they’d like to know is: Does this property by itself guarantee that the network will remain connected?
Claim: Let G be a graph on n nodes, where n is an even number. If every node of G has degree at least , then G is connected.
Decide whether you think the claim is true or false, and give a proof of either the claim or its negation.
7. Kleinberg, Jon. Algorithm Design (p. 110, q. 9). There’s a natural intuition that two nodes that are far apart in a communication network—separated by many hops—have a more tenuous connection than two nodes that are close together. There are a number of algorithmic results that are based to some extent on different ways of making this notion precise. Here’s one that involves the susceptibility of paths to the deletion of nodes.
Suppose that an n-node undirected graph G = (V,E) contains two nodes s and t such that the distance between s and t is strictly greater than .
(a) Show that there must exist some node v, not equal to either s or t, such that deleting v from G
s destroys allfrom s to t.)s t paths. (In other words, the graph obtained from G by deleting v contains no path
l
t.hymtli wne rprovedctim.li
i Consider there exist another rode U it thepath connect l S u and u t do nt involved V
t fnmstweodetnotegthmghvispthe path athln.to
he path go thmgh u is path2
只 sincethe distance strictly longer than È
oo渊.to thhe sul mofh 2length7 ntl.veof 2 pathneedstrictlyat lalongerst mithanodesn ⼏ to
(b) Give an algorithm with running timesatisfyhis propertyO(m + conn) to find such a nodetradiction thenv. we provethe⼼皆
Consider BFS from s and front thentheywillmeetat V start BFsfmns.ve he queue Qs
stat BY front we have Qt
uidequenedsleep.hn the neighbours
n.ofuvideqI explore the uneighbourseuen.tv.lt
where nyn.ae sets if i rusheetn itv E nm is theoutputted node
I
Coding Question
8. Implement depth first search in either C, C++, C#, Java, or Python. Be efficient and implement it in O(n+m) time, where n is the number of graph nodes, and m is the number of graph edges. Remember to submit a makefile along with your code, just as with week 1’s coding question.
Input format: the input will start with a positive integer, giving the number of instances that follow. For each instance, there will be a positive integer, giving the number of graph nodes. For each node there will be a line of space-delimited input. The first string in a line will be the node name. All following strings in a line will be the names of the adjacent nodes to the first node in the line.
You can assume the order the nodes will be listed is in increasing lexographic order (0-9, then A-Z, then a-z), both in each list of adjacent nodes, as well as the order nodes’ lines are listed.
A sample input is the following: to do
A 2 stadez.it
um A B
B A
nod C
9
1 2 9
2 3 5 6
3 2 7
4 6
5 2
6 2 4
7 3
8 9
9 1 8
The sample input has two instances. The first instance corresponds to the graph below on the left. The second instance corresponds to the graph below on the right.
Target output: for each instance, your program should output the names of nodes visited in depth first traversal of the graph, with ties between nodes visiting the first node in lexicographic ordering. Start your traversal with the first node in lexicographic order. Each instance’s traversal should be on a separate line. Each output line should be terminated by a newline. The correct output to the sample input would be:
A B C
1 2 3 7 5 6 4 9 8