Algorithms: A Brief Introduction, Lecture notes of Algorithms and Programming

An introduction to algorithms, including examples and pseudo-code. It covers topics such as greedy algorithms, optimization, and designing an algorithm. The document also discusses the properties of an algorithm and how to measure its complexity. It is intended for students studying computer science and discrete mathematics.

Typology: Lecture notes

2021/2022

Uploaded on 05/11/2023

mjforever
mjforever 🇺🇸

4.8

(25)

254 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms: A
Brief
Introduction
CSE235
Introduction
Algorithms
Examples
Greedy
algorithm
Algorithms: A Brief Introduction
Slides by Christopher M. Bourke
Instructor: Berthe Y. Choueiry
Fall 2007
Computer Science & Engineering 235
Introduction to Discrete Mathematics
Section 3.1 of Rosen
1 / 18
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Algorithms: A Brief Introduction and more Lecture notes Algorithms and Programming in PDF only on Docsity!

Algorithms: A Brief Introduction CSE

Introduction Algorithms Examples Greedy algorithm

Algorithms: A Brief Introduction

Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry

Fall 2007

Computer Science & Engineering 235 Introduction to Discrete Mathematics 1 / 18^ Section 3.1 of Rosen

Algorithms: A Brief Introduction CSE

Introduction Algorithms Examples Greedy algorithm

Algorithms

Brief Introduction

Real World Computing World Objects Data Structures, ADTs, Classes Relations Relations and functions Actions Operations

Problems are specified by (1) a formulation and (2) a query. Formulation is a set of objects and a set of relations between them Query is the information one is trying to extract from the formulation, the question to answer. Algorithms^1 are methods or procedures that solve instances of a problem 2 / 18^1 ”Algorithm” is a distortion of^ al-Khwarizmi, a Persian mathematician

Algorithms: A Brief Introduction CSE

Introduction Algorithms PseudocodeDesign

Examples Greedy algorithm

Algorithms

General Techniques

There are many broad categories of Algorithms: Randomized algorithms, Monte-Carlo algorithms, Approximation algorithms, Parallel algorithms, et al. Usually, algorithms are studied corresponding to relevant data structures. Some general styles of algorithms include

(^1) Brute Force (enumerative techniques, exhaustive search) (^2) Divide & Conquer (^3) Transform & Conquer (reformulation) (^4) Greedy Techniques

Algorithms: A Brief Introduction CSE

Introduction Algorithms PseudocodeDesign

Examples Greedy algorithm

Pseudo-code

Algorithms are usually presented using some form of pseudo-code. Good pseudo-code is a balance between clarity and detail. Bad pseudo-code gives too many details or is too implementation specific (i.e. actual C++ or Java code or giving every step of a sub-process). Good pseudo-code abstracts the algorithm, makes good use of mathematical notation and is easy to read.

Algorithms: A Brief Introduction CSE

Introduction Algorithms PseudocodeDesign

Examples Greedy algorithm

Designing An Algorithm

A general approach to designing algorithms is as follows. (^1) Understand the problem, assess its difficulty (^2) Choose an approach (e.g., exact/approximate, deterministic/probabilistic) (^3) (Choose appropriate data structures) (^4) Choose a strategy (^5) Prove termination (^6) Prove correctness (^7) Prove completeness (^8) Evaluate complexity (^9) Implement and test it. (^10) Compare to other known approaches and algorithms.

Algorithms: A Brief Introduction CSE

Introduction Algorithms Examples Greedy algorithm

MAX

When designing an algorithm, we usually give a formal statement about the problem we wish to solve. Problem Given a set A = {a 1 , a 2 ,... , an} integers. Output the index i of the maximum integer ai.

A straightforward idea is to simply store an initial maximum, say a 1 then compare it to every other integer, and update the stored maximum if a new maximum is ever found.

Algorithms: A Brief Introduction CSE

Introduction Algorithms Examples Greedy algorithm

MAX

Analysis

This is a simple enough algorithm that you should be able to:

Prove it correct Verify that it has the properties of an algorithm. Have some intuition as to its cost.

That is, how many “steps” would it take for this algorithm to complete its run? What constitutes a step? How do we measure the complexity of the step? These questions will be answered in the next few lectures, for now let us just take a look at a couple more examples.

Algorithms: A Brief Introduction CSE

Introduction Algorithms Examples Greedy algorithm

Other examples

Check Bubble Sort and Insertion Sort in your textbooks, which you have seen ad nauseum, in CSE155, CSE156, and will see again in CSE310. I will be glad to discuss them with any of you if you have not seen them yet.

Algorithms: A Brief Introduction CSE

Introduction Algorithms Examples Greedy algorithm

Example

Change-Making Problem

For anyone who’s had to work a service job, this is a familiar problem: we want to give change to a customer, but we want to minimize the number of total coins we give them. Problem Given An integer n and a set of coin denominations (c 1 , c 2 ,... , cr) with c 1 > c 2 > · · · > cr Output A set of coins d 1 , d 2 , · · · , dk such that

∑k i=1 di^ =^ n^ and k is minimized.

Algorithms: A Brief Introduction CSE

Introduction Algorithms Examples Greedy algorithm

Example

Change-Making Algorithm

Change Input : An integer n and a set of coin denominations (c 1 , c 2 ,... , cr ) with c 1 > c 2 > · · · > cr. Output : A set of coins d 1 , d 2 , · · · , dk such that Pki=1 di = n and k is minimized. 1 C ← ∅ 2 for i = 1,... , r do 3 while n ≥ ci do 4 C ← C ∪ {ci} 5 n ← n − ci 6 end 7 end 8 output C

Algorithms: A Brief Introduction CSE

Introduction Algorithms Examples Greedy algorithm

Change-Making Algorithm

Analysis

Will this algorithm always produce an optimal answer? Consider a coinage system:

where c 1 = 20, c 2 = 15, c 3 = 7, c 4 = 1 and we want to give 22 “cents” in change.

What will this algorithm produce? Is it optimal?

Algorithms: A Brief Introduction CSE

Introduction Algorithms Examples Greedy algorithm

Change-Making Algorithm

Analysis

Will this algorithm always produce an optimal answer? Consider a coinage system:

where c 1 = 20, c 2 = 15, c 3 = 7, c 4 = 1 and we want to give 22 “cents” in change.

What will this algorithm produce? Is it optimal? It is not optimal since it would give us one c 4 and two c 1 , for three coins, while the optimal is one c 2 and one c 3 for two coins.

Algorithms: A Brief Introduction CSE

Introduction Algorithms Examples Greedy algorithm

Change-Making Algorithm

Proving optimality

Proof.

Algorithms: A Brief Introduction CSE

Introduction Algorithms Examples Greedy algorithm

Change-Making Algorithm

Proving optimality

Proof.

Let C = {d 1 , d 2 ,... , dk} be the solution given by the greedy algorithm for some integer n. By way of contradiction, assume there is another solution C′^ = {d′ 1 , d′ 2 ,... , d′ l} with l < k.