KEMBAR78
01 - Introduction to Algorithms.pptx
01
Introduction to
Algorithms
CS 221 – ALGORITHMS AND COMPLEXITY
Introduction
The study of algorithms, sometimes called algorithmics, has
come to be recognized as the cornerstone of computer
science.
Algorithmics is more than a branch of computer science. It is
the core of computer science, and, in all fairness, can be said
to be relevant to most of science, business, and technology.
- David Harel, Algorithmics: the Spirit of Computing
Why study Algorithms?
Computer programs would not exist without algorithms.
With computer applications becoming indispensable in almost all
aspects of our professional and personal lives, studying algorithms
becomes a necessity for more and more people.
Usefulness in developing analytical skills.
The precision inherently imposed by algorithmic thinking limits the
kinds of problems that can be solved with an algorithm.
What is an Algorithm?
An algorithm is a sequence of unambiguous instructions for
solving a problem, i.e., for obtaining a required output for
any legitimate input in a finite amount of time.
Methods of solving the same problem
illustrates several important points:
The nonambiguity requirement for each step of an algorithm
cannot be compromised.
The range of inputs for which an algorithm works has to be
specified carefully.
The same algorithm can be represented in several different ways.
There may exist several algorithms for solving the same problem.
Algorithms for the same problem can be based on very different
ideas and can solve the problem with dramatically different speeds.
Euclid’s Algorithm
Recall that the greatest common divisor of two
nonnegative, not-both-zero integers m and n, denoted
gcd(m, n), is defined as the largest integer that divides both
m and n evenly, i.e., with a remainder of zero.
Euclid of Alexandria (third century b.c.) outlined an
algorithm for solving this problem in one of the volumes of
his Elements most famous for its systematic exposition of
geometry.
Euclid’s Algorithm (cont.)
gcd(m, n) = gcd(n, m mod n)
where m mod n is the remainder of the division of m by n,
until m mod n is equal to 0. Since gcd(m, 0) = m (why?), the
last value of m is also the greatest common divisor of the
initial m and n.
For example, gcd(60, 24) can be computed as follows:
gcd(60, 24) = gcd(24, 12) = gcd(12, 0) = 12.
Euclid’s algorithm for computing gcd(m, n)
Pseudocode:
Consecutive Integer Checking Algorithm
Middle-School Procedure
Middle-School Procedure (cont.)
To find the gcd(60, 24), get the
◦prime factors of 60 = 2.2.3.5
◦prime factors of 24 = 2.2.2.3
gcd(60, 24) = 2.2.3 = 12
Sieve of Erathostenes
the algorithm for generating consecutive primes not
exceeding any given integer n > 1.
Sieve of Erathostenes (cont.)
Fundamentals of Algorithmic Solving
Problems
1. Understanding the problem
An input to an algorithm specifies an instance of the problem the algorithm
solves.
2. Ascertaining the Capabilities of the Computational Device
sequential algorithm – executes one after another
parallel algorithm – executes concurrently
3. Choosing between Exact and Approximate Problem Solving
4. Algorithm Design Techniques
An algorithm design technique (or “strategy” or “paradigm”) is a general
approach to solving problems algorithmically that is applicable to a variety of
problems from different areas of computing.
Fundamentals of Algorithmic Solving
Problems
5. Designing an Algorithm and Data Structures
Methods of Specifying an Algorithm: Pseudocode and Flowchart
6. Proving an Algorithm’s Correctness
7. Analyzing an Algorithm
qualities of an algorithm:
oefficiency – time and space
osimplicity
ogenerality
8. Coding an Algorithm
optimality
Algorithm
Design and
Analysis Process
Important Problem Types
Sorting
Searching
String processing
Graph problems
Combinatorial problems
Geometric problems
Numerical problems
Fundamental Data Structures
Linear Data Structure
Graphs
Trees
Sets and Dictionaries
-End -

01 - Introduction to Algorithms.pptx

  • 1.
    01 Introduction to Algorithms CS 221– ALGORITHMS AND COMPLEXITY
  • 2.
    Introduction The study ofalgorithms, sometimes called algorithmics, has come to be recognized as the cornerstone of computer science. Algorithmics is more than a branch of computer science. It is the core of computer science, and, in all fairness, can be said to be relevant to most of science, business, and technology. - David Harel, Algorithmics: the Spirit of Computing
  • 3.
    Why study Algorithms? Computerprograms would not exist without algorithms. With computer applications becoming indispensable in almost all aspects of our professional and personal lives, studying algorithms becomes a necessity for more and more people. Usefulness in developing analytical skills. The precision inherently imposed by algorithmic thinking limits the kinds of problems that can be solved with an algorithm.
  • 4.
    What is anAlgorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time.
  • 5.
    Methods of solvingthe same problem illustrates several important points: The nonambiguity requirement for each step of an algorithm cannot be compromised. The range of inputs for which an algorithm works has to be specified carefully. The same algorithm can be represented in several different ways. There may exist several algorithms for solving the same problem. Algorithms for the same problem can be based on very different ideas and can solve the problem with dramatically different speeds.
  • 6.
    Euclid’s Algorithm Recall thatthe greatest common divisor of two nonnegative, not-both-zero integers m and n, denoted gcd(m, n), is defined as the largest integer that divides both m and n evenly, i.e., with a remainder of zero. Euclid of Alexandria (third century b.c.) outlined an algorithm for solving this problem in one of the volumes of his Elements most famous for its systematic exposition of geometry.
  • 7.
    Euclid’s Algorithm (cont.) gcd(m,n) = gcd(n, m mod n) where m mod n is the remainder of the division of m by n, until m mod n is equal to 0. Since gcd(m, 0) = m (why?), the last value of m is also the greatest common divisor of the initial m and n. For example, gcd(60, 24) can be computed as follows: gcd(60, 24) = gcd(24, 12) = gcd(12, 0) = 12.
  • 8.
    Euclid’s algorithm forcomputing gcd(m, n)
  • 9.
  • 10.
  • 11.
  • 12.
    Middle-School Procedure (cont.) Tofind the gcd(60, 24), get the ◦prime factors of 60 = 2.2.3.5 ◦prime factors of 24 = 2.2.2.3 gcd(60, 24) = 2.2.3 = 12
  • 13.
    Sieve of Erathostenes thealgorithm for generating consecutive primes not exceeding any given integer n > 1.
  • 14.
  • 15.
    Fundamentals of AlgorithmicSolving Problems 1. Understanding the problem An input to an algorithm specifies an instance of the problem the algorithm solves. 2. Ascertaining the Capabilities of the Computational Device sequential algorithm – executes one after another parallel algorithm – executes concurrently 3. Choosing between Exact and Approximate Problem Solving 4. Algorithm Design Techniques An algorithm design technique (or “strategy” or “paradigm”) is a general approach to solving problems algorithmically that is applicable to a variety of problems from different areas of computing.
  • 16.
    Fundamentals of AlgorithmicSolving Problems 5. Designing an Algorithm and Data Structures Methods of Specifying an Algorithm: Pseudocode and Flowchart 6. Proving an Algorithm’s Correctness 7. Analyzing an Algorithm qualities of an algorithm: oefficiency – time and space osimplicity ogenerality 8. Coding an Algorithm optimality
  • 17.
  • 18.
    Important Problem Types Sorting Searching Stringprocessing Graph problems Combinatorial problems Geometric problems Numerical problems
  • 19.
    Fundamental Data Structures LinearData Structure Graphs Trees Sets and Dictionaries
  • 20.

Editor's Notes

  • #11 Step 1: If n = 0, return the value of m as the answer and stop; otherwise, proceed to Step 2. Step 2: Divide m by n and assign the value of the remainder to r. Step 3: Assign the value of n to m and the value of r to n. Go to Step 1.
  • #12 #include <iostream> using namespace std; int gcd(int m, int n) {     if(n==0)         return m;     else         return gcd(n, m % n); } int main() {          int m, n;     cout << "Enter the value of m and n: ";     cin >> m >> n;     cout << "gcd(" << m << ", " << n << ") = " << gcd(m,n) << endl;     return 0; } int m, n, r;     cout << "Enter the value of m and n: ";     cin >> m >> n;     cout << "gcd(" << m << ", " << n << ") = ";     while(n != 0)     {         r = m % n;         m = n;         n = r;     }     cout << m << endl;