Top Banner
Chapter 4. Queues - 2 Internet Computing Laboratory @ KUT Youn-Hee Han
16

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

Jan 17, 2018

Download

Documents

Cathleen Rose

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)
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 4. Queues - 2 Internet Computing KUT Youn-Hee Han.

Chapter 4. Queues - 2

Internet Computing Laboratory @ KUT

Youn-Hee Han

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

Data Structure2

4. Queuing TheoryQueuing 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

…..ServerServerServer.…...

Customer QueueServers

Leaving Customer

Multi-server Queuing System

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

Data Structure3

5. Queue ApplicationsBusiness 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)

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

5. Queue ApplicationsGoal 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 |

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

5. Queue ApplicationsStructures for Categorizing Data

Initialization before calling fillQueues

After calling fillQueues

Data Structure5

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

5. Queue ApplicationsSource Codes: Categorizing Data

File Name: catagorize.c

Data Structure6

#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;}

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

5. Queue ApplicationsSource 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");

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

5. Queue ApplicationsSource 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;}

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

5. Queue ApplicationsSource 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;}

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

5. Queue ApplicationsSource 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; }

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

5. Queue ApplicationsC 로 Random Number 만들기

void srand(unsigned int seed); Random Number Generation 에 대한 seed 값 설정 흔히 사용하는 초기화 방법

int rand( void ); 하나의 (pseudo-)random number ( 정수 ) 를 하나 발생시킴 발생되는 정수의 범위 : 0 ~ RAND_MAX (32767)

두 함수 모두 <stdlib.h> 를 필요로 함

Data Structure11

time_t seed; //time_t 의 구조체 변수 seed 변수 생성time(&seed); // 시스템 상의 현재 시간을 seed 에 얻어온다 .srand((unsigned int) seed);//srand 호출을 통하여 Random Number Generation 에 대한 seed 값 설정

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

5. Queue ApplicationsC 로 Random Number 만들기

For example 10 부터 1000 사이의 정수를 Random 하게 50000 개를 만들어서 배열에 저장하라 .

Data Structure12

#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]); }}

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

Data Structure13

5. Queue ApplicationsGoal 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

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

Data Structure14

5. Queue ApplicationsBFS 을 위한 Queue

시작 노드를 enqueue dequeue 와 동시에 인접 노드들을 enqueue 한번 enqueue 한 노드는 다시 enqueue 하지 않음 Queue 에서 dequeue 된 노드를 순차적으로 나열하면

그것이 BFS 의 탐색 순서가 됨

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

Data Structure15

5. Queue ApplicationsBFS 의 Pseudo-codeBreadthFirstSearch(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;  가 본 것으로 표시

}      } }

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

Data Structure16

5. Queue ApplicationsDFS vs. BFS