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

Algorithms - Introduction

The document provides an overview of algorithms, defining them as sequences of instructions for problem-solving. It discusses Euclid's algorithm for finding the greatest common divisor (GCD) and highlights characteristics of algorithms such as unambiguity, finite execution, and language independence. Additionally, it covers the fundamentals of algorithmic problem-solving, including understanding the problem, designing algorithms, proving correctness, and analyzing efficiency.

Uploaded by

Adolf Hitler
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Algorithms - Introduction

The document provides an overview of algorithms, defining them as sequences of instructions for problem-solving. It discusses Euclid's algorithm for finding the greatest common divisor (GCD) and highlights characteristics of algorithms such as unambiguity, finite execution, and language independence. Additionally, it covers the fundamentals of algorithmic problem-solving, including understanding the problem, designing algorithms, proving correctness, and analyzing efficiency.

Uploaded by

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

Introduction

Analysis of Algorithms
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.
OR
• 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.
What Is an Algorithm?
What Is an Algorithm?
• Algorithms are essential to computer science, but they are not only
part of computer science.
Euclid’s Algorithm for finding GCD
• Problem: Find gcd(m,n), the greatest common divisor of two
nonnegative, not both zero integers m and n
Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ?
• Euclid’s algorithm is based on repeated application of equality
gcd(m,n) = gcd(n, m mod n)
• until the second number becomes 0.

• Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12


Euclid’s Algorithm for finding GCD
Step 1 If n = 0, return m and stop; otherwise go to Step 2
Step 2 Divide m by n and assign the value fo the remainder to r
Step 3 Assign the value of n to m and the value of r to n. Go to
Step 1.

while n ≠ 0 do
r ← m mod n
m← n
n←r
return m
Other method for computing
gcd(m,n)
Consecutive integer checking algorithm
Step 1 Assign the value of min{m,n} to t
Step 2 Divide m by t. If the remainder is 0, go to Step 3;
otherwise, go to Step 4
Step 3 Divide n by t. If the remainder is 0, return t and stop;
otherwise, go to Step 4
Step 4 Decrease t by 1 and go to Step 2
Other method for computing
gcd(m,n)
• Note that unlike Euclid’s algorithm, this algorithm, in the form
presented, does not work correctly when one of its input numbers is
zero.
• This example illustrates why it is so important to specify the set of an
algorithm’s inputs explicitly and carefully.
Characteristics of an Algorithm
• Unambiguous: Algorithm should be clear and unambiguous. Each of
its steps should be clear in all aspects and must lead to only one
meaning.
• E.g., Cook a dish, bring me water

• Clearly defined inputs and outputs: If an algorithm gets inputs, then


those inputs should be well-defined. Similarly, the algorithm must
clearly define what output will be obtained and those should also be
well-defined.
• E.g., Dividing a number with another (you can not divide a number by zero)
Characteristics of an Algorithm
• Finite: The algorithm must produce the desired output in finite time,
i.e. it should not end up in an infinite loops or something like that.

• Language Independent: The Algorithm must be language-


independent, i.e. it must be just plain instructions that can be
implemented in any language, and yet it will generate the desired
output.

• However, in this class, we will be using Java as the programming


language for coding.
Algorithms
• There may exist several algorithms for solving the same problem.
• E.g., Searching a value from an array or sorting an array
• However, they may produce the results with dramatically different
speeds.
• E.g., Sorting an array
Fundamentals of Algorithmic
Problem Solving
Fundamentals of Algorithmic
Problem Solving
Understanding the problem

• You may get a common problem (searching, sorting etc.) and find an
already-developed algorithm.

• However, mostly, you will have to develop your own algorithm.


Fundamentals of Algorithmic
Problem Solving
Understanding the problem

• You should clearly specify the set of instances of input that are to be
handled by your algorithm.
• E.g., Binary Search

• A correct algorithm is not one that works most of the time, but one
that works correctly for all legitimate inputs.
Fundamentals of Algorithmic
Problem Solving
Ascertaining the Capabilities of the Computational Device

• You need to ascertain the capabilities of the computational device the


algorithm is intended for.

• Most of the algorithms are designed for the von Neumann machine.

• The architecture of that machine is based on random-access machine


(RAM) model.
Fundamentals of Algorithmic
Problem Solving
Ascertaining the Capabilities of the Computational Device

• The RAM model assumes that instructions are executed one after
another, one operation at a time.

• Accordingly, algorithms designed to be executed on such machines


are called sequential algorithms.
Fundamentals of Algorithmic
Problem Solving
Ascertaining the Capabilities of the Computational Device

• The RAM model does not consider newer computers that can execute
operations concurrently, i.e., in parallel.

• Algorithms that take advantage of this capability are called parallel


algorithms.
Fundamentals of Algorithmic
Problem Solving
Ascertaining the Capabilities of the Computational Device

• Most of the time, you would not worry about the speed and memory
of the today’s computers.

• However, if the problem at your hand processes huge volumes of


data, or deals with applications where the time is critical, then you
need to be aware of the speed and memory available on that device.
Fundamentals of Algorithmic
Problem Solving
Design an algorithm

• You may need to combine several techniques.

• You should select appropriate data structures (arrays, linked lists etc.)

• Specify the algorithm


• Using a natural language or pseudocode.
Fundamentals of Algorithmic
Problem Solving
Prove the correctness

• The simplest form is to feed various inputs and verifying the


correctness of the output.

• Proof by induction (mathematical induction) etc.


Fundamentals of Algorithmic
Problem Solving
Analyze an algorithm

• How much efficient is the developed algorithm

• Time efficiency: how fast the algorithm runs

• Space efficiency: how much extra memory it uses.


Fundamentals of Algorithmic
Problem Solving
Code an algorithm

• Implement in any programming language.


Important Problem Types
• Sorting

• Searching

• Graph Problems
Important Problem Types
Sorting

• Arrange items in a certain order.


• Can be numbers, names, dates etc.
• Sorting students by GPA
• Ranking Internet search results on their rankings
• Helps solve other problems easily, like (binary) searching
Important Problem Types
Searching

• Find a given value, called a search key, in a given set of values.

• Linear/sequential search
• Binary search
Important Problem Types
Graphs

• Graphs can be used for modeling a wide variety of applications,


including transportation, communication, social networks etc.
Fundamental Data Structures
• Linear data structures
• Arrays, Linked Lists

• Graphs
• Undirected Graphs
• Trees

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy