Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Consider the following game. A “dealer” produces a sequence s1···sn of “cards,” face up, where each card si has a value vi. Then two players take turns picking a card from the sequence, but can only pick the first or the last card of the (remaining) sequence. The goal is to collect cards of largest total value. (For example, you can think of the cards as bills of different denominations.) Assume n is even. (a) Show a sequence of cards such that it is not optimal for the first player to start by picking up the available card of larger value. That is, the natural greedy strategy is suboptimal. (b) Give an O(n2) algorithm to compute an optimal strategy for the first player. Given the initial sequence, your algorithm should precompute in O(n2) time some information, and then the first player should be able to make each move optimally in O(1) time by looking up the precomputed information.

Short Answer

Expert verified
  1. The sequence of cards is 2,10,3,1.
  2. we will provide an algorithm which give optimal solution.

Step by step solution

01

Define Greedy Algorithms

In greedy algorithms, the solution to the problem is obtained by picking a best option at the moment. At each stage of the algorithm, local optimal choice is selected. The aim of the question is to give sequence for the given game in which greedy approach will not give optimal solution.

02

Determine the sequence for which greedy approach fails

Consider a card sample with values: 2,10,3,1. Consider First player and Second player be P1 and P2 respectively.

P1picks the card with value 2. Card remaining: 10,3,1

And then P2 picks the card:10. Card remaining: 3,1

Now P1 will pick card:3.

And finally P2 will pick remaining last card: 1

Now, add the total value of player P1 and P2.

P1=2+3=5

Whereas,

P2=10+1=11

So player P1 loose, this proves that greedy approach is not optimal for first player for sequence 2,10,3,1.

03

Create recursive formula

Let OPTi,j be the score difference between largest total score of first player and the corresponding score of second player for sequencesisj.

There are two choices for first player:

  • Either picks the first card to gaining score vi,OPTi+1,j in the next game.
  • Pick the last to gain score vj, cmdOPTi,j1 from remaining cards

So, the recursive equation is:

OPTi,j=maxviOPTi+1,j,vjOPTi,j1vi;ifi=j

04

Determine an algorithm

The algorithm is as follows:

fori=1to n   OPTi,i=viforj=1to n   fori=j to 1

      OPTi,j=maxviOPTi+1,j,vjOPTi,j1return OPT1,n      OPTi,j=maxviOPTi+1,j,vjOPTi,j1return OPT1,n

05

Determine the runtime of the algorithm

Each update takes On. There are two inner loops that run n times each, which takes run time On2.

Thus, the time complexity is On2.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

|ψ=12|00+12|11is one of the famous “Bell states,” a highly entangled state of its two qubits. In this question we examine some of its strange properties. (a) Suppose this Bell state could be decomposed as the (tensor) product of two qubits (recall the box on page ), the first in state α0|0+α1|1and the second in stateβ0|0+β1|1. Write four equations that the amplitudes α0,α1,β0andβ1must satisfy. Conclude that the Bell state cannot be so decomposed.

(b) What is the result of measuring the first qubit of |ψ?

(c) What is the result of measuring the second qubit after measuring the first qubit? (d) If the two qubits in state|ψ are very far from each other, can you see why the answer to (c) is surprising?

The Fibonacci numbers F0,F1,F2,... are defined by the rule

F0=0,F1=1,Fn=Fn1+Fn2.

In this problem we will confirm that this sequence grows exponentially fast and obtain some bounds on its growth.

(a) Use induction to prove that Fn20.5nfor n6.

(b) Find a constant c<1such thatFn2cn for all n0. Show that your answer is correct.

(c) What is the largestc you can find for which Fn=Ω(2cn)?

Question: 0.1. In each of the following situations, indicate whether f=O(g),orf=Ω(g),or both (in which case f=(g))

Show that, if c is a positive real number, then g(n) = 1 + c + c2 + · · · + cn is:

(a) Θ(1) if c < 1.

(b) Θ(n) if c = 1.

(c) Θ(cn) if c > 1.

The moral: in big-Θ terms, the sum of a geometric series is simply the first term if the series is strictly decreasing, the last term if the series is strictly increasing, or the number of terms if the series is unchanging.

Question: An Eulerian tourin an undirected graph is a cycle that is allowed to pass through each vertex multiple times, but must use each edge exactly once.

This simple concept was used by Euler in to solve the famous Konigsberg bridge problem, which launched the field of graph theory. The city of Konigsberg (now called Kaliningrad, in western Russia) is the meeting point of two rivers with a small island in the middle. There are seven bridges across the rivers, and a popular recreational question of the time was to determine whether it is possible to perform a tour in which each bridge is crossed exactly once. Euler formulated the relevant information as a graph with four nodes (denoting land masses) and seven edges (denoting bridges), as shown here.

Notice an unusual feature of this problem: multiple edges between certain pairs of nodes.

(a) Show that an undirected graph has an Eulerian tour if and only if all its vertices have even degree. Conclude that there is no Eulerian tour of the Konigsberg bridges.

(b) An Eulerian pathis a path which uses each edge exactly once. Can you give a similar if-and-only-if characterization of which undirected graphs have Eulerian paths?

(c) Can you give an analog of part (a) for directedgraphs?

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free