Chapter 1 Introduction to Algorithms
Chapter 1 Introduction to Algorithms
ALGORITHMS
Course Code : AR212
Credit Hours :3
Prerequisite : AR211
Dr.Laith Baniata 1
Instructor Information
Dr.Laith Baniata 2
This course is to design efficient computer algorithms, prove their
correctness, and analyze their running times. it includes mathematical
analysis of algorithms (summations and recurrences), advanced data
structures (balanced search trees), algorithm design techniques (divide-
and-conquer, dynamic programming, and greedy algorithms), graph
algorithms (breadth-first and depth-first search, minimum spanning
trees, shortest paths).
Course Title: DESIGN AND ANALYSIS OF ALGORITHMS
Credit Hour(3)
[Pre-req. AR211
Dr.Laith Baniata 3
Textbook: Introduction to Algorithms, Thomas Cormen, Charls Leiserson & Ronald Rivest. 3rd Edition,
MIT Press, 2009.
Dr.Laith Baniata 4
COURSE OBJECTIVES
Dr.Laith Baniata 5
COURSE SYLLABUS
Dr.Laith Baniata 7
Chapter 1
Introduction to Algorithms
Dr.Laith Baniata
Autonomous Systems Dept.
Design and Analysis of algorithms Course
Dr.Laith Baniata 8
Outline
1. Computational problems
2. Algorithms
3. Problem-solving Process
4. Components of an algorithm
5. Algorithm’s Representations
6. Algorithm Efficiency
7. Algorithms Classifications
Dr.Laith Baniata 9
Computational problems
• Example:
• Input: an integer number n
• Output: Is the number prime?
• Example:
• Input: A list of names of people
• Output: The same list sorted alphabetically
Dr.Laith Baniata 101
0
Algorithms
Input Algorithm
Dr.Laith Baniata
Output 1
11
1
Correct and incorrect algorithms
Dr.Laith Baniata 12
Problems and Algorithms
Dr.Laith Baniata 13
The Problem-solving Process
Analysis
Problem
specification
Design
Algorithm
Implementation
Program
Compilation
Executable
Dr.Laith Baniata
Dr. Aryaf Al-Adwan (solution) 141
4
Practical Examples
• Electronic Commerce
• The ability of keeping the information (credit card numbers, passwords, bank
statements) private, safe, and secure.
• Algorithms involves encryption/decryption techniques.
Dr.Laith Baniata 15
Hard problems
• We can identify the Efficiency of an algorithm from its speed (how long does the
algorithm take to produce the result).
• Some problems have unknown efficient solution.
• These problems are called NP-complete problems.
• If we can show that the problem is NP-complete, we can spend our time developing
an efficient algorithm that gives a good, but not the best possible solution.
Dr.Laith Baniata 16
Components of an Algorithm
Dr.Laith Baniata 17
Components of an Algorithm Cont.
• Selections
• An instruction that decides which of two possible sequences is
executed
• The decision is based on true/false condition
• Repetitions
• Also known as iteration or loop
• Documentation
• Records what the algorithm does
Dr.Laith Baniata 18
Algorithm’s Representations
2. Pseudo code
Dr.Laith Baniata 19
Pseudocode
Dr.Laith Baniata 20
Flow Charts
Dr.Laith Baniata 21
Flow Chart’s Symbols
Dr.Laith Baniata 22
Flow Chart and
Pseudocode
Example
Dr.Laith Baniata 23
Algorithm Efficiency
Dr.Laith Baniata 24
Algorithm Efficiency Cont.
• Consider now:
• A faster computer A running insertion sort against
• A slower computer B running merge sort
• Both must sort an array of one million numbers
• Suppose
• Computer A execute one billion (109) instructions per second
• Computer B execute ten million (107) instructions per second
• So computer A is 100 times faster than computer B
• Assume that
• c1 = 2 and c2 = 50
Dr.Laith Baniata 25
Algorithm Efficiency Cont.
• To sort one million numbers
• Computer A takes
2 . (106)2 instructions
109 instructions/second
= 2000 seconds
• Computer B takes
50 . 106 . lg(106) instructions
107 instructions/second
100 seconds
• By using algorithm whose running time grows more slowly, Computer B runs 20 times faster than
Computer A
• For ten million numbers
• Insertion sort takes 2.3 days
• Merge sort takes 20 minutes
Dr.Laith Baniata 26
Classifications of Algorithms
• There are various ways to classify algorithms, each with its own merits:
1) By implementation
2) By design paradigm
3) By field of study
4) By complexity
Dr.Laith Baniata
282
8
By Implementation
Dr.Laith Baniata 29
By design paradigm
Dr.Laith Baniata 30
By Field of Study
• Every field of science has its own problems and needs efficient algorithms.
1. Search Algorithms
2.Sorting Algorithms
3. Graph Algorithms
4.String Algorithms
5.Machine learning Algorithms
6. Cryptography Algorithms
7.And more
Dr.Laith Baniata 31
By complexity
Dr.Laith Baniata 32
Dr.Laith Baniata 33