← homeProgramming (Програмування)

What is NP-complexity?

NP-completeness is a class of problems for which it is very difficult to find a solution, but easy to verify the correctness of an already found one. In other words, if you guess the answer, you can check it quickly, ...

This content has been automatically translated from Ukrainian.
NP-completeness is a class of problems for which it is very difficult to find a solution, but easy to verify the correctness of an already found one. In other words, if you guess the answer, you can check it quickly, but the actual search process takes an extraordinarily long time.
Examples of NP-complete problems:
  • Traveling salesman problem - how to find the shortest route that visits all cities.
  • Timetable scheduling - to create an optimal schedule so that all teachers, classrooms, and students coincide without conflicts.
  • Subset sum problem - to divide a set of numbers into groups with the same sum.
Where this is important in real life:
  • Logistics and delivery routes.
  • Production planning and schedules.
  • Cryptography and data security.
A simple Ruby example to demonstrate the idea of brute force (using subsets):
# NP-complete 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 "Found solutions:"
solutions.each { |s| p s }
Result:
Found solutions:
[9, 11]
Here we can see several options at once. And if we increase the array to 20–25 numbers, the number of checks will increase sharply - this is what combinatorial explosion looks like, which is why such NP-complete problems become practically unsolvable through brute force.

🔥 More posts

All posts
What is a brain stack?
Jul 28, '25 19:37

What is a brain stack?

The brain is not just a sponge of neurons, but a system of layers, each of which performs its own...

What is integer overflow?
Programming (Програмування)Aug 15, '25 08:28

What is integer overflow?

You have a counter that can only count up to a certain number. For example, a pocket calculator t...

What is a HAR file (HTTP Archive)?
Programming (Програмування)Aug 25, '25 18:23

What is a HAR file (HTTP Archive)?

HAR file (HTTP Archive) is a special file format .har that stores the log of a web browser's inte...

What is exponential growth?
Sep 16, '25 18:57

What is exponential growth?

Exponential growth is the process where a quantity increases in a geometric progression. In other...

What is factorial complexity?
Programming (Програмування)Sep 16, '25 19:03

What is factorial complexity?

Factorial complexity is a situation where the number of options or combinations grows like the fa...

What is ivar in Ruby / Rails?
Programming (Програмування)Oct 19, '25 20:12

What is ivar in Ruby / Rails?

ivar is short for instance variable. In Ruby, it is written with a @ before the name, for example...

Main methods of authentication in API
Programming (Програмування)Oct 19, '25 20:26

Main methods of authentication in API

When we create an API in Ruby on Rails, it is important to control who has access to resources. H...

What is ORM and why is it needed?
Programming (Програмування)Oct 26, '25 14:00

What is ORM and why is it needed?

When we work with databases, we usually have to write SQL queries - selections, inserts, updates,...