160 likes | 362 Views
Chapter 4. Queues - 2. Internet Computing Laboratory @ KUT Youn-Hee Han. 4. Queuing Theory. Queuing theory a field of applied mathematics that is used to predict performance of queues. Queuing Type Single-server queue Hot-food vender Multi-server queue Many bank tellers in a bank
E N D
Chapter 4. Queues - 2 Internet Computing Laboratory @ KUT Youn-Hee Han
4. Queuing Theory • Queuing theory • a field of applied mathematics that is used to predict performance of queues. • Queuing Type • Single-server queue • Hot-food vender • Multi-server queue • Many bank tellers in a bank • Multiple single-server queues • Two common Elements in Queuing Theory • Customer • Any person or thing needing service • Service • Any activity needed to accomplish the required result • Two factors affecting a queue • Arrival Rate ( Queue Time) • Service Time • Response Time • Queue Time + Service Time Multi-server Queuing System Servers Queue Server Customer Leaving Customer ….. Server .…... Server Data Structure
5. QueueApplications • Business Online Application • Customer online requests, jobs, or orders • Computer System • Job (or process) scheduling • Print spool • 교재에서 주어진 두 개의 Queue Applications • Categorizing data • Queue Simulation • To study the performance of any queue application • (Optional Study) • PPT 자료에서 주어지는 Queue Application • Goal Seeking BFS (Breadth First Search) Data Structure
5. QueueApplications • Goal of Categorizing Data (교재 168~) • Rearrange data in separated groups without destroying their original order in each group • For example • Four different groups • Group 1: less than 10 • Group 2: between 10 and 19 • Group 3: between 20 and 29 • Group 4: between 30 and greater • Input • Output • Note: the numbers in each group have kept their original order 3 22 12 6 10 34 65 29 9 30 81 4 5 19 20 57 44 99 | 3 6 9 4 5 | 12 10 19 | 22 29 20 | 34 65 30 81 57 44 99 | Data Structure
5. QueueApplications • Structures for Categorizing Data • Initialization before calling fillQueues • After calling fillQueues Data Structure
5. QueueApplications • Source Codes: Categorizing Data • File Name: catagorize.c #include <stdio.h> #include <stdlib.h> #include "stdbool.h" #include "queues.h" void fillQueues (QUEUE*, QUEUE*, QUEUE*, QUEUE*); void printQueues (QUEUE*, QUEUE*, QUEUE*, QUEUE*); void printOneQueue (QUEUE* pQueue); int main (void) { QUEUE* q0to9; QUEUE* q10to19; QUEUE* q20to29; QUEUE* qOver29; q0to9 = createQueue (); q10to19 = createQueue (); q20to29 = createQueue (); qOver29 = createQueue (); fillQueues (q0to9, q10to19, q20to29, qOver29); printQueues (q0to9, q10to19, q20to29, qOver29); return 0; } Data Structure
5. QueueApplications • Source Codes: Categorizing Data • File Name: catagorize.c void fillQueues (QUEUE* q0to9, QUEUE* q10to19, QUEUE* q20to29, QUEUE* qOver29) { int category; int item; int* dataPtr; inti; printf("Categorizing data:\n"); srand(79); for (i = 1; i <= 25; i++) { if (!(dataPtr = (int*) malloc (sizeof (int)))) printf("Overflow in fillQueues\a\n"), exit(100); *dataPtr = item = rand() % 51; // (0 ~ RAND_MAX)%51, RAND_MAX=32767 category = item / 10; printf("%3d", item); if (!(i % 11)) printf("\n"); Data Structure
5. QueueApplications • Source Codes: Categorizing Data • File Name: catagorize.c switch (category) { case 0 : enqueue (q0to9, dataPtr); break; case 1 : enqueue (q10to19, dataPtr); break; case 2 : enqueue (q20to29, dataPtr); break; default : enqueue (qOver29, dataPtr); break; } } printf("\nEnd of data categorization\n\n"); return; } Data Structure
5. QueueApplications • Source Codes: Categorizing Data • File Name: catagorize.c void printQueues (QUEUE* q0to9, QUEUE* q10to19, QUEUE* q20to29, QUEUE* qOver29) { printf("Data 0.. 9:"); printOneQueue (q0to9); printf("Data 10..19:"); printOneQueue (q10to19); printf("Data 20..29:"); printOneQueue (q20to29); printf("Data over 29:"); printOneQueue (qOver29); return; } Data Structure
5. QueueApplications • Source Codes: Categorizing Data • File Name: catagorize.c void printOneQueue (QUEUE* pQueue) { int lineCount; int* dataPtr; lineCount = 0; while (!emptyQueue (pQueue)) { dequeue (pQueue, (void*)&dataPtr); if (lineCount++ >= 10) { lineCount = 1; printf ("\n "); } printf("%3d ", *dataPtr); } printf("\n"); return; } Data Structure
5. QueueApplications • C로 Random Number 만들기 • void srand(unsigned int seed); • Random Number Generation에 대한 seed 값 설정 • 흔히 사용하는 초기화 방법 • int rand( void ); • 하나의 (pseudo-)random number (정수)를 하나 발생시킴 • 발생되는 정수의 범위: 0 ~ RAND_MAX (32767) • 두 함수 모두 <stdlib.h>를 필요로 함 time_t seed; //time_t의 구조체 변수 seed 변수 생성 time(&seed); //시스템 상의 현재 시간을 seed에 얻어온다. srand((unsigned int) seed); //srand 호출을 통하여 Random Number Generation에 대한 seed값 설정 Data Structure
5. QueueApplications • C로 Random Number 만들기 • For example • 10부터1000 사이의 정수를 Random 하게 50000개를 만들어서 배열에 저장하라. #include <stdio.h> #include <stdlib.h> const int LOW = 10; const int HIGH = 1000; const int NUM_DATA = 50000; int main(void) { int data[50000]; int i; time_t seed; time(&seed); srand((unsigned int) seed); for (i = 0 ; i < NUM_DATA ; i++) { data[i] = rand() % (HIGH - LOW + 1) + LOW; } for (i = 0 ; i < NUM_DATA-1 ; i++) { printf ("%d\t", data[i]); } } Data Structure
5. QueueApplications • Goal Seeking 너비우선탐색 (BFS: Breadth First Search) • 소모적 탐색(消耗, Exhaustive Search) 방법의 일종 • 탐색 순서에 있어서 깊이보다는 폭을 우선적으로 취한다. • 탐색 방법 • 0) A가 Origin • 1) A에서 거리가 1인 모든 노드를 방문 • 2) 다음 방문한 노드에서 부터 거리가 1인 모든 노드, 즉 A에서 거리가 2인 모든 노드들을 방문한다. • 3) 위와 같은 방법 반복 • F까지의 경로가 있는가? • A-B-G-C-E-H-D-F Data Structure
5. QueueApplications • BFS을 위한 Queue • 시작 노드를 enqueue • dequeue와 동시에 인접 노드들을enqueue • 한번 enqueue한 노드는 다시 enqueue하지 않음 • Queue에서 dequeue된 노드를 순차적으로 나열하면그것이 BFS의 탐색 순서가 됨 Data Structure
5. QueueApplications • BFS의 Pseudo-code BreadthFirstSearch(Origin) { createQueue(); 새로운 큐를 만들기 enqueue(Origin); 출발지를 큐에 삽입 Mark Origin as Visited; 출발지를 가 본 것으로 표시 while (!queueEmpty( )) { 빈 큐가 아닐 동안 queueFront(Front); 큐 front에 있는 노드를 Front로 복사 dequeue( ); 큐 front 제거 for (Each Unvisited Nodes C Adjacent to Front) { enqueue(C); 큐에 삽입 Mark C as Visited; 가 본 것으로 표시 } } } Data Structure
5. QueueApplications • DFS vs. BFS Data Structure