Algorithm Design and Analysis Big Questions and Answers

[Algorithm Analysis and Design] Answer to the Optimal Order of Service Problem_Optimal Order of Service Problem Algorithm

Optimal Order of Service Problem

There are n customers waiting for the same service at the same time. The service time required by customer i is ti,1

Reference Answer

I. Optimal Order of Service Problem

II. Running Environment (Software and Hardware Environment)

Running Software: Window 764 bit

Hardware : Asus PC

Writing program: C++ language

Compilation environment: VC++6.0

Three, the idea of algorithm design

First, to minimize the average waiting time of n customers, that is: to make the n customers wait for the minimum service time sum. Because, the average waiting time = the sum of waiting service time/n.

Next, since the service time of each customer i is ti, to realize the minimization of the sum of waiting service time, we should arrange for the customer with the smallest value of ti as possible, to be served.

Therefore, this problem is a locally optimal design problem, i.e., a greedy algorithm.

Fourth, the flow chart of the algorithm

Page

Fifth, the algorithm design analysis

Assuming that the original problem’s time is T, and that a certain optimal series of services is already known, the optimal solution is min={t(1), t(2), …… , t(n)} (where t(i) is the service time required by the ith customer), then the waiting is time required by each customer is:

T(1)=t(1);

T(2)=t(1)+t(2);

……

T(n)=t(1)+t(2)+…… +t(n);

Then, the total waiting time, which is the optimal solution

Tmin=n*t(1)+(n-1)*t(2)+(n-2)*t(3)…… +(n+1-i)*t(i)+…… +2*t(n-1)+1*t(n);

Since, the average waiting time is the sum of the waiting time of n customers divided by n, the problem is transformed into the problem of finding the order of service that minimizes the sum of the waiting time of customers.

VI. Source Code

#include

#include

#include

#include

longn=-1; //number of customers is n

long*wait; //customers each wait time wait

voidinputData()

{//inputData n, wait time wait

ifstreamfin ;

fin.open(*input.txt’, los::nocreate);

if(!fin){

cout

return;

< p>

}

fin>>n;

wait==newlong[n];

for(1ongi=0;i

{

fin&gt ;>wait[i];

)

fin.close0;

}

voidShellSort(long*x)

(//Shell sorting, implementing the Sort data from smallest to largest longi, j, temp. gap=n/2;

while(gap>0){

for(i=gap; i

j=i-gap;

while(j&gt ;=0){

if(x[j]>x[j+gap])

{

temp=x[j]; x[j]=x[j+gap]; x[j+gap]=temp;//implementing a size swap

j= j-gap;

}

else{j=-1;}

}

}

gap=gap/2;

}

}

}

/**

Function name: AveWait0

Description: Calculate the average wait time

Parameters: wait time for each customer

**/

doubleAveWait(long*x)

{

doubleave=0.0;

ShellSort(x);

for(longi=0; i

p>{

ave+=1.0*(n-i)*x[i];

}

ave/=n; //find the average wait time

returnave;

)

voidoutputData(doubleout)

(//output result

ofstreamfout;

fout.open(“output.txt”);

fout

fout.close0;

)

voidmain0

{//main call function

inputData();

if(n!=-1)(

doubleavewait=AveWait(wait);

outputData(avewait):

}

}

VII. Analysis of running results

Test results:

input.txt:

125622l9901002234334597810

output.txt:

532.00

VIII. Takeaways and Lessons Learned

This problem minimizes the average customer waiting time, which translates into minimizing the sum of service waiting time. The problem is solved by a greedy algorithm using local optimization.

Through this problem, but also a deeper understanding of the nature of the greedy algorithm, the future for other similar local optimization problems, optimal substructure problems, can be solved using the greedy algorithm.

Algorithm analysis and design topics

The first question to use the greedy idea to find out the shortest time m jobs to the machine at the same time to start processing and then sequentially the remaining jobs in the shortest completion of the job out into the completed machine processing when the last machine completion time is the shortest time used to the idea is the way specific algorithms to achieve the words. As I am also a student =, = write code is not very skillful. Maybe when I write a good test you can’t make it.

Second question

1. What is the backpack problem =, = our textbook is not the same do not understand the specific problem.

2.4 queens

#include<iostream.h>

constintn=4;

constintn_sub=n-1;

intqueen[n];

boolrow[n];

boolpassive[2*n-1];

boolnegative[2*n-1];

intmain()

{

intcur=0;

boolflag=false;

queen[0]=-1;

intcount=0;

while(cur>=0)

{

while(cur>=0&&queen[cur]<n&&!flag)

{

queen[cur]++;

if(queen[cur]>=n)

{

queen[cur]=-1;

cur–;

if(cur>=0)

{

row[queen[cur ]]=false;

passive[queen[cur]+cur]=false;

negative[n_sub+cur-queen[cur]]=false;

}

false;

}

else

{

if(row[queen[cur]]==false)

{

flag=true;

if(passive[queen[cur]+cur]==true||negative[n_sub+cur- queen[cur]]==true){

flag=false;

}

else

flag=true;

if(flag){

if(cur==n-1)

{

count++ )

}

row[queen[cur]]=true;

passive[queen[cur]+cur]=true;

negative[n_sub+cur-queen[cur]]=true;

cur++;< /p>

if(cur>=n){

cur–;

row[queen[cur]]=false;

passive[queen[cur]+cur]=false;

negative[n_sub+cur-queen [cur]]=false;

}

flag=false;

}

}

}

}

}

cout<<n<< “The queens problem has a total of”<<count& lt;< “number of solutions”<<endl;

return0;

}

This is code. The state space tree can’t be drawn here….

The third question

You Baidu basically have it =, =. I’m sorry for posting this on Baidu, but you can see for yourself

For example, the answer to 1.

The worst case scenario gives an upper bound on the execution time of the algorithm, and we can be sure that the algorithm won’t take longer than that no matter what inputs we give it, which facilitates comparisons and analyses.

The question in Chapter 1 in Algorithm Design Techniques and Analysis is begging to be answered

n!/2^n+n^(n/2)= Θ(n!/2^n)

Because

Therefore

n!/2^n>>n^(n/2)