算法 例题

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

CIS 675 Homework 2
Instructions: All answers must be proved: it is not sufficient to simply state the answer.
You may work with other students,but please note who you worked with on your submission. If you consult outside sources (such as Wikipedia), cite your source. All answers must be written in your own words.
Treat single arithmetic operations (addition, subtraction, multiplication, and division) as constant time operations.
1. A binary tree is full if all vertices have either 0 or two children. Let B
n denote the number of full binary trees with n vertices.
1. What are the values of B
3 , B
5
, and B
7
?
2. Is it possible to have a full binary tree with an even number of vertices?
Why or why not?
3. For general n, derive a recurrence relation for B
n
. Include base cases.
Hint: Think about the left and right sub-trees of a full binary tree of size n. What are the possible sizes for these sub-trees?
2. Both QuickSort and MergeSort require splitting the input array into two smaller arrays, recursively sorting the subarrays, and then joining the sorted subarrays to obtain a sorted version of the initial input array. One way to handle the recursive calls is to allocate new memory for the subarrays, copy the elements from the input array to the newly allocated memory, and pass these new subarrays in the recursive call. However, this requires a lot of extra memory.
1
1. In MergeSort, each sub-array is approximately half the size of the input
array. Suppose the original input array originally used n units of mem- ory. Suppose we allocate new memory for the subarrays every time the array is split. Then over the entire set of recursive calls, how much total memory is required?
1
2. Describe how QuickSort can be implemented in place (that is, without
allocating new memory for the subarrays). If you need a small amount of memory for temporary storage, that’s fine.
3. In an undirected graph G with vertex set V and edge set E, the degree d(u) of a node u is the number of neighbors that u has (i.e., the number of nodes that u is linked to). In a directed graph, a node u has an indegree , which counts the number of incoming edges, and an outdegree , which counts the number of outgoing edges.
1. Show that in an undirected graph,
u∈V d(u) = 2|E|. Here, |E| repre-
sents the size of the edge set E: in other words, the number of edges.
2. Show that in an undirected graph, there must be an even number of
vertices with odd degree.
3. Does a similar statement hold for the number of vertices with odd in-
degree in a directed graph? Why or why not?
4. Let G be a directed graph. Let s be a ‘start vertex’ . An infinite path of G is an infinite sequence v0 , v1 , v2 ,... of vertices such that v0 = s and for all i > 0, there is an edge from v i to v i+1 . In other words, this is a path of infinite length. Because G has a finite number of vertices, some vertices in an infinite path are visited infinitely often.
1. If p is an infinite path, let Inf (p) be the set of vertices that
occur infinitely many times in p. Prove that Inf (p) is a subset of a single strongly connected component of G.
2. Describe an algorithm to determine if G has an infinite path.
Prove that it is correct.
5. Suppose we have a directed graph with non-negative edge weights. It is possible for this graph to have multiple shortest paths between two nodes. In
2
class, we saw how to find the length of the shortest path from a node s to all other nodes. Suppose that in addition to finding the length of these shortest paths, we also want to know how many shortest paths there are. Describe an algorithm to do this.
2
6. Suppose that you are given a directed graph with non-negative edge weights, and a target node t. You want to find the shortest path to target node t from every other node. (Notice that this is different from what we covered in class: in class, we considered the problem of finding the shortest path from the specified node to every other node.) Describe an algorithm to calculate these values.
3。

相关文档
最新文档