AlgorithmChapter I
AlgorithmChapter I
Lecture-01
Introduction
What is an Algorithm
Definition, characteristics, Writing an algorithm
Algorithm and Basic Concept
Algorithm Complexity
Analysis of the algorithms
Computing run time of algorithms
Bounds
Important Problem Type
Searching and Sorting- linear and binary search
String processing
Fundamental Data Structure
Liner data Structure
Non Liner Data structure
Algorithm: Definition
Not all procedures can be called an algorithm. An algorithm should have the
following characteristics −
Unambiguous − Algorithm should be clear and unambiguous. Each of its
steps (or phases), and their inputs/outputs should be clear and must lead to
only one meaning.
Input − An algorithm should have 0 or more well-defined inputs.
Output − An algorithm should have 1 or more well-defined outputs, and
should match the desired output.
Finiteness − Algorithms must terminate after a finite number of steps.
Feasibility − Should be feasible with the available resources.
Independent − An algorithm should have step-by-step directions, which
should be independent of any programming code.
To prove that the proposed algorithm solve the problem faster than other
solutions
Writing an Algorithm
▪ Brute force
▪ Try all possible combinations
▪ Divide and conquer
▪ breaking down a problem into two or more sub-problems of
the same (or related) type, until these become simple enough
to be solved directly.
▪ Decrease and conquer
▪ Change an instance into one smaller instance of the problem.
▪ Solve the smaller instance.
▪ Convert the solution of the smaller instance into a solution
for the larger instance.
Algorithm design strategies
▪ Transform and conquer
▪ a simpler instance of the same problem, or
▪ a different representation of the same problem, or
▪ an instance of a different problem
▪ Greedy approach
▪ The problem could be solved in iterations
▪ Dynamic programming
▪ An instance is solved using the solutions for smaller instances.
▪ The solution for a smaller instance might be needed multiple times.
▪ The solutions to smaller instances are stored in a table, so that each
smaller instance is solved only once.
▪ Additional space is used to save time.
▪ Backtracking and branch-and-bound
Algorithm: Design Considerations
The complexity of an algorithm f(n) gives the running time and/or the
storage space required by the algorithm in terms of n as the size of input
data.
Analysis of algorithms
Issues:
▪ Correctness
▪ Space efficiency
▪ Time efficiency
▪ Optimality
Approaches:
▪ Theoretical analysis
▪ Empirical analysis
*Apriori analysis vs. posterior analysis
Types of the Algorithm Analysis
Efficiency of an algorithm can be analyzed at two different stages, before
implementation and after implementation. They are the following
We shall learn about a priori algorithm analysis. Algorithm analysis deals with
the execution or running time of various operations involved. The running
time of an operation can be defined as the number of computer instructions
executed per operation.
Why to perform analysis of the Algorithms
The analysis of an algorithm is done because:
▪ By analysis we can have idea of slow and faster parts of the algorithm, and
accordingly we can plan our implementation strategies.
Worst-Case/ Best-Case/ Average-Case Analysis
Ο Notation
Ω Notation
θ Notation
Big-O Notation
Definition: f(n) is in O(g(n)) if order of growth of f(n) ≤ order of growth of
g(n) (within constant multiple),
Examples:
▪ 10n is O(n2)
▪ 5n+20 is O(n)
f (n) : there exist positive constants c and n0 s.t.
O( g (n)) =
0 f ( n ) cg ( n ) for all n n 0
Omega Notation, Ω
The notation Ω(n) is the formal way to express the Lower bound of an
algorithm's running time.
The notation θ(n) is the formal way to express both the lower bound and the
upper bound of an algorithm's running time. It is represented as follows −
i=1; c1 1
sum = 0; c2 1
while (i <= n) { c3 n+1
j=1; c4 n
while (j <= n) { c5 n*(n+1)
sum = sum + i; c6 n*n
j = j + 1; c7 n*n
}
i = i +1; c8 n
}
T(n) = c1 + c2 + (n+1)*c3 + n*c4 + n*(n+1)*c5+n*n*c6+n*n*c7+n*c8
= (c5+c6+c7)*n2 + (c3+c4+c5+c8)*n + (c1+c2+c3)
= a*n2 + b*n + c
➔ So, the growth-rate function for this algorithm is O(n2)
Growth-Rate Functions – Example3
Cost Times
for (i=1; i<=n; i++) c1 n+1
n
for (j=1; j<=i; j++) c2 ( j + 1)
j =1
n j
for (no=1; no<=j; no++) c3 (k + 1)
j =1 k =1
n j
x=x+1; c4 k
j =1 k =1
( j + 1)
n j
T(n) = c1*(n+1) + c2*( ) + c3* ( (k + 1) ) + c4*(
j
n
k
) j =1 j =1 k =1 j =1 k =1
Statement of problem:
Input: A sequence of n numbers <a1, a2, …, an>
Key= item to be Searched
Output: Index of the item to be searched
Instance: The sequence <5, 3, 2, 8, 3>
Algorithms:
Sequential Search
Binary Search(for sorted arrays)
Linear Search
Output: A reordering of the input sequence <a´1, a´2, …, a´n> so that a´i ≤
a´j whenever i < j
Algorithms:
Selection sort
Insertion sort
Merge sort
Quick Sort
(many others)
String Processing
A string is a sequence of characters from an alphabet.
Text strings: letters, numbers, and special characters.
String matching: searching for a given word/pattern in a text.
Examples:
(i) searching for a word or phrase on WWW or in a Word
document
(ii) searching for a short read in the reference genomic sequence
String Matching
Given a text string T of length n and a pattern string P
of length m, the exact string matching problem is to
find all occurrences of P in T.
Example: T=“AGCTTGA” P=“GCT”
Applications:
Searching keywords in a file
Searching engines (like Google and Openfind)
Database searching (GenBank)
More string matching algorithms (with source codes):
http://www-igm.univ-mlv.fr/~lecroq/string/
String Matching
Brute Force algorithm
The brute force algorithm consists in checking, at all positions in the text
between 0 and n-m, whether an occurrence of the pattern starts there or not.
Then, after each attempt, it shifts the pattern by exactly one position to the
right.
Informal definition
A graph is a collection of points called vertices, some of which
are connected by line segments called edges.
Modeling Real-life Problems
Modeling WWW
Communication networks
Project scheduling …
Examples of Graph Algorithms
Graph traversal algorithms
Shortest-path algorithms
……..
Data Structures Review
….
Data Structures:
An implementation of an ADT is a translation into
statements of a programming language,
─ the declarations that define a variable to be of that
ADT type
─ the operations defined on the ADT (using procedures
of the programming language)
Each data structure is built up from the basic data types of the
underlying programming language using the available data
structuring facilities, such as
arrays, records (structures in C), pointers, files, sets, etc.
Fundamental data structures
list
array
linked list graph
string
tree and binary tree
stack
queue
priority queue
Linear Data Structures
Arrays ◼ Arrays
A sequence of n items of the same data type that
◼ fixed length (need
are stored contiguously in computer memory and
made accessible by specifying a value of the preliminary
array’s index. reservation of
memory)
Linked List ◼ contiguous memory
A sequence of zero or more nodes each locations
containing two kinds of information: some data
and one or more links called pointers to other ◼ direct access
◼ arbitrary memory
a1 a2 an locations
◼ access by following
links
◼ Insert/delete
Stacks and Queues
Stacks
A stack of plates
─ insertion/deletion can be done only at the top.
─ LIFO
Two operations (push and pop)
Queues
A queue of customers waiting for services
─ Insertion/enqueue from the rear and deletion/dequeue from the
front.
─ FIFO
Two operations (enqueue and dequeue)
Priority Queue and Heap
3 4
Dense
Complete
Graph Representation
Adjacency matrix
n x n boolean matrix if |V| is n.
The element on the ith row and jth column is 1 if there’s an edge from
ith vertex to the jth vertex; otherwise 0.
The adjacency matrix of an undirected graph is symmetric.
Adjacency linked lists
A collection of linked lists, one for each vertex, that contain all the
vertices adjacent to the list’s vertex.
Which data structure would you use if the graph is a 100-node star
shape?
2 3 4
0111 4
0001
0001
0000
Weighted Graphs
Graphs or digraphs with numbers assigned to the edges.
5
1 2
6 7
9
3 4
8
Graph Properties -- Paths and Connectivity
Paths
A path from vertex u to v of a graph G is defined as a sequence of
adjacent (connected by an edge) vertices that starts with u and ends
with v.
Simple paths: a path in a graph which does not have repeating
vertices..
Path lengths: the number of edges, or the number of vertices – 1.
Connected graphs
A graph is said to be connected if for every pair of its vertices u and v
there is a path from u to v.
Connected component
The maximum connected subgraph of a given graph.
Graph Properties – A cyclicity
Cycle
A simple path of a positive length that starts and ends a the
same vertex.
Acyclic graph
A graph without cycles
DAG (Directed Acyclic Graph) 1 2
3 4
Trees
Trees
A tree (or free tree) is a connected acyclic graph.
Forest: a graph that has no cycles but is not necessarily connected.
Properties of trees
For every two vertices in a tree there always exists rooted
exactly one simple
1 3 5
path from one of these vertices to the other. 3
─ Rooted trees: The above property makes it possible to select an
2
arbitrary vertex in a free tree and consider it4as the1root of
5
the so 4
called rooted tree.
─ Levels in a rooted tree. 2
Forest
Rooted Trees (I)
Ancestors/predecessors
For any vertex v in a tree T, all the vertices on the simple path from the root to
that vertex are called ancestors.
Descendants/children
rooted
All the vertices for which a vertex v is an ancestor are said to be descendants of
v. 3
Leaves
A vertex without children is called a leaf.
Subtree
A vertex v with all its descendants is called the subtree of T rooted at v.
Rooted Trees (II)
Depth of a vertex
The length of the simple path from the root to the vertex.
Height of a tree
The length of the longest simple path from the root to a
leaf.
h=2
3
4 1 5
2
Ordered Trees
Ordered trees
An ordered tree is a rooted tree in which all the children of each vertex
are ordered.
Binary trees
A binary tree is an ordered tree in which every vertex has no more than
two children and each children is designated either a left child or a right
child of its parent. 9
3 9
2 5 8
Topics To be covered in next class