Professional Documents
Culture Documents
05/20/16
PDM MCA
PDM MCA
PDM MCA
PDM MCA
ALGORITHM
Algorithm Analysis
The complexity of an algorithm is a function g(n) that gives the
upper bound of the number of operation (or running time)
performed by an algorithm when the input size is n.
There are two interpretations of upper bound.
Worst-case Complexity
The running time for any given size input will be lower than the
upper bound except possibly for some values of the input where
the maximum is reached.
Average-case Complexity
The running time for any given size input will be the average
number of operations over all problem instances for a given size.
PDM MCA
Algorithm Analysis
Because, it is quite difficult to estimate the statistical behavior
of the input, most of the time we content ourselves to a worst
case behavior. Most of the time, the complexity of g(n) is
approximated by its family o(f(n)) where f(n) is one of the
following functions. n (linear complexity), log n (logarithmic
complexity), na where a 2 (polynomial complexity), an
(exponential complexity).
Optimality
Once the complexity of an algorithm has been estimated, the
question arises whether this algorithm is optimal. An algorithm
PDM MCA
Algorithm Analysis
for a given problem is optimal if its complexity reaches the
lower bound over all the algorithms solving this problem. For
example, any algorithm solving the intersection of n segments
problem will execute at least n2 operations in the worst case
even if it does nothing but print the output. This is abbreviated
by saying that the problem has (n2) complexity. If one finds an
O(n2) algorithm that solve this problem, it will be optimal and of
complexity (n2).
Reduction
Another technique for estimating the complexity of a problem is
the transformation of problems, also called problem reduction.
As an example, suppose we know a lower bound for
PDM MCA
Algorithm Analysis
a problem A, and that we would like to estimate a lower bound
for a problem B. If we can transform A into B by a
transformation step whose cost is less than that for solving A,
then B has the same bound as A.
PDM MCA
Complexity
measurement
space complexity is also important: This is essentially the
number of memory cells which an algorithm needs
Time complexity of an algorithm quantifies the amount of time
taken by an algorithm to run
PDM MCA
10
Big O Notation
O notation approximates the cost function of an algorithm
The approximation is usually good enough, especially when
considering the efficiency of algorithm as n gets very large
Allows us to estimate rate of function growth
Instead of computing the entire cost function we only need to count
the number of times that an algorithm executes its barometer
instruction(s)
The instruction that is executed the most number of times in an
algorithm (the highest order term)
PDM IT
11/320
Big O Notation
The cost function of an algorithm A, tA(n), can be approximated by
another, simpler, function g(n) which is also a function with only 1
variable, the data size n.
The function g(n) is selected such that it represents an upper bound
on the efficiency of the algorithm A (i.e. an upper bound on the
value of tA(n)).
This is expressed using the big-O notation: O(g(n)).
For example, if we consider the time efficiency of algorithm A then
tA(n) is O(g(n)) would mean that
A cannot take more time than O(g(n)) to execute or that
(more than c.g(n) for some constant c)
the cost function tA(n) grows at most as fast as g(n)
PDM IT
12/320
13
Bottom up approach
A bottom-up approach is the piecing together of systems to
give rise to grander systems, thus making the original systems
sub-systems of the emergent system. Bottom-up processing is
a type of information processing based on incoming data from
the environment to form a perception. Information enters the
eyes in one direction (input), and is then turned into an image
by the brain that can be interpreted and recognized as a
perception (output). In a bottom-up approach the individual
base elements of the system are first specified in great detail
PDM MCA
14
String processing
String : A finite sequence S of zero or more
characters is called string.
String with zero characters is called empty
string.
String will be denoted by enclosing their
characters in single quotation marks.
E.g. TO BE
05/20/16
PDM MCA
15
Storing Strings
Strings are stored in 3 types of structures
1. Fixed Length Structure
2. Variable Length Structure with fixed
maximum
1. Linked Structures.
05/20/16
PDM MCA
16
String Operations
1.
SUBSTRING(string,initial,length)
2.Indexing : Also called pattern matching i.e. position where a string
pattern P first appears in agiven string T.
INDEX(text,pattern)
3. Concatenation : string consists of characters of first string followed by
characters of 2nd string.
S1//S2
4. Length : the number of characters in a string.
LENGTH(string)
05/20/16
PDM MCA
17
Word Processing
Following operations are there:
Replacement : replacing one string in the text by
another.
REPLACE(text,pattern1,pattern2)
PDM MCA
18
Arrays
Array - a collection of a fixed number of components
wherein all of the components have the same data type
One-dimensional array - an array in which the components
are arranged in a list form
The general form of declaring a one-dimensional array is:
dataType arrayName[intExp];
where intExp is any expression that evaluates to a
positive integer
Declaring an array
The statement
int num[5];
declares an array num of 5 components of the
type int
The components are num[0], num[1],
num[2], num[3], and num[4]
Accessing Array
Components
The general form (syntax) of accessing an array component is:
arrayName[indexExp]
where indexExp, called index, is any expression whose
value is a nonnegative integer
Index value specifies the position of the component in the array
The [] operator is called the array subscripting operator
The array index always starts at 0
05/20/16
PDM MCA
23
Linked List
PDM IT
24/320
Definition of Linked
Lists
A linked list is a sequence of items (objects) where every item is
linked to the next.
Graphically:
data
data
data
data
tail_ptr
head_ptr
PDM IT
25/320
Definition Details
Each item has a data part (one or more data members), and a
link that points to the next item
One natural way to implement the link is as a pointer; that is, the
link is the address of the next item in the list
It makes good sense to view each item as an object, that is, as an
instance of a class.
We call that class: Node
The last item does not point to anything. We set its link member
to NULL. This is denoted graphically by a self-loop
PDM IT
26/320
John
Mary
Dan
Sue
tail_ptr
head_ptr
PDM IT
27/320
node
pointer to a
next node
pointer to
an element
28/320
node
pointer to a
next node
pointer to
an element
29/320
node
pointer to a
next node
pointer to
an element
30/320
node
pointer to a
next node
pointer to
an element
31/320
32/320
33/320
Operations on Linked
Lists
PDM IT
34/320
data
newPtr
data
data
head_ptr
tail_ptr
data
data
data
data
tail_ptr
head_ptr
The link value in the new item = old head_ptr
The new value of head_ptr = newPtr
PDM IT
35/320
data
newPtr
data
data
head_ptr
tail_ptr
data
data
data
A
tail_ptr
head_ptr
The link value in the new item = NULL
The link value of the old last item = newPtr
PDM IT
36/320
data
data
newPtr
data
data
head_ptr
tail_ptr
data
data
data
tail_ptr
head_ptr
The link-value in the new item = link-value of 2nd item
The new link-value of 2nd item = newPtr
PDM IT
37/320
data
data
data
data
tail_ptr
head_ptr
data
data
data
head_ptr
data
tail_ptr
38/320
data
data
data
data
tail_ptr
head_ptr
data
data
data
data
tail_ptr
head_ptr
39/320
data
data
data
data
tail_ptr
head_ptr
data
data
data
data
tail_ptr
head_ptr
New link-value of the item located before the deleted one =
the link-value of the deleted item
PDM IT
40/320
Implementation of
Linked List
A linked list is a collection of Node objects, and must support a
number of operations
Therefore, it is sensible to implement a linked list as a class
The class name for it is List
PDM IT
41/320
PDM IT
42/320
Contd.
PDM IT
43/320
Contd.
PDM IT
44/320
Contd.
PDM IT
45/320
46/320
PDM IT
47/320
Contd.
PDM IT
48/320
Implementation
PDM IT
49/320
Stacks
A stack is a list of elements in which an
element may be inserted or deleted only at one
end, called the top of the stack.
The elements are removed from a stack in the
reverse order of that in which they were
inserted into the stack.
Stack is also known as a LIFO (Last in Fast
out) list or Push down list.
Stack Operations
PUSH (STACK, TOP, MAXSTR, ITEM): This procedure pushes
an ITEM onto a stack
1. If TOP = MAXSIZE, then Print: OVERFLOW, and Return.
2. Set TOP := TOP + 1 [Increases TOP by 1]
3. Set STACK [TOP] := ITEM. [Insert ITEM in TOP position]
4. Return
POP (STACK, TOP, ITEM): This procedure deletes the top
element of STACK and assign it to the variable ITEM
1. If TOP = 0, then Print: UNDERFLOW, and Return.
2. Set ITEM := STACK[TOP]
3. Set TOP := TOP - 1 [Decreases TOP by 1]
4. Return
Applications of Stack
Converting algebraic expressions from one
form to another. E.g. Infix to Postfix, Infix to
Prefix, Prefix to Infix, Prefix to Postfix,
Postfix to Infix and Postfix to prefix.
Evaluation of Postfix expression.
Parenthesis Balancing in Compilers.
Depth First Search Traversal of Graph.
Recursive Applications.
Algebraic Expressions
Infix: It is the form of an arithmetic expression in which
we fix (place) the arithmetic operator in between the two
operands. E.g.: (A + B) * (C - D)
Prefix: It is the form of an arithmetic notation in which
we fix (place) the arithmetic operator before (pre) its
two operands. The prefix notation is called as polish
notation. E.g.: * + A B C D
Postfix: It is the form of an arithmetic expression in
which we fix (place) the arithmetic operator after (post)
its two operands. The postfix notation is called as suffix
notation and is also referred to reverse polish notation.
E.g: A B + C D - *
Evaluation of Postfix
Expression
Postfix expression: 6 5 2 3 + 8 * + 3 + *
Queue
A queue is a data structure where items are
inserted at one end called the rear and deleted
at the other end called the front.
Another name for a queue is a FIFO or
First-in-first-out list.
Operations of a Queue:
enqueue: which inserts an element at the end of
the queue.
dequeue: which deletes an element at the front of
the queue.
Representation of
Queue
Representation of
Queue
Now, delete an element 11.
Next insert another element, say 66 to the queue. We cannot insert 66 to the
queue as it signals queue is full. The queue status is as follows:
Applications of
Queue
It is used to schedule the jobs to be processed
by the CPU.
When multiple users send print jobs to a
printer, each printing job is kept in the printing
queue. Then the printer prints those jobs
according to first in first out (FIFO) basis.
Breadth first search uses a queue data structure
to find an element from a graph.
Circular Queue
A circular queue is one in which the insertion
of new element is done at the very first
location of the queue if the last location of the
queue is full.
Suppose if we have a Queue of n elements
then after adding the element at the last index
i.e. (n-1)th , as queue is starting with 0 index,
the next element will be inserted at the very
first location of the queue which was not
possible in the simple linear queue.
Circular Queue
operations
Circular Queue
Representation using
Arrays
Let us consider a circular queue, which can hold maximum (MAX) of six
elements. Initially the queue is empty.
Now, delete two elements 11, 22 from the circular queue. The circular queue
status is as follows:
Again, insert 77 and 88 to the circular queue. The status of the Circular queue
is:
DEQUE Representation
using arrays
Types of DEQUE
There are two variations of deque. They are:
Input restricted deque (IRD)
Output restricted deque (ORD)
Priority Queue
A priority queue is a collection of elements
that each element has been assigned a priority
and such that order in which elements are
deleted and processed comes from the
following riles:
An element of higher priority is processed before
any element of lower priority.
Two element with the same priority are processed
according to the order in which they were added to
the queue.
Edge
Root
A distinguished node, depicted at the top of the tree
PDM IT
74/320
Contd.
1. A single node, with no edges, is a tree. The
root of the tree is its unique node.
2. Let T1, , Tk (k >=1) be trees with no nodes
in common, and let r1, ,rk be the roots of
those trees, respectively. Let r be a new node.
Then there is a tree T consisting of the nodes
and edges of T1, , Tk , the new node r and
the edges <r,r1>,,<r,rk>. The root of T is r
and T1, ,Tk are called the subtrees of T.
PDM IT
75/320
Contd.
r is called the parent of r1, , rk
r1, , rk are the children of r
r1, , rk are the siblings of one another
Node v is a descendant of u if
u=v
or
v is a descendant of a child of u
A path exists from u to v.
PDM IT
76/320
Tree Terminology
Every node is a descendant of itself.
If v is a descendant of u, then u is an ancestor
of v.
Proper descendants:
The descendants of a node, other than the node
itself.
Proper ancestors
The ancestors of a node, other than the node itself.
PDM IT
77/320
properties
Leaf
a node with no children
Num_nodes = Num_edges + 1
Every tree has exactly one more node than edge
Every edge, except the root, is the head of exactly
one of the edges
PDM IT
78/320
Tree Terminology
Height of a node in a tree
Length of the longest path from that node to a leaf
PDM IT
79/320
Tree terminology
w has depth 2
u has height 3
tree has height 4
PDM IT
80/320
Trees
A tree is a collection of nodes
The collection can be empty
(recursive definition) If not empty, a tree consists
of a distinguished node r (the root), and zero or
more nonempty subtrees T1, T2, ...., Tk, each of
whose roots are connected by a directed edge from
r
PDM IT
81/320
Some Terminologies
Leaves
Nodes with no children
Sibling
nodes with same parent
PDM IT
82/320
Some Terminologies
Path
Length-number of edges on the path
Depth of a node
length of the unique path from the root to that node
The depth of a tree is equal to the depth of the deepest leaf
Height of a node
length of the longest path from that node to a leaf
all leaves are at height 0
The height of a tree is equal to the height of the root
83/320
Example: UNIX
Directory
PDM IT
84/320
Binary Trees
PDM IT
85/320
Contd.
An ordered tree with at most two children for
each node
If node has two child, the 1st is called the left
child, the 2nd is called the right child
If only one child, it is either the right child or
the left child
PDM IT
86/320
Example: Expression
Trees
87/320
Tree traversal
Used to print out the data in a tree in a certain
order
Pre-order traversal
Print the data at the root
Recursively print out all data in the left subtree
Recursively print out all data in the right subtree
PDM IT
88/320
Preorder, Postorder
and Inorder
Preorder traversal
node, left, right
prefix expression
++a*bc*+*defg
PDM IT
89/320
Preorder, Postorder
and Inorder
Postorder traversal
left, right, node
postfix expression
abc*+de*f+g*+
Inorder traversal
left, node, right.
infix expression
a+b*c+d*e+f*g
PDM IT
90/320
Binary Trees
Possible operations on the Binary Tree ADT
parent
left_child, right_child
sibling
root, etc
Implementation
Because a binary tree has at most two children, we can keep direct
pointers to them
PDM IT
91/320
Graph
A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)
Graph
Treesterminology
vs graphs
Trees are special cases of graphs!!
Graph terminology
Adjacent nodes: two nodes are adjacent if they are
connected by an edge
5 is adjacent to 7
7 is adjacent from 5
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: All edges of a path are distinct.
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.
1-97
Graph
terminology
Graph
terminology (cont.)
What is the number of edges in a complete
directed graph with N vertices?
N * (N-1)
O( N )
Graph
terminology
Graph
terminology (cont.)
What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2
O( N )
Graph
terminology
Graph terminology (cont.)
Weighted graph: a graph in which each edge
carries a value
Graph implementation
Array-based implementation
A 1D array is used to represent the vertices
A 2D array (adjacency matrix) is used to
represent the edges
Array-based
implementation
Graph implementation
(cont.)
Linked-list implementation
A 1D array is used to represent the vertices
A list is used for each vertex v which contains the
vertices which are adjacent from v (adjacency list)
Linked-list
implementation
Adjacency list
Good for sparse graphs -- |E|~O(|V|)
Memory requirements: O(|V| + |E|)=O(|V|)
Vertices adjacent to another vertex can be found
quickly