Data Structures and Algorithms · Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh...
Transcript of Data Structures and Algorithms · Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh...
IIT Bombay
Data Structures and AlgorithmsProf. Ajit A. Diwan
Prof. Ganesh RamakrishnanProf. Deepak B. Phatak
Department of Computer Science and EngineeringIIT Bombay
Session: Mumbai Vada-Pav Restaurant (Randomized simulation)
1Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
IIT Bombay
Random Numbers
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 2
• In the previous example, we simulated the arrival time of customers using some ‘assumed’ time instances.
• In practice, such events will occur randomly• When we throw a dice, we get a random value between 1 and 6• We do not know, which will be next value• On an average, we make 600 throws, we will get 100 of each value
• It is possible to ‘simulate’ such events by generating random numbers• Called ‘pseudo’ random numbers, (generated by an algorithm)
• C++ provides functions to generate random numbers
IIT Bombay
Random Numbers
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 3
• Use ‘rand()’ to generate ‘uniformly distributed’ random numbers• Returns a number from 0 to RAND_MAX
int r, dice, i;
for (i=0; i<10; i++) {
r = rand();
dice = r%6 + 1;
cout << dice << “ “;
}
2 5 4 2 6 2 5 1 4 2
IIT Bombay
Random number generation
• We wish to generate a different sequence of numbers for each ‘run’
• Use ‘srand()’• Initialize random number generator with a ‘seed’
• Different seed ensures a different sequence to be generated • Use current time as seed for random generator
std::srand(std::time(0)); //use current time as seed
Output from two different executions:
4 2 6 5 5 1 6 1 1 2
1 2 1 3 1 3 6 5 6 6
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 4
IIT Bombay
Data Structures
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 5
Maintain information for food and customer arrival
• Structure 1: foodInfo• Contains information of food item (ID, name, and rate)
• Structure 2: customerInfo• Contains information of customer (ID and arrival time) that arrive and
stand in the queue to place an order
• Queue 1: customerQueue• It is of type structure, ‘customerInfo’• Contains information of customer
IIT Bombay
Data Structures
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 6
Maintain information of the orders placed and The order that goes to the kitchen for preparation
• Structure 3: orders• Contains information of the order placed by each customer (Token
ID, Items, Quantity, Cost, Number of items ordered, Total Cost to be paid, and time for placing order)
• Structure 4: kitchen• Contains the information stored in structure ‘order’, structure
‘customerInfo’, preparation and fulfilment time of order
IIT Bombay
Functions
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 7
• Function 1: loadMenu• Passes information of one menu item to the function
‘setFoodItems’
• Function 2: setFoodItems• Loads the menu of the restaurant
• Function 3: customerArrives• Pushes the information of the customer on the queue
‘customerQueue’ (includes customer id and arrival time)
IIT Bombay
Functions
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 8
• Function 4: placeOrder• Generates token ID• Randomizes
• Number of items• Item ID, Quantity of each item
• Calculates cost, and total cost• Calculates time for placing order (based on randomization)• Sends the order information to the kitchen• Customer exits from the queue and waits at a table
IIT Bombay
Functions
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 9
• Function 5: orderGoesToKitchen• Loads information of
• Order placed • Customer
• Calculates • Preparation time for the order• Order fulfilment time
• Updates the ‘listInKitchen’ list
• Function 6: dispatchOrders• Sorts the ‘listInKitchen’ list based on fulfilment time• Dispatches orders
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 10
struct foodInfo{int foodID;string foodName;float rate;
}; //End of structure
struct customerInfo{int custID;int arrivalTime;
}; //End of structure
struct orders {int tokenID;int numberOfItems;int item[20];int qty[20];int cost[20];float totalCost;int placingOrderTime;
}; //End of structure
struct kitchen {struct orders order;struct customerInfo cinfo;int orderfulfilmentTime;int preparationTime;
}; //End of structure
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 11
class restaurant {private:
static int custID, tokenID, foodIndex;struct foodInfo food[100]; struct orders order;list<kitchen> listInKitchen; queue<customerInfo> customerQueue;
public:void loadMenu();void setFoodItems(int id, string name, float amount);void customerArrives(int time);void placeOrder(); void orderGoesToKitchen();void dispatchOrders();
}; //End of class
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 12
int restaurant::custID = 0;int restaurant::tokenID = 1000;int restaurant::foodIndex = 0;
int generateRandom(int low, int high) {//Return a random number between low and highreturn ((rand() % (high-low+1)) + low);
} //End of function
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 13
void restaurant::setFoodItems(int id, string name, float rate) {food[foodIndex].foodID = id;food[foodIndex].foodName = name;food[foodIndex].rate = rate;foodIndex++;
} //End of function
void restaurant::loadMenu() {setFoodItems(1,"Vada Pav",10.0);setFoodItems(2,"Uttappa",18.0);…setFoodItems(25,"Kothmir Wadi",26.0);
} //End of function
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 14
void restaurant::customerArrives(int time) {struct customerInfo cust;cust.custID = ++custID;cust.arrivalTime = time;customerQueue.push(cust);cout << "Customer: " << customerQueue.back().custID << ", "
<< customerQueue.back().arrivalTime << endl;} //End of function
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 15
void restaurant::placeOrder() {int index=0, itemID, qty, i, low;order.tokenID = ++tokenID;int totalCost, ptime;
// Code for placing order // Code for calculating placing order time// Code to display order info of customer
} //End of function
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 16
//Place order for numberOfItems items. where numberOfItems<=6order.numberOfItems = generateRandom(1,6);itemID = generateRandom(1,3);index = generateRandom(1,3);for (i=0;i<order.numberOfItems;i++) {
itemID = itemID + index;qty = generateRandom(1,10); //Qty is from 1 to 10order.item[i] = itemID;order.qty[i] = qty;order.cost[i] = food[itemID].rate * qty; totalCost = totalCost + order.cost[i];
}order.totalCost = totalCost;
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 17
//Calculate placing order timelow = order.numberOfItems * 5;ptime = generateRandom(low, low+15);if (listInKitchen.empty())
order.placingOrderTime = ptime + customerQueue.front().arrivalTime;else {
if (customerQueue.front().arrivalTime > listInKitchen.back().order.placingOrderTime)order.placingOrderTime = ptime + customerQueue.front().arrivalTime;
elseorder.placingOrderTime = ptime + listInKitchen.back().order.placingOrderTime;
} //End of else
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 18
//Display order info of customercout << "Order placed for customer " << customerQueue.front().custID
<< " with token " << order.tokenID << " and timetaken is " << ptime << endl;orderGoesToKitchen();customerQueue.pop();
} //End of function placeOrder()
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 19
void restaurant::orderGoesToKitchen() {kitchen k;int pTime;k.order = order;k.cinfo = customerQueue.front();pTime = order.numberOfItems * 120;k.preparationTime = generateRandom(pTime,pTime+60) ;k.orderfulfilmentTime = k.preparationTime + order.placingOrderTime;
listInKitchen.push_back(k);cout << "List in kitchen updated for token id: " << listInKitchen.back().order.tokenID
<< ", Preparation time: " << listInKitchen.back().preparationTime << endl << endl;} //End of function
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 20
bool compare(kitchen first, kitchen second) {return (first.orderfulfilmentTime < second.orderfulfilmentTime);
} //End of function
void restaurant::dispatchOrders() {listInKitchen.sort(compare);int i;while (!listInKitchen.empty()) {
cout << "Dispatched Token ID: " << listInKitchen.front().order.tokenID<< " fulfilled at: " << listInKitchen.front().orderfulfilmentTime << endl;
listInKitchen.pop_front();}
} //End of function
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 21
int main() {std::srand(std::time(0)); restaurant MVPRestaurant; int i, custarrive=0;MVPRestaurant.loadMenu();
for (i=1; i<=5; i++) {custarrive = custarrive + generateRandom(1,20);MVPRestaurant.customerArrives(custarrive);
} cout << endl;for (i=1; i<=5; i++) {
MVPRestaurant.placeOrder();} cout << endl;MVPRestaurant.dispatchOrders();return 0;
} //End of main()
IIT Bombay
Thank you
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 22