1 Introduction
1 Introduction
Note: Slides are written by Dr. Monica Nicolescu at University of Nevada, Reno
General Information
• Instructor: Dr. Ahmad Qawasmeh
– E-mail: [email protected]
– Office hours: Su, Tue, Thu: 11-12
– Mon, Wed: 10-11
– Office: IT 236
2
Class Policy
• Grading
– First Exam (30%)
• Closed books, closed notes
– Second exam (30%)
• Closed books, closed notes
– Final exam (40%) Introduction to Algorithms,
3
Why Study Algorithms?
• Necessary in any computer programming problem
– Improve algorithm efficiency: run faster, process more data, do
something that would otherwise be impossible
– Solve problems of significantly large size
– Technology only improves things by a constant factor
• Compare algorithms
• Algorithms as a field of study
– Learn about a standard set of algorithms
– New discoveries arise
– Numerous application areas
• Learn techniques of algorithm design and analysis
4
Applications
• Multimedia
– CD player, DVD, MP3, JPG, DivX, HDTV
• Internet
– Packet routing, data retrieval (Google)
• Communication
– Cell-phones, e-commerce
• Computers
– Circuit layout, file systems
• Science
– Human genome
• Transportation
– Airline crew scheduling, UPS deliveries
5
Roadmap
6
Analyzing Algorithms
• Predict the amount of resources required:
• memory: how much space is needed?
2 ∗ (106 ) instructions
2
Your friend = 9
= 2000seconds
10 instructions / second
50 ∗ (106 )lg106 instructions
You =
7
≈ 100seconds
10 instructions / second
20 times better!!
8
Algorithm Analysis: Example
• Alg.: MIN (a[1], …, a[n])
m ← a[1];
for i ← 2 to n
if a[i] < m
then m ← a[i];
• Running time:
– the number of primitive operations (steps) executed
before termination
T(n) =1 [first step] + (n) [for loop] + (n-1) [if condition] +
(n-1) [the assignment in then] = 3n - 1
• Order (rate) of growth:
– The leading term of the formula
– Expresses the asymptotic behavior of the algorithm
9
Typical Running Time Functions
• 1 (constant running time):
– Instructions are executed once or a few times
• logN (logarithmic)
– A big problem is solved by cutting the original problem in smaller
sizes, by a constant fraction at each step
• N (linear)
– A small amount of processing is done on each input element
• N logN
– A problem is solved by dividing it into smaller problems, solving
them independently and combining the solution
10
Typical Running Time Functions
• N2 (quadratic)
– Typical for algorithms that process all pairs of data items (double
nested loops)
• N3 (cubic)
– Processing of triples of data (triple nested loops)
• NK (polynomial)
• 2N (exponential)
– Few exponential algorithms are appropriate for practical use
11
Why Faster Algorithms?
12
Asymptotic Notations
• A way to describe behavior of functions in the limit
– Abstracts away low-order terms and constant factors
13
Asymptotic Notations - Examples
• Θ notation
– n2/2 – n/2 = Θ(n2)
– (6n3 + 1)lgn/(n + 1) = Θ(n2lgn)
– n vs. n2 n ≠ Θ(n2)
• Ω notation • O notation
– n3 vs. n2 n3 = Ω(n2) – 2n2 vs. n3 2n2 = O(n3)
– n vs. logn n = Ω(logn) – n2 vs. n2 n2 = O(n2)
– n vs. n2 n ≠ Ω(n2) – n3 vs. nlogn n3 ≠ O(nlgn)
14
Mathematical Induction
• Used to prove a sequence of statements (S(1), S(2), …
n
n (n + 1)
S(n)) indexed by positive integers. S(n): ∑
i =1
i=
2
• Proof:
– Inductive step: assume that S(n) is true and prove that S(n+1) is
16
Recurrences
Def.: Recurrence = an equation or inequality that
describes a function in terms of its value on smaller
inputs, and one or more base cases
• E.g.: T(n) = T(n-1) + n
Iterative methods:
• Insertion sort
• Bubble sort
• Selection sort
2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A
19
Specialized Data Structures
Problem:
– Keeping track of customer account parent
information at a bank or flight L R
key data
reservations
– This applications requires fast
search, insert/delete, sort
Left child Right child
Solution: binary search trees
– If y is in left subtree of x,
then key [y] ≤ key [x]
– If y is in right subtree of x, 15
21
Greedy Algorithms
• Problem
– Schedule the largest possible set of non-overlapping
activities for SEM 234
22
Greedy Algorithms
• Similar to dynamic programming, but simpler approach
– Also used for optimization problems
23
Graphs
• Applications that involve not only a set of items, but also the
connections between them
Hypertext Circuits
24
Searching in Graphs
• Graph searching = systematically follow the
edges of the graph so as to visit the vertices of
the graph u v w
Original Labeled
26
Minimum Spanning Trees
• A connected, undirected graph:
∞ otherwise
28
Variants of Shortest Paths
• Single-source shortest path (Bellman-Ford, DAG shortest paths,
Disjkstra)
– G = (V, E) ⇒ find a shortest path from a given source vertex s to each
vertex v ∈ V
• Single-destination shortest path
– Find a shortest path to a given destination vertex t from each vertex v
– Reverse the direction of each edge ⇒ single-source
• Single-pair shortest path
– Find a shortest path from u to v for given vertices u and v
– Solve the single-source problem
• All-pairs shortest-paths (Matrix multiplication, Floyd-Warshall)
– Find a shortest path from u to v for every pair of vertices u and v
29
Number Theoretic Algorithms
• Secured communication: RSA public-key
cryptosystem
– Easy to find large primes
– Hard to factor the product of large primes
Secure Message
eavesdropper
Bob Alice
Authenticated Message
30
NP-Completeness
• Not all problems can be solved in polynomial
time
– Some problems cannot be solved by any computer no
matter how much time is provided (Turing’s Halting
problem) – such problems are called undecidable
– Some problems can be solved but not in O(nk)
• Can we tell if a problem can be solved ?
– NP, NP-complete, NP-hard
• Approximation algorithms
31
Readings
• Chapter 1
• Appendix A
32