All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is NP-complexity?

Post cover: What is NP-complexity?
This content has been automatically translated from Ukrainian.
NP-complexity this is a class of problems for which it is very difficult to find a solution, but it is easy to check the correctness of the already finished one. In other words, if you guessed the answer, you can check it quickly, but the search process itself takes an extremely long time.
Examples of NP-hard problems:
  • Salesman's task - how to find the shortest route that passes through all cities.
  • Class schedule - draw up an optimal schedule so that all teachers, classrooms and students coincide without conflicts.
  • Subsets - divide the set of numbers into groups with the same sum.
Where it matters in real life:
  • Logistics and delivery routes.
  • Production planning and schedules.
  • Cryptography and data security.
A simple Ruby example for demonstrating the idea of a complete search (on the example of subsets):
# NP-hard problem: finding a subset with the desired sum
numbers = [3, 7, 9, 11, 15]
target = 20

solutions = []

(1..numbers.size).each do |k|
  numbers.combination(k).each do |subset|
    solutions << subset if subset.sum == target
  end
end

puts "Solutions Found:"
solutions.each { |s| ps}
Result:
Solutions found:
[9, 11]
Several options are visible here. And if you increase the array to 20–25 numbers, the number of checks will increase sharply - that's what it looks like combinatorial explosion, whereby similar NP-hard problems become practically insoluble by an enumeration.

This post doesn't have any additions from the author yet.

What is a brain stack?
28 Jul 19:37

What is a brain stack?

meme code
meme code@memecode
What is integer overflow?
15 Aug 08:28

What is integer overflow?

meme code
meme code@memecode
What is HAR file (HTTP Archive)?
25 Aug 18:23

What is HAR file (HTTP Archive)?

meme code
meme code@memecode
What is Bubble Sort (algorithm explanation)?
16 Sep 18:42

What is Bubble Sort (algorithm explanation)?

meme code
meme code@memecode
What is exponential growth?
16 Sep 18:57

What is exponential growth?

meme code
meme code@memecode
What is factorial complexity?
16 Sep 19:03

What is factorial complexity?

meme code
meme code@memecode
Offset vs Cursor Pagination in Rails: What to Choose and Why
24 Sep 15:22

Offset vs Cursor Pagination in Rails: What to Choose and Why

meme code
meme code@memecode
What is Row Security in PostgreSQL and why is it Rails developers
04 Oct 19:06

What is Row Security in PostgreSQL and why is it Rails developers

meme code
meme code@memecode
What is ivar in Ruby /Rails?
19 Oct 20:12

What is ivar in Ruby /Rails?

meme code
meme code@memecode
Basic methods of authentication in the API
19 Oct 20:26

Basic methods of authentication in the API

meme code
meme code@memecode
How do OAuth 1 differ from OAuth 2
19 Oct 20:34

How do OAuth 1 differ from OAuth 2

meme code
meme code@memecode
What is ORM and why is it needed?
26 Oct 14:00

What is ORM and why is it needed?

meme code
meme code@memecode