Академический Документы
Профессиональный Документы
Культура Документы
NP=P? Algorithms
for solving NP-problems
by matrix method
in Scilab program
Издательские решения
Under license from Ridero
2018
Fonts by «ParaType»
5
LUDMILA NAUMOVA
thor 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.
6
CHAPTER 1. THE ESSENCE OF THE
METHOD; COMMANDS AND
TYPICAL ALGORITHMS IN THE
PROGRAM SCILAB 6.0.1
7
LUDMILA NAUMOVA
8
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
Task-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,
— PERMUTATIONS
9
LUDMILA NAUMOVA
10
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
11
LUDMILA NAUMOVA
12
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
13
LUDMILA NAUMOVA
— PLACEMENTS
But the Dean set a condition that the 3rd, 1st, and 5th stu-
dents do not settle together. Let’s set this condition as a one-di-
mensional matrix Z:
— > Z= [3 1 5]
Z=
14
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
3. 1. 5.
Let’s sort the matrix Z in descending order:
— > R=gsort (Z)
Find the intersection of rows of matrixes R and Y:
T=intersect (R,Y,1)
Finding the common row index for R and Y matrixes
— > [c, iR, iY] =intersect (R,Y,1)
iY =
6.
iR =
1.
c=
5. 3. 1.
Thus, it is necessary to delete the 6th row of the
matrix Y in order to fulfill the Dean’s condition
and get the final answers in the form of matrix
rows:
— > Y (6,:) = []
Let’s once again we find the intersection of the rows of matri-
ces R and Y, and so we repeat until the intersection of matrices is
empty set.
We get the final answers to the task — these are the rows
of the matrix Y as all possible answers.
— COMPLEX PLACEMENTS
Task-model №1—1
Another example of accommodation: 9 students can be accom-
modated only in 2 rooms in pairs, that is only 4 places. Find all
these pairs-solutions.
To save space, suppose that we have already found a solution
to place 9 students in 4 places in the form of rows of the matrix
(assume matrix E) similar to the Task-model №1 (get a matrix
of 4 columns-the number of seats, and rows — equal to the num-
15
LUDMILA NAUMOVA
— > F (:,4) = []
— > F (:,3) = []
— > V=gsort (F,‘c’)
— > K=P
— > K (:,1) = []
— > K (:,1) = []
— > Q=gsort (K,‘c’)
After sorting the two matrices, combine them and delete the
same rows in the resulting matrix
— > W=cat (2,V,Q)
16
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
Exclude the same rows and find the answer you are looking for
— > U=unique (W,1)
Answer: variants of pairs in each row of the matrix, that is, the
first option-the first row — is the 1st room of two students in the
1st and 2nd column, ie, under the numbers 3,1; 2nd room of two
students in the 3rd and 4th column, ie, under the numbers 5,4. The
second option is the 2nd row — this is the 1st room of two stu-
dents in the 1st and 2nd column, i.e. under the numbers 4.1; 2nd
room of two students in the 3rd and 4th column, i.e. under the
numbers 5.3, etc. and so all the rows of the resulting matrix U. But
as we wrote at the beginning of solving the task — model №1—1,
this is only the answer obtained from one of the rows of the ma-
trix E. All the same should be done with all the rows of the ma-
trix E.
17
LUDMILA NAUMOVA
18
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
sum (H,2)
We perform an action on each received sum line
by row:
— > D=sum (H,2) -17
Set the zero matrix:
— > S=0
The answer looking for is found, but make sure it’s the
only one.
Find again the intersection of two matrices
— > [c, iD, iS] =intersect (D (:),S (:))
Make sure that the intersection gives an empty set, if not, then
repeat the procedure of crossing two matrices, previously chang-
ing the value in the eighth row of the matrix D from zero to an-
other.
— COMBINATIONS
19
LUDMILA NAUMOVA
ber of each quark is repeated three times, since only three quarks
can be a particle):
— > n= [1 1 1 2 2 2 3 3 3]
Let’s sort the initial matrix for convenience of calculations
in descending order.
— > m=gsort (n)
Now we find all possible permutations from 1 to 9.
— > P=perms (m)
Remove the last columns-6 columns, as the number of addi-
tional numbers in the original matrix was 6.
— > v (:,9) = []
— > v (:,8) = []
— > v (:,7) = []
— > v (:,6) = []
— > v (:,5) = []
— > v (:,4) = []
Sort the resulting matrix in descending order:
— > x=gsort (v)
Exclude the same rows
— > M=unique (v,1)
Again, let’s sort in descending order, but not the whole matrix,
but the rows
— > Z=gsort (M,‘c’)
Again, exclude the same rows:
— > H=unique (Z,1)
Get the matrix of size:
— > size (H)
The resulting matrix will be the answer, each row of the ma-
trix is a variant of the answer, and the number of rows of the ma-
trix is the number of variants, the number of columns of the matrix
is the number of quarks of which the particle consists — 3.
20
CHAPTER 2. DETAILED SOLUTION
OF PROBLEMS IN THE PROGRAM
SCILAB 6.0.1
2.1. MODEL-PROBLEM №1
21
LUDMILA NAUMOVA
5. 4. 3. 2. 1.
5. 4. 3. 1. 2.
5. 4. 2. 3. 1.
5. 4. 2. 1. 3.
5. 4. 1. 3. 2.
5. 4. 1. 2. 3.
5. 3. 4. 2. 1.
5. 3. 4. 1. 2.
5. 3. 2. 4. 1.
5. 3. 2. 1. 4.
5. 3. 1. 4. 2.
5. 3. 1. 2. 4.
5. 2. 4. 3. 1.
5. 2. 4. 1. 3.
5. 2. 3. 4. 1.
5. 2. 3. 1. 4.
5. 2. 1. 4. 3.
5. 2. 1. 3. 4.
5. 1. 4. 3. 2.
5. 1. 4. 2. 3.
5. 1. 3. 4. 2.
5. 1. 3. 2. 4.
5. 1. 2. 4. 3.
5. 1. 2. 3. 4.
4. 5. 3. 2. 1.
4. 5. 3. 1. 2.
4. 5. 2. 3. 1.
4. 5. 2. 1. 3.
4. 5. 1. 3. 2.
4. 5. 1. 2. 3.
4. 3. 5. 2. 1.
4. 3. 5. 1. 2.
4. 3. 2. 5. 1.
4. 3. 2. 1. 5.
4. 3. 1. 5. 2.
4. 3. 1. 2. 5.
22
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROG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
1. 4. 5. 2. 3.
1. 4. 3. 5. 2.
1. 4. 3. 2. 5.
1. 4. 2. 5. 3.
24
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
1. 4. 2. 3. 5.
1. 3. 5. 4. 2.
1. 3. 5. 2. 4.
1. 3. 4. 5. 2.
1. 3. 4. 2. 5.
1. 3. 2. 5. 4.
1. 3. 2. 4. 5.
1. 2. 5. 4. 3.
1. 2. 5. 3. 4.
1. 2. 4. 5. 3.
1. 2. 4. 3. 5.
1. 2. 3. 5. 4.
1. 2. 3. 4. 5.
Since there are only three places, remove the last
columns of the matrix P to three. That is, first
remove the fifth column:
— > P (:,5) = []
P=
5. 4. 3. 2.
5. 4. 3. 1.
5. 4. 2. 3.
5. 4. 2. 1.
5. 4. 1. 3.
5. 4. 1. 2.
5. 3. 4. 2.
5. 3. 4. 1.
5. 3. 2. 4.
5. 3. 2. 1.
5. 3. 1. 4.
5. 3. 1. 2.
5. 2. 4. 3.
5. 2. 4. 1.
5. 2. 3. 4.
5. 2. 3. 1.
5. 2. 1. 4.
5. 2. 1. 3.
25
LUDMILA NAUMOVA
5. 1. 4. 3.
5. 1. 4. 2.
5. 1. 3. 4.
5. 1. 3. 2.
5. 1. 2. 4.
5. 1. 2. 3.
4. 5. 3. 2.
4. 5. 3. 1.
4. 5. 2. 3.
4. 5. 2. 1.
4. 5. 1. 3.
4. 5. 1. 2.
4. 3. 5. 2.
4. 3. 5. 1.
4. 3. 2. 5.
4. 3. 2. 1.
4. 3. 1. 5.
4. 3. 1. 2.
4. 2. 5. 3.
4. 2. 5. 1.
4. 2. 3. 5.
4. 2. 3. 1.
4. 2. 1. 5.
4. 2. 1. 3.
4. 1. 5. 3.
4. 1. 5. 2.
4. 1. 3. 5.
4. 1. 3. 2.
4. 1. 2. 5.
4. 1. 2. 3.
3. 5. 4. 2.
3. 5. 4. 1.
3. 5. 2. 4.
3. 5. 2. 1.
3. 5. 1. 4.
3. 5.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1. 3. 4.
1. 3. 2.
1. 3. 2.
1. 2. 5.
1. 2. 5.
1. 2. 4.
1. 2. 4.
1. 2. 3.
1. 2. 3.
We exclude the same rows in the matrix P:
— > M=unique (P,1)
M=
1. 2. 3.
1. 2. 4.
1. 2. 5.
1. 3. 2.
1. 3. 4.
1. 3. 5.
1. 4. 2.
1. 4. 3.
1. 4. 5.
1. 5. 2.
1. 5. 3.
1. 5. 4.
2. 1. 3.
2. 1. 4.
2. 1. 5.
2. 3. 1.
2. 3. 4.
2. 3. 5.
2. 4. 1.
2. 4. 3.
32
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
2. 4. 5.
2. 5. 1.
2. 5. 3.
2. 5. 4.
3. 1. 2.
3. 1. 4.
3. 1. 5.
3. 2. 1.
3. 2. 4.
3. 2. 5.
3. 4. 1.
3. 4. 2.
3. 4. 5.
3. 5. 1.
3. 5. 2.
3. 5. 4.
4. 1. 2.
4. 1. 3.
4. 1. 5.
4. 2. 1.
4. 2. 3.
4. 2. 5.
4. 3. 1.
4. 3. 2.
4. 3. 5.
4. 5. 1.
4. 5. 2.
4. 5. 3.
5. 1. 2.
5. 1. 3.
5. 1. 4.
5. 2. 1.
5. 2. 3.
5. 2. 4.
5. 3. 1.
5. 3. 2.
33
LUDMILA NAUMOVA
5. 3. 4.
5. 4. 1.
5. 4. 2.
5. 4. 3.
Let’s sort the rows of the resulting matrix M
in descending order
— > V=gsort (M,‘c’)
V=
3. 2. 1.
4. 2. 1.
5. 2. 1.
3. 2. 1.
4. 3. 1.
5. 3. 1.
4. 2. 1.
4. 3. 1.
5. 4. 1.
5. 2. 1.
5. 3. 1.
5. 4. 1.
3. 2. 1.
4. 2. 1.
5. 2. 1.
3. 2. 1.
4. 3. 2.
5. 3. 2.
4. 2. 1.
4. 3. 2.
5. 4. 2.
5. 2. 1.
5. 3. 2.
5. 4. 2.
3. 2. 1.
4. 3. 1.
5. 3. 1.
3. 2. 1.
34
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
4. 3. 2.
5. 3. 2.
4. 3. 1.
4. 3. 2.
5. 4. 3.
5. 3. 1.
5. 3. 2.
5. 4. 3.
4. 2. 1.
4. 3. 1.
5. 4. 1.
4. 2. 1.
4. 3. 2.
5. 4. 2.
4. 3. 1.
4. 3. 2.
5. 4. 3.
5. 4. 1.
5. 4. 2.
5. 4. 3.
5. 2. 1.
5. 3. 1.
5. 4. 1.
5. 2. 1.
5. 3. 2.
5. 4. 2.
5. 3. 1.
5. 3. 2.
5. 4. 3.
5. 4. 1.
5. 4. 2.
5. 4. 3.
Again, delete the same rows of the sorted matrix V:
— > Y=unique (V,1)
Y=
35
LUDMILA NAUMOVA
3. 2. 1.
4. 2. 1.
4. 3. 1.
4. 3. 2.
5. 2. 1.
5. 3. 1.
5. 3. 2.
5. 4. 1.
5. 4. 2.
5. 4. 3.
— > size (Y)
ans =
10. 3.
It turned out 10 accommodation options for
5 students in three places. But the Dean set
a condition that the 3rd, 1st, and 5th students do
not settle together. Let’s set this condition as
a one-dimensional matrix Z:
— > Z= [3 1 5]
Z=
3. 1. 5.
Let’s sort the matrix Z in descending order:
— > R=gsort (Z)
R=
5. 3. 1.
Find the intersection of rows of matrices R and Y:
T=intersect (R, Y,1)
T=
5. 3. 1.
Finding the common row index for R and Y matrices:
— > [c, iR, iY] =intersect (R,Y,1)
iY =
6.
iR =
36
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
1.
c=
5. 3. 1.
Thus, it is necessary to delete the 6th row of the
matrix Y in order to fulfill the Dean’s condition
and get the final answers in the form of matrix
rows:
Y (6,:) = []
Y=
3. 2. 1.
4. 2. 1.
4. 3. 1.
4. 3. 2.
5. 2. 1.
5. 3. 2.
5. 4. 1.
5. 4. 2.
5. 4. 3.
Let’s once again we find the intersection of the rows
of matrices R and Y, and so we repeat until the
intersection of matrices is empty set.
37
LUDMILA NAUMOVA
38
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
Decision:
Program start:
loading the source environment
— > n= [5 3 1 4]
n=
5. 3. 1. 4.
We find all possible permutations of the row of the
matrix E
— > P=perms (n)
P=
4. 1. 3. 5.
4. 1. 5. 3.
4. 3. 1. 5.
4. 3. 5. 1.
4. 5. 1. 3.
4. 5. 3. 1.
1. 4. 3. 5.
1. 4. 5. 3.
1. 3. 4. 5.
1. 3. 5. 4.
1. 5. 4. 3.
1. 5. 3. 4.
3. 4. 1. 5.
3. 4. 5. 1.
3. 1. 4. 5.
3. 1. 5. 4.
3. 5. 4. 1.
3. 5. 1. 4.
5. 4. 1. 3.
39
LUDMILA NAUMOVA
5. 4. 3. 1.
5. 1. 4. 3.
5. 1. 3. 4.
5. 3. 4. 1.
5. 3. 1. 4.
Since rooms 2 under the conditions of the problem,
divide the matrix P into 2 matrices with an equal
number of columns: that is, one matrix with the
first two columns, the other with the 3rd and 4th
columns.
— > F=P
F=
4. 1. 3. 5.
4. 1. 5. 3.
4. 3. 1. 5.
4. 3. 5. 1.
4. 5. 1. 3.
4. 5. 3. 1.
1. 4. 3. 5.
1. 4. 5. 3.
1. 3. 4. 5.
1. 3. 5. 4.
1. 5. 4. 3.
1. 5. 3. 4.
3. 4. 1. 5.
3. 4. 5. 1.
3. 1. 4. 5.
3. 1. 5. 4.
3. 5. 4. 1.
3. 5. 1. 4.
5. 4. 1. 3.
5. 4. 3. 1.
5. 1. 4. 3.
40
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
5. 1. 3. 4.
5. 3. 4. 1.
5. 3. 1. 4.
— > F (:,4) = []
F=
4. 1. 3.
4. 1. 5.
4. 3. 1.
4. 3. 5.
4. 5. 1.
4. 5. 3.
1. 4. 3.
1. 4. 5.
1. 3. 4.
1. 3. 5.
1. 5. 4.
1. 5. 3.
3. 4. 1.
3. 4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
3. 1. 4.
— > K (:,1) = []
K=
44
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
3. 5.
5. 3.
1. 5.
5. 1.
1. 3.
3. 1.
3. 5.
5. 3.
4. 5.
5. 4.
4. 3.
3. 4.
1. 5.
5. 1.
4. 5.
5. 4.
4. 1.
1. 4.
1. 3.
3. 1.
4. 3.
3. 4.
4. 1.
1. 4.
5. 3.
5. 3.
5. 1.
5. 1.
3. 1.
3. 1.
5. 3.
5. 3.
5. 4.
45
LUDMILA NAUMOVA
5. 4.
4. 3.
4. 3.
5. 1.
5. 1.
5. 4.
5. 4.
4. 1.
4. 1.
3. 1.
3. 1.
4. 3.
4. 3.
4. 1.
4. 1.
After sorting the two matrices, combine them and
delete the same rows in the resulting matrix
— > W=cat (2,V,Q)
W=
4. 1. 5. 3.
4. 1. 5. 3.
4. 3. 5. 1.
4. 3. 5. 1.
5. 4. 3. 1.
5. 4. 3. 1.
4. 1. 5. 3.
4. 1. 5. 3.
3. 1. 5. 4.
3. 1. 5. 4.
5. 1. 4. 3.
5. 1. 4. 3.
4. 3. 5. 1.
4. 3. 5. 1.
3. 1. 5. 4.
3. 1. 5. 4.
5. 3. 4. 1.
46
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
5. 3. 4. 1.
5. 4. 3. 1.
5. 4. 3. 1.
5. 1. 4. 3.
5. 1. 4. 3.
5. 3. 4. 1.
5. 3. 4. 1.
— > U=unique (W,1)
U=
3. 1. 5. 4.
4. 1. 5. 3.
4. 3. 5. 1.
5. 1. 4. 3.
5. 3. 4. 1.
5. 4. 3. 1.
Answer: variants of pairs in each row of the matrix,
that is, the first option-the first row — is the 1st
room of two students in the 1st and 2nd column,
ie, under the numbers 3,1; 2nd room of two
students in the 3rd and 4th column, ie, under the
numbers 5,4. The second option is the 2nd row —
this is the 1st room of two students in the 1st and
2nd column, i.e. under the numbers 4.1; 2nd room
of two students in the 3rd and 4th column, i.e.
under the numbers 5.3, etc. and so all the rows
of the resulting matrix U. But as we wrote at the
beginning of the solution of the task-model №1—
1, this is only the answer obtained from one
of the rows of the matrix E. All the same should
be done with all the rows of the matrix E.
47
LUDMILA NAUMOVA
2.3. TASK-MODEL №2
0. 0. 0.
2. 0. 0.
6. 0. 0.
6. 2. 0.
9. 0. 0.
9. 2. 0.
9. 6. 0.
9. 6. 2.
48
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
— 17.
— 15.
— 11.
— 9.
— 8.
— 6.
— 2.
0.
Set the zero matrix
— > S=0
S=
0.
Find the intersection with the zero matrix:
— > [c, iD, iS] =intersect (D (:),S (:))
iS =
1.
iD =
49
LUDMILA NAUMOVA
8.
c=
0.
The eighth line intersects with the zero matrix:
— > H (8,:)
ans =
9. 6. 2.
The answer is: 9,6,2.
To exclude another answer, change the value in the
eighth row of the matrix D to another number, for
example, 1 and again find the intersection of the
zero matrix with the matrix D and so on until the
intersection gives an empty set.
50
CONCLUSION
The main advantage of the program Scilab in solving NP-prob-
lems — is the presence of commands sort, permutation, intersec-
tion of sets, removing the same rows or columns from which you
can find the placement and combinations. However, these com-
mands are not yet available for individual parts of rows and parts
of columns, which complicates the task (or vice versa for entire
blocks of rows and columns). Unfortunately, the program Scilab is
not yet perfect for solving NP-problems, there are still many oper-
ations that would be useful in solving NP-problems and have to go
around. If the commands used in Excel tables were inserted into
the Scilab program for the matrices, this would make the matter
much easier. A table in Excel plays the role of a matrix in Scilab.
For example, in Excel — there is a copy of any part of the table
and create new tables from it, insert and delete both columns and
rows of the table, and entire blocks of columns or rows from an-
other table in any interval between columns or rows, for each ele-
ment of the table, you can divide a large table into tables by the
specified columns or rows, you can set the table elements., which
can be not numbers, and alphabetic characters and set the formula
in the index for them (meaning that the table in Excel simulates
the matrix in the program Scilab). The synthesis of the two pro-
grammes would yield tangible results.
But, despite all the shortcomings, we can see how to build al-
gorithms and in what direction you can go in solving NP-problems.
The solution of problems can lie on the surface, but we can
not always see at the right angle. Example: Fermat’s famous last
theorem. The farm found a short elegant solution, only the fields
on the page were not enough. The solution was found in our time,
but it took many pages. Let us presume that most likely a short
proof would look like this: Fermat’s Theorem
51
LUDMILA NAUMOVA
(1)
Then, equation (1) can be represented as (2) :
(2)
Equation (2) of the second degree is obtained. This equation is
Diophantine, since the roots of the equation must be natural num-
bers. This equation is Diophantine, since the roots of the equation
must be natural numbers. But Diophantine equation of the form (2)
has the solution to Pythagorean triples. The General form of the
52
NP=P? ALGORITHMS FOR SOLVING NP-PROBLEMS BY MATRIX METHOD
IN SCILAB PROGRAM
(3)
k, a, b, c — natural number.
a, b, c — Pythagorean triplets.
Compare equations (2) and (3). There are no solutions to the
equation (2), since the form of equation (2) cannot be reduced
to the form (3), it is impossible to find such a natural number k,
which would lead equation (2) to the form of equation (3). It fol-
lows that equation (1) has no solutions. The reader can check the
arguments and write to e — mail: mathmillena@mail.ru. But it evi-
dence was a retreat to say that the solution might be on the sur-
face.
53
LUDMILA NAUMOVA
54