Chapter 1: Introduction: Definition of An Algorithm
Chapter 1: Introduction: Definition of An Algorithm
Definition of an Algorithm
Chapter 1: Introduction An algorithm is a sequence of unambiguous instructions for solving a problem in a
finite amount of time. An input to an algorithm is an instance of the problem.
Progress is possible only if we
train ourselves to think about
programs without thinking of
them as pieces of executable
code. (Edsger Dijkstra)
CS 3343 Analysis of Algorithms Chapter 1: Slide – 2
Fundamentals of Algorithmic Problem Solving 6 We will use pseudocode in the book’s style.
CS 3343 Analysis of Algorithms Chapter 1: Slide – 3
Design and Analysis Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Algorithm Design Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Important Problem Types 8
Important Problem Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Fundamental Data Structures 9
Abstract Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Linear Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Binary Search Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
First Child-Next Sibling Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Sets and Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1 2
Pseudocode for Euclid’s Algorithm Fundamentals of Algorithmic Problem Solving 6
Sieve of Eratosthenes
algorithm Sieve(n)
// Implements the sieve of Eratosthenes
// Input: An integer n > 1
// Output: A list L of all primes ≤ n CS 3343 Analysis of Algorithms Chapter 1: Slide – 6
for p ← 2 to n√do A[p] ← true
for p ← 2 to ⌊ n⌋ do
if A[p] then // p is prime Algorithm Design Techniques
add p to the list L Brute Force. Straightforward, naive approach.
j ← p∗p Divide-and-Conquer. Divide into smaller insts.
while j ≤ n do // eliminate multiples of p Decrease-and-Conquer. Decrease instance size.
A[j] ← false Transform-and-Conquer. Modify instance first.
j ← j+p Space and Time Tradeoffs. Use more space now to save time later.
return L Dynamic Programming. Record results of smaller, reoccuring instances.
CS 3343 Analysis of Algorithms Chapter 1: Slide – 5
Greedy Technique. Make locally optimal decisions.
Iterative Improvement. Improve one change at a time.
CS 3343 Analysis of Algorithms Chapter 1: Slide – 7
3 4
Important Problem Types 8
Linear Data Structures
Important Problem Types
Sorting. Arrange items in order.
Searching. Find items in a data structure.
String Processing. E.g., string matching.
Graph Problems. Paths, coloring,
Combinatorial Problems. Find correct/best combination, permutation, or
subset. Stack ADT: push, pop.
Geometric Problems. Points, lines, shapes, volumes. Queue ADT: enqueue, dequeue.
Numerical Problems. Continuous values and models. Priority Queue ADT: add item, find/delete largest.
CS 3343 Analysis of Algorithms Chapter 1: Slide – 8 CS 3343 Analysis of Algorithms Chapter 1: Slide – 10
Graphs
Fundamental Data Structures 9
A data type is a group of data items and the operations defined on them.
5 6
Trees First Child-Next Sibling Representation
CS 3343 Analysis of Algorithms Chapter 1: Slide – 12 CS 3343 Analysis of Algorithms Chapter 1: Slide – 14
A bit vector can be used to represent a subset of a small set. E.g., 0011010100
might represent the subset {2, 3, 5, 7} of {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.
7 8