Cosas en las que pienso mientras me titulo

Computational Complexity

Sorting Algorithms

Consider the following set A≡{a1,a2,…,an}⊆ZA \equiv \{a_1,a_2,\dots,a_n\} \subseteq \mathbb{Z}. We want to obtain a set of permutations ϕ\phi such that ϕ(A)=A′={a1′,…,an′}\phi(A) = A^{'} =\{a_1^{'},\dots, a_n^{'}\} where a1′≤⋯≤an′a_1^{'} \leq \dots \leq a_n^{'}.

To do that there are a number of algorithms we can use:

Insertion Sort

Assume A[1 ⁣:j]A[1 \colon j] is sorted. Check A[j]A[j] and compare it to other numbers, if lower than them, put it to its right. I present the pseudocode for a hypothetical implementation of Insertion Sort:

Merge Sort

This is an algorithm that belongs to the Divide & Conquer family of algorithms. This algo. lets you divide the set in two, from a1a_1 to an2a_{\frac{n}{2}} & an2+1a_{\frac{n}{2} + 1} to ana_n where n=2pn = 2^p. You sort each half, then sort both halves. The pseudocode for this implementation is as follows:

Big O Notation

Define the time it takes for an algorithm to sort AA, given ∣A∣=n\vert A \vert = n, to be T(n)T(n).

Consider O(f(n))O(f(n)). This means that for all n>non>n_o for some no∈Nn_o \in \mathbb{N}, then

T(n)<cf(n)T(n) < c f(n)

For a constant value cc.

Ω\Omega Notation

Consider Ω(f(n))\Omega(f(n)). This means that for all n>non>n_o for some no∈Nn_o \in \mathbb{N}, then

T(n)≥c1f(n)T(n) \geq c_1 f(n)

For a constant value c1c_1.

Θ\Theta Notation

Consider Θ(f(n))\Theta(f(n)). Then T(n)T(n) is both O(n)O(n) and Ω(n)\Omega(n).

Algo. Analysis

To analyze our models we need a computer (of course) and a process that lets us obtain insigths about it. Here are some examples:

Complexity Analysis for Insertion Sort

  1. Best Case Scenario.- The algo. is already sorted, which makes the time it takes to implement it linear with respect to its size. This means that the time it takes is:

T(n)=c1n+c2T(n) = c_1 n + c_2

For n the size of the set.

  1. Worst Case Scenario.- The algo. takes a quadratic time with respect to its size. This means:

T(n)=∑i=1nc0i+c4=c1n2+c2n+c3\begin{align*} T(n) &= \sum_{i=1}^n c_0 i +c_4\\ &=c_1 n^2 + c_2 n + c_3\\ \end{align*}

Complexity Analysis for Merge Sort

Once each half is sorted, you need to check at most αn/2=α1n\alpha n/2 = \alpha_1 n cases when comparing the first and the second half. Then the total time for this is at most, given αi=2−iα\alpha_i = 2^{-i} \alpha for a given measurable α\alpha:

T(n)=2T(n2)+α0n=4(T(n4))+α1n+α0n…=2pT(1)+α2pp\begin{align*} T(n) &= 2 T\left(\frac{n}{2}\right) + \alpha_0 n\\ &= 4\left(T\left(\frac{n}{4}\right)\right) + \alpha_1 n + \alpha_0 n\\ & \dots\\ &= 2^{p}T(1) + \alpha 2^p p\\ \end{align*}

Also T(n)=Θ(nlog⁡n)T(n) = \Theta(n \log n).

Polynomial Time Algorithms

Time for worst case is O(nk)O(n^k) for some k>0k>0. Some graph theory problems are in Polynomial Time.

Graph Theory Problems

Given a weighted n-graph (E,V,ω)(E,V,\omega) check how long is the shortest simple path between two vertices V0→VNV_0 \to V_N (solvable in polynomial time).

Given a weighted n-graph (E,V,ω)(E,V,\omega) check how long is the longest simple path between two vertices V0→VNV_0 \to V_N (unsolvable in polynomial time, solution can be checked in polynomial time).

If you can find a Hamiltonian Cycle for the graph, you find a longest symple path. Determining whether a graph contains a Hamiltonian Cycle is NOT solvable in polynomial time.

A problem is P\textbf{P} if it's solvable in polynomial time. It is NP\textbf{NP} if it can be verified to be solvable in polynomial time. You don't know if the solution is in polyomial time. It is NPC\textbf{NPC} (also referred to as NP Complete\textbf{NP Complete}) if the following condition holds:

If a given problem which is NP complete can be solved in polynomial time, then evey problem in NP can ve solved in polynomial time.

So far we know P⊆NPP \subseteq NP. If P=NPP = NP then if you can verify a problem in polynomial time you can solve it in polynomial time.

References

Cormun, Leisenson, Rivest & Stein. Introduction to Algorithms.

CC BY-SA 4.0 Eduardo Vázquez Kuri. Last modified: September 01, 2026. Website built with Franklin.jl and the Julia programming language.