Professional Documents
Culture Documents
Sami Data Structure
Sami Data Structure
Sami Data Structure
concerned about the time that customers are required to wait for
receiving their service. The delays in receiving service which will lead to
system consist of the structure of the line, groups of demand, arrival and
inability to optimize the capability of the service will result in long queues.
the customer prefers is the initial step for the improvement of the service
-1-
Nanjing University of Aeronautics and Astronautics
similar study, Sarkar et al. applied simulation to find the best alternative in
node, where service time for each customer at the first node is a random
variable and the consecutive service times are correlated with the one at
the first node. Hammond and Mahesh also tried to propose a simulation
Jiantong and Liu and Wang and Sun , applied the six-sigma methodology
order to reduce the service time of a bank’s tellers. Reviewing the relevant
literature reveals that tellers and lobby services are the two important
-2-
Nanjing University of Aeronautics and Astronautics
be able to provide quick service and at the same time ensure that tellers
are not idle and their productive work time is properly used . It should be
noted that waiting time relies on several factors such as arrival rate of
customers in the system, time of the day, rate at which the service is
which she/he is served, the bearable queue length, the tolerable waiting
time, etc.
P{W>0}=∑j=c∞πj=A(cμ/θ,λ/θ)E1,c1+[A(cμ/θ,λ/μ)
−1]E1,cPW>0=∑j=c∞πj=Acμ/θ,λ/θE1,c1+Acμ/θ,λ/μ−1E1,c (1)
where:
A(x,y)=xeyyxγ(x,y)=1+∑n=1∞yn∏k=1n(x+k),x>0,y>0Ax,y=xeyyxγx,y=1+∑n=1∞yn∏k=1nx+
k,x>0,y>0 (2)
and
E1,c=ρ⋅E1,c−11+ρ⋅E1,c−1,E1,0=1,ρ=λ/cμc≥1E1,c=ρ⋅E1,c−11+ρ⋅E1,c−1,E1,0=1,ρ=λ/cμc≥1 (3)
Probability of abandonment due to impatience: the fraction of
abandonment P{Ab}PAb can be understood simply as the product between the
probability of abandoning due to impatience P{Ab|W>0}PAb|W>0 and the
probability of waiting in the system P{W>0}PW>0 , i.e.:
P{Ab}=P{Ab|W>0}×P{W>0}PAb=PAb|W>0×PW>0 (4)
Therefore, it follows that:
P{Ab|W>0}=∑n=c∞πnPn−c{Ab}/P{W>0}=1ρA(cμ/θ,λ/μ)+1−1ρPAb|
W>0=∑n=c∞πnPn−cAb/PW>0=1ρAcμ/θ,λ/μ+1−1ρ (5)
-3-
Nanjing University of Aeronautics and Astronautics
Average waiting time of customers who are waiting: exploring the linear relationship
between the fraction of abandonment P{Ab}PAb and the average waiting
time E{W}EW that occurs not only in the Erlang-A system, but in other models with
exponential patience and expressed by P{Ab}=θ.E(W)PAb=θ.EW , we find:
E[W|W>0]=1θ.[1ρA(cμ/θ,λ/θ)+1−1ρ]EW|W>0=1θ.1ρAcμ/θ,λ/θ+1−1ρ (6)
#include <bits/stdc++.h>
using namespace std;
};
int main(){
srand(time(0));
while(1){
cout<<"Please Enter the number of windows for the bank.\n";
cin>>wnum;
if(wnum<=0)break; // if wnum<=0 program will terminate
int n=1+rand()%100;
vector<customer> qu(n); // randomly generate all the customer arrival time and
bussiness time
for(int i=0;i<n;++i){
-4-
Nanjing University of Aeronautics and Astronautics
qu[i].ari_hr=9+rand()%8;
qu[i].ar_min=rand()%60;
qu[i].bt=rand()%30;
}
sort(qu.begin(),qu.end()); //sorting the list accprding to their arrival time
/*
last[i] will store the time at which the last customer finishes work at window i+1
serve_cnt[i] will count the number of customer served at window i+1
*/
int last[wnum]={0},cust_num=0,serve_cnt[wnum]={0};
for(int i=0;i<n;++i) {
if(qu[i].bt==0)continue;
++cust_num;
int min_last=0; //store the window number which finishes earliest
for(int j=1;j<wnum;++j) {
if(last[j]<last[min_last])min_last=j;
}
++serve_cnt[min_last];
int wt=last[min_last]-qu[i].ari_hr*60-qu[i].ar_min; //waiting time calulation
wt=max(wt,0); //waiting cant be negative
-5-
Nanjing University of Aeronautics and Astronautics
-6-
Nanjing University of Aeronautics and Astronautics
1.4 Summary
The validation was made by comparing the deviations of the performance measures
obtained through analytical queueing models and those observed with the actual data
and also estimated with the results from discrete simulation models. In order to meet
this objective, we studied different analytical queueing models with abandonment
found in the literature, which use exponential and generic probability distributions in
the abandonment process.
Mixed and non-mixed distributions based on Lognormal and Exponential were used to
model the abandonment process, among the possible distributions adjusted by the
adherence test applied to these data. The arrival, service and abandonment rates
were extracted using data from the Bank Call Center.
In addition, estimates were obtained for performance measures of the system, such
as: the average waiting time, the probability of waiting, the probability of
abandonment and traffic intensity. These parameters (rates) were inserted into the
analytical queueing models considered in this study and these performance measures
were calculated. Comparing these measurements with those extracted from the actual
data, it can be affirmed that the generic analytical queueing models to represent user
patience, considered in this study, produced results very close to those extracted from
the actual data and, therefore, captured well the reality of the Call Center in all the
analyzed performance measures.
2. Expression conversion
A conversion expression specifies how to convert a number from one unit to another. For
example, to convert a length in metres to millimetres, you must multiply the metres length by
1,000. A conversion expression is what is used to do this.
Conversion expressions can be used in five different ways: to just convert a number from one
unit to another, to add two numbers, to subtract two numbers, to multiply two numbers and to
divide two numbers. Sometimes, the necessary arithmetic can be done without a conversion.
For example, if you don't define a specific 'conversion' for adding two numbers, the system
-7-
Nanjing University of Aeronautics and Astronautics
will just convert one to the units of the other and do a normal add. The full rules are shown in
the table below. In this table A represents any unit of measure and B represents any unit of
measure with different units to A. Where rules are given, one of them must be available. If
not, the operation will be abandoned and the result will be undefined.
1. Create a stack
2. for each character ‘t’ in the input stream {
if (t is an operand)
output t
else if (t is a right parentheses){
POP and output tokens until a left parentheses is popped(but do not output)
}
else {
POP and output tokens until one of lower priority than t is encountered or a left
parentheses is encountered
or the stack is empty
PUSH t
}
3. POP and output tokens until the stack is empty.
For better understanding, let us trace out an example A * B – (C + D) + E
-8-
Nanjing University of Aeronautics and Astronautics
-9-
Nanjing University of Aeronautics and Astronautics
return postfix;
}
- 10 -
Nanjing University of Aeronautics and Astronautics
}
}
return stk.top();
}
- 11 -
Nanjing University of Aeronautics and Astronautics
goto Label;
}
}
2.3
2.4 Program Run Results
2.5 Summary
We need to develop an algorithm to convert any infix expression to a postfix
expression. To do this we will look closer at the conversion process.
Only one stack is necessary to convert an infix to a postfix expression. To adjust the
order of operators from infix to postfix, the stack that we used in the algorithm will
be used. Only operators and open parentheses symbol '(' will include the stack we
use. Postfix expressions do not contain parentheses. In the postfix output, we will not
output parentheses.
3 huffman code
A Huffman code is a special type of optimal prefix code that is widely used for data
compression without loss. By means of Huffman coding, an algorithm developed by
David A. Huffman when he was a Sc.D, the method of discovering or using such a code
continues. MIT student and published in the paper "A Method for the Construction of
Minimum-Redundancy Codes" in 1952 .[1]
- 12 -
Nanjing University of Aeronautics and Astronautics
The output from the Huffman algorithm can be viewed as a code table of variable length
to encode a source symbol (such as a character in a file). For each potential value of the
source symbol, the algorithm extracts this table from the approximate likelihood or
frequency of occurrence (weight). As in other methods of entropy encoding, using fewer
bits than less common symbols, more common symbols are usually represented. The
method of Huffman can be effectively applied, finding a code linear in time to the
number of input weights if these weights are sorted.[2] However, while optimal among
methods that separately encode symbols.
Huffman coding uses a specific method for choosing the representation for each symbol,
resulting in a prefix code (sometimes called "prefix-free codes", that is, the bit string
representing some particular symbol is never a prefix of the bit string representing any
other symbol). Huffman coding is such a widespread method for creating prefix codes
that the term "Huffman code" is widely used as a synonym for "prefix code" even when
such a code is not produced by Huffman's algorithm.
A0
B 011 011
C,111
D, 11
- 13 -
Nanjing University of Aeronautics and Astronautics
struct MinHeapNode {
char data;
unsigned freq;
struct MinHeap {
unsigned size;
unsigned capacity;
- 14 -
Nanjing University of Aeronautics and Astronautics
return temp;
}
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array
= (struct MinHeapNode**)malloc(minHeap->
capacity * sizeof(struct MinHeapNode*));
return minHeap;
}
- 15 -
Nanjing University of Aeronautics and Astronautics
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest],
&minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
- 16 -
Nanjing University of Aeronautics and Astronautics
++minHeap->size;
int i = minHeap->size - 1;
minHeap->array[i] = minHeapNode;
}
int n = minHeap->size - 1;
int i;
printf("\n");
}
- 17 -
Nanjing University of Aeronautics and Astronautics
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
{
struct MinHeapNode *left, *right, *top;
while (!isSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
return extractMin(minHeap);
}
- 18 -
Nanjing University of Aeronautics and Astronautics
if (root->left) {
arr[top] = 0;
printCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {
- 19 -
Nanjing University of Aeronautics and Astronautics
if (freq[i] != 0) {
a[i]=i+'a';
printf("%c - %d\n",
a[i], freq[i]);
}
} printf("\n");
}
}i++; }
printFrequency(f,a);
}
int main()
{
char S[2000];
FILE *filePointer;
char ch;
int i=0;
//2
filePointer = fopen("data.txt", "r");
//3
- 20 -
Nanjing University of Aeronautics and Astronautics
if (filePointer == NULL)
{
printf("File is not available \n");
}
else
{
//4
while ((ch = fgetc(filePointer)) != EOF)
{
S[i]=ch;
i++;
}
}
fclose(filePointer);
char arr[26];
int freq[26]={0};
printf("The data from file is: \n %s \n",S);
findFrequncy(S,arr,freq);
return 0;
}
Results
- 21 -
Nanjing University of Aeronautics and Astronautics
3.4 Summary
the real problem lies with the decoding phase. If we try and decode the string
00011011111011, it will be quite ambiguous since, it can be decoded to the multiple
strings, few of which are-
aaadacdad (0 | 0 | 0 | 11 | 0 | 111 | 11 | 0 | 11)
aaadbcad (0 | 0 | 0 | 11 | 011 | 111 | 0 | 11)
aabbcb (0 | 0 | 011 | 011 | 111 | 011)
… and so on
To prevent such ambiguities during decoding, the encoding phase should satisfy
the “prefix rule” which states that no binary code should be a prefix of another
code. This will produce uniquely decodable codes. The above codes for ‘a’, ‘b’, ‘c’
and ‘d’ do not follow prefix rule since the binary code for a, i.e. 0, is a prefix of binary
code for b i.e 011, resulting in ambiguous decodable codes.
4. Hash search
The Hash Table is a data structure that associatively stores data. Data is stored in an array
format in a hash table, where every data value has its own unique index value. If we
know the index of the desired data, data access becomes very rapid.
- 22 -
Nanjing University of Aeronautics and Astronautics
Thus, regardless of the size of the data, it becomes a data structure in which insertion
and search operations are very easy. The Hash Table uses an array as a storage medium
and uses a hash method to create an index from which an element is to be inserted or
located.
- 23 -
Nanjing University of Aeronautics and Astronautics
char name[50];
char sex[10];
char number[M];
}ST;
void Inittable(ST* A);
void Readfromfile(ST* A);
int Hashfun(char* number);
void Query(ST* A, char* number);
//int getprime(int n);
void print(ST* A);
//__________________
void Inittable(ST* A) {
ST stud;
strcpy_s(stud.number, sizeof(stud.number), "-1");
strcpy_s(stud.name, sizeof(stud.name), "none");
strcpy_s(stud.sex, sizeof(stud.sex), "none");
for (int i = 0; i < N; i++)
A[i] = stud;
}
//__________________
void Readfromfile(ST* A) {
FILE* fp;
fopen_s(&fp, "input.txt", "r");
char name[50];
char sex[10];
char number[M];
int index;
int count;
while (fscanf_s(fp, "%s %s %s", name,sizeof(name), sex,sizeof(sex),
number,sizeof(number))==3) {
ST stud;
strcpy_s(stud.name,sizeof(stud.name), name);
strcpy_s(stud.sex, sizeof(stud.sex), sex);
strcpy_s(stud.number, sizeof(stud.number), number);
count = 0;
index = Hashfun(stud.number);
while (1) {
if (count == 80)
break;
if (strcmp(A[index].number, "-1") == 0) {
A[index] = stud;
break;
}
- 24 -
Nanjing University of Aeronautics and Astronautics
index = (index + 1) % N;
count++;
}
}
}
//_______________
void Query(ST* A, char* number) {
int index = Hashfun(number);
int count = 0;
while (1) {
if (strcmp(A[index].number, number) == 0) {
printf("found at index %d with total hash %d\n", index,count);
return;
}
else if (strcmp(A[index].number, "-1") == 0 || count==80) {
printf("not found with total hash %d\n",count);
return;
}
else {
count++;
index = (index + 1) % N;
}
}
}
//_________________
int Hashfun(char* number) {
unsigned long long int num;
sscanf_s(number,"%llu\n",&num );
return num % N;
}
//__________________
void print(ST* A) {
for (int i = 0; i < N; i++) {
printf("%s %s %s\n", A[i].name, A[i].sex, A[i].number);
}
}
//__________________
int main(){
ST* A= (ST*)malloc(sizeof(ST) * N);;
Inittable(A);
Readfromfile(A);
Query(A,"15840971965");
Query(A, "13777904575");
Query(A, "15840971000");
- 25 -
Nanjing University of Aeronautics and Astronautics
Query(A, "13501099785");
Query(A, "23140971965");
return 0;
}
- 26 -
Nanjing University of Aeronautics and Astronautics
4.4 Summary
A hash table (hash map) in computing is a data structure that implements an abstract
data form associative list, a structure that can map keys to values. A hash table
utilizes a hash function to compute an index through an array of buckets or slots,
often called a hash code, from which the desired value can be found.
Bubble Sort
Bubble sort, is a simple comparison-based sorting algorithm that repeatedly steps
through the list, compares adjacent elements and swaps them if they are in the
wrong order. The pass through the list is repeated until the list is sorted.
Complexity:
Worst complexity: n^2
Best complexity: n
Insertion Sort
Insertion sort is a simple sorting algorithm that builds the final list one item at a time.
This is an in-place comparison-based sorting algorithm. It is much less efficient on
large lists than more advanced algorithms such as quick sort, heap sort, or merge
sort.
Complexity:
Worst complexity: n^2
Best complexity: n^2
Selection Sort
Selection sort is an in-place comparison-based algorithm in which the list is divided
into two parts, the sorted part at the left end and the unsorted part at the right end.
Initially, the sorted part is empty and the unsorted part is the entire list.
Complexity:
Worst complexity: n^2
Best complexity: n^2
Shell Sort
Shell sort, also known as Shell sort or Shell’s method, is an in-place comparison
sorting algorithm. It can be seen as either a generalization of sorting by exchange or
sorting by insertion.
Complexity:
Average complexity: n*log(n)^2 or n^(3/2)
Best complexity: n
- 27 -
Nanjing University of Aeronautics and Astronautics
The performance is in a non-decreasing order (each element according to the desired total order is
no smaller than the previous element);
The output is a permutation of the input (a reordering, but still maintaining all the original
elements).
In addition, the input data is often stored in an array that allows random access rather than a list
that allows only sequential access, although, after sufficient adjustment, several algorithms can be
applied to either data form.
- 28 -
Nanjing University of Aeronautics and Astronautics
int iC = 0, iM = 0;
for(i=1;i<n;i++){
j=i-1;
x=A[i];
iM++;
while(j>-1 && A[j]>x)
{
iC++;
A[j+1]=A[j];
iM++;
j--;
}
A[j+1]=x;
}
printf("\n\nDirect insertion sort needs %d times comparisons, %d times of
moves",iC,iM);
}
for(i=0;i<n-1;i++)
{
for(j=k=i;j<n;j++)
{
sC++;
if(A[j]<A[k])
k=j;
}
swap(A[i],A[k]);
sM++;
}
- 29 -
Nanjing University of Aeronautics and Astronautics
qC++;
if(low<high){
int p = partition(Quick,low,high);
QuickSort(Quick,low,p-1);
low = p+1;
QuickSort(Quick, low, high);
}
// cout<<"Quick sort needs "<<qC<<" times comparisons, "<<qM<<" times of moves\n";
}
comparisons++;
if(bubble[j]>bubble[j+1]){
swap(bubble[j],bubble[j+1]);
moves++;
}
}
}
int mC=0,mM=0;
void Merge(int A[], int low, int mid, int high){
- 30 -
Nanjing University of Aeronautics and Astronautics
int i = low;
int j = mid+1;
int k = low;
int B[high+1];
mC++;
while (i <= mid && j <= high){
mC++;
if (A[i] < A[j]){
mC++;
B[k++] = A[i++];
mM++;
} else {
B[k++] = A[j++];
mM++;
}
}
mC++;
while (i <= mid){
mC++;
B[k++] = A[i++];
mM++;
}
while (j <= high){
mC++;
B[k++] = A[j++];
mM++;
}
- 31 -
Nanjing University of Aeronautics and Astronautics
}
}
if (p/2 < n){
Merge(arr, 0, p/2-1, n-1);
}
printf("\n\nMerge sort needs %d times comparisons, %d times of moves",mC,mM);
}
int temp,hC=0,hM=0;;
void heapify(int arr[], int size, int i)
{
int largest = i;
int left = 2*i + 1;
int right = 2*i + 2;
hC++;
if (left < size && arr[left] >arr[largest])
largest = left;
hC++;
if (right < size && arr[right] > arr[largest])
largest = right;
hC++;
if (largest != i)
{
swap(arr[i],arr[largest]) ;
hM++;
heapify(arr, size, largest);
}
}
- 32 -
Nanjing University of Aeronautics and Astronautics
int main(){
int size;
while(1){
printf("\n\nPlease enter input size of problem: ");
scanf("%d",&size);
int i;
for( i=0; i<size; i++){
int val = rand();
arr[i] = val;
}
BubbleSort(arr,size);
InsertionSort(arr,size);
SelectionSort(arr,size);
qC = 0; qM = 0;
QuickSort(arr,0,size-1) ;
printf("\n\nQuick sort needs %d times comparisons, %d times of moves",qC,qM);
heapSort(arr,size);
MergeSort(arr,size);
return 0;
}
- 33 -
Nanjing University of Aeronautics and Astronautics
5.4 Summary
1. Understand and implement quick and merge sort.
2. Understand the use cases of insertion, bucket, counting, radix sort.
3. Learn how heap sort works with the tree data structure.
4. Know why you shouldn't use bubble sort or selection sort
- 34 -
Nanjing University of Aeronautics and Astronautics
- 35 -