C Language algorithm

Source: Internet
Author: User

Although designing a good algorithm is more like an art than a technology, there are still some effective algorithms designed to solve many problems, and you can use these methods to design algorithms and see how they work. In general, in order to obtain better performance, the algorithm must be carefully adjusted. However, in some cases, when the algorithm is adjusted and the performance is still unable to meet the requirements, then we must find another way to solve the problem.

This chapter first introduces the concept of optimization, and then introduces an intuitive problem solving method: greedy algorithm. Finally, this algorithm is used to solve the problem of ship loading problem, knapsack problem, topology sort problem, binary coverage problem, shortest path problem and minimum cost spanning tree.

1.1 Optimization problems

Many of the examples in this chapter and in subsequent chapters are the Optimization problems (optimization problem), each of which contains a set of constraints (c o n s t r a i n T) and an optimization function (optimization function) that meet the constraints of the question The solution of the problem is called the feasible solution (feasible solution), and the feasible solution of the optimal function to obtain the best value is called the optimal solution (optimal solution).

Example 1-1 [The problem of thirsty babies] there is a very thirsty, clever little baby, she might get something including a glass of water, a bucket of milk, multiple cans of different kinds of juice, many different bottles or cans of soda, that is, the baby can get n different kinds of drinks. Based on the different experiences of the previous n drinks, the baby knows that some of these drinks are more palatable to them, so the baby takes the following method to give each drink a satisfactory value: Drink 1 oz. I drink, compare it to a value of SI as satisfaction to give the first type of drink.

Usually, the baby will try to drink the maximum satisfaction of the drink to maximize the needs of her thirst, but unfortunately: the highest degree of satisfaction with the drink sometimes does not have enough to meet the needs of the baby's thirst. AI is the total amount of the first I drink (in ounces), and this baby needs T-ounce drink to quench their thirst, so how much of the different kinds of drinks need to be drunk to meet the needs of the baby's thirst?

The satisfaction of various beverages is known. The problem to be solved is the amount of the first drink that Xi is to be consumed by a baby:

Finds a set of real number XI (1≤i≤n) that makes N I=1si xi the largest and satisfies: N. i=1xi=t and 0≤xi≤ai.

It should be pointed out that if n. I=1ai < T, it is not possible to find a solution to the problem, because even drinking all the drinks will not make the baby thirst.

The precise mathematical description of the above problem clearly indicates the work that must be done by the program, and according to these mathematical formulae, the input/output can be described as follows:

Input: N,t,si, AI (where 1≤i≤n,n is integer, T, si, AI is positive real number).

Output: The real number XI (1≤i≤n), which makes N I=1si xi the largest and N-i=1xi=t (0≤xi≤ai). If n? i=1ai

In this problem, the restriction condition is N-i=1xi=t and 0≤xi≤ai,1≤i≤n. and the optimization function is N. I=1si XI. Any set of real XI which satisfies the restriction condition is a feasible solution, and the most feasible solution for N-i=1si Xi is the optimal solution.

Example 1-2 [loading problem] there is a big ship ready to load the goods. All the goods to be loaded are in the container and all the containers are of the same size, but the weight of the crates varies. The weight of the first container is WI (1≤i≤n) and the maximum carrying capacity of the cargo ship is C, and our aim is to mount the most cargo on the freighter.

This problem can be described as an optimization problem: There is a set of variable XI, which may take a value of 0 or 1. If XI is 0, the container I will not be loaded on board, and if XI is 1, the container I will be loaded on board. Our aim is to find a group of Xi that satisfies the restrictive conditions N. i=1wi xi≤c and X I? {0, 1}, 1≤i≤n. The corresponding optimization function is n. i=1xi.

Each group of Xi that satisfies the restriction condition is a feasible solution, and the solution that makes the maximum value of N-i=1xi is the optimal solution.

Example 1-3 [Minimum cost communication network] all possible communication connections between cities and cities can be considered as a non-direction graph, each side of the graph is given a weight value, which represents the cost of building the communication connection represented by this edge. The connected subgraph containing all vertices (cities) in the graph is a feasible solution. If all weights are nonnegative, all possible feasible solutions can be represented as a set of spanning trees in the non-direction graph, and the optimal solution is a spanning tree with the lowest cost.

In this problem, you need to select a subset of the set of edges in the non-direction graph, which must satisfy the following constraints: all edges constitute a spanning tree. The optimization function is the sum of the weights of all the edges in the subset.

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: info-contact@alibabacloud.com 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.