Algoritmai ir duomenų struktūros (ADS)

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

CS201: Data Structures and Discrete Mathematics I Linked Lists, Stacks and Queues.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Parallel and Distributed Simulation
An Introduction to Hashing. By: Sara Kennedy Presented: November 1, 2002.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
CHAPTER 7 Queues.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
 Balancing Symbols 3. Applications
CS Data Structures II Review COSC 2006 April 14, 2017
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Queues.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate your code.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Data Structures - Queues
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
CSC 205 Programming II Lecture 22 Carwash Simulation.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Data Structures Using C++
Data Structures: A Pseudocode Approach with C1 Chapter 4 Objectives Upon completion you will be able to: Explain the design, use, and operation of a queue.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Chapter 7 Queues Introduction Queue applications Implementations.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
1 1 Nastaran Shafiei VERIFICATION OF A NON-BLOCKING ARRAY-BASED QUEUE ALGORITHM.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
Learning Javascript From Mr Saem
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
Queues Chapter 4.
QueueStack CS1020.
Queues Queue Concept Queue Design Considerations
Stacks and Queues.
Queues Queues Queues.
Queues Chapter 4.
CSCE 3110 Data Structures & Algorithm Analysis
Smart none of us are as smart as all of us. smart none of us are as smart as all of us.
Robert Andruškevič AT27D.   Tai yra operacinė sistema, daugiausia naudojama išmaniuosiuose telefonuose, nors ją galima įdiegti ir kituose mobiliuosiuose.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues.
Algoritmai ir duomenų struktūros (ADS)
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Programų sistemų inžinerija
Parallel and Distributed Simulation
البرمجة بلغة الفيجول بيسك ستوديو
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Saulius Ragaišis, VU MIF
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Algoritmai ir duomenų struktūros (ADS)
Queues: Implemented using Arrays
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Queues Jyh-Shing Roger Jang (張智星)
Breadth First Search - A B C D E F G H I front FIFO Queue.
Decisions, decisions, decisions
Queues.
CSC 248 – fundamentals of Data structure
Stacks, Queues, and Deques
Learning Intention I will learn about the standard algorithm for input validation.
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

Algoritmai ir duomenų struktūros (ADS) 6 paskaita Saulius Ragaišis, VU MIF saulius.ragaisis@mif.vu.lt 2012-03-12

Modeliavimo programos pavyzdys Sąlyga: savitarnos parduotuvė dirba T laiko vienetų; joje yra N kasų; kiekvienu laiko momentu su tikimybe M (%) ateina klientas; jo apsipirkimo laikas yra atsitiktinis, bet ne didesnis nei A; baigęs pirkti, jis stoja į eilę prie atsitiktinės kasos; jo aptarnavimo prie kasos laikas yra atsitiktinis, bet ne didesnis nei B.

Modeliavimo programos pavyzdys Rasti: kiek reikia turėti parduotuvės vežimėlių, kad jų nepritrūktų (daroma prielaida, kad aptarnavus klientą prie kasos, jo vežimėliu iš karto gali naudotis naujas klientas).

Programos parametrų failas 500 modeliavimo laikas 5 kasu skaičius 50 kliento atėjimo tikimybė (%) 40 maksimalus kliento apsipirkimo laikas 10 maksimalus kliento aptarnavimo prie kasos laikas

Eilėje saugomų duomenų tipas unit QueueData; interface type QueueDataType = ...; implementation end.

ADT Eilė unit Queue; interface uses QueueData; type QueueType = ...; procedure createQueue (var queue : QueueType); function isEmptyQueue (const queue : QueueType) : boolean; function isFullQueue (const queue : QueueType) : boolean; procedure enQueue (var queue : QueueType; data : QueueDataType; var err : boolean); procedure deQueue (var queue : QueueType; var data : QueueDataType; var err : boolean); procedure firstInQueue (const queue : QueueType; var data : QueueDataType; var err : boolean); function lengthOfQueue(const queue : QueueType) : integer; procedure destroyQueue (var queue : QueueType); implementation

Programa program Shop; uses QueueData, Queue; const PARAMS_FILE_NAME = 'params.txt'; MAX_CASH_DESKS = 10; MAX_ACTIVE_CLIENTS = 20; { Global variables } var errors : boolean; simulationTime, noOfCashDesks, newClientProbability, maxBuyingTime, maxCashingTime : integer; procedure loadParams; ... { Program variables } var time, maxNoOfShoppingCarts, usedShoppingCarts, noOfActiveClients, i, j, k : integer; cashingTime : QueueDataType; activeClients : array[1..MAX_ACTIVE_CLIENTS] of integer; cashDesks : array[1..MAX_CASH_DESKS] of QueueType; cashingLeft : array[1..MAX_CASH_DESKS] of integer; begin loadParams; if errors then exit; ...

Programa (2) while not errors and (time < simulationTime) do { Main simulation loop } begin inc(time); { Get new client } if random(100) <= newClientProbability then { New client should be added to active clients } inc(noOfActiveClients); if noOfActiveClients > MAX_ACTIVE_CLIENTS then writeln('Number of active clients become too big ( >', MAX_ACTIVE_CLIENTS, ' ).'); errors := true; break; end; activeClients[noOfActiveClients] := time + random(maxBuyingTime) + 1; { New client gets a shopping cart } inc(usedShoppingCarts); if usedShoppingCarts > maxNoOfShoppingCarts then maxNoOfShoppingCarts := usedShoppingCarts; { Check active clients who finished buying } ... { Check queues at cash desks } ... end; { Main simulation loop }

Programa (3) { Check active clients who finished buying } j := 0; for i := 1 to noOfActiveClients do begin if activeClients[i] > time then inc(j); activeClients[j] := activeClients[i]; end else k := random(noOfCashDesks) + 1; cashingTime := random(maxCashingTime) + 1; enQueue(cashDesks[k], cashingTime, errors); if errors then writeln('The queue of cash desk becomes too long.'); break; end; if lengthOfQueue(cashDesks[k]) = 1 then cashingLeft[k] := cashingTime + 1; dec(noOfActiveClients);

Programa (4) { Check queues at cash desks } for k := 1 to noOfCashDesks do if lengthOfQueue(cashDesks[k]) > 0 then begin dec(cashingLeft[k]); if cashingLeft[k] = 0 then deQueue(cashDesks[k], cashingTime, errors); if errors then writeln('Program error: trying to dequeue.'); break; end; firstInQueue(cashDesks[k], cashingTime, errors); cashingLeft[k] := cashingTime;

Programa (5) Pilnas programos tekstas bus pateiktas internete. { Output simulation results } writeln('Max number of shopping carts is ', maxNoOfShoppingCarts); { Clearing data structues } for i := 1 to MAX_CASH_DESKS do destroyQueue(cashDesks[i]); end. Pilnas programos tekstas bus pateiktas internete.

Programos vykdymas D:\MIF\A&DS\_Pvz>shop_a Max number of shopping carts is 244 Pagrindinės programos pradžioje pridėjus: Randomize; D:\MIF\A&DS\_Pvz>shop_r Max number of shopping carts is 257 Max number of shopping carts is 256 Max number of shopping carts is 267 Max number of shopping carts is 251 Ar programos rezultatai teisingi?

Apibendrinimas Visada vertėtų pavertinti, ar programos rezultatai prasmingi. Modeliavome 500 laiko vienetų, o kliento atėjimo tikimybė yra 50%. Taigi per visą laiką turėtų ateiti apie 250 klientų. O programa sako, kad tiek vežimėlių ir reikia. Kyla įtarimas, kad vežimėliai neatlaisvinami... Patikrinus, pasirodo, kad programoje praleistas dec(usedShoppingCarts);

Klausimai ?