Ebook Data structures and problem solving using C++ (2/E): Part 2
➤ Gửi thông báo lỗi ⚠️ Báo cáo tài liệu vi phạmNội dung chi tiết: Ebook Data structures and problem solving using C++ (2/E): Part 2
Ebook Data structures and problem solving using C++ (2/E): Part 2
Chapter 14SimulationAn important use of computers is for simulation, in which the computer is used to emulate the operation of a real system and gathe Ebook Data structures and problem solving using C++ (2/E): Part 2er statistics. For example, we might want to simulate the operation of a bank with k tellers to determine the minimum value of k that gives reasonable service time. Using a computer for this task has many advantages. First, the information would be gathered without involving real customers. Second, Ebook Data structures and problem solving using C++ (2/E): Part 2a simulation by computer can be faster than the actual implementation because of the speed of the computer Third, the simulation could be easily repliEbook Data structures and problem solving using C++ (2/E): Part 2
cated. In many cases, the proper choice of data structures can help US improve the efficiency of the simulation.In this chapter, we show:•how to simulChapter 14SimulationAn important use of computers is for simulation, in which the computer is used to emulate the operation of a real system and gathe Ebook Data structures and problem solving using C++ (2/E): Part 2s the follow ing game: .-V people, numbered I to jV. are sitting in a circle: starling al person I. a hot potato is passed; after M passes, the person holding the hot potato is eliminated, the circle closes ranks, and the game continues with the person who was silting after the eliminated person pic Ebook Data structures and problem solving using C++ (2/E): Part 2king up the hot potato; the last remaining person wins. A common assumption is that M is a constant, although a random number generator can be used toEbook Data structures and problem solving using C++ (2/E): Part 2
change M after each elimination.The Josephus problem arose in the first century A.D. in a cave on a mountain in Israel where Jewish zealots were beinChapter 14SimulationAn important use of computers is for simulation, in which the computer is used to emulate the operation of a real system and gathe Ebook Data structures and problem solving using C++ (2/E): Part 2r than surrender to the Romans. He suggested the game that now bears his name. The hot potatoAn important use of computers is simulation, in which the computer is used to emulate the operation of a real system and gather statistics.In the Josephus problem, a hot potato is repeatedly passed; when pas Ebook Data structures and problem solving using C++ (2/E): Part 2sing terminates, the player holding the potato is eliminated; the game continues, and the last remaining player wins.472SimulationFigure 14.1 The JoseEbook Data structures and problem solving using C++ (2/E): Part 2
phus problem: At each step, the darkest circle represents the initial holder and the lightly shaded circle represents the player who receives the hot Chapter 14SimulationAn important use of computers is for simulation, in which the computer is used to emulate the operation of a real system and gathe Ebook Data structures and problem solving using C++ (2/E): Part 2game to get the last lot and convinced the remaining intended victim that the two of them should surrender. That is how we know about this game; in effect. Josephus cheated.1If M = 0, the players arc eliminated in order, and the last player always wins. For other values of M. things are not so obvio Ebook Data structures and problem solving using C++ (2/E): Part 2us. Figure 14.1 shows that if N = 5 and M = I, the players arc eliminated in the order 2. 4, 1. 5. In this case, player 3 wins. The steps arc as folloEbook Data structures and problem solving using C++ (2/E): Part 2
ws.1At the start, the potato is at player 1. After one pass it is at player 2.2Player 2 is eliminated. Player 3 picks up the potato, and after one pasChapter 14SimulationAn important use of computers is for simulation, in which the computer is used to emulate the operation of a real system and gathe Ebook Data structures and problem solving using C++ (2/E): Part 2tato and passes it to player 5.5Player 5 is eliminated, so player 3 wins.First, we write a program that simulates, pass for pass, a game for any values of ;V and M. The running lime of the simulation is Ơ(MV). which is acceptable if the number of passes is small. Each step lakes Ơ(M) lime because it Ebook Data structures and problem solving using C++ (2/E): Part 2 performs M passes. We then show how to implement each step in ớ(log N) lime, regardless of the number of passes performed. The running time of the siEbook Data structures and problem solving using C++ (2/E): Part 2
mulation becomes O(N log AT).I.Thanks lo David Teague for relaying this story. The version that we solve differs from the historical description In ExChapter 14SimulationAn important use of computers is for simulation, in which the computer is used to emulate the operation of a real system and gathe Ebook Data structures and problem solving using C++ (2/E): Part 2gests that we represent the players in a linked list. We create a linked list in which the elements 1, 2, ... .Nare inserted in order. We then set an iterator to the front element. Each pass of the potato corresponds to a + + operation on the iterator. At the last player (currently remaining) in the Ebook Data structures and problem solving using C++ (2/E): Part 2 list we implement the pass by resetting the iterator to the first element. This action mimics the circle. When we have finished passing, we remove thEbook Data structures and problem solving using C++ (2/E): Part 2
e element on which the iterator has landed.An implementation is shown in Figure 14.2 The linked list and iterator are declared at lines 8 and 9. respeChapter 14SimulationAn important use of computers is for simulation, in which the computer is used to emulate the operation of a real system and gathe Ebook Data structures and problem solving using C++ (2/E): Part 2 by passing the potato (lines 20 to 25) and then eliminating a player (lines 30-33). This procedure is repeated until the test at line 18 tells US that only one player remains. At that point we return the player's number at line 36.The running time of this routine is O(.W.V) because that is exactly Ebook Data structures and problem solving using C++ (2/E): Part 2the number of passes that occur during the algorithm. For small Af, this running time is acceptable, although we should mention that the case M = 0 doEbook Data structures and problem solving using C++ (2/E): Part 2
es not yield a running time of ớ(0); obviously the running time is Ớ(/V). We do not merely multiply by zero when trying to interpret a Big-Oh expressiChapter 14SimulationAn important use of computers is for simulation, in which the computer is used to emulate the operation of a real system and gathe Ebook Data structures and problem solving using C++ (2/E): Part 2m (in logarithmic time). Doing so allows US to implement each round of passing in a single operation. Figure 14.1 shows why. Suppose that we have N players remaining and are currently at player p from the front. Initially ;V is the total number of players and p is 1. After M passes, a calculation te Ebook Data structures and problem solving using C++ (2/E): Part 2lls US that we are at player ((P + M) mod .V) from the front, except if that would give US player 0. in which case. we go to player /V. The calculatioEbook Data structures and problem solving using C++ (2/E): Part 2
n is fairly tricky, but the concept is not.Applying this calculation to Figure 14.1. we observe that M is 1, /V is initially 5. and p is initially I. Chapter 14SimulationAn important use of computers is for simulation, in which the computer is used to emulate the operation of a real system and gathe Ebook Data structures and problem solving using C++ (2/E): Part 2 is 3. also shown in part (b). so the third element in the list is deleted and N falls to 3. The next value of p is 4 mod 3. or 1. so we are back at the first player in the remaining list, as shown in pari (c). This player is removed and N becomes 2. At this point, we add M to p. obtaining 2. Becaus Ebook Data structures and problem solving using C++ (2/E): Part 2e 2 mod 2 is 0. we set p to player N, and thus the last player in the list is the one that is removed. This action agrees with part (d). After the remEbook Data structures and problem solving using C++ (2/E): Part 2
oval. N is 1 and we arc done.We can represent the players by a linked list and use the iterator to simulate the passing.If we implement each round of Chapter 14SimulationAn important use of computers is for simulation, in which the computer is used to emulate the operation of a real system and gatheChapter 14SimulationAn important use of computers is for simulation, in which the computer is used to emulate the operation of a real system and gatheGọi ngay
Chat zalo
Facebook