Professional Documents
Culture Documents
Chapter 1 Introduction To Algorithm-1
Chapter 1 Introduction To Algorithm-1
Chapter 1 Introduction To Algorithm-1
INTRODUCTION
4
26-Jan-19 H.M OJONG@EMITINA
1.1 WHAT IS AN ALGORITHM?
Procedure to accomplish a specific task.
Must solve a general, well-specified problem.
Definition:
A procedure (unambiguous sequence of instructions) for
solving a problem in a finite amount of time.
Obtains required output for specified legitimate inputs.
26-Jan-19 10
H.M OJONG@EMITINA
NOTE:
This algorithm fails if one of its input is zero, illustrating
the importance of specifying the set of inputs of an
algorithm explicitly.
Exercise :
Given that m = 60, and n = 24, use the consecutive integer
checking algorithm to fine the gcd.
Hint: step 1: min{60, 24} = 24, let p = 24
step 2: divide 60 by 24, and proceed with the rest of
the instructions
26-Jan-19 11
H.M OJONG@EMITINA
b) Middle-school algorithm for computing gcd(m,n)
Step 1: Find the prime factors of m.
Step 2: Find the prime factors of n.
Step 3: Identify all the common factors in the two prime
expansions found in Step 1 and Step 2. (If p is a common
factor occurring 𝑝𝑚 and 𝑝𝑛 times in m and n, respectively,
it should be repeated min{𝑝𝑚 , 𝑝𝑛 } times.)
Step 4: Compute the product of all the common factors and return
it as the greatest common divisor of the numbers given.
Exercise:
Use the middle-school algorithm to compute the gcd for
60 and 24.
prime factors of 60 = 2 x 2 x 3 x 5
prime factors of 24 = 2 x 2 x 2 x 3
∴ gcd(60, 24) = 2 x 2 x 3 = 12
26-Jan-19 H.M OJONG@EMITINA 12
1.2 FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
Fig 1.2 shows the sequence of steps required in designing and
analyzing an algorithm.
H.M OJONG@EMITINA 13
26-Jan-19
a) Understanding the Problem
- First point toward designing an algorithm is a complete understanding
of the given problem.
- Specify the exact set of instances the algorithm needs to handle. An
input to an algorithm specifies an instance of the problem the algorithm
solves.
b) Ascertaining the Capabilities of the Computational Device
- Most algorithms are designed for the von Neumann machine
architecture, so called random-access machine (RAM). This
architecture assumes instructions are executed one after the other.
Algorithms designed to be executed on such machines are called
sequential algorithm.
- Some newer computers can execute instructions in parallel (i.e.,
concurrently). Algorithms that run on such machines are called parallel
algorithms.
- For very complex problems, or problems that process huge volumes of
data, or deal with applications where the time is critical, it is necessary
26-Jan-19
to consider the speed and H.M OJONG@EMITINA
memory of a particular computer system.14
c) Choosing between Exact and Approximate Problem Solving
- Exact algorithm:
• Solves the problem exactly
• Can be unacceptably slow due to the problem’s intrinsic complexity,
e.g., involving a very large number of choices.
- Approximate algorithm:
• Used for problems that cannot be solved exactly for most of their
instances, e.g., extracting square roots, solving nonlinear equations, and
evaluating definite integrals.
d) Methods of Specifying an Algorithm
- Pseudocode:
• Mixture of a programming language and a programming language-like
construct.
• Usually more precise than a natural language.
- Flowchart:
• Graphical representation of algorithms using standard geometric shapes
and arrows.
26-Jan-19 H.M OJONG@EMITINA 15
e) Proving an Algorithm’s Correctness
- Correctness involves a prove that the algorithm yields a required
result for every legitimate input in a finite amount of time.
- Commonly done using method of mathematical induction.
- The notion of correctness for exact algorithms is more straightforward
than it is for approximation algorithms.
f) Analysing an Algorithm
Consider the following characteristics when analysing an
algorithm
- The characteristic that comes after correctness is efficiency.
Two kinds of algorithm efficiency exist
•time efficiency: indicates how fast the algorithm goes
• Space efficiency: indicates how much extra memory is used by the algorithm.
• Set of inputs the algorithm accepts: design an algorithm that can handle a set of
inputs that is natural for the problem at hand.
E.g., it is unnatural to exclude integers equal to 1 as possible inputs for a gcd
algorithm. However, for a quadratic equation we can exclude inputs for
complex coefficient, except it is explicitly required.
g) Coding an Algorithm
- Involves the transformation of the algorithm into a working program
using a specified programming language.
- Establish the validity of the program by testing the set of specified
inputs.
26-Jan-19 H.M OJONG@EMITINA 17
1.3 IMPORTANT PROBLEM TYPES
This section introduces the most common problem types:
Sorting
Searching
String processing
Graph problems
Combinatorial problems
Geometric problems
Numerical problems
21
26-Jan-19 H.M OJONG@EMITINA
1.3.5 Combinatorial Problems
- Problems that ask, explicitly or implicitly, to find a
combinatorial object—such as a permutation, a combination,
or a subset—that satisfies certain constraints.
1.3.6 Geometric Problems
- Deals with geometric objects such as points, lines, and
polygons.
- Two computational geometric problems are:
a) Closest-pair problem: finds the closest pair of points amongst n
given points in a plain.
b) Convex-hull problem: finds the smallest convex polygon that
would include all the points of a given set.
24
26-Jan-19 H.M
Fig 1.3 singly OJONG@EMITINA
linked list of n elements
Accessing a node: starts from the first node and transverse the pointer chain
until the particular node is reached.
Time to access an element in a node varies; depends on where the element
is located in the list.
In a doubly linked list, each node contains pointers to both its successor and
its predecessor, except for the first and last nodes.
Fig 1.5 (b) depicts a directed graph with six vertices and nine
directed edges:
V = {a, b, c, d, e, f}, and
E = {(a,c), (b,c), (b,f), (c,f), (d,a), (d,c), (d,e), (e,c), (e,f)}
- The following inequality holds for the number of edges |E|
possible in an undirected graph with |V| vertices and no
loops: 0≤ |E| ≤ |V|(|V| - 1)/2
26-Jan-19 H.M OJONG@EMITINA 30
- A graph with every pair of vertices connected by an edge
is called complete.
- A graph is said to be dense if relatively few edges are
missing (and |E| is close to |V|(|V|-1)/2 ).
- A graph is said to be sparse if the number of edges are few
relative to the number of its vertices.(|E| is much less than
|V|(|V|-1)/2 ).
Graph Representations
- Computer algorithm represent graphs as adjacency matrix
and adjacency list.
a) Adjacency Matrix:
Graph G = (V, E) is in adjacency matrix representation if G
consists of a |V| x |V| matrix A = (𝑎𝑖𝑗 ) such that:
26-Jan-19 H.M OJONG@EMITINA 31
a) Adjacency Matrix Representation:
Graph G = (V, E) is in adjacency matrix representation if G
consists of a |V| x |V| matrix A = (𝑎𝑖𝑗 ) such that:
1 if (i, j) ∈ E
𝑎𝑖𝑗 =
0 otherwise
Exercise:
Give the adjacency matrix representation of the graph
below:
Fig 1.8(a)
Fig 1.9
35
26-Jan-19 H.M OJONG@EMITINA
Weighted Graphs
Graph with weight or cost attached to it edges.
Represented by a weighted matrix or cost matrix.
Existing edges are represented by the value of their weights, while
non-existing edges are represented by the special symbol ∞.
Adjacency list representation includes both the name of the vertex
and the weight of the corresponding edge.
Fig 1.10 (a), (b), and (c) shows a weighted graph with the
corresponding cost matrix and adjacency list respectively.