Sei sulla pagina 1di 41

INTRODUCTION TO ALGORITHM

First Semester 2018/2019 Academic Year


By
Humphrey OJONG
UB (COT) © 2019

26-Jan-19 H.M OJONG@EMITINA 1


COURSE OBJECTIVE
This course has the following objectives:

26-Jan-19 H.M OJONG@EMITINA 2


LESSON I

INTRODUCTION

26-Jan-19 H.M OJONG@EMITINA 3


1.0 OVERVIEW
 Algorithms are essential for the development of good and
efficient computer programs.
 Algorithms helps in the development of analytical skills;
precisely defines procedure for getting concrete solutions to
problems.
 Algorithms differ in design strategies, level of sophistication,
and efficiency.
 An algorithmic problem should specify the complete set
instances it must work on, and its output after running one of
these instances.

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.

Fig. 1 Notion of algorithm

26-Jan-19 H.M OJONG@EMITINA 5


1.1.1 Characteristics of an Algorithm
 Not all procedures can be called an algorithm. An algorithm
should have the following characteristics:
- Unambiguous: each step (or phrase), and their inputs/outputs should
be clear and give one meaning only.
- Input: Should have zero or more well-defined inputs.
- Output: should have one or more well-defined outputs, which
correspond to the desired output.
- Finiteness: must terminate after a finite number of steps.
- Feasibility: should be feasible with the available resources.
- Independent: should be independent of any given programming
code.

26-Jan-19 H.M OJONG@EMITINA 6


Example 1:
Write an algorithm to fine the greatest common divisor
(gcd) of two nonnegative, non-zero integers m and n.

Problem: phrase in italic.


Input: m, n: 𝜖 ℤ∗+ , n<m
Output: n or p: n<p<m

The algorithm for the gcd known as Euclid algorithm,


denoted by gcd(m,n) is based on repeatedly applying the
equality:
gcd(m,n) = gcd(n, m mod n)
and gcd(m, 0) = m.

26-Jan-19 H.M OJONG@EMITINA 7


 Euclid’s algorithm for computing gcd(m,n)
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 reminder to
r.
step 3: Assign the value of n to m and the value of r to n. Go
to step 1.
This can be expressed in pseudocode as:

26-Jan-19 H.M OJONG@EMITINA 8


Fine the gcd of a) 18 and 72
b) 77 and 18
Solution:
a) gcd(72, 18) = gcd(18, 0) since 72 mod 18 = 0
b) gcd(77, 18) = gcd(18, 5) // 77 mod 18 = 5
=gcd(5, 3)
= gcd(3, 2)
= gcd(2, 1)
= gcd(1, 0) = 1
Note:
The algorithm ends when the value of n equals 0.

26-Jan-19 H.M OJONG@EMITINA 9


 Other ways to solve the gcd problem are:
a) By definition, the gcd of two integers m, n is the largest integer that
divides both numbers evenly.
i.e., p ∈ ℤ∗+ : p mod m = 0 ∧ p mod n = 0
This is known as the consecutive integer checking.
Consecutive integer checking algorithm for computing gcd(m,n)
Step 1: Assign the value of min{m, n} to p
Step 2: Divide m by p. If the remainder of this division is 0, go
to Step 3; otherwise, go to Step 4.
Step 3: Divide n by p. If the remainder of this division is 0,
return the value of p as the answer and stop; otherwise,
proceed to Step 4.
Step 4: Decrease the value of p by 1. Go to Step 2.

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.

Fig 1.2 Algorithm design and analysis process

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.

- Simplicity: depends on the algorithm writer. Simpler algorithms are easier to


understand and to program.

26-Jan-19 H.M OJONG@EMITINA 16


- Generality:
Two issues of generality exists
• Generality of the problem the algorithm solves: for some problems, it is easier
to design an algorithm that will solve the problem in a more general term, than to
solve a specific case. E.g., it is easier to design an algorithm for a more general
problem of computing the greatest common divisor of two integers than to
design one that determines whether two integers are relatively prime(i.e., their
only common divisor is equal to 1)

• 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

26-Jan-19 H.M OJONG@EMITINA 18


1.3.1 Sorting
- Sorting problem is to rearrange elements of a given list in an
ascending or descending order.
- Specifies a particular element in the list, called a key, used in
the sorting process.
- Two properties include
• Stability: A sorting algorithm is called stable if it preserves the
relative order of any two equal elements in its input. That is, if an
input list contains two equal elements in positions i and j where i < j,
then in the sorted list they have to be in positions 𝑖 ′ and 𝑗 ′ such that
𝑖′ < 𝑗′.
Consider a list of students sorted alphabetically, and again to be
sorted according to GPA: a stable algorithm will yield a list in
which students with the same GPA will still be sorted alphabetically.
• In-place: An algorithm is said to be in-place if it does not require
extra memory, except, possibly, for a few memory units.
26-Jan-19 H.M OJONG@EMITINA 19
1.3.2 Searching
- Deals with finding a given value (search key) in a given set.
- No single algorithm fits all situation best, Some algorithms work
faster than others but require more memory; some are very fast but
applicable only to sorted arrays only.
- Considered with the following two operations: addition to, and
deletion from the data set of an item.
1.3.3 String Processing
- Sequence of characters from an alphabet
- Text strings: comprise letters, numbers, and special characters.
- Bit strings: zeros and ones.
- Gene sequences: modeled by string of characters from the four-
character alphabets{A, C, G, T}

26-Jan-19 H.M OJONG@EMITINA 20


1.3.4 Graph Problems
- Graph: collection of points called vertices, some of which
are connected by line segments called edges.
- Can be used for modeling a wide variety of applications,
including transportation, communication, social and
economic networks, project scheduling, and games
- Two well known graph problems are:
a) Traveling Salesman Problem(TSP); problem of finding the
shortest tour through n cities that visits every city exactly once.
b) Graph-coloring Problem; seeks to assign the smallest number of
colors to the vertices of a graph so that no two adjacent vertices
are the same color.

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.

26-Jan-19 H.M OJONG@EMITINA 22


1.3.7 Numerical Problems
- Problems that involve mathematical objects of continuous
nature: solving equations and systems of equations,
computing definite integrals, evaluating functions, and so
on.
- Are mostly solved only approximately.

26-Jan-19 H.M OJONG@EMITINA 23


1.4 FUNDAMENTAL DATA STRUCTURES
 Data structure:
A scheme of organizing related data items.
1.4.1 Linear Data Structures
- Consist of arrays and linked lists.
- Basic operations include: searching for, inserting, or deleting
elements
a) Array:
SEE LESSONS ON ARRAYS
b) Linked list:
 Sequence of zero or a finite collection of elements called nodes.
 Each node contains: some data, and one or more links called pointers to
other nodes to the linked list.
 Each node in a singly linked list contains a single pointer to the next
element except the last element.

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.4 doubly linked list of n elements

 Two special lists implementations are: Stacks, and Queues.


a) Stack:
• List with insertion (push) and deletion(pop) operations done at one
end(top of stack) only.
• Operates in a last-in-first-out (LIFO) manner.
• Used in the implementation of recursive algorithms.
Stack Operations:
• A stack of n elements can be denoted by an array S[1,…,n].
• Let S.top be an attribute that indexes the top element of the stack. (also
26-Jan-19 known as the Stack Pointer) 25
H.M OJONG@EMITINA
Fig 1.5 (a) Fig 1.5 (b) Fig 1.5 (c)

Fig 1.5(a): stack has four elements, top element = 19


Fig 1.5(b): shows two push operations; PUSH(S, 3) and PUSH(S, 9) into S.
Fig 1.5(b): shows a pop operation; POP(S)- element 3 is now at the top of the
stack. Elements POP from the stack are not physically removed.
• S.top = 0 ⇒ stack is empty, and contain no element. A pop operation on an
empty stack results in stack a underflow error.
• S.top = n ⇒ stack is full. A push operation on a full stack leads to a stack
overflow.
26-Jan-19 H.M OJONG@EMITINA 26
• Stack implementation algorithms:
i) Test for an empty stack:
STACK-EMPTY (S)
1 if S.top = = 0
2 return TRUE
3 else return FALSE
ii) Insert into S
PUSH(S, x)
1 S.top = S.top + 1
2 S[S.top] = x
iii) Delete from S
1 if STACK-EMPTY(S)
2 error “underflow’’
3 else S.top = S.top – 1
4 return S[S.top + ]

26-Jan-19 H.M OJONG@EMITINA 27


b) Queue:
• List with the enqueue and dequeue operations.
• Elements are inserted at the rear end (enqueue) and deleted from the front
end (dequeue)
• Operates in a first-in-first-out (FIFO) manner.
• Used in the application of algorithms for graph and other problems.
• First element is said to be at the head of the queue, while the last element is
at the tail of the queue.

26-Jan-19 H.M OJONG@EMITINA 28


1.4.2 Graphs
- A graph G = (V, E) is defined by a pair of two sets:
 V: a finite nonempty sets of items (vertices), and
 E: set of pair of vertices called edges.
 A pair of unordered vertices u and v are said to be adjacent to
each other and connected to the undirected edge (u, v) if the pair
(u, v) is the same as the pair (v, u). A graph, G, is undirected if
every edge is undirected.
 If a pair of vertices (u, v) is not the same as the pair (v, u) then
the edge(u, v) is said to be directed. The edge(u, v) leaves from
vertex u (edge’s tail) to vertex v(edge’s head). A graph, G, is
said to be directed (a digraph) if every edge is directed.
- Vertices of a graph or digraph can be labeled with letters,
integers, or character strings.

26-Jan-19 H.M OJONG@EMITINA 29


- Example 2:
V = {1, 2, 3, 4, 5}, and
E = {(1,2), (1,4), (1,5), (2, 3), (2, 4), (2, 5), (2, 3), (3, 4)}
The graph has five vertices and eight undirected edges. Fig 1.5 (a)

Fig 1.5 (b) Digraph

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

Always symmetric for an undirected graph i.e., A[i, j] = A[j, i]


∀ 0 ≤ i, j ≤ n-1, hence, A = 𝐴𝑇 . In some applications only
entries on and above the diagonal of the adjacency matrix are
stored thereby reducing the memory needed to store the graph
by half.
Example 3:

Fig. 1.6 unordered graph of six vertices and seven edges

26-Jan-19 H.M OJONG@EMITINA 32


- The adjacency matrix representation is given in Fig. 1.7

Fig 1.7 adjacency matrix representation of Fig 1.6

Exercise:
Give the adjacency matrix representation of the graph
below:

26-Jan-19 H.M OJONG@EMITINA 33


b) Adjacency List Representation:
 Collection of linked list containing all the vertices adjacent to
the list’s vertex.
 Each list starts with a header identifying the vertex from which
the list is generated.
 Fig 1.8 (a) shows a digraph with its adjacency list
representation in Fig 1.8(b)
 The sum of the lengths of all the adjacency lists is |E| for a
directed graph, and 2|E| for an undirected graph.

Fig 1.8(a)

NOTE: Fig 1.8(b)

The adjacency list is easily obtained from the adjacency matrix


26-Jan-19 H.M OJONG@EMITINA 34
Exercise:
Fig 1.9 shows a digraph

Fig 1.9

a) What is the adjacency matrix representation of the graph?


b) What is the adjacency list representation of the graph?
c) Is the graph complete?
d) Is the graph dense or sparse?
e) What is the sum of the lengths of all the adjaceny lists?

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.

26-Jan-19 H.M OJONG@EMITINA 36


Trees
 Also known as a free tree, is a connection of acyclic graphs. Fig
1.11(a)
 A cycle is a path of a positive length that starts and ends at the same
vertex and does not traverse the same edge more than once.
 A graph with no cycle is said to be acyclic.

Fig 1.11(a) Free tree Fig 1.11(b) Forest

 A graph that has no cycles but is not necessarily connected is called


a forest: each of its connected components is a tree. Fig 1.11(b)

26-Jan-19 H.M OJONG@EMITINA 37


26-Jan-19 H.M OJONG@EMITINA 38
26-Jan-19 H.M OJONG@EMITINA 39
26-Jan-19 H.M OJONG@EMITINA 40
QUESTIONS!!

26-Jan-19 H.M OJONG@EMITINA 41

Potrebbero piacerti anche