100% found this document useful (1 vote)
136 views32 pages

1.1 Algorithm Paradigms

This document provides an introduction to algorithms and data structures. It discusses why algorithms and data structures are important, provides examples of commonly used algorithms like the discrete Fourier transform and Google's PageRank. It also discusses algorithm design paradigms like brute force, divide and conquer, greedy algorithms, and dynamic programming. Pseudocode guidelines are provided. Overall, the document serves as a high-level overview of key concepts relating to algorithms and data structures.

Uploaded by

Stanley Wang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
136 views32 pages

1.1 Algorithm Paradigms

This document provides an introduction to algorithms and data structures. It discusses why algorithms and data structures are important, provides examples of commonly used algorithms like the discrete Fourier transform and Google's PageRank. It also discusses algorithm design paradigms like brute force, divide and conquer, greedy algorithms, and dynamic programming. Pseudocode guidelines are provided. Overall, the document serves as a high-level overview of key concepts relating to algorithms and data structures.

Uploaded by

Stanley Wang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Intro.

to
Algorithms
CS 251 – Data Structures
and Algorithms
Algorithms and Data Structures Table of Contents
01 Why do we care?

Real Life Examples


02 Thankfully, someone cared

Algorithmic Paradigms
03 Can algorithms follow an
established pattern?
01
Algorithms and Data Structures
Why do we care?
Algorithms

“Informally, an algorithm is any well-defined


computational procedure that takes some value,
or set of values, as input and produces some
value, or set of values, as output. An algorithm is
thus a sequence of computational steps that
transform the input into the output.”

- Cormen, Thomas H.; Leiserson, Charles E.;


Rivest, Ronald L.; Stein, Clifford. Introduction to
Algorithms (The MIT Press) (p. 5). The MIT Press.
Kindle Edition.
Algorithms are:

Effective Precise Finite


Return correct “No more, no less” Run in a limited
results amount of
time/steps
Should an
algorithm be
efficient?
public boolean isPrimeBasic(int n) public boolean isPrimeSqrt(int n)
{ {
comparisons = 1; comparisons = 1;

if (n <= 1) if (n <= 1)
{ {
return false; return false;
} }

for (int i = 2; i < n; i += 1) for (int i = 2; i * i < n; i += 1)


{ {
comparisons += 1; comparisons += 1;

if (n % i == 0) if (n % i == 0)
{ {
return false; return false;
} }
} }

return true; return true;


} }
Data Structures

“A data structure is a collection of data values,


the relationships among them, and the functions
or operations that can be applied to the data. If
any one of these three characteristics is missing
or not stated precisely, the structure being
examined does not qualify as a data structure.”

- Peter Wegner and Edwin D. Reilly. 2003. Data


structures. Encyclopedia of Computer Science.
John Wiley and Sons Ltd., GBR, 507–512.
Data structure examples
Traditional data structure functions

Read Write
At (key) Add (key [,item])
Find (key) Insert (key [,item])
Get (key) Edit (key [,item])
Search (key) Update (key [,item])
Lookup (key) Delete (key)
Exists (key) Swap (key1, key2)
Could we design Could we design
algorithms data structures
without data without
structures? algorithms?
We analyze A&DSs because:

01 02
Predict Performance Comparison
“How much time/space “Which one is [objectively]
does it require?” better?”

03 04
Provide Guarantees Theoretical Basis
“Will it always work?” “Can we make a theory
out of it?”
02
Examples
Thankfully, someone cared
Discrete Fourier Transform

Break down waveform of 𝑁 samples into periodic


elements.

Applications: DVD, JPEG, MRI, astrophysics,…

Brute force: 𝑁 2 steps.

FFT algorithm: 𝑁 log(𝑁) steps.


Google’s Page Rank

Pages that appear higher in the hierarchy have


more back-links or links to them.

PR(A) = (1-d) + d (PR(T1)/C(T1) + ... + PR(Tn)/C(Tn))

• PR(A) is the PageRank of page A.


• PR(Ti) is the PageRank of pages Ti which link
to page A.
• C(Ti) is the number of outbound links on page
Ti.
• d is a damping factor which can be set
between 0 and 1.
Pseudocode guidelines

1. DO define all your variables.


2. DO show the structure of the algorithm using keywords (e.g., for, if, else, etc.)
and indentations.
3. DO simplify tedious procedures by defining them in words rather than in specific
commands (this requires a certain amount of discernment).
4. DO NOT clutter up the pseudocode with brackets.
5. DO NOT reference any undefined variables or methods.
6. DO be very clear about variables, structures, procedures, etc. that are used in
your analysis.
7. DO be consistent with your notation within a single algorithm.
8. DO define your input and output.
9. DO NOT write code when a question asks for pseudocode.
Algorithm mystery
Input: v1, v2, and v3, the vertices of a triangle
and p, another point on the same plane
Output: It’s a mystery!
Let n be a large integer
Draw v1, v2, v3, and p
For n times:
Let v be one of the vertices chosen randomly
Re-set p to be the midpoint between v and p
Draw p
End for
End Algorithm
03
Algorithmic Paradigms
Can algorithms follow an established
pattern?
Algorithmic paradigms

An algorithmic paradigm or algorithm


design paradigm is a generic model or
framework which underlies the design of a
class of algorithms.

- Wikipedia
Example 1: Find 6

5 4 8 1 0 6 9 3
Example 2: Find 6 again

0 1 3 4 5 6 8 9
Example 1 Example 2

5 4 8 1 0 6 9 3 0 1 3 4 5 6 8 9

Solution: Linear Search Solution: Binary Search


Search the entire list one element Recursively compare X with the
at a time until you find X or reach median and eliminate half the
the end of the list. choices.
Algorithm LinearSearch
Input: A an array of elements, and X the element
we are searching
Output: The index in A where X is at, or -1
otherwise
Let n = |A|-1
For (i = 0, n, 1)
If (A[i] == X)
return i
End if
End for
return -1
End Algorithm
Algorithm BinarySearch
Input: A an array of elements, X the element we are
searching, l the leftmost index, and r the rightmost
index
Output: The index in A where X is at, or -1 otherwise
If (r < l)
return -1
End if
Let m = l + ((r - l) / 2)
If (V[m] == X)
return m
End if
If (V[m] > key)
return BinarySearch(V, key, l, m - 1)
End if
return BinarySearch(V, key, m + 1, r)
End Algorithm
Brute force

Example 1 Checks for all possibilities until we find


what we are looking for or realize there is
no solution.
5 4 8 1 0 6 9 3
Linear Search is also known as Best Effort
Search or Exhaustive Search.
Solution: Linear Search
Search the entire list one element Often very time consuming, but it’s not
at a time until you find X or reach always the worst approach.
the end of the list.
Divide and conquer

Divide the problem recursively into


subproblems and solve them.

Combine solutions to subproblem to form


the solution of the original problem.

Classic examples: Quick Sort, Merge Sort,


Top-Down Parsers, FFT.
Prune and search

Example 2 Similar to Divide and Conquer.

Main idea: recursively prune the search


0 1 3 4 5 6 8 9 space (reduce the size) by a constant
factor.

Solution: Binary Search Not that time consuming.


Recursively compare X with the
median and eliminate half the
choices.
Greedy

An approach to solve an optimization


problem where we attempt to find a global
optimal solution by choosing the optimal
value locally.

Examples: Travelling Salesman Problem,


Activity Selection Problem, ID3, Dijkstra,
Kruskal, Prim, A*.
Dynamic programming

Combines optimal sub solutions to form an


optimal solution.

Keeps track of sub solutions to avoid


repetitive calculations (memoization).

Examples: Floyd-Warshall, All-pairs,


Knapsack Problem, Longest Common
Substring/Subsequence.
Fibonacci using Dynamic Programing
That’s enough!
Do you have any questions?

CREDITS: This presentation template was created by Slidesgo, including


icons by Flaticon, infographics & images by Freepik and illustrations by
Stories

You might also like