Sei sulla pagina 1di 2

HW1 SOLUTION

1. Chapter 2
Problem 2: Suppose you have algorithms with the six running times listed
below.(Assume these are the exact number of operations performed as a function
of the input size n.) Suppose you have a computer that can perform 10
10
operations
per second, and you need to compute a result in at most an hour of computation.
For each of the algorithms, what is the largest input size n for which you would be
able to get the result within an hour.
(a) n
2
(b) n
3
(c) 100n
2
(d) nlog n
(e) 2
n
(f) 2
2
n
Solution : The operations that a computer can perform within an hour is
10
10
3600
(a) n
2
= 10
10
3600, we have n = 6 10
6
(b) n
3
= 10
10
3600, we have n = 3.302 10
4
(c) 100n
2
= 10
10
3600, we have n = 6 10
5
(d) nlog n = 10
10
3600, we have n 1.291 10
12
(e) 2
n
= 10
10
3600, we have n = log
2
(10
10
3600) 45
(f) 2
2
n
= 10
10
3600, we have n = log
2
(log
2
(10
10
3600)) 5
Problem 3: 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)).
f
1
(n) = n
2.5
f
2
(n) =

2n
f
3
(n) = n + 10
f
4
(n) = 10
n
f
5
(n) = 100
n
f
6
(n) = n
2
log n
Solution : In ascending order of growth, the list is f
2
=

2n, f
3
(n) = n + 10,
f
6
(n) = n
2
log n, f
1
(n) = n
2.5
, f
4
(n) = 10
n
, f
5
(n) = 100
n
.
2. Chapter 3
Problem 2 :. Give an algorithm to detect whether a given undirected graph
contains a cycle. If the graph contains a cycle, then your algorithm should output
Date: Jun 2, 2006.
1
2 HW1 SOLUTION
one.(It should not output all cycles in the graph, just one of them.) The running
time of your algorithm should be O(m+n) for a graph with n nodes and m edges.
Solution: To detect a cycle in undirected graph G, we will modify DFS a little
bit as follows:
Modied-DFS(G):
(1) for each vertex u G = (V, E)
(2) do color[u] WHITE
(3) Parent[u] NIL
(4) for each vertex u G = (V, E)
(5) do if color[u] WHITE
(6) then DFS-VISIT(u)
DFS-VISIT(u):
(1) color[u] GRAY
(2) for each vertex v Adj[u]
(3) do if color[v] = WHITE
(4) then Parent[v] u
(5) DFS-VISIT(v)
(6) do if color[v] = GRAY and Parent[u] = v
(7) return the cycle (u, parent[u]), (parent(u), parent(parent[u])), ..., (v, u)
(8) color[u] BLACK
Proof: If DFS-visit(u) encounters a neighbor of u, call it v and v is not the
parent of u, then v is an ancestor of vertex u in the DFS tree. This means there
is a path P
vu
from vertex v to u that does not use the edge (u, v). Thus we could
extend this path by edge (u, v) and obtain a cycle.
Running Time: If the graph does not contain a cycle, then the algorithm
is exactly the same as DFS and the running time is O(m + n). If the graph does
contain a cycle, it will take at most n operations plus the operations of DFS to nd
the cycle. Hence the running time of our algorithm is O(m+n)
Problem 6 : We have a connected graph G = (V, E), and a specic vertex
u V . Suppose we compute a depth-rst search tree rooted at u, and obtain a tree
T that includes all nodes of G. Suppose we then compute a breadth-rst search
rooted at u, and obtain the same tree T.Prove that G = T.(In other words, if T is
both a depth-rst search tree and a breadth-rst search tree rooted at u, then G
cannot contain any edges that do not belong to T.)
Proof: Assume that G contains an edge e = (x, y) that does not belong to
T. Since T is a depth-rst search tree and (x, y) is an edge of G that is not an
edge of T, one of x or y is ancestor of the other(by Property 3.7 in Page 85 in the
textbook). On the other hand, T is a breadth-rst search tree and assume that x
and y belong to layers L
i
and L
j
respectively, then i and j dier by at most 1(by
Property 3.4 in Page 81 in the textbook). Notice that one of x or y is ancestor of
the other, we have that i = j and hence i and j dier by 1. However, combining
that one of x or y is ancestor of the other and that i and j dier by 1 implies that
the edge (x, y) is in the tree T. It contradicts the assumption that e = (x, y) that
does not belong to T. Thus G cannot contain any edges that do not belong to T.

Potrebbero piacerti anche