0% found this document useful (0 votes)
4 views

CSE408 Lecture 1

The document provides an overview of algorithms, defining them as sequences of instructions that solve specific problems and produce outputs from valid inputs. It discusses the historical context of algorithms, key requirements, and various design strategies, as well as the importance of analyzing their efficiency. Additionally, it highlights well-known computational problems and includes examples of algorithm implementations.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CSE408 Lecture 1

The document provides an overview of algorithms, defining them as sequences of instructions that solve specific problems and produce outputs from valid inputs. It discusses the historical context of algorithms, key requirements, and various design strategies, as well as the importance of analyzing their efficiency. Additionally, it highlights well-known computational problems and includes examples of algorithm implementations.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A.

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
What is an algorithm?

An Algorithm is a:

 Sequence of clear instructions


 Solves a specific problem
 Produces required output
 Accepts valid input
 Completes in finite time
 Ensures clarity and precision

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Foundation of algorithm

• The first ever algorithm was proposed by Ada Lovelace


Mathematician who used the concepts of zero and
decimal position of number.
• During the 1940’s and 1950’s research oriented towards
building efficient computer system , so that they can be
used in scientific, commercial engineering problems.
• Structured Programming came into existence after Alan
Turning introduced the idea of effective procedure in
1936.
Donald Knuth is the father of algorithms

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Requirements of an algorithm?

1. Finiteness
 terminates after a finite number of steps
2. Definiteness
 rigorously and unambiguously specified
3. Clearly specified input
 valid inputs are clearly specified
4. Clearly specified/expected output
 produce correct output from valid input
5. Effectiveness
 steps are sufficiently simple and basic

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Fundamentals of Algorithmic Problem Solving

 Recipe
 Process
 Method
 Technique
 Procedure
 Routine.

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Some Well-known Computational Problems

Few Examples are:


Sorting Traveling salesman problem
Searching Knapsack problem
Shortest paths in a graph Chess
Minimum spanning tree Towers of Hanoi
Primality testing Program termination

Some of these problems don’t have efficient algorithms,


or algorithms at all!
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Example of computational problem: sorting

 Statement of problem:
• Input: A sequence of n numbers <a1, a2, …, an>

• Output: reordering so that a´i ≤ a´j whenever i < j

 Instance: The sequence <5, 3, 2, 8, 3>

 Algorithms:
• Selection sort
• Insertion sort
• Merge sort
• (many others)

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Basic Issues Related to Algorithms
 How to design algorithms

 How to express algorithms

 Proving correctness

 Efficiency (or complexity) analysis


 Theoretical analysis

 Empirical analysis

 Optimality

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Two main issues related to algorithms

 How to design algorithms

 How to analyze algorithm efficiency

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Algorithm design strategies

 Brute force  Greedy approach

 Divide and conquer  Dynamic programming

 Decrease and conquer  Backtracking and branch-and-bound

 Transform and conquer  Space and time tradeoffs


Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Analysis of Algorithms

 How good is the algorithm?


• Correctness
• Time efficiency
• Space efficiency

 Does there exist a better algorithm?


• Lower bounds
• Optimality

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Why study algorithms?
 Theoretical Importance
 Core of computer science

 Practical Importance
 Toolkit of known algorithms

 Design Framework
 Solves unique, new problems
 Provides structured approaches

 Efficiency Analysis
 Measures performance
 Optimizes time and resources

Example: Google’s PageRank


A. Levitin “Introduction to the DesignTechnology
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. & Analysis of Algorithms,” 2 ed., Ch. 1
nd
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Here is the step-by-step algorithm of the
code:

1. Declare three variables num1, num2, and num3 to store the three numbers to be
added.

2. Declare a variable sum to store the sum of the three numbers.

3. Use the cout statement to prompt the user to enter the first number.

4. Use the cin statement to read the first number and store it in num1.

5. Use the cout statement to prompt the user to enter the second number.

6. Use the cin statement to read the second number and store it in num2.

7. Use the cout statement to prompt the user to enter the third number.

8. Use the cin statement to read and store the third number in num3.

9. Calculate the sum of the three numbers using the + operator and store it in the sum
variable.

10. Use the cout statement to print the sum of the three numbers.

11. The
Copyright main
© 2007 Pearsonfunction
Addison-Wesley.returns 0, which A.indicates
All rights reserved. the
Levitin “Introduction successful
to the execution
Design & Analysis of ofCh.the
Algorithms,” 2nd ed., 1 program.
 int findFirstElement(int arr[], int n) {
 return arr[0];
 }
 Constant Time O(1)

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
 int sumOfArray(int arr[], int n) {
 int sum = 0;
 for (int i = 0; i < n; i++) {
 sum += arr[i]; // Executes n times
 }
 return sum;
 }
 Linear Time O(n)

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
 void printAllPairs(int arr[], int n) {
 for (int i = 0; i < n; i++) {
 for (int j = 0; j < n; j++) {
 cout << arr[i] << ", " << arr[j] << endl;
 }
 }
 }
 Quadratic Time O(n²)

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
 int binarySearch(int arr[], int left, int right, int x) {
 while (left <= right) {
 int mid = left + (right - left) / 2;

 if (arr[mid] == x)
 return mid;
 else if (arr[mid] < x)
 left = mid + 1;
 else
 right = mid - 1;
 }
 return -1;
 }

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
! ! !
a nk You
Th

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1

You might also like