This document discusses the design and analysis of algorithms. It introduces algorithms and defines them as sets of rules to solve computational problems. It emphasizes the importance of both designing algorithms through techniques like divide-and-conquer as well as analyzing their performance through complexity analysis. The document provides examples of analyzing worst-case, best-case, and average-case runtime and uses an example algorithm to find the largest number in an array to demonstrate space and time analysis methods.
Introduction
Algorithm
Aset of well defined rules to solve a computation problem
Specific examples: Find Minimum! and Multiplication
Design: the description of algorithm (pseudo language) and
proof of its correctness
Analysis: performance evaluation (complexity analysis)
3
AlgorithmInput Output
American British
input
output
N1
N2
4.
Motivation: Algorithm Efficiencyvs. CPU
Speed
Task: Sort n=106 numbers
Computer A = 109 instructions/second
Algorithm A = 2n2 instructions
Computer B = 107 instructions/second
Algorithm B = 50nlgn instructions
A =
B =
4
seconds2000
second/nsinstructio10
nsinstructio102
9
26
seconds100
second/nsinstructio10
nsinstructiolg101050
7
66
20 times better!!
5.
Algorithm Design Techniques
Brute Force & Exhaustive
Search
follow definition / try all
possibilities
Divide & Conquer
break problem into distinct
subproblems
Transformation
convert problem to another
one
Dynamic Programming
break problem into overlapping
subproblems
Greedy
repeatedly do what is best now
Iterative Improvement
repeatedly improve current
solution
Randomization
use random numbers
5
6.
Analysis of Algorithms
Worst case: (e.g. cards reversely ordered)
Provides an upper bound on running time
An absolute guarantee that the algorithm would
not run longer, no matter what the inputs are
Best case: (e.g., cards already ordered)
Input is the one for which the algorithm runs the
fastest
Average case: (general case)
Provides a prediction about the running time
Assumes that the input is random
6
2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A
7.
Conclusion
We studiedthe importance of designing and analysis of algorithm
Different methods and approaches are discussed to create and
evaluate an algorithm
It helps us to utilize resources effectively to achieve desired results
in a reasonable time
Related Study Materials
Text Books
Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein,
Introduction to Algorithms, Third Edition, 2009.
Kleinberg and Tardos, Algorithm Design, 2005.
Online Lectures by Tim Roughgarden on Algorithms: Design and Analysis,
Part 1, Stanford University
7
8.
References
1. Adil Khan,Course slides on Data structure and algorithms, Innopolis
University, 2016.
2. Monica Nicolescu, course slides on Analysis of Algorithms
CS 477/677.
3. Arjun Dasgupta et. al., CSE5311, DESIGN AND ANALYSIS OF
ALGORITHMS.
4. Jennifer Welch, CSCE 411, Design and Analysis of Algorithms, 2013.
5. Anany Levitin, Fundamentals of the Analysis of Algorithm Efficiency,
2nd edition, chapter 2.
8
9.
Appendix: Farmer CrossesRiver Puzzle
A farmer wants to cross a river and
take with him a wolf, a goat, and a
cabbage.
There is a boat that can fit himself
plus either the wolf, the goat, or
the cabbage.
If the wolf and the goat are alone on
one shore, the wolf will eat the goat.
If the goat and the cabbage are
alone on the shore, the goat will eat
the cabbage.
How can the farmer bring the wolf,
the goat, and the cabbage across
the river?
9
10.
Appendix: Our MachineModel
RAM: Generic Random Access Machine
Executes operations sequentially
Set of primitive operations:
Arithmetic. Logical, Comparisons, Function calls
Simplifying assumption: all ops cost 1 unit
Eliminates dependence on the speed of our computer,
otherwise impossible to verify and to compare
10
11.
Sample Algorithm
FINDING LARGESTNUMBER
INPUT: unsorted array ‘A[n]’of n numbers
OUTPUT: largest number
----------------------------------------------------------
1 large ← A[j]
2 for j ← 2 to length[A]
3 if large < A[j]
4 large ← A[j]
5 end
12.
Space and TimeAnalysis
(Largest Number Scan Algorithm)
SPACE S(n): One “word” is required to run the algorithm (step
1…to store variable ‘large’)
TIME T(n): n-1 comparisons are required to find the largest
(every comparison takes one cycle)
running time execution time
for basic operation
Number of times
basic operation is
executed
input size
T(n) ≈ copC(n)