Data Structures Algorithms Dfs Bfs Qns
Data Structures Algorithms Dfs Bfs Qns
Data Structures Algorithms Dfs Bfs Qns
1
(2) We call a graph G = (V, E) a near-tree if it is connected and has at most n + 8 edges, where
n := |V |. Give an algorithm with running time O(n) that takes a near-tree with edge costs and
returns a minimum spanning tree of G. You may assume that all of the edge costs are distinct.
Solution. The crucial subroutine is the following procedure DetectCycleByDepthFirstTrav-
eral which we will call simply F . It is a modification of the depth first traversal algorithm. We
maintain and update an associative array P which contains the partial DFS tree. (We remark that
even though G is an undirected graph, once we specify a root vertex, the DFS tree is a directed
graph.) Thus P [t] = s means that node t has node s as parent on the DFS tree.
F takes as input a graph G, a node s, and the node which is the parent of s in the depth first
tree P . Note that P is constructed by F and grows with each subsequent call to F .
F returns the first nontrivial, non-tree edge (u, v), else it returns NIL to indicate that the graph
G is already a tree. By nontrivial non-tree edge, we mean an edge that completes a nontrivial cycle
in the undirected graph G; a trivial cycle in an undirected graph is one of the form s → t → s.
1: Let P be a global variable, the associative array representing the DFS tree
2: s is the root node if and only if P [s] = NIL
3: procedure F(G, s)
4: Mark s as explored
5: for each edge (s, t) incident to s do
6: if t is marked explored and t 6= P [s] then
7: return (s, t) // this edge completes a nontrivial cycle in G
8: end if
9: if t is not marked explored then
10: Let P [t] := s
11: F(G, t, P [t])
12: end if
13: end for
14: return NIL
15: end procedure
Now, with the above subroutine in hand, we may describe the idea of the algorithm to find an
MST in a near-tree. We use F to search for a cycle. If there is no cycle, then the graph is a tree and
we are done. Otherwise, we delete the heaviest edge from the cycle. We do this 9 times.
1: procedure NearTreeMST(G)
2: Choose an initial node s
3: for i = 1, 2, . . . , 9 do
4: Let (u, v) = F (G, s)
5: if (u, v) = NIL then
6: return // G is already a tree
7: end if
8: Let w = LeastCommonAncestor(u, v)
9: Let e1 be the heaviest edge on the path from u to w
10: Let e2 be the heaviest edge on the path from v to w
11: Delete from G the heaviest of e1 , e2 , (u, v) // to use the Cycle Property of MSTs
12: end for
13: end procedure
2
(3) Dr. Foo drives his car from Pune to Goa. When full, his car’s gas tank holds enough fuel to enable
him to drive for n kilometers. His gps navigation system tells him in advance the distances between
the gas stations on his route. The doctor wishes to make as few gas stops as possible along the way.
Give an efficient algorithm by which Dr. Foo may determine at which gas stations to stop so as to
achieve his goal of minimizing the total number of gas stops. Prove that your algorithm is correct
and determine its running time.
Solution. The optimal strategy goes as follows. Starting with a full tank of gas, Foo should go to
the farthest gas station at position P1 from Goa which is at most n miles from Pune. Starting from
that point, he should go to the farthest gas station P2 which is within n miles of position P1 . And
so on.
Put differently, at each gas station, Foo should ask whether he can make it to the next gas station
without stopping at this one. If he can, skip this one. This description shows that the algorithm is
O(m), where m is the total number os stations.
(i) Now, consider an optimal solution with s stations which stops first at the kth station. Then
we claim that the rest of the optimal solution must be an optimal solution to the subproblem of the
remaining m − k stations. If this were not the case, we could find a solution to the problem on m − k
stations which stopped at < s − 1 stations and we could use this to construct an optimal solution
on m stations with < s stops, contradicting our supposition.
(ii) Moreover, any optimal solution must choose as the first station the farthest station (the kth
station, say) which is within n miles of Pune. For if not and we had chosen the jth station with
j < k, then upon leaving the jth station we could get no further than if we stopped at the kth
station. Thus stopping at station j < k results in a solution which can do no better than stopping
at the kth station.
Taken together, (i) and (ii) prove that our algorithm is correct.
(4) We are given two sets A and B, each consisting of n positive integers. You may reorder the sets in
any manner you choose. After reordering, write ai for the ith element of A and bi for the ith element
of B. The payoff obtained from the chosen orderings is
n
Y
abi i .
i=1
1
2
x y
−2