Академический Документы
Профессиональный Документы
Культура Документы
Mystery(n)
if n 1 then begin
for i := 1 to n1/2 print(x);
Mystery(n/4);
Mystery(n/4);
end
(a) How many xs does Mystery(1) print? How many xs
does Mystery(4) print?
(b) If we call Mystery(n), where n is a power of 4 and n
1, how many xs (as a function of n) does call Mystery(n)
print? Justify your answer/show your work (ie give
recurrence and solve by substitution.)
Hint: Using 4k = 22k and taking logs base 2 makes
calculations easier.
Answer:
(a) Mystery(1) prints one x. Mystery(4) prints four xs.
(b) We have to solve T(n) = 2T( n 4 ) + n 1 2 for all n 1,
with T(1) = 1. Solve by substitution.
So Mystery prints n (1 +
!"#! !
!
1/2
(1 +
!"#! !
!
) xs (which is consistent
!"#! !
!
)=4
Nil,
,
This DFS increments a counter each time DFS-VISIT is called to grow a new tree in the DFS
forest. Every vertex visited (and added to the tree) by DFS-VISIT is labeled with that same
counter value. Thus u.cc = v.cc if and only if u and v are visited in the same call to DFS-VISIT
from DFS, and the final value of the counter is the number of calls that were made to DFS-VISIT
by DFS. Also, since every vertex is visited eventually, every vertex is labeled. Thus all we need to
show is that the vertices visited by each call to DFS-VISIT from DFS are exactly the vertices in
one connected component of G.
All vertices in a connected component are visited by one call to DFS-VISIT from DFS:
Let u be the first vertex in component C visited by DFS-VISIT. Since a vertex becomes nonwhite only when it is visited, all vertices in C are white when DFS-VISIT is called for u. Thus,
by the white-path theorem, all vertices in C become descendants of u in the forest, which means
that all vertices in C are visited (by recursive calls to DFS-VISIT) before DFS-VISIT returns to
DFS.
All vertices visited by one call to DFS-VISIT from DFS are in the same connected component:
If two vertices are visited in the same call to DFS-VISIT from DFS, they are in the same
connected component, because vertices are visited only by following paths in G (by following
edges found in adjacency lists, starting from some vertex)
RELAX(u, v, w):
1. If v.d > u.d + w(u, v)
v.d = u.d + w(u, v)
v.pi = u
RULES of shortest paths:
Given a weighted, directed graph with weight function w: ER, let p = <v0, v1, , vk>
be a shortest path from vertex v0 to vertex vk and, for any i and j such that 0 I j k,
let pij = <vi, vi+1, , vj> be the subpath of p from vertex vi to vj.
Shortest path cannot contain a neg-weight cycle, since removing the cycle from the
path produces a path with the same source and destination vertices and a lower path
weight.
Can have 0-weight cycles we can remove a 0-weight cycle from any path to produce
another path whose weight is the same. Thus if there is a shortest path from a source
vertex s to a destination vertex v that contains a 0-weight cycle, then there is another
shorest path from s to v without this cycle. As long as a shortest path has 0-weight
cycles, we can repeatedly remove these cycles from the path until we have a path that
is cycles free.
OLD HOMEWORKS:
MST
MST
Properties
lowest
cost
Suppose to the contrary that there is no MST for the graph Gwhich includes the edge (u, v).
Let T be any MST of G. By our assumption, T does not contain (u, v). Adding (u, v) to Twill
introduce a cycle since T is a free tree. This cycle involves (u, v). Therefore there is a path
from v to u that does not pass through this edge. This means that there exists another
edge (u', v') in T such that u' U and v' V - U.