You are on page 1of 19

Week 02: Introduction CS478

Introduction - II

Dr. Zahid Halim

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Classification of Problems
 Polynomial (P) Problems
 Are solvable in polynomial time
 Non-Deterministic Polynomial (NP) Problems
 This class of problems has solutions that are verifiable in polynomial
time.
 NP-Hard
 At least as hard as the hardest problems in NP
 Polynomial time reducible to problems
 NP-Complete
 Is in NP Hard and in NP

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478
• The general class of questions for which some algorithm can provide an
answer in polynomial time is called "class P" or just “P”

• The class of questions for which an answer can be verified in polynomial time
is called NP

• NP-complete problems are a set of problems to each of which any other


NP-problem can be reduced in polynomial time, and whose solution may still
be verified in polynomial time. That is, any NP problem can be transformed
into any of the NP-complete problems.
– Informally, an NP-complete problem is an NP problem that is at least as "tough" as
any other problem in NP.

• NP-hard problems are those at least as hard as NP problems, i.e., all NP


problems can be reduced (in polynomial time) to them. NP-hard problems
need not be in NP, i.e., they need not have solutions verifiable in polynomial
time.
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Week 02: Introduction CS478

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Insertion Sort
• Input
– A sequence of number (a1,…,an)
• Output
– A permutation (reordering) (b1,…,bn) of input sequence such that b1 ≤ b2 ≤
… ≤bn

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Insertion Sort…

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Insertion Sort…

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Insertion Sort…

?
• Loop Invariant
– Initialization
– Maintenance
– Termination

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Merge

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Merge…

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Merge…

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Merge…

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Merge…

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Merge Sort

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Merge Sort…

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Analysis

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Analysis…

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Week 02: Introduction CS478

Recall the trade off…

There are other variants of Merge Sorts including k-


way merge sorting, but the common variant is the Double
Memory Merge Sort. Though the running time is O(N logN)
and runs much faster than insertion sort and bubble sort,
merge sort’s large memory demands makes it not very
practical for main memory sorting.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

You might also like