ppt_dsa
ppt_dsa
• Abstract Data type (ADT) is a type (or class) for objects whose
behaviour is defined by a set of value and a set of operations.
• The definition of ADT only mentions what operations are to be
performed but not how these operations will be implemented.
ADT Diagram
Array
TREE STRUCTURES
Need for non-linear structures
– Trees and its representation –
Binary Tree – expression trees –
Binary tree traversals – left
child right sibling data
structures for general trees –
applications of trees – Huffman
Algorithm - Binary search tree.
What Is a Tree?
root
node edge
2 3 4 5 6
7 8 9 10 11 12
2 3 4 5 6
7 8 9 10 11 12
13
2 3 4 5 6
7 8 9 10 11 12
13
level 0
level 1
depth = 2 level 2
Binary Trees
• In a binary tree, nodes have at most two children.
• Recursive definition: a binary tree is either:
1) empty, or
2) a node (the root of the tree) that has
• one or more data items
• a left child, which is itself the root of a binary tree
• a right child, which is itself the root of a binary tree
• Example:
26
4 18 38
} root
26 32
12
null
12 32
4 18 38
18 38
4 null null null null null
7
7
null null
• see ~cscie119/examples/trees/LinkedTree.java
4 is the root of 4 18 38
12’s left subtree
7
Preorder Traversal
•preorder traversal of the tree whose root is N:
1) visit the root, N
2) recursively perform a preorder traversal of N’s left subtree
3) recursively perform a preorder traversal of N’s right subtree
5 9
2 6 8
root: 4
print 4
Postorder Traversal
•postorder traversal of the tree whose root is N:
1) recursively perform a postorder traversal of N’s left subtree
2) recursively perform a postorder traversal of N’s right subtree
3) visit the root, N
5 9
2 6 8
•Note that the root is printed after the two recursive calls.
4
root: 4
print 4
time
Inorder Traversal
•inorder traversal of the tree whose root is N:
1) recursively perform an inorder traversal of N’s left subtree
2) visit the root, N
3) recursively perform an inorder traversal of N’s right subtree
5 9
2 6 8
root: 4
print 4
print 5 ...
root: 7 root: 7 root: 7 root: 7 root: 7 root: 7 root: 7
time
Level-Order Traversal
•Visit the nodes one level at a time, from top to
bottom and left to right.
5 9
2 6 8
5 13
3 8 10
2 6 15 26 18
+ /
a * d e
b c
char isdec
example: “bat” binary
stored in a text file as the following sequence of bits:
a
01100010 01100001 97 01110100
01100001
b 98 01100010
c 99 01100011
… … …
• Unicode uses 16 bits per character to accommodate foreign-
language characters. (ASCII codes are a subset.)
•Fixed-length encodings are simple, because
• all character encodings have the same length
• a given character always has the same encoding
Variable-Length Character Encodings
• Problem: fixed-length encodings waste space.
• Solution: use a variable-length encoding.
• use encodings of different lengths for different characters
• assign shorter encodings to frequently occurring characters
• Example: e 01 “test” would be encoded as
o 100 00 01 111 00 000111100
s 111
t 00
• Challenge: when decoding/decompressing an encoded document,
how do we determine the boundaries between characters?
• example: for the above encoding, how do we know whether the
next character is 2 bits or 3 bits?
• One requirement: no character’s encoding can be the prefix of another
character’s encoding (e.g., couldn’t have 00 and 001).
Huffman Encoding
•Huffman encoding is a type of variable-length encoding that is based
on the actual character frequencies in a given document.
•Huffman encoding uses a binary tree:
• to determine the encoding of each character
• to decode an encoded file – i.e., to decompress a compressed
file, putting it back into ASCII
•Example of a Huffman tree (for a text with only six chars):
Leaf nodes are characters.
0 1
Left branches are labeled with
a 0, and right branches are
0 1 0 1 labeled with a 1.
t e If you follow a path from root
0 1 0 1 to leaf, you get the encoding
a s of the character in the leaf
o i
example: 101 = ‘i’
Building a Huffman Tree
1)Begin by reading through the text to determine
the frequencies.
2)Create a list of nodes that contain (character,
frequency) pairs for each character that appears in
‘o’ ‘e’
text. ‘a’
the‘i’ ‘s’ ‘t’
21 23 25 26 27 40
‘o’ ‘i’
21 23
5) Repeat steps 3 and 4 until there is only a single node in the list,
which will be the root of the Huffman tree.
Completing the Huffman Tree Example I
• Merge the two remaining nodes with the lowest frequencies:
‘a’ ‘s’ ‘t’ ‘e’ -
25 26 27 40 44
‘o’ ‘i’
21 23
‘t’ ‘e’ - -
27 40 44 51
- - -
44 51 67
- -
67 95
‘t’ ‘e’ - -
27 40 44 51
- -
0 1 0 1
67 95
t e
0 1 0 1
‘t’ ‘e’ - -
27 40 o i a s
44 51
0 1 a ?
e ?
1 i 101
0 1 0
o 100
t e
0 1 s 111
0 1
t 00
o a s
4) Read through the inputi file a second time, and write the
Huffman code for each character to the output file.
< 26 12 32
12 26
4 18 38
< 12
7
26
12 32
4 18 38
7
Implementing Binary-Tree Search
public class LinkedTree {// private Nodekeys
Nodes have root;
that are ints
…
public LLList search(int key) { Node n =
searchTree(root, key);
return (n == null ? null : n.data);
}
private static Node searchTree(Node root, int key) {
// write together
}
}
•If we find a node that has the specified key, we return its data
field, which holds a list of the data items for that key.
4 18 38
}
Node newNode = new Node(key, data);
if (parent == null) // the tree was empty
root = newNode;
else if (key < parent.key)
parent.left = newNode; else
parent.right = newNode;
}
Deleting Items from a Binary Search Tree
• Three cases for deleting a node x
• Case 1: x has no children.
Remove x from the tree by setting its parent’s reference to null.
26 26
ex: delete 4 12 32 12 32
4 18 38 18 38
ex: delete 12 12 32 18 32
18 38 38
26
12 32
4 18 38
7 35
Deleting Items from a Binary Search Tree (cont.)
•Case 3: x has two children (continued):
• replace x with the smallest node in x’s right subtree— call it y
– y will either be a leaf node or will have one right child. why?
18 45 18 45 18 45
30 y 30 y 35
35 35
trav = trav.right; 4 18 38
}
// Delete the node (if any) and return the removed items.
if (trav == null) // no such key
return null;
else {
LLList removedData = trav.data; deleteNode(trav, parent);
return removedData;
}
}
•This method uses a helper method to delete the node.
Implementing Case 3
private void deleteNode(Node toDelete,
Node parent) {
if (toDelete.left != null &&
toDelete.right != null) {
// Find a replacement – and
toDelete
// the replacement's parent. Node
// Get the smallest item = toDelete;
replaceParent
// in the right subtree. 26
Node replace = toDelete.right;
// What should go here?
18 45
30
// Replace toDelete's key and data
// with those of the replacement item. 35
toDelete.key = replace.key;
toDelete.data = replace.data;
// Recursively delete the replacement
// item's old node. It has at most one
// child, so we don't have to
// worry about infinite recursion.
deleteNode(replace, replaceParent);
} else {
...
}
Balanced Trees
•A tree is balanced if, for each node,
the node’s subtrees have the same
height or have heights that differ by
1. n nodes:
• For a balanced tree with
26
• height = O(log2n).
12 32
• gives a worst-case time complexity
that is logarithmic (O(log2n)) 4 30 38
• the best worst-case time complexity for
a binary tree
What If the Tree Isn't Balanced?
• Extreme case: the tree is equivalent to a linked list
• height = n - 1
4
• worst-case
time complexity = O(n) 12
36
38
UNIT-III
Graphs
Definitions – Representation of graph -
GRAPH TERMINOLOGY
Undirected Graphs
In undirected graphs, edges have no specific
direction D
Edges are always "two-way" A
C
2 edges here
Minimum? V = {A, B, C, D}
Maximum for undirected? E = {(C, B), (A, B),
(B, A), (C, D)}
Maximum for directed?
Minimum? 0
Maximum for undirected? |V||V+1|/2 O(|V|2)
Maximum for directed? |V|2 O(|V|2)
Kingston 30 Edmonds
Bainbridge 35
Seattle
60
Bremerton
Examples Again
What, if anything, might weights represent for
each of these?
Do negative weights make sense?
San Francisco
Dallas
Example path (that also happens to be a cycle):
[Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle]
Path Length and Cost
Path length: Number of edges in a path
Path cost: Sum of the weights of each edge
Example where
P= [ Seattle, Salt Lake City, Chicago, Dallas,
San Francisco, Seattle]
3.5 Chicago
Seattle
2 2 length(P) = 5
cost(P) = 11.5
2 Salt Lake City
2.5
2.5 2.5 Length is sometimes
called "unweighted cost"
3
San Francisco Dallas
Simple Paths and Cycles
A simple path repeats no vertices (except the
first might be the last):
[Seattle, Salt Lake City, San Francisco, Dallas]
[Seattle, Salt Lake City, San Francisco, Dallas, Seattle]
C
A
undirected B
acyclic
A
connected
C
All trees are graphs, but NOT all
F
graphs are trees
G H
How does this relate to the trees
we know and "love"?
Rooted Trees
We are more accustomed to rooted trees where:
We identify a unique root
We think of edges as directed: parent to
children
D E
Picking a root gives a unique B
rooted tree A
A
The tree is simply drawn
B C
differently and with C
undirected edges F D E F
G H G H
Rooted Trees
We are more accustomed to rooted trees where:
We identify a unique root
We think of edges as directed: parent to
children
D E
Picking a root gives a unique F
B
rooted tree
A G H C
The tree is simply drawn
differently and with C A
undirected edges F B
G H D E
Directed Acyclic Graphs (DAGs)
A DAG is a directed graph with no directed cycles
Every rooted directed tree is a DAG
But not every DAG is a rooted directed tree
Another fact:
If an undirected graph is connected,
then |E| ≥ |V|-1 (pigeonhole principle)
Density / Sparsity
|E| is often much smaller than its maximum size
D A B C D
A A F T F F
C
B T F F F
B
C F T F T
D F F F F
Adjacency Matrix Properties
Running time to:
Get a vertex’s out-edges:
Get a vertex’s in-edges:
Decide if some edge exists:
Insert an edge:
A B C D
Delete an edge:
A F T F F
Space requirements: B T F F F
C F T F T
D F F F F
Best for sparse or dense graphs?
Adjacency Matrix Properties
Running time to:
Get a vertex’s out-edges: O(|V|)
Get a vertex’s in-edges: O(|V|)
Decide if some edge exists: O(1)
Insert an edge: O(1)
A B C D
Delete an edge: O(1)
A F T F F
B T F F F
Space requirements:
O(|V|2) C F T F T
D F F F F
Best for sparse or dense graphs? dense
Adjacency Matrix Properties
How will the adjacency matrix vary for an
undirected graph?
Will be symmetric about diagonal axis
Matrix: Could we save space by using only
about half the array? A B C D
A F T F F
B T F F F
C F T F T
D F F T F
D
A B /
A
C
B A /
B
C D B /
D /
Adjacency List Properties
Running time to:
Get a vertex’s out-edges: A B /
Insert an edge: D /
Delete an edge:
Space requirements:
A B /
D
A B C / A /
C
B C D B /
D / C /
Which is better?
Graphs are often sparse
Streets form grids
Airlines rarely fly to all cities
APPLICATIONS OF
GRAPHS: TRAVERSALS
Application: Moving Around WA State
Related Problems:
Is an undirected graph connected?
Is a digraph weakly/strongly connected?
For strongly, need a cycle back to starting node
Graph Traversals
Basic Algorithm for Traversals:
Select a starting node
Make a set of nodes adjacent to current node
Visit each node in the set but "mark" each
nodes after visiting them so you don't revisit
them (and eventually stop)
Repeat above but skip "marked nodes"
In Rough Code Form
traverseGraph(Node start) {
Set pending = emptySet();
pending.add(start)
mark start as visited
while(pending is not empty) {
next = pending.remove()
for each node u adjacent to next
if(u is not marked) {
mark u
pending.add(u)
}
}
}
}
Running Time and Options
Assuming add and remove are O(1), entire
traversal is O(|E|) if using an adjacency list
Order processed: A, B, D, E, C, F, G, H
This is a "pre-order traversal" for trees
The marking is unneeded here but because we
support arbitrary graphs, we need a means to
process each node exactly once
DFS with Stack, Example with Tree
DFS2(Node start) {
initialize stack s to hold start
A mark start as visited
while(s is not empty) {
B C next = s.pop() // and "process"
for each node u adjacent to next
D E F if(u is not marked)
mark u and push onto s
G H
}
}
Order processed: A, C, F, H, G, B, E, D
A different order but still a perfectly fine
traversal of the graph
BFS with Queue, Example with Tree
BFS(Node start) {
initialize queue q to hold start
A mark start as visited
while(q is not empty) {
B C next = q.dequeue() // and "process"
for each node u adjacent to next
D E F if(u is not marked)
mark u and enqueue onto q
G H
}
}
Order processed: A, B, C, D, E, F, G, H
A "level-order" traversal
DFS/BFS Comparison
BFS always finds the shortest path (or
"optimal solution") from the starting node
to a target node
Storage for BFS can be extremely large
A k-nary tree of height h could result in a queue
size of kh
Performance:
Like BFS, IDFS finds shortest paths
Like DFS, IDFS uses less space
Some work is repeated but minor
compared to space savings
Saving the Path
Our graph traversals can answer the standard
reachability question:
"Is there a path from node x to node y?"
Easy:
Store the previous node along the path:
When processing u causes us to add v to the
search, set v.path field to be u)
When you reach the goal, follow path fields back to
where you started (and then reverse the answer)
What's an easy way to do the reversal? A Stack!!
Example using BFS
What is a path from Seattle to Austin?
Remember marked nodes are not re-enqueued
Note shortest paths may not be unique
0 1 Chicago
Seattle
Austin
1
3
San Francisco
2
Dallas
Topological Sort
Problem: Given a DAG G=(V, E), output all the
vertices in order such that if no vertex appears
before any other vertex that has an edge to it
CSE 332
…
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
CSE 332
…
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed?
In-deg:
Example
Output:
CSE 332
…
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed?
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
Example
Output:
126
CSE 331 CSE 440
CSE 332
…
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1
Example
Output:
126
CSE 331 CSE 440 142
CSE 332
…
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1
0
Example
Output:
126
CSE 331 CSE 440 142
143
CSE 332
…
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 0 0 0
0
Example
Output:
126
CSE 331 CSE 440 142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 0 0
0
Example
Output:
126
CSE 331 CSE 440 142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 0 0
0
Example
Output:
126
CSE 331 CSE 440 142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
MATH CSE 341
CSE 312 332
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0
0 0
Example
Output:
126
CSE 331 CSE 440 142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
MATH CSE 341
CSE 312 332
126 312
CSE 351 CSE 333
CSE 352
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0
0 0
Example
Output:
126
CSE 331 CSE 440 142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
MATH CSE 341
CSE 312 332
126 312
CSE 351 CSE 333 341
CSE 352
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0
0 0
Example
Output:
126
CSE 331 CSE 440 142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
MATH CSE 341
CSE 312 332
126 312
CSE 351 CSE 333 341
CSE 352 351
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0 0
Example
Output:
126
CSE 331 CSE 440 142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
MATH CSE 341
CSE 312 332
126 312
CSE 351 CSE 333 341
CSE 352 351
333
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0 0
Example
Output:
126 352
CSE 331 CSE 440 142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
MATH CSE 341
CSE 312 332
126 312
CSE 351 CSE 333 341
CSE 352 351
333
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0 0
Example
Output:
126 352
CSE 331 CSE 440 142 440
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
MATH CSE 341
CSE 312 332
126 312
CSE 351 CSE 333 341
CSE 352 351
333
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0 0
Running Time?
labelEachVertexWithItsInDegree();
Using a queue:
Label each vertex with its in-degree,
Enqueue all 0-degree nodes
While queue is not empty
v = dequeue()
Output v and remove it from the graph
For each vertex u adjacent to v, decrement the in-degree
of u and if new degree is 0, enqueue it
Running Time?
labelAllWithIndegreesAndEnqueueZeros();
for(i=0; i < numVertices; i++) {
v = dequeue();
put v next in output
for each w adjacent to v {
w.indegree--;
if(w.indegree==0)
enqueue(w);
}
}
Introduction to Algorithms
Introduction – Notion of Algorithm –
Fundamentals of Algorithmic problem
solving – Important problem types –
Mathematical analysis for recursive & non
recursive algorithms – Brute Fore –
Selection Sort – Bubble Sort.
L1.132
What is course about?
The theoretical study of design and
analysis of computer algorithms
Basic goals for an algorithm:
• always correct
• always terminates
• This class: performance
• Performance often draws the line between
what is possible and what is impossible.
L1.133
Design and Analysis of Algorithms
L1.134
The problem of sorting
Example:
Input: 8 2 4 9 3 6
Output: 2 3 4 6 8 9
L1.136
Insertion sort
INSERTION-SORT (A, n) ⊳ A[1 . . n]
for j ← 2 to n
do key ← A[ j]
i←j–1
“pseudocode” while i > 0 and A[i] > key
do A[i+1] ← A[i]
i←i–1
A[i+1] = key
1 i j n
A:
key
sorted
L1.137
Example of insertion sort
8 2 4 9 3 6
L1.138
Example of insertion sort
8 2 4 9 3 6
L1.139
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
L1.140
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
L1.141
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
L1.142
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
L1.143
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
L1.144
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
L1.145
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
L1.146
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
L1.147
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
2 3 4 6 8 9 done
L1.148
Running time
BIG IDEAS:
• Ignore machine dependent constants,
otherwise impossible to verify and to compare algorithms
“Asymptotic Analysis”
L1.151
-notation
DEF:
(g(n)) = { f (n) : there exist positive constants c1, c2, and
n0 such that 0 c1 g(n) f (n) c2 g(n)
for all n n0 }
Basic manipulations:
• Drop low-order terms; ignore leading constants.
• Example: 3n3 + 90n2 – 5n + 6046 = (n3)
L1.152
Asymptotic performance
When n gets large enough, a (n2) algorithm
always beats a (n3) algorithm.
.
• Asymptotic analysis is a
useful tool to help to
structure our thinking
toward better algorithm
• We shouldn’t ignore
T(n) asymptotically slower
algorithms, however.
• Real-world design
n0 situations often call for a
n
careful balancing L1.153
Insertion sort analysis
Worst case: Input reverse sorted.
n
T ( n) ( j ) n 2 [arithmetic series]
j 2
Average case: All permutations equally likely.
n
T ( n) ( j / 2) n 2
j 2
Is insertion sort a fast sorting algorithm?
• Moderately so, for small n.
• Not at all, for large n.
L1.154
Example 2: Integer
Multiplication
L1.155
Better Integer Multiplication
L1.156
Example 3:Merge sort
MERGE-SORT A[1 . . n]
1. If n = 1, done.
2. Recursively sort A[ 1 . . n/2 ]
and A[ n/2+1 . . n ] .
3. “Merge” the 2 sorted lists.
L1.157
Merging two sorted arrays
20 12
13 11
7 9
2 1
L1.158
Merging two sorted arrays
20 12
13 11
7 9
2 1
L1.159
Merging two sorted arrays
20 12 20 12
13 11 13 11
7 9 7 9
2 1 2
L1.160
Merging two sorted arrays
20 12 20 12
13 11 13 11
7 9 7 9
2 1 2
1 2
L1.161
Merging two sorted arrays
20 12 20 12 20 12
13 11 13 11 13 11
7 9 7 9 7 9
2 1 2
1 2
L1.162
Merging two sorted arrays
20 12 20 12 20 12
13 11 13 11 13 11
7 9 7 9 7 9
2 1 2
1 2 7
L1.163
Merging two sorted arrays
20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2
1 2 7
L1.164
Merging two sorted arrays
20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2
1 2 7 9
L1.165
Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2
1 2 7 9
L1.166
Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2
1 2 7 9 11
L1.167
Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11 13
7 9 7 9 7 9 9
2 1 2
1 2 7 9 11
L1.168
Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11 13
7 9 7 9 7 9 9
2 1 2
1 2 7 9 11 12
L1.169
Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11 13
7 9 7 9 7 9 9
2 1 2
1 2 7 9 11 12
L1.171
Recurrence for merge sort
(1) if n = 1;
T(n) =
2T(n/2) + (n) if n > 1.
• We shall usually omit stating the base
case when T(n) = (1) for sufficiently
small n, but only when it has no effect on
the asymptotic solution to the recurrence.
• Lecture 2 provides several ways to find a
good upper bound on T(n).
L1.172
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
L1.173
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
T(n)
L1.174
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
T(n/2) T(n/2)
L1.175
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/2 cn/2
L1.176
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/2 cn/2
(1)
L1.177
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/2 cn/2
h = lg n cn/4 cn/4 cn/4 cn/4
(1)
L1.178
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2
h = lg n cn/4 cn/4 cn/4 cn/4
(1)
L1.179
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4
(1)
L1.180
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4 cn
…
(1)
L1.181
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4 cn
…
(1) #leaves = n (n)
L1.182
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4 cn
…
(1) #leaves = n (n)
Total (n lg n)
L1.183
Conclusions
L1.184
ALGORITHM ANALYSIS
AND DESIGN
INTRODUCTION
18
Algorithm Analysis – Asymptotic Notations - Divide
and Conquer – Merge Sort – Binary Search - Greedy
Algorithms – Knapsack Problem – Dynamic
Programming – Warshall’s Algorithm for Finding
Transitive Closure – Backtracking – Sum of Subset
Problem – Branch and Bound – Travelling Salesman
Problem.
18
* Algorithm Analysis and Design
Algorithm
• An algorithm is a step by step procedure for
solving the given problem.
18
* Algorithm Analysis and Design
Definition
18
* Algorithm Analysis and Design
Properties
• Definiteness
• Effectiveness
• Finiteness
18
* Algorithm Analysis and Design
Definiteness
• Each instruction should be clear and
unambiguous.
• It must be perfectly clear what should be done.
• Example Directions which are not permitted
– “add 6 or 7 to x”
– compute x/0
– remainder of m/n when m and n are –ve numbers
• it is not clear which of the 2 possibilities should be
done.
19
* Algorithm Analysis and Design
Definiteness Contd..
• Achievement using programming language
for algorithms
– designed such that each legitimate sentence has
a unique meaning
19
* Algorithm Analysis and Design
Effectiveness
• Each step must be such that it can at least in
principle be done by a person using paper and
pencil in a finite amount of time.
• Example -Effective
– Performing arithmetic on integers
• Example not effective
– Some arithmetic with real numbers.
– if k is the largest integer n such that x^n + y^n = z^n in
all positive integers then go to step 4
19
* Algorithm Analysis and Design
Finiteness
19
* Algorithm Analysis and Design
Example Algorithm
• An algorithm to find the maximum in array
of n numbers
Algorithm max(a,n)
// a is an array of size n
{ result:= a[1];
for i = 2 to n do
{ if (a[i] > result)
then result :=a[i];
}
return result;
}
19
* Algorithm Analysis and Design
Development of an algorithm
• Steps Involved
– Devise
– Validate
– Analyse
– Test
19
* Algorithm Analysis and Design
19
* Algorithm Analysis and Design
General Techniques
19
* Algorithm Analysis and Design
19
* Algorithm Analysis and Design
Example Sorting
Approaches
• Incremental
– Insertion sort
• Divide-and-conquer
– Quick sort
– Merge sort etc.
19
* Algorithm Analysis and Design
20
* Algorithm Analysis and Design
20
* Algorithm Analysis and Design
Analyse algorithms
• This phase performs performance analysis
• Execution requires CPU time and memory
• Task of determining time and space
requirements.
• Performance in cases considered separately
– Best case
– Worst case
– Average case etc.
20
* Algorithm Analysis and Design
Good algorithm
• One which works correctly
– should give the desired output
• Readable
– Steps are clear and understandable
• Efficient in terms of time and memory utilization .
– give the desired output faster
– utilize less resources.
• Given multiple solutions, finding out the better one.
20
* Algorithm Analysis and Design
Performance Measures
• Quality of the solution
– How much time does the solution take?.
– How much space does the solution occupy?
• Simplicity of the solution
• Performance Improvement
– Improving the algorithm design.
– Continuous improvements in hardware and
communication infrastructure
20
* Algorithm Analysis and Design
Testing
• Two phases
– Debugging
– Profiling (performance measurement)
20
* Algorithm Analysis and Design
Debugging
• Process of executing programs on sample
data sets to determine whether faulty results
occur and if so to correct them.
• Requirement
– A proof of correctness prove that the algorithm
will correctly for all cases.
– Not easy to obtain mathematical proofs.
20
* Algorithm Analysis and Design
Profiling
• Executing programs with sample data sets
to measure the time and space requirements.
• It is helpful for optimization since it can
point out logical places that require
optimization.
20
* Algorithm Analysis and Design
20
* Algorithm Analysis and Design
Mathematical Formulae
Contd..
⎡⎤
• Floor(x) or x is the largest integer less
than or equal to x
20
* Algorithm Analysis and Design
Mathematical Formulae
Contd..
loga xy=log a x+loga y
log a(x/y) = loga x- loga y
log a x^n = n log a x
log a 1=0
log x x=1
log a x=1/log x a
loga(x) = loga(b) * logb(x)
log a x= log b x/ log b a
alog a x=x
if 2k=n then k=log 2 n
xlogby=ylogbx
21
* Algorithm Analysis and Design
Mathematical Formulae
Contd..
for n>=k>=0
21
* Algorithm Analysis and Design
Mathematical Formulae
Contd..
• A.P.
– Nth term - a + (n − 1)d
– Sum - Sn =n/2*(2a + (n − 1)d)
• G.P.
– Nth term - ar(n−1)
– Sum - Sn = a(1 − rn)/(1 − r) (valid only if r ‡ 1)
21
* Algorithm Analysis and Design
21
* Algorithm Analysis and Design
21
* Algorithm Analysis and Design
21
* Algorithm Analysis and Design
21
* Algorithm Analysis and Design
21
* Algorithm Analysis and Design
Performance Analysis
21
* Algorithm Analysis and Design
21
* Algorithm Analysis and Design
Apriori Analysis
22
* Algorithm Analysis and Design
22
* Algorithm Analysis and Design
Space Complexity
• Amount of memory the algorithm needs to
run to completion
• Requirements
– Fixed part
– Variable part
• S(P)=c+Sp
22
* Algorithm Analysis and Design
Example Algorithm
Algorithm abc(a,b,c)
{return a+b+b*c+(a-c)/(a+b) +2.0;
}
S(P) = c + Sp
Sp = 0 S(P)>3
22
* Algorithm Analysis and Design
Example Algorithm
Algorithm sum(a,n)
{s = 0.0;
for i:=1 to n do s:=s+a[i];
return s; }
22
* Algorithm Analysis and Design
Example Algorithm
algorithm rsum(a,n)
{ if (n<=0) then return 0.0
else return rsum(a,n-1)+a[n]; }
• Instance characterized by n
• Recursive stack space required at each level
•(n,return address,*a[]) 3 words
• Depth of recursion considering first invocation - (n+1)
• S(n)>=3*(n+1)
22
* Algorithm Analysis and Design
Time Complexity
22
* Algorithm Analysis and Design
Apriori Measures
• Instruction count
– Basic operations
– Basic steps
• Size of input.
22
* Algorithm Analysis and Design
• Initialization instructions
• Loops.
– Number of passes of the loops
• Count the basic operations/steps
22
* Algorithm Analysis and Design
• Search
– Basic operation is compare x and y
• Matrix multiplication
– Basic operation is multiplication of real numbers
22
* Algorithm Analysis and Design
Step in Algorithm
• Step is a meaningful segment of program or
computational unit that is independent of the
instance characteristics
• Example
– 10 or 100 additions can be one step
– 200 multiplications can be another step
– but not n additions
23
* Algorithm Analysis and Design
Example 1
Algorithm sum(a,n)
{s=0.0;
count:=count+1;
for i: =1 to n do
{count:=count+1;
s:=s+a[I];
count:=count+1;}
count:=count+1;
count:count+1;
return s;}
count = 2n+3
23
* Algorithm Analysis and Design
Example 2
algorithm rsum(a,n)
{
count:=count+1;
if (n<=0) then
{ count:=count+1;
return 0.0; }
else
{ count:=count+1;
return rsum(a,n-1)+a[n];} }
Count ?
23
* Algorithm Analysis and Design
23
* Algorithm Analysis and Design
Example 1
23
* Algorithm Analysis and Design
Example 2
23
* Algorithm Analysis and Design
Example 3
s/e freq freq total total
algorithm rsum(a,n) n=0 n>0 n=0 n>0
{if (n<=0) then 1 1 1 1 1
1 1 0 1 0
{ return 0.0; }
else
{return rsum(a,n-1)+a[n]; 1+x 0 1 0 1+x
}} -----------------
2 2+x
23
* Algorithm Analysis and Design
Computing Complexity
X = trsum(n-1)
trsum(n)= { 2 2+ trsum(n-1) if
if
n=0
n>0
23
* Algorithm Analysis and Design
23
* Algorithm Analysis and Design
Complexity - Representation
• when the step count cannot be uniquely
represented, the step count for the best,
worst and average cases can be separately
listed.
23
* Algorithm Analysis and Design
24
* Algorithm Analysis and Design
24
* Algorithm Analysis and Design
• A(n) =
24
* Algorithm Analysis and Design
Computing
Average Case Complexity
• Two steps
– An understanding of the average nature of the
input
– Performing a run-time analysis of the algorithm
for different cases
• Difficult to estimate the statistical behavior
of the input
24
* Algorithm Analysis and Design
Expressing Complexity
• Goodness of an algorithm often expressed in
terms of its worst-case running time.
• Two reasons for this:
– the need for a bound on one’s pessimism
– ease of calculation of worst-case
24
* Algorithm Analysis and Design
Example Algorithm
Algorithm insert(A,i,k)
//To insert k at position i in a[1..n]
{Copy a[i…n-1] to a[i + 1…n]
Copy k to a[i] }
24
* Algorithm Analysis and Design
Algorithm insert(A,i,k)
//To insert k at position i in a[1..n]
{Copy a[i…n-1] to a[i + 1…n] n-1 copies
Copy k to a[i] }
1 copy
24
* Algorithm Analysis and Design
Algorithm insert(A,i,k)
//To insert k at position i in a[1..n]
{Copy a[i…n-1] to a[i + 1…n] 0 copies
Copy k to a[i] }
1 copy
24
* Algorithm Analysis and Design
24
* Algorithm Analysis and Design
Compare Algorithms
• Analysis to get an idea about the fastness
• Exact step count is not required
• Example
• An algorithm with step count 10 n+10 <
100n+10.
• Constant associated is not much relevant
– 100n but it is 40 or 80n.
• c3n < c1n2+c2n
24
* Algorithm Analysis and Design
25
* Algorithm Analysis and Design
Algorithm Analysis
• Precise mathematical analysis is difficult
• Steps to simplify the analysis
– Identify fastest growing term
– Neglect the slow growing terms
– Neglect the constant factor in the fastest growing term
are performed.
• Simplification result
– algorithm’s time complexity.
• Focuses on the growth rate of the algorithm with
respect to the problem size
25
* Algorithm Analysis and Design
25
* Algorithm Analysis and Design
Asymptotic Notations
• Describing complexity
25
* Algorithm Analysis and Design
Upper Bound
• A set of numbers ranging from a to b.
• Upper bound for the set
• Any number greater than or equal to b
– Could be b or b+1, or b+2, …
• Moves closer as the number increases
• No point of time it is greater than upper bound.
25
* Algorithm Analysis and Design
n
0
25
* Algorithm Analysis and Design
O-Notation Contd..
• If f(n)=O(g(n)) →f(n) is at least as good as
g(n).
• Informally O(g(n)) denotes the set of all
functions with a smaller or same order of
growth as g(n).
• n2 belongs O(n3).
25
* Algorithm Analysis and Design
O-Notation Examples
• n2+10n = O(n2)
• 5n3+6 =O(n3)
• 3logn+8 = O(logn)
• n2+10n = O(n3)
25
* Algorithm Analysis and Design
25
* Algorithm Analysis and Design
25
* Algorithm Analysis and Design
Analysis Rules If
• The running time of an if/else statement is
not more than the running time of the test,
plus the larger of the running times of
statements contained inside then and else
conditions .
26
* Algorithm Analysis and Design
Properties of O notation.
• Constant factors can be omitted
– 2n3+6 ε O(n3) , 2 and 6 are omitted
• Growth rate of a sum is given by the rate of its fastest
growing term.
– 5n3+3n+8 ε O(n3)
• if f(n)>g(n), g(n)>b(n) then f(n)>b(n)
– O(n2)>O(n), O(n)>O(logn)so O(n2)>O(logn)
• Higher powers of n grows faster than lower powers
– O(n3)> O(n2)
26
* Algorithm Analysis and Design
Example Problem 1
Read(A)
X:= A*A
-------
Write(X)
any segment for which each statement is
executed only once will have a total number of
statements executed that is independent of n.
Time complexity will be O(1)
26
* Algorithm Analysis and Design
Example Problem2
for i :=1 to n do
for j:=1 to n do
{
---
}
• complexity n*n =O(n2)
26
* Algorithm Analysis and Design
Example Problem3
for i:=1 to n do
for j:=1 to i do
{
---
}
complexity is 1+2+3+4+ -- --+n=n*(n+1)/2=O(n2)
26
* Algorithm Analysis and Design
Example Problem4
i=1;
While (i<=n) do
{ ---
i:= 2*i;
--- }
• Values of i=1, 2,4, ---,2k, 2 k+1if 2k <=n <2 k+1
• If n is a proper power of 2
– Loop executed k+1 times where k= log2
•if n is not a proper power of 2 n
–Loop executed k+1 times where k =
⎤ ⎡
⎤ ⎡ log2
•Complexity is 1+ log2 n
so O(logn)
26
* Algorithm Analysis and Design
Example Problem2
Algorithm power(b,n)
{p:=1; q:=b;
While (n>0) do
{If n is odd, then p:=p*q;
n:=n /2;
q:=q*q;}
Return (p); }
• Complexity = O(logn)
26
* Algorithm Analysis and Design
Example Problem2
Algorithm fact(n)
{ if n=1 then return(1);
else return(n*fact(n-1)); }
Solving Recurrences
• 3 methods
– using iteration
– using recurrence trees
– using master theorem
26
* Algorithm Analysis and Design
26
* Algorithm Analysis and Design
27
* Algorithm Analysis and Design
Example Problem1
• T(n) = T(n-1) + c
= T(n-2) + c + c
= T(n-(n-1) + (n-1) c
= T(1) + (n-1) c
= a + c(n-1)
= O(n)
27
* Algorithm Analysis and Design
Example Problem2
• T(1) = 1
• T(n) = T(n-1) + n
= [T(n-2) + n-1] + n
= T(n-2) + n-1 + n
------------ -
= T(n-(n-1) + 2+3+. . .+n-1+ n
= T(1) + 2+3+. . .+n-1+ n
= n*(n+1)/2
= O(n2)
27
* Algorithm Analysis and Design
Example Problem3
• T(1) = 1
• T(n) = T(n/2)+1
= T(n/4)+1+1
= T(n/22)+1+1
---------
= T(n/2k)+1+1+1--- k times
= T(1)+k where k =log2(n)
= logn+1
= O(logn)
27
* Algorithm Analysis and Design
Example Problem4
• T(1) = 0
• T(n) = T(n/2) + n
= T(n/22)+n/2+n
---------
= T(n/2k)+2+4+---+n/2+n
= T(1)+ (21)+ (22)+---+ (2k-1)+ (2k)
where k =log2(n)
= 0+2*(2k-1)/(2-1)
= 2*(n-1)
= O(n)
27
* Algorithm Analysis and Design
Example Problem5
• Example of algorithm that ------ into 2 values and
seeks to solve both
• T(n) = 2 T(n/2) + an
= 22 T(n/4) + an + an
= 2k T(n/2k) + akn
= n + an log n
= O(n log n)
27
* Algorithm Analysis and Design
Example Problem6
• T(n) = 2 T(√n) + log n for n > 2
Substitute m = log n
T(2m) = 2T(2 m/2) + m
when n=2 m=1 Termination condn is m=1
Substitute T(2m) with S(m)
= 2*S(m/2)+m
= 22S(m/4) + 2*m
= 2k S(1) + k*m where k=logm
= m + m log m
= O(log n * log(log n))
27
* Algorithm Analysis and Design
Example Problem7
Show that
T(n) = T(⎡n/2⎤ ) + 1 is O(log n)
T(n) = T(n/2) + 1
= T(n/4) + 1 + 1
= T(n/2k) + k where k=log2 n
= a+k
= a + log n
= O(log n)
27
* Algorithm Analysis and Design
Example Problem8
• T(n) = 3T(⎣n/4⎦) + n
= n + 3T (n/4)
= n + 3[ (n/4) + 3T ( n/42)]
= n[1 + ¾ +32/42] + 33 T(n/43)
= n[1 + ¾ +32/42+……+3k-1/4k-1] + 3k T(n/4k) where k = log4n
= n[(1*(3/4) k -1 )/3/4-1] + 3 ka
= c * n(3/4)log4n +4n + 3 log4n * a
= c * n * nlog43/4 + a*nlog43 +b*n
= c * n log44+ log43- log44 + a n log43 +b*n = O(n log43)+O(n)
= O(n)
27
* Algorithm Analysis and Design
Recurrence Tree
• A convenient way to visualize what happens
when a recursion is iterated
• It is good for generating guesses for the
substitution method.
• We may describe the execution of a
recursive program on a given input by a
rooted tree, which is known as recurrence
tree
27
The steps involved in
building the Recurrence Tree
28
* Algorithm Analysis and Design
T(n)
T(n/2) T(n/2)
lg n
T(n/4) T(n/4)
T(n/4) T(n/4)
28
* Algorithm Analysis and Design
8
T(8)
T(4) T(4) 4+4
lg 8
T(2) T(2) T(2) T(2) 2+2+2+2
T(1)
T(1) T(1) T(1) T(1) T(1) T(1) T(1) 1+1…+1
28
* Algorithm Analysis and Design
n
T(n)
T(n/2) T(n/2) 2*(n/2) = n
lg n
T(n/4) T(n/4) 4*(n/4) = n
T(n/4) T(n/4)
Total: n lg n
28
* Algorithm Analysis and Design
Example Problem
• T(n) = 3T(n/2) + n
= n* [1 + 3/2+............. + (3/2)k]
T(n) n
= n* [1-(3/2) k+1] / [(3/2)-1]
T(n/2) T(n/2) T(n/2) 3/2 n
= 2n[1-(3/2) k+1]
T(n/4) T(n/4) (3/2)2 n = 3n *(3/2) log 2 n
.
.
.
.
Example Problem
• T(n) = 2T(n/2) + n2
n2 + n2/2 + n2/4 + ............................
n2 = n 2 [1 + ½ +.........................…(1/2k)]
(n/2)2 (n/2) = n2 [1-(1/2) k+1] /(1/2)
2
(n/4) (n/4) (n/4) (n/4)2 = 2n2[1-(1/2 k+1)]
2 2 2 = 2 n2[1-1/2n]
= O(n2)
.
.
.
.
Θ(1 Θ(1
) )
28
* Algorithm Analysis and Design
Example Problem
Solve T(n) = T(n/4) + T(n/2) + n2
n
2
(n/4) (n/2)
2 2
(n/16) (n/8) (n/8) (n/4)
2 2 2 2
…
.
.
.
Θ(1 Θ(1 .
) ) Total = …
= Θ(n2)
28
* Algorithm Analysis and Design
Example Problem
Solve T(n) = T(n/3) + T(2n/3) + n
n=(3/2)k
So k = log (3/ 2) n
c*n*log (3/ 2) n
Master Theorem
T(1)= d
T(n)=aT(n/b)+cn
Has solution
28
* Algorithm Analysis and Design
Verification Example 1
T(1)= 0
T(n)=4T(n/2)+cn for n=2
a = 4; b = 2; a>b
T(n) = O(n logba ) if a>b
T(n) = O(n log24 ) = O(n2)
28
* Algorithm Analysis and Design
Verification Example 2
T(1)= 0
T(n)=3T(n/2)+n for n=2
a = 3; b = 2; a>b
T(n) = O(n logba ) if a>b
T(n) = O(n log23 ) = O(n1.6)
29
* Algorithm Analysis and Design
Verification Example 3
T(1) = 0
T(n) = T(n/2) + n
a = 1; b = 2; a<b
T(n) = O(n) if a<b
T(n) = O(n)
29
* Algorithm Analysis and Design
Verification Example 4
• T(1) = 1
• T(n) = 2T(n/2) + n
a = 2; b = 2; a=b
T(n) = O(nlogn) If a=b
T(n) = O(nlogn)
29
* Algorithm Analysis and Design
n
0
29
* Algorithm Analysis and Design
θ -Notation
• f(n) = θ(g(n)) if there exist positive
constants n0, c1 and c2 such that to the right
of n0 the value of f(n) always lies between
c1g(n) and c2g(n) inclusive.
29
* Algorithm Analysis and Design
Other notations
• for every positive real constant c there exists a
nonnegative integer N such that for all n>= N
• f(n)<=c*g(n)
• so 5n is o(n2) 6 log(n) is o(n) and 8n is o(nlogn)
• The asymptotic upper bound provided by O may
or may not be asymptotically tight. But o small is
used to denote an upeer bound that is not
asymptotically tight.
29
* Algorithm Analysis and Design
29
* Algorithm Analysis and Design
Examples
O(n2) θ (n2) Ω (n2)
29
* Algorithm Analysis and Design
Examples Contd..
o(n2) = O(n2) - θ (n2)
2logn+3 6 n2
5n+7 4n2+6
8nlogn 3n2+5n
29
* Algorithm Analysis and Design
29
* Algorithm Analysis and Design
Posterior Analysis
• Technique of coding a given solution and
then measuring its efficiency.
• Provides the actual time taken by the
program.
• Draw back
– Depends upon the programming language, the
processor and a lot of other external parameters.
30
* Algorithm Analysis and Design
Comparison
• Gettime() is a function that returns the
current time in milli seconds.
• The issues
– What value of n to be selected
– Which data set is to be used
• best,average or worst case
– What is the accuracy required.
– How many values of n are required.
30
* Algorithm Analysis and Design
Comparison Contd..
• If asymptotic behaviour is already known
then 2 or 3 values can generate the curve.
• Asymptotic behaviour omits initial values
of n and constant terms involved
– For an accurate estimate more values of n
– More samples needed from small vales of n.
30
* Algorithm Analysis and Design
Example Case
• A reasonable set of values for n for the
sequential search algorithm is
10, 20, 30, 40, 50, 60, 70, 80, 90,
100, 200, 300, 400, 500, ----- .
30
* Algorithm Analysis and Design
Example Algorithm
Algorithm seqsearch(a,x,n)
{
i:=n; a[0]:=x;
while (a[i]<>x) do i:=i-1;
return i;
}
worst case when x is which is not present in a.
for definiteness set a[i]=i for 1 to n and x=0
30
* Algorithm Analysis and Design
30
* Algorithm Analysis and Design
30
* Algorithm Analysis and Design
30
* Algorithm Analysis and Design
30
* Algorithm Analysis and Design
Assignment 1 Problem1
Solve T(n) = 3T(⎣n/4⎦) + n
30
* Algorithm Analysis and Design
Assignment 1 Problem2
Solve
a for n<=2
T(n) = 8 T(n/2)+bn2 for n>2
31
* Algorithm Analysis and Design
Assignment 1 Problem2
Solve a for n<=2
T(n) = 8 T(n/2)+bn2 for n>2
31
* Algorithm Analysis and Design
Example Problem1
• T(n) = T(n-1) + c
= T(n-2) + c + c
= T(n-(n-1) + (n-1) c
= T(1) + (n-1) c
= a + c(n-1)
= O(n)
31
* Algorithm Analysis and Design
Example Problem1
• T(n) = T(n-1) + c
= T(n-2) + c + c
= T(n-(n-1) + (n-1) c
= T(1) + (n-1) c
= a + c(n-1)
= O(n)
31
* Algorithm Analysis and Design
n f(n) = O(g(n))
0
31
* Algorithm Analysis and Design
31
ALGORITHM ANALYSIS AND
DESIGN
Strategy
• Given a function on n inputs
– input splitted into k disjoint subsets
– yielding k subproblems.
• Solve subproblems
• Combine the subsolutions into a
solution
solution
Problem Problem
31
Algorithm Analysis and Design
Solving Subproblems
• Large Subproblems
– Solved by reapplication of divide and conquer.
– Subproblems same type as the original problem
implemented using recursive algorithm
• Smaller subproblems solved independently.
31
Algorithm Analysis and Design
Control Abstraction
31
Algorithm Analysis and Design
32
Algorithm Analysis and Design
Time Complexity
32
Algorithm Analysis and Design
32
Algorithm Analysis and Design
Example
h(n)=f(n)/nlog b a=c*(logn)0
So u(n)= θ(logn)
32
Algorithm Analysis and Design
Binary Search
Algorithm binsearch(a,i,j,x)
{if (i=j) then
{ if (x=a[i]) then return i;
else return 0; }
else
{mid:= (i+j) div 2;
if (x=a[mid] then return mid;
else if (x<a[mid]) then
return binsearch(a,i,mid-1,x);
else return binsearch(a,mid+1,j,x); }}
32
Algorithm Analysis and Design
Computing Complexity
Example 1 3 5 7 9 11 13
Unsuccessfull cases
32
Algorithm Analysis and Design
32
Algorithm Analysis and Design
32
Algorithm Analysis and Design
2(n-1) Comparisons
32
Algorithm Analysis and Design
Improvement
Algorithm smaxmin(i,j,n,max,min)
{max:=a[1]; min:=a[1];
For i:= 2to n do
{if (a[i]>max) then max:=a[i];
else if (a[i]<min) then min:=a[i];
}}
33
Algorithm Analysis and Design
MAXMIN
Algorithm maxmin(i,j,max,min)
{if (i=j) then max:=min:=a[i];
else if (i=j-1) then
if (a[i]<a[j]) then
{max:=a[j];min:=a[i];}
else {max:=a[i];min:=a[j];}}
else
{ mid:= L(i+j)/2 ;
maxmin( i,mid,max,min);
maxmin(mid+1,j,max1,min1);
if (max<max1) then max:=max1;
if (min>min1) then min:= min1;}}
33
Algorithm Analysis and Design
Time Complexity
T(n)= T(n/2)+T(n/2)+2 for n>2; =1 for n=2
When n is a power of 2, T(n) = 2*T(n/2) +2
= -----
= 2 k-1 *T(2) +2* (2 k-1 –1) = 2 k-1 +2 k –2
= 3*n/2 –2
it is the best, average and worst case complexity.
Compared to the 2n-1 comparisons of straight maxmin
this approach is better.
But it requires logn +1 levels of stack.
33
Algorithm Analysis and Design
Considering Index
Comparisons
• When element and index comparisons of
the same cost
• In languages that does not support switch
statement modification required
33
Algorithm Analysis and Design
Improvement
Algorithm maxmin(i,j,max,min)
{if (i > j) then
if (a[i]<a[j]) then {max:=a[j];min:=a[i];}
else {max:=a[i];min:=a[j];}}
else
{ mid:= L(i+j)/2 ;
maxmin( i,mid,max,min);
maxmin(mid+1,j,max1,min1);
if max<max1) then max:=max1;
if (min>min1) then min:= min1;}}
33
Algorithm Analysis and Design
Complexity
C(n)= 2* C(n/2)+3 for n>2
=2 for n=2
• unfolding recurrence
– C(n) = 2 k-1*C(2) + 3* 0ε k-2 2i
=2 k +3* 2 k-1 –3
=5n/2 –3
33
Algorithm Analysis and Design
Comparison
33
Algorithm Analysis and Design
Summary
• When element comparisons are costlier dandc yields
a better algorithm.
• Dandc always willnot give better algorithm.
– Only a design technique that will guide to better designs.
• Constants should be specified, during comparisons if
relevant( when both has same order complexity).
33
Algorithm Analysis and Design
Merge Sort
• Divides list into two sub lists
• Sort sub lists separately
– Recursive invocation
• Merges the sub lists
25 11 18 17 13 45 28 30
33
Algorithm Analysis and Design
Algorithm Mergesort
Algorithm mergesort(low,high)
{ if (low<high) then
{ mid:= L (low+high)/2
megesort(low,mid);
mergesort(mid+1,high);
mege(low,mid,high);
}}
33
Algorithm Analysis and Design
Algorithm Merge
Algorithm merge(low,mid,high) if (h>mid) then
{h:=low;i:=low; j:=mid+1; for k:=j to high do
while ((h<=mid) and (j<=high)) do {b[i]:=a[k];i:=i+1;}
{if (a[h]<=a[j]) then else
{b[i]:=a[h];h:=h+1;} for k:=h to mid do
else {b[i]:=a[k];i := i+1;}
{b[i]:=a[j];j:=j+1;} for k:=low to high do
a[k]:=b[k];
i:=i+1;}
}
34
Algorithm Analysis and Design
Complexity
T(n)= 2*T(n/2)+cn for n>1
a for n=1
• Unfolding recurrence
T(n) = 2 T(n/2) + cn
= 22 T(n/4) + 2cn
= 2k T(n/2k) + kcn
= 2k T(1) +kcn = an + cn log n
= O(n log n)
34
Algorithm Analysis and Design
Refinements
• 2n locations – Extra n locations
– Associate an extra field with key
• For small values of n recursion inefficient
• Much time is wasted on stacking.
– Use an efficient nonrecursive sort for small n
34
Algorithm Analysis and Design
Refinement 1
For n<16 insertion sort is used.
Algorithm insertion sort(a,n)
{for j:=2 to n do
{ item := a[j]; i := j-1;
while ((i>=1) and (item<a[j])) do
{a[i+1]:= a[i]; i := i-1; }
a[i+1] := item; } }
34
Algorithm Analysis and Design
Complexity
• Insertion sort for worst case
– 2 ε n j=n(n+1)/2-1=θ(N2).
34
Algorithm Analysis and Design
Algorithm2
algorithm mergesort2(low,high)
{if (high-low)<15 then ///when size is <16
return insertionsort1(a,link,low,high)
else
{ mid := L(low+high)/2 ///divides into 2
q:=mergesort(low,mid);
r:=mergesort(mid+1,high);
return merge1(q,r);}}
34
Algorithm Analysis and Design
Refinement 2
• An auxillary array with values 0..n used
• Each index points to the original array
0 0 0 0 0 0 0 0
25 11 18 17 13 45 28 30
34
Algorithm Analysis and Design
Demonstration
• Interpreted as pointers to elements of a
link 0 0 0 0 0 0 0 0
value 25 11 18 17 13 45 28 30
1 2 3 4 5 6 7 8
0 01 0 03 06 0 08 0
25 11 18 17 13 45 28 30
34
Algorithm Analysis and Design
Demonstration Contd..
0 1 0 3 6 0 8 0
25 11 18 17 13 45 28 30
1 2 3 4 5 6 7 8
0 14 01 33 67 0 88 06
25 11 18 17 13 45 28 30
34
Algorithm Analysis and Design
Demonstration Contd..
0 4 1 3 7 0 8 6
25 11 18 17 13 45 28 30
1 2 3 4 5 6 7 8
07 45 11 33 470 8 6
25 11 18 17 13 45 28 30
Sorting Over
34
Algorithm Analysis and Design
Algorithm3
Algorithm merge1(q,r)
{i:=q;j:=r;k:=0;
while ((i<>0) and j<>0)) do
{if (a[i] < a[j]) then
{link[k]:=i;k:=i;i:=link[i];}
else {link[k]:=j;k:=j;j:=link[j];}}
If (i = 0) then link[k]:=j; else link[k]:=i;
return link[0];}
35
ALGORITHM ANALYSIS AND
DESIGN
GREEDY STRATEGY
35
Introduction
• Solution to problems with n inputs
• Required to obtain a subset that satisfies some
constraints
• Optimization meassure
• Best choice at any moment
35
Terminology
• Objective function
– Function representing the optimization measure
• Feasible solution
– Any subset that satisfies the given constraints
• Optimal solution
– Feasible solution that either maximizes or minimizes a
given objective function.
35
Working
• Works in stages – One input at a time
• At each stage, decision about a particular input
• Inputs one at a time based on selection
procedure
• Discarded if Inclusion results in an infeasible
solution
35
Selection Procedure
• Based on the optimization measure
• Different Optimization measures possible for a
problem.
35
Example
• Taking change a particular amount given a
collection of coins
• Minimise the number of coins used
• Example Case
– Amount to be raised
– 68 paise
– Coins available
– 1 , 1 , 1 ,2,2,2,5,5,5,10,10,10,20,20,20,50,50,50
35
Greedy Solution
• Selection procedure
– value of the coin
– highest one first
• Feasibility of partial solution
– Sum of coins <=68
35
Demonstration
Coin values 1,1,1,2,2,2,5,5,5,10,10,10,20,20,20,50,50,50
1 1 1 2 2 2 5 5 5
1 1 1 2 2 2 5 5 5
0 0 0 0 0 0 0 0 0
Success
2 1
1 5
5
0 0
35
Control Abstraction
Algorithm Greedy(a,n)
{solution :=Ø
for i:=1 to n do
{x:=select(a)
if(feasible(solution,x) then
solution:=union(solution,x);}
return solution;}
35
Different Paradigms
• Subset Paradigm
• Ordering Paradigm
36
Subset Paradigm
• Determine a subset of the given n inputs
36
Ordering Paradigm
• Determine an ordering of all the inputs
1 2 5 4
3 4 2 1 5 3
36
Knapsack Problem
There are n objects to be placed in a knapsack of capacity M. Each
object i contributes a profit Pi and weight Wi. The total profit
.should be maximized subject to the constraint that total weight
where M is the capacity of the Knapsack . Pi And Wi are
positive numbers where and
36
Knapsack Problem
general knapsack problem with n=3 m=20
Pi = { 25,24,15} Wi={18,15,10} for i= 1 ,2,3
I 1 2 3
Pi 25 24 15
Wi 18 15 10
PiXi 25 2/15*24=3.2 0 tot=28.2
PiXi 0 10/15*24=16 15 tot=31
36
Spanning Tree
• Let G=(V,E) be an undirected connected
graph.
• A sub-graph T=(V,E’) of G is a spanning tree of
G if T is a tree
36
Conditions - Spanning Tree
•For T a subgraph of G to be a spanning tree.
–T should contain all vertices V in G
–T should be a tree- there are no cycles
36
Minimum Cost Spanning Tree
• A Spanning tree which has minimum cost
– Sum of edge costs
12
35
25 93 22 48
20
2 56
36
PRIMS Algorithm
12
• Constraint 35
25 93
– Each vertex once 22 48
– Tree 20
2 56
• Selection Principle
– Vertex not present in Tree
– Vertex which can be connected with lowest cost to
the spanning tree is next vertex selected
36
Informal Algorithm
ST:=∅
Y:= {u,v}, where <u,v> is the edge with lowest cost
ST:={(u,v)}
While the instance is not solved do
{Select a vertex in V-Y that is nearest to Y
Add the corresponding edge to ST
If Y= V then mark instance ‘solved’ // included all
} the vertices
37
Near Array
• For every un included a vertex j which is not in the
spanning Tree, near[ j] represents a vertex which is
nearest to vertex j in the spanning tree.
• Edge(j,near[j]) represents the shortest path of
connecting j to the current spanning tree
12
5 4 4 3 1 20
35 5 35
25 93 25 4 2 22
22 48 22
1 20 1 20
5 1 25
3 3
2 2 2 56
56
37
Example
37
Demonstration
1 - -
1 28
2 1 28
2
3 ∞ ∞
6 7 3 4 ∞ ∞
5 6 25
∞ 25
5 6 6- -
∞ 4 7
∞
∞ Cost
Near
37
Demonstration
1 - -
28
1 2 11 28
28
2
3 ∞∞ ∞∞
6 7 3 4 ∞5 ∞222
24 5 5- -2
5 6 - -
22
4 7 ∞5 ∞24
Near Cost
37
Demonstration
-
1 - -
1 28 2 11 28
28
2
3 ∞4 ∞ 12
6 7 3 4 - -
24 5 -
-
18
5 12 6 - -
4 7 54 24
18
Near cost
37
Demonstration
1 - -
1 28
2 133 281616
2
3 - -
16
6 7 3 4 - -
18
5 - -
5 6 - -
4 7 44 18
18
Near cost
37
Demonstration
1 - -
1 2 - -
2
14 3 - -
6 7 3 4 - -
18
5 - -
5 6 - -
4 7 422 18
14
14
Near cost
37
Solution
37
ALGORITHM ANALYSIS AND
DESIGN
BACKTRACKING
Introduction
Backtracking- When
Backtracking- How
Solving A Maze
• Given a maze, find a path from start to finish
• At each intersection, you have to decide between three
or fewer choices:
– Go straight
– Go left
– Go right
• Sufficient information not available on the best choice
• Each choice leads to another set of choices
• One or more sequences of choices may (or may not)
lead to a solution
* 2:15 AM Algorithm Analysis and Design
Coloring A Map
Solving A Puzzle
Backtracking- Demonstration
dead end
?
dead end
dead end
?
start ? ? dead end
dead end
?
success!
* 2:15 AM Algorithm Analysis and Design
Representation
The decision sequences can
be represented by a tree
Three kinds of nodes:
Types Of Nodes
• Live node
– A node which has been generated, and all of its
children have not yet been generated
• Dead node
– A generated node which is not to be expanded
further or all of whose children have been
generated.
• E-node
– The live node whose children are being generated.
* 2:15 AM Algorithm Analysis and Design
dead end
dead end
dead end
start
dead end
Child Node dead end
Goal Node ?
Live Node success!
E-node Dead Node
* 2:15 AM Algorithm Analysis and Design
Constraints
• Explicit constraints
– Rules that restrict each element to be chosen from the
given set
– Only tuples that satisfy explicit constraints will appear
in the tree
• Implicit constraints
– Rules that specify how each elements in a tuple should
be related
– Tuples in the tree that satisfy the implicit constraints
represent a goal state
* 2:15 AM Algorithm Analysis and Design
Constraints-Example
Solution Space
Comparison
Backtracking -Impementation
• Generating Function
– Generate next child, given the parent
• Bounding function
– Check whether the particular path can lead to an answer state
and returns false If it cannot.
* 2:15 AM Algorithm Analysis and Design
Control Abstraction
Algorithm backtrack(n)
{k=1;
while (k#0) do
{
if (there remains an untried x[k] ε T(x[1],x[2],. . . ,x[k-1]) and
Bk(x[1],x[2], . . .,x[k]) is true) then
{
if x[1]. …x[k] is a path to an answer node) then write x[1:k]);
k:=k+1;}
else k:=k-1;}}
* 2:15 AM Algorithm Analysis and Design
Recursive Formulation
Algorithm Backtrack(k)
//This schema describes the backtracking process using
//recursion. On entering, the first k-1 values
//x[1],x[2],….,x[k-1] of the solution vector
//x[1 : n] have been assigned. x[] and n are global.
{ for (each x[k] ε T(x[1],……,x[k-1]) do
{if(Bk (x[1],x[2],……x[k] ≠ 0 ) then
{if(x[1],x[2],….,x[k] is a path to an answer node)
then write (x[1 : k]);
if(k < n) then Backtrack(k + 1);}}}
* 2:15 AM Algorithm Analysis and Design
Efficiency Of Backtracking
Analysis
• Random path
– starts from the root of the tree and
– is continued till
• a leaf node is reached
• or a node with all bounded children is reached
• Each time a child node of the current node is
selected as the random node at the next level.
• The total nodes considering all levels is equal to
1+m1+m1m2+- - -+m1*m2*--mn
* 2:15 AM Algorithm Analysis and Design
Algorithm
Algorithm Estimate
{ // r is the children at a particular level; m –total nodes;k -level
k:=1;m:=1;r:=1;
repeat
{Tk= {x[k] / x[k] ε T(x[1],……,x[k-1] and Bk(x[1],. . ,x[k]) is true
};
If (size(Tk)=0 ) then return m;
r:= r* size(tk); m:=m+r;
X[k]:= choose(Tk); k:=k+1;
}until (false);}
* 2:15 AM Algorithm Analysis and Design
4 Queens Problem
• There are 4 queens that are
to be placed on a
4x4 chessboard
• Queens should be placed on the
chessboard such that
no two queens can attack each other.
• Two queens attack each other when they are in the
– Same row
– Same column or
– Same diagonal.
* 2:15 AM Algorithm Analysis and Design
Attacking Positions
۩ ۩ ۩ *۩ ۩ ۩
۩ ۩ ۩ ۩ ۩ ۩
۩ ۩ ۩ ۩
۩ ۩ ۩ ۩
Rule 1 Rule 2 Rule 3.a. Rule 3.b.
* 2:15 AM Algorithm Analysis and Design
Problem Specification
Solution Space
• The solution space consists of 44 4 tuples.
• The implicit constraint
– no two queens are in the same column
• No two xi’s can be the same
– no two queens can be on the same diagonal.
• Column constraint restricts the solution space to
consist of combinations of 4 tuples.
• So the solution space gets reduced to 4! Tuples.
• Second constraint specify that no two xi’s should
be on the same diagonal.
* 2:15 AM Algorithm Analysis and Design
Solution Space
Problem State-representation
1
2
∙ 3
1
1 1 2 2 2 2
2 2 2 2
1
1 1 2
2 2
3
•• •. •. •. 3 3
1 1
1
1
2 2
2 2
3 3
3 3
•. •. •. .• 4
4 44 4
* 2:15 AM Algorithm Analysis and Design
1 1 1 1
2 2 2
. . . . 3
1 1 1 1
2 . . . 2 2
3 3
. . . . . . 4
* 2:15 AM Algorithm Analysis and Design
N Queens Problem
Bounding Function
Algorithm place(k,i)
{
for j:=1 to k-1 do
if ((x[j]=i) or (abs(x[j]-i)= abs(j-k) ))
then return false;
return true;
}
Algorithm Analysis and Design
Algorithm
Algorithm nqueens(k,n)
{ for i:=1 to n do
{if place(k,i) then
{x[k]:=i;
if (k=n) then write(x[1..n]);
else nqueens(k+1,n);
}}}
Algorithm Analysis and Design
Complexity
Complexity Contd..
Sum Of Subsets
n=6 {w1,w2,………..w6}
= {5,10,12,13,15,18}
m = 30
X2=1
* 2:15 AM Algorithm Analysis and Design
X1=1
X1=4
X1=2 X1=3
X3=3
X3=4 X3=4 X3=4
X4=4
Algorithm Analysis and Design
Example: {w1,w2,………..w6} =
{5,10,12,13,15,18} & m= 30 X1=1
X1=0
level i.
X3=1
X3=0 X3=1 X3=0 X3=1
X3=0 X3=1 X3=0
r : sum of remaining weights
wi+1 ,wi+2………wn X4=1
X4=0 X4=1 X4=1 X4=1 X4=1 X4=1 X4=1
X4=1 X4=0 X4=0 X4=0 X4=0 X4=0
The node is nonpromising
if s+w i+1 > m X5=1 X5=0
X5=0X5=0 X5=0 X5=0 X5=0 X5=0X5=0 X5=0 X5=0 X5=0 X5=0X5=0X5=0
X5=0
X5=1 X5=1 X5=1 X5=1X5=1 X5=1 X5=1 X5=1 X5=1 X5=1 X5=1X5=1X5=1 X5=1
X5=1
if s+r < m
* 2:15 AM Algorithm Analysis and Design
Bounding Function
• A node can lead to an answer state if
• Bk is true iff
Algorithm
Algorithm sumofsub(s,k,r)
{//generate left child with xk=1 fixed tuple
formulation
x[k]:=1;
if (s+w[k]=m) then write (x[1:k]);
else if (s+w[k]+w[k+1]<=m)
then sumofsub(s+w[k],k+1,r-w[k]);
if ((s+r-w[k]>=m) and (s+w[k+1]<=m)) then
{x[k]:=0;
sumofsub(s,k+1,r-w[k]);}}
Algorithm Analysis and Design
Algorithm Analysis and Design
Terminology II
• Each non-leaf node in a tree is a parent of one or
more other nodes (its children)
• Each node in the tree, other than the root, has
exactly one parent parent
Usually, however,
we draw our trees
downward, with
parent the root at the top
children children
Algorithm Analysis and Design
ATTACKING POSITIONS
۩ ۩
* * * * * *
* * * * * *
* * * *
* * * *
Rule 1 Rule 2 Rule 3.a. Rule 3.b.