Introduction to algorithms 5.1 employment issues

Source: Internet
Author: User

Chapter 5 discusses a series of interesting questions about probability analysis, Random Algorithms, probabilities, and expectations, including many interesting probability exercises.

Certificate ------------------------------------------------------------------------------------------------------------------------------------------------

Employment Problem description:

Suppose you want to hire a new office assistant and hire an agent to recommend you a candidate every day (N consecutive candidates are recommended). If you interview this person, if the candidate is better than the current office assistant, you will quit your current Office Assistant and then hire the new one. For an interview, you must pay an agency fee and an Office Assistant fee.

Assume that the interview cost is CI, and the hiring cost is Ch. Assume that M is hired throughout the process, the total cost is NCI + MCH. Since N is a fixed value, the change in the total cost depends on the m value.

This scenario is used as a general computing paradigm model. Generally, we need to check every Member in the queue and maintain a current winner to find the maximum or minimum values in the sequence. The employment problem is to build a model for the updated frequency of the current winning member.

Worst case

In the worst case, we hire every candidate, M = n.

Hire-Assistant (N) 1BestLimit 0 candidate 0 is a least-qualified dummy candidate2For ILimit 1To N3DoInterview candidateI4IfCandidateIIs better than candidateBest5Then BestBytesI6 hire candidateI

Note that N people always have to finish all the interviews, so the cost of the interview is certain. The key is that the number of times of employment is uncertain, depending on the ranking and order of N people.

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------

Probability Analysis

In fact, we neither know the order in which candidates appear nor control this order, so we use probability analysis. Probability Analysis is to use probability Technology in problem analysis.To use the probability score
Analysis, you must use knowledge about the input distribution or make assumptions about it, and then analyze the algorithm to calculate an expected running time

In some cases, we make some assumptions about all possible input sets. For other problems, you may not be able to describe a reasonable input distribution. In this case, you cannot use the probability analysis method.

In employment problems,It can be assumed that the applicant appears in a random order.. Suppose we can compare any two candidates and determine which one is better. In other words, there is a full sequence between all candidates.
System. Therefore, you can use a unique number from 1 to n to indicate the applicant's level of excellence. Rank (I) is used to represent the position of candidate I. This ordered order
Column <rank (1), rank (2 ),...,
Rank (n)> is an arrangement of sequences <1, 2,..., n>. If the applicant appears in a random order, the ranking list is 1 to n! In
Each of them appears with an equal probability.

Random Algorithm

In many cases, we know little about input distribution. Even if we know some information about input distribution, we cannot create a model for this distribution. HoweverProbability and randomness can often be used as a tool for algorithm design and analysis by randomizing the behavior of some parts of an algorithm..

For example, if an employment agent gives us a list of candidates for employment questions, we randomly select a candidate for an interview every day to ensure the randomness of the application sequence.

Generally, if the behavior of an algorithm is not only determined by the input, but also by the value generated by the random number generator, the algorithm is random.

The preceding descriptions show the differences between Probability Analysis and random algorithms:

Probability Analysis assumes that the input follows a certain distribution (for example, if the input is random), and then analyzes the expected situation based on the probability premise.

The random algorithm re-sorts the input through an algorithm to randomize the input.

Certificate --------------------------------------------------------------------------------------------------------------------------------------------------

Several exercises are quite difficult. You can find relevant answers online.

Exercise 5.1.2

Assume that random (a, B) returns any number between A and B with the same probability to describe an implementation of the random (a, B) process, it only calls the existing implementation of random (0, 1 ). As a function of A and B, what is the expected running time of your program? Assume that the run time of ramdom (0, 1) is a constant.

There is a discussion on

Thought 1:

This question is equivalent to generating n integers randomly on the premise that 0 and 1 can be generated. (The probability generated by each number is equal)
Mark the number to be generated as 0, 1, 2,..., n-1
Take the smallest M, SO 2 ^ m> = n-1
A random 0-1 function is used to generate an M-bit integer (each digit is randomly generated). In this way, an integer in [0, 2 ^ m) can be randomly generated.
A random integer in [0, 2 ^ m) is generated. If the number is within [0, n-1], the number is obtained.
If this number is outside [0, n-1], discard it and generate a new one.

Idea 2:

K = B-A + 1. If K is an even number, S is divided into two groups and one group is eliminated through R (). If s is an odd number,
The preceding method is used to group and the majority of groups are eliminated. This algorithm is also correct. In a round of tests, if the output of R () is 0 or 1, the problem scale can be reduced by 1.

Thought 3:

Or, in binary mode, enter the first half or second half of the [a, B] range based on whether ramdom () is 0 or 1, in this way, if the Interval Length is not the integer power of 2, the probability of each number being generated is different. We should do some processing to expand the range of generation, like in thought 1, we should discard the number not in the scope.

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------

Exercise 5.1.3

Suppose you want to output 0 and 1 with a probability of 1/2 each. You can use a process that outputs 0 or 1 as needed.
Biased-random. It outputs 1 in probability P and 0 in probability 1-P, where 0 <p <1, but you do not know the value of P. The following code uses biased-
As a subroutine algorithm, random returns a non-biased result. What is the expected running time of your algorithm?

Analysis: The design idea is to use symmetry.
Assume there are two benuli test sequences a and B Based on biased-random. Each test sequence generates a sequence of 0, 1 values. Each round of A and B is performed once. If the result of this round of test is
AI> Bi (that is, AI = 1, Bi = 0) indicates that the algorithm ends and the result is 1. If AI <Bi indicates that the algorithm ends, the result is 0; if AI = Bi, the next iteration starts.

Since each round of test is independent, as long as we can prove that each round is equal to the probability of 1 and 0 under the conditions of the result.

While true
X = biased-random
Y = biased-random
If X! = Y
Then return x

The probability of 1 is p * Q (x gets 1, y gets 0)

The probability of getting 0 is Q * p (x gets 0, y gets 1)


Certificate -------------------------------------------------------------------------------------------------------------------------------------------------


Chapter 5 has many interesting exercises, which are worth thinking about.


Introduction to Algorithms

Introduction to algorithms on csdn


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.