0% found this document useful (0 votes)
146 views5 pages

Cse 421 Midterm

This algorithm sorts k arrays with a total of n elements in O(nklog(k)) time by: 1) Repeatedly merging pairs of arrays into single merged arrays 2) Merging is done by comparing elements and adding smaller elements to the merged array 3) This merging and re-merging is repeated log(k) times until all arrays are merged into one sorted array

Uploaded by

api-522855390
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views5 pages

Cse 421 Midterm

This algorithm sorts k arrays with a total of n elements in O(nklog(k)) time by: 1) Repeatedly merging pairs of arrays into single merged arrays 2) Merging is done by comparing elements and adding smaller elements to the merged array 3) This merging and re-merging is repeated log(k) times until all arrays are merged into one sorted array

Uploaded by

api-522855390
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

a. True. n​3.05​ = n​3​ * n​.05​. If we divide by n​3​ on both sides, it is a matter of comparing
log(n) and n​.05​, and log(n) grows faster than n​.05​. C being 1 in this case would
fulfill the requirement of f(n) < cg(n), at large values of n.
b. False. 2​2n​ = 2​n​ * 2​n​. The amount that 2​n​ is multiplied by will grow exponentially,
and so there is no constant that can lower 2​2n​ enough so that 2​n​ will be greater than
it at high values of n.
c. True. In order to determine if there is an odd length cycle, first, all of the
edges/vertices must be examined, giving a runtime of O(m + n), and there does
exist an algorithm that finds a cycle and reports it in that time (according to HW 2
Q1). After that, it’s just a matter of counting how many nodes there are in a cycle.
That could take up to O(m) time to reiterate over it, so each edge will be looked at
twice. The maximum number of edges in a graph is m = n(n-1)/2. Thus, O(2m +
n) = O(2(n(n-1)/2) + n) = O(n(n-1) + n) = O(n​2​ - n + n) = O(n​2​). An algorithm is
said to run in polynomial time if it runs in time O(n​d​) for some constant d on
inputs of size n. In this case, d is 2 and n is the number of vertices.
d. False. The solved recurrence has a = 3, b = 2, and d = 5, which means a < b​d​. The
recurrence is thus n​5​. There is no constant to multiply n​3​ by that will make n​5​ less
than n​3​ at high levels of n.
e. False: Counter example is as follows to the right
If this is our graph with our cycle and our vertice
in the top right with two equal heaviest edges,
any MST for this graph would require at least
one of those edges. As those are the only two
edges connected to the top right vertice, you have
to use at least one of them to reach that vertex in
the MST.
f. True. If we were to treat the vertices on each end
of the edge as one vertex cuts, then by the cut
property, the smallest edge must be in every
MST.
g. False. The fastest we saw in lecture was Karatsuba’s which was around O(n​1.46​)...?
No matter how small you split up the bits I don’t think you could get it to n​1.1​, you
have to combine them afterwards anyways, even though at the smallest level “1
bit numbers” cost a constant amount you still have to add them all up later.
h. False. For a vertex cover, you’d need at least one of the vertices from each of the
edges in the matching because a vertex from an edge in the matching cannot cover
more than one edge found in the matching, due to the fact that all the matching
edges are separated by at least one edge in between them all.
i. True. If at least n sets are required, to cover n elements, then each of the n points
requires a unique set to cover it. If you simply just looped through all m sets,
adding them to the set cover if they covered a new node. By the time the set
covers the universe, if you have at least n sets, you’ll know that’s the case; if it’s
any less, then it’s the other case. This happens in O(m), which is well within
polynomial time if the input is the number of sets.
2. This algorithm adds the edges of vertices with a degree of one. These edges must be part
of the matching because there is no other way to saturate those vertices. Furthermore,
after adding this edge, the vertex on the other end is removed, along with any other edges
it’s connected to, because they cannot possibly be part of the matching as one of their
ends is already saturated. Thus, the edges added to ME must all be part of the perfect
matching. Through this process of adding and eliminating vertices/edges, the matching is
built up. Obviously this runs in O(n) times, which is well within polynomial run time, as
each node can only be removed once.
Base case: The algorithm holds for a graph of size 2
Assume the algorithm holds for a graph G with 2n vertices.
Now consider a graph G` with 2(n + 1) vertices. Since the graphs have no cycles, there
must be at least one vertex with a degree of 1. The edge connecting this vertex to the rest
of G`, E, must be in the graph’s perfect matching because there’s no other way to saturate
that vertex. If we were to remove this edge and those two vertices, we have a graph G
with 2n vertices, which the algorithm already works for as we hypothesized. If G had a
perfect matching and G`did as well, then the two extra vertices would have to be
connected to each other (as all the other vertex pairs are taken). Additionally, if all the
other vertices are saturated, that means that at the end of the algorithm, regardless of how
many there were, no edges remain between those two extra vertices and all of the other
vertices in the graph, as those vertices have been removed. The algorithm would then
correctly identify the last two vertices with degree 1 as saturated, add the edge to ME,
and return. G`’s perfect matching is G’s perfect matching plus the extra edge, and vice
versa. Through the hypothesis, the algorithm holds for all values of n.
Let there be an array, ME, containing the edges in the matching, which is empty at the
start
while(T has vertices)
if(Vertex V has a degree of 1)
Add V’s Edge, E to ME
Remove both ends of E from T.
Else
Return no matching
Return ME
3. A: O(n​log2(5)​)
This recurrence ends up being T(n) = 5T(n/2) + n. Since a, 5, is greater than b​d​, 2​1​, our
runtime is O(n​logb(a)​), which is O(n​log2(5)​).
B: ᘯ(2​n+1​)
This recurrence ends up being T(n) = 2T(n - 1) + 1. The summation is as follows:
∑ 2​i​T(n - i) + 1 from i = 0 to i = n
n + ∑ 2​i​T(n-i) from i = 0 to i = n
n + (2​n + 1​ - 1)/(2-1)∑ T(n - i) from i = 0 to i = n, based on the identity we learned in class
n + 2​n + 1​ ∑ T(n - i) from i = 0 to i = n, 2​n + 1​ > 2​n + 1​ - 1
At this point we know the run time is at least n + 2​n + 1​, so it’s ᘯ(2​n+1​ + n) at least.
C: O(n​2​log​3​(n))
This recurrence ends up being T(n) = 9T(⅓) + n​2​. Since a, 9, is equal to b​d​, 3​2​, our
runtime is O(n​d​log​b​(n)), or O(n​2​log​3​(n)).
4. My algorithm divides the input arrays into smaller subgroups until they are in pairs, and
naively sorts each pair with pointers into one merged array. This is repeatedly done with
resulting arrays until all the arrays have merged into one. This runs in O(nklog(k)) time
because all nk elements are traversed log(k) times, the number of times we’ll have to
merge on each level.

Assume the input is given to us in the form of an Arraylist of arraylists, I


while(size of I > 1)
Empty arraylist E
For the arraylists at I[0] and I[1], called A​1​ and A​2
Int ptr1 = 0
Int ptr2 = 0
while (ptr1 < A​1​ size && ptr2 < A​2​ size)
if(A​1​[ptr1] <= A​2​[ptr2])
Add A​1​[ptr1] to E
Ptr1++
Else
Add A​2​[ptr2] to E
Ptr2++
While (ptr1 < A​1 ​size)
Add A​1​[ptr1] to E
Ptr1++
While (ptr2 < A​2 ​size)
Add A​2​[ptr2] to E
Ptr2++
Remove A​1​ and A​2​ from I
Add E to the end of I.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy