Why is the complexity class expression this?

In summary: The coefficients may vary from compiler to compiler, and depending on the hardware.In summary, the complexity class for the given C++ code is N^2, and the expression for the worst case number of instructions is 8N^2+12N+6. However, the specific coefficients may vary depending on the compiler and hardware used.
  • #1
Xari
3
0
I'm trying to understand complexity class algorithms off of my professor's lecture notes, but I still can't get a hang of it. void sort(int a[], int N) { //N is the length of the array
for (int base=0; base<N; ++base)
for (int check=base+1; check<N; ++check)
if (a[base] > a[check])
std::swap(a[base], a[check]);​
}

On his notes he says the expression for this is 8N^2+12N+6.From what I understand fully the complexity class for this is N^2 because it is the fastest growing out of the rest. We ignore constants because they're irrelevant when going to infinity.

However, I want to know how he got the coefficients.
 
Technology news on Phys.org
  • #2
Xari said:
I'm trying to understand complexity class algorithms off of my professor's lecture notes, but I still can't get a hang of it. void sort(int a[], int N) { //N is the length of the array
for (int base=0; base<N; ++base)
for (int check=base+1; check<N; ++check)
if (a[base] > a[check])
std::swap(a[base], a[check]);​
}

On his notes he says the expression for this is 8N^2+12N+6.From what I understand fully the complexity class for this is N^2 because it is the fastest growing out of the rest. We ignore constants because they're irrelevant when going to infinity.

However, I want to know how he got the coefficients.
I don't think one can answer this question with the information given. It depends on how the compiler translates the C++ code into machine language.
Probably the precise 8N²+12N+6 formula was empirically derived with a given compiler for a worst case scenario.
Or maybe these values are just arbitrary. The number of instructions executed in a worst case scenario will be given by a second-degree polynomial (N for the first loop, N/2 on average for the inner loop) in N.
 
Last edited:

1. Why is the complexity class expression P usually described as "efficient"?

The complexity class P refers to the set of decision problems that can be solved by a deterministic Turing machine in polynomial time. This means that the time required to solve the problem increases at a polynomial rate as the size of the input increases. Compared to other complexity classes such as EXP or NP, which have exponential or nondeterministic time complexity, P is considered efficient because the time required to solve problems in this class does not grow as quickly.

2. How is the complexity class NP different from P?

The complexity class NP also refers to decision problems, but it includes problems that can be verified in polynomial time by a nondeterministic Turing machine. This means that while solutions to problems in NP may be difficult to find, they can be efficiently checked for correctness. In contrast, P only includes problems that can be solved in polynomial time by a deterministic Turing machine.

3. Why is the class NP-hard important in complexity theory?

The class NP-hard refers to decision problems that are at least as hard as the hardest problem in NP. This means that if a problem is NP-hard, it is at least as difficult as any other problem in NP. Understanding and classifying NP-hard problems is important in complexity theory because it helps us determine the difficulty of other problems and can guide the development of efficient algorithms for solving them.

4. What is the significance of the P vs. NP problem?

The P vs. NP problem is one of the most famous and important open problems in computer science and mathematics. It asks whether all decision problems in NP can be solved in polynomial time by a deterministic Turing machine. If the answer is yes, then P = NP and many difficult problems become much easier to solve. However, despite decades of research, the P vs. NP problem remains unsolved and is considered one of the most challenging problems in the field.

5. How are complexity classes used in practical applications?

Complexity classes are used in a variety of practical applications, particularly in the field of computer science. They help us understand the difficulty of different problems and can guide the development of efficient algorithms. For example, problems that fall into the class NP are often considered difficult and require specialized algorithms to solve. On the other hand, problems in class P are considered more tractable and can be solved efficiently by many different algorithms.

Similar threads

  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
5
Views
820
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
Back
Top