12+

Бесплатный фрагмент - NP=P? Algorithms for solving NP-problems by matrix method in Scilab program

Объем: 54 бумажных стр.

Подробнее

Introduction

From the course of school mathematics, we all know the problems of combinatorics, such as the problem of permutations, combinations, placement, represented by the corresponding formulas. But these formulas only give us the number of solutions, not the solutions themselves. There were no common standard algorithms for solving these types of problems. These types of problems with large numbers can be referred to NP problems. But with the help of Scilab program typical algorithms of such problems are revealed and solutions are given, and not only the number of solutions. The essence of the algorithms consists in operating with elements of natural series in rows and columns of the matrix, as well as in operating with rows and columns of the matrix using Scilab’s commands. NP-problems, in principle, represent all the same combinatorics problems, but in large numbers. So in one NP-problem can be present immediately as permutations and combinations and placement, can be these operations (combinations, placement, permutations) sequentially repeated, but with other, obtained in the course of solving the problem, data, can be set additional or any other conditions or calculations. The bottom line is that knowing the typical algorithms of permutations, placement, combinations, these algorithms can be used as much as necessary in a single problem and thus solve NP problems. Let us emphasize once again that the algorithms below give the solutions themselves, not just answers about the number of solutions, although they also give. In large numbers, the solution of these problems requires a large resolution power of the computer, but the algorithms remain the same. This book provides examples with small numbers, but the point remains the same. Thus, the author wants to show that often the solution of the problem lies on the surface, but sometimes we can not see the solution at this angle. The author is confident that more and more NP-problems will move into the category of P — problems. This process is inevitable with the development of programs and the growth of computer power.

Chapter 1. The essence of the method; commands and typical algorithms in the program Scilab 6.0.1

— The essence of the method

Any set can be written as a matrix with elements of this set. The essence of the method used consists of operating on natural numbers (elements of sets), applying a matrix approach, that is, operating on elements of matrices, their columns and rows, as well as in the interactions between matrices (sets).

Common algorithms for combinatorics problems, such as permutation, combination, placement problems, which are given below, are applicable for NP — problems. These types of problems (on permutations, combinations, placements) with large numbers can be attributed to NP — problems. NP-problems, in essence, represent all the same problems of combinatorics, but in a complicated version, in one problem can be present immediately as permutations and combinations and placement, these operations (combinations, placement, permutations) can be repeated sequentially, but with other data obtained in the course of solving the problem, additional conditions or calculations can be set. But with the help of Scilab program typical algorithms of such problems are revealed and solutions are given, not only the number of solutions. The bottom line is that knowing the typical algorithms of permutations, placement, combinations, they can be used as many as standard algorithms in one problem and thus solve NP — problems.

— NP-problems and their models in small numbers, General algorithms

Here are examples of NP-problems:

Suppose that you are organizing a group of four hundred University students. The number of places is limited, and only one hundred students will get a place in the hostel. The situation is complicated by the fact that the Dean has provided you with a list of pairs of students who can’t live together, and asked that no pair from this list did not get to the final version.

Is it true that among the numbers {-2, -3, 15, 14, 7, -10, …} are there any such that their sum is 0?

Or else, for example: 50, 2, 47, 5, 21, 4, 78, 1. Problem: is it possible to choose among these numbers such that their sum will give 100?

You want to find the shortest path that passes exactly one time through each of the six cities A, B.C. D. I. F. You are given a matrix of distances between all pairs of cities,

Tasks like the above 3 examples seem unsolvable (so far no one has been able to prove that some of them are actually as complex as it seems, i.e. that there is really no way to get an answer using a computer).

We make models of these problems in small numbers to find an algorithm and solve these problems:

Let’s say you’re hosting a group of 5 University students. The number of places is limited, and only 3 students will get a place in the hostel. The situation is complicated by the fact that the Dean provided you with a list of students who can’t live together, and asked that none of this list was not in the final version.

Problem-model №1—1

Let’s say you’re hosting a group of 9 University students. The number of places is limited to 4 — 2 rooms for 2 people, and only 4 students will get a place in the hostel.

Find these solutions.

You want to find the shortest path that passes exactly once through each of the four cities A, B. C. D.. You are given a matrix of distances between all pairs of cities,

Solutions of NP-problems and their model problems are similar and have the same algorithms, solutions of model problems are given below.

— Specify the source data

The initial data in the commands are given by a vector (a single matrix), but it is also possible by a two-dimensional matrix, several matrices, etc. each object is assigned a serial number. For example, we have 5 students:

Let’s give each student a number in order: 1. – first student; 2. – 2nd student....etc to 5. As a one-dimensional matrix n

Program start:

— > n= [1 2 3 4 5]

n =

1. 2. 3. 4. 5.

— Permutations

The permutation is performed using the perms (n) command:

Now we find all possible permutations from 1 to 5, there will be 120. The answer will be written in the form of a matrix, where each row is a variant of one of the permutations, the number of rows in the matrix will be equal to the number of permutations, and the number of columns will be equal to the originally specified elements (in our case 5).

— > P=perms (n)

— Permutations followed by matrix replacement and finding solutions

As an example with a complex permutation (replacement of the matrix obtained as a permutation to another matrix), the problem-model №3:

You want to find the shortest path that passes exactly once through each of the four cities A, B. C. D. You are given a matrix of distances between all pairs of cities,

Decision.

The essence of the solution is that finding all the permutations between the four cities in the form of rows of the matrix, replace the rows of the resulting matrix with the rows of another matrix, the elements of which are the distances between the cities and calculate the paths, then find the smallest.

Set the initial conditions: cities A, B,C, D are numbered in order and assign each city number 1,2,3,4, respectively. Let’s set the distance between cities by matrices, for example. distance between town A and B as a matrix ab whose elements is a pair of 1 and 2 (this is the number of cities A and B):

— > ab= [1 2];

— > ac= [1 3];

— > ba= [2 1];

— > bc= [2 3];

— > bd= [2 4];

— > ca= [3 1];

— > cb= [3 2];

— > cd= [3 4];

— > da= [4 1];

— > db= [4 2];

— > dc= [4 3];

— > M= [1 2 3 4]

M =

— 2. 3. 4.

We find all possible permutations and obtain the matrix P.

— -> P=perms (M);

The result is a matrix of 4 columns (cities) and rows-variants of permutations.

If in the condition of the problem it was necessary to return back to the initial point, then to the matrix obtained as a result of permutations it would be necessary to add another one column where the element in each row would be the element of the first row of the matrix P.

The program does not provide a command to replace the original matrix, therows of which are the paths indicated by a sequential enumeration of cities, with a matrix of distances between these cities (for example, such a command could be called command “between”. The value of command “between” the elements with values 1 and 2 is 10, for example, as the initial data between ([1 2]) =10; insert values between the elements of the matrix rows P as between (P:,1)). So we’ll have to go the other way. Divide the resulting matrix P into 3 parts, and then connect again, as between 4 cities can build a path of three distances between cities. These 3 matrices will consist: the 1st of the first two columns, the 2nd of the second and third columns, the 3rd of the third and fourth columns.

— > N=P;

— > N (:,4) = [];

— > N (:,3) = [];

— > A=N;

— > X=P;

— > X (:,1) = [];

— > X (:,3) = [];

— > X (:,3) = [];

— > Q=P;

— > Q (:,1) = [];

— > Q (:,1) = [];

— > T=cat (2,A,X,Q);

Let’s create a matrix U, such that we replace the values of the elements of the matrix T (that is, the numbers of cities) by the distances between them from the problem condition. For example, in the line items written as [1 2 2 3 4] will be written as [ab bc cd].

— > U= [dc cb ba; dc ca ab; db bc ca; db ba ac; da ac cb; da ab bc; cd db ba; cd da ab; cb bd da; cb ba ad;

ca ad db; ca ab bd; bd dc ca; bd da ac; bc ca ad; bc cd da; ba ad dc; ba ac cd; ad dc cb; ad db bc; ac cd db;

ac cb bd; ab bd dc; ab bc cd];

The distances between cities are known to us by the problem condition, let us set them and write down the matrix again:

— > ab= [10 0];

— > ac= [5 0];

— > ba= [10 0];

— > bc= [3 0];

— > bd= [6 0];

— > ca= [5 0];

— > cb= [3 0];

— > cd= [7 0];

— > da= [4 0];

— > db= [6 0];

— > dc= [7 0];

— > U= [dc cb ba; dc ca ab; db bc ca; db ba ac; da ac cb; da ab bc; cd db ba; cd da ab; cb bd da; cb ba ad;

ca ad db; ca ab bd; bd dc ca; bd da ac; bc ca ad; bc cd da; ba ad dc; ba ac cd; ad dc cb; ad db bc; ac cd db;

ac cb bd; ab bd dc; ab bc cd];

Let’s sum up the rows of the obtained matrix and find the smallest element:

— > Y=sum (U,2);

— > min (Y)

ans =

12.

The smallest distance is 12. Create matrix H and find the intersection of matrixes to find the string and define the path.

— > H= [12];

— > [c, iY, iH] =intersect (Y (:),H (:))

iH =

1.

iY =

5.

c =

12.

We see that the intersection of matrixes with the value of element 12 points to row 5 in matrix Y. Row 5 in matrix T points the way:

T (5,:)

ans =

4. 1. 1. 3. 3. 2.

The first answer is the shortest path 12 km path from city 4 to city 1, then from city 1 to city 3, from city 3 to city 2.

Бесплатный фрагмент закончился.

Купите книгу, чтобы продолжить чтение.