Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4. Queues - 2 Internet Computing KUT Youn-Hee Han.

Similar presentations


Presentation on theme: "Chapter 4. Queues - 2 Internet Computing KUT Youn-Hee Han."— Presentation transcript:

1 Chapter 4. Queues - 2 Internet Computing Laboratory @ KUT Youn-Hee Han

2 Data Structure2 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 ….. Server.…... Customer Queue Servers Leaving Customer Multi-server Queuing System

3 Data Structure3 5. Queue Applications 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)

4 5. Queue Applications 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 Data Structure4 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 |

5 5. Queue Applications Structures for Categorizing Data Initialization before calling fillQueues After calling fillQueues Data Structure5

6 5. Queue Applications Source Codes: Categorizing Data File Name: catagorize.c Data Structure6 #include #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; }

7 5. Queue Applications Source Codes: Categorizing Data File Name: catagorize.c Data Structure7 void fillQueues (QUEUE* q0to9, QUEUE* q10to19, QUEUE* q20to29, QUEUE* qOver29) { int category; int item; int* dataPtr; int i; 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");

8 5. Queue Applications Source Codes: Categorizing Data File Name: catagorize.c Data Structure8 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; }

9 5. Queue Applications Source Codes: Categorizing Data File Name: catagorize.c Data Structure9 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; }

10 5. Queue Applications Source Codes: Categorizing Data File Name: catagorize.c Data Structure10 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; }

11 5. Queue Applications C 로 Random Number 만들기 void srand(unsigned int seed);  Random Number Generation 에 대한 seed 값 설정  흔히 사용하는 초기화 방법 int rand( void );  하나의 (pseudo-)random number ( 정수 ) 를 하나 발생시킴  발생되는 정수의 범위 : 0 ~ RAND_MAX (32767) 두 함수 모두 를 필요로 함 Data Structure11 time_t seed;//time_t 의 구조체 변수 seed 변수 생성 time(&seed);// 시스템 상의 현재 시간을 seed 에 얻어온다. srand((unsigned int) seed); //srand 호출을 통하여 Random Number Generation 에 대한 seed 값 설정

12 5. Queue Applications C 로 Random Number 만들기 For example  10 부터 1000 사이의 정수를 Random 하게 50000 개를 만들어서 배열에 저장하라. Data Structure12 #include 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]); }

13 Data Structure13 5. Queue Applications 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

14 Data Structure14 5. Queue Applications BFS 을 위한 Queue 시작 노드를 enqueue dequeue 와 동시에 인접 노드들을 enqueue 한번 enqueue 한 노드는 다시 enqueue 하지 않음 Queue 에서 dequeue 된 노드를 순차적으로 나열하면 그것이 BFS 의 탐색 순서가 됨

15 Data Structure15 5. Queue Applications 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; 가 본 것으로 표시 }

16 Data Structure16 5. Queue Applications DFS vs. BFS


Download ppt "Chapter 4. Queues - 2 Internet Computing KUT Youn-Hee Han."

Similar presentations


Ads by Google