Course Name: Design and Analysis of Algorithm: B.Tech V Sem Cse

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

B.

TECH V SEM CSE


ACADEMIC YEAR: 2021-2022

Course Name: Design and Analysis of Algorithm


Topic: Transform and Conquer (Pre-Sorting and BST)

Course code : CS 3102


Credits : 4
Mode of delivery : Hybrid (Power point presentation)
Faculty : Mr. Tarun Jain
Email-id : tarun.jain@jaipur.manipal.edu
1
Assignment

quiz Assessment
Mid term examination criteria’s

End term Examination

11/26/21 CS3102 (DAA), Dept. of CSE 2


Transform and Conquer (Pre-Sorting and BST)

11/26/21 CS3102 (DAA), Dept. of CSE 3


Course Information
• Course Handout
• Communicate through eMail
• Office hours
• To be communicated
• Grading policy
• Will be communicated as per university guidelines

CS3102 (DAA), Dept. of CSE


11/26/21 4
Syllabus
• Introduction: Fundamentals of Algorithms, Important Problem Types,
Analysis of algorithm efficiency. Analysis Framework: Asymptotic Notations
and Basic Efficiency Classes. Mathematical Analysis of Nonrecursive and
Recursive Algorithms: Brute force Techniques, Divide and Conquer. Decrease
and Conquer: Insertion Sort, Depth First Search, Breadth First Search,
Topological Sorting. Transform and Conquer: Presorting, BST, Heapsort. Space
and Time tradeoffs: Input Enhancement in String Matching. Dynamic
Programming: Warshall's and Floyd's Algorithms, The Knapsack Problem.
Greedy Techniques: Prim's, Kruskal's and Dijkstra's Algorithm, Huffman Trees.
Coping with limitations of algorithmic power. Backtracking: nQueens problem,
Hamiltonian Circuit Problem, Subset Sum Problem. Branch and Bound:
Assignment Problem, Knapsack Problem, TSP. P, NP, and NP-complete
Problems.

CS3102 (DAA), Dept. of CSE


11/26/21 5
More Information
• Textbook
•Introduction to Algorithms 3rd ,Cormen, Leiserson, Rivest
and Stein, The MIT Press,
•Fundamentals of Computer Algorithms, 2nd, Sartaj Sahni,
Ellis Horowitz, Sanguthevar Rajasekaran
• Others
•Introduction to Design & Analysis Computer Algorithm 3rd,
Sara Baase, Allen Van Gelder, Adison-Wesley, 2000.
•Algorithms, Richard Johnsonbaugh, Marcus Schaefer, Prentice
Hall, 2004.
•Introduction to The Design and Analysis of Algorithms 2 nd
Edition, Anany Levitin, Adison-Wesley, 2007.

CS3102 (DAA), Dept. of CSE


11/26/21 6
Course Objectives
• CS1501.1 Analyse the running times of algorithms using asymptotic analysis.
• CS1501.2 Demonstrate and Design algorithms using
divide-and-conquer paradigm to solve business problems hence enhance
skills.
• CS1501.3 Illustrate the concept of greedy and dynamic-programming
approach to solve real life problems to enhance entrepreneurship capabilities.
• CS1501.4 Demonstrate the concept of backtracking
and branch & bound algorithms.
• CS1501.5 Synthesize and analyse various advanced algorithms concept such as
graphs, string matching, approximation algorithms and complexity classes to
enhance employability.

CS3102 (DAA), Dept. of CSE


11/26/21 7
Transform and Conquer

This group of techniques solves a problem by a transformation

 to a simpler/more convenient instance of the same problem


(instance simplification)

 to a different representation of the same instance


(representation change)

 to a different problem for which an algorithm is already


available (problem reduction)

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-8
Instance simplification - Presorting
Solve a problem’s instance by transforming it into
another simpler/easier instance of the same problem

Presorting
Many problems involving lists are easier when list is sorted.
 searching
 computing the median (selection problem)
 checking if all elements are distinct (element uniqueness)

Also:
 Topological sorting helps solving some problems for dags.
 Presorting is used in many geometric algorithms.
 Presorting is a special case of preprocessing.

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-9
How fast can we sort ?

Efficiency of algorithms involving sorting depends on


efficiency of sorting.

Theorem (see Sec. 11.2): log2 n!  n log2 n comparisons are


necessary in the worst case to sort a list of size n by any
comparison-based algorithm.

Note: About nlog2 n comparisons are also sufficient to sort array


of size n (by mergesort).

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-10
Searching with presorting

Problem: Search for a given K in A[0..n-1]

Presorting-based algorithm:
Stage 1 Sort the array by an efficient sorting algorithm
Stage 2 Apply binary search

Efficiency: Θ(nlog n) + O(log n) = Θ(nlog n)

Good or bad?
Why do we have our dictionaries, telephone directories, etc.
sorted?

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-11
Element Uniqueness with presorting
 Presorting-based algorithm
Stage 1: sort by efficient sorting algorithm (e.g. mergesort)
Stage 2: scan array to check pairs of adjacent elements

Efficiency: Θ(nlog n) + O(n) = Θ(nlog n)

 Brute force algorithm


Compare all pairs of elements

Efficiency: O(n2)

 Another algorithm? Hashing, which works well on avg.


Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-12
Binary Search Tree

Arrange keys in a binary tree with the binary search


tree property:
K

<K >K 5
3 10
1 7 12
Example: 5, 3, 1, 10, 12, 7, 9
9
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-13
Dictionary Operations on Binary Search Trees

Searching – straightforward
Insertion – search for key, insert at leaf where search terminated
Deletion – 3 cases:
deleting key at a leaf
deleting key at node with single child
deleting key at node with two children

Efficiency depends of the tree’s height: log2 n  h  n-1,


with height average (random files) be about 3log2 n

Thus all three operations have


• worst case efficiency: (n)
• average case efficiency: (log n) (CLRS, Ch. 12)

Bonus: inorder traversal produces sorted list

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-14
Balanced Search Trees
Attractiveness of binary search tree is marred by the bad (linear)
worst-case efficiency. Two ideas to overcome it are:

 to rebalance binary search tree when a new insertion


makes the tree “too unbalanced”
• AVL trees
• red-black trees

 to allow more than one key and two children


• 2-3 trees
• 2-3-4 trees
• B-trees

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-15
Balanced trees: AVL trees
Definition An AVL tree is a binary search tree in which, for
every node, the difference between the heights of its left and
right subtrees, called the balance factor, is at most 1 (with
the height of an empty tree defined as -1)
1 2

10 10
0 1 0 0
5 20 5 20

1 -1 0 1 -1

4 7 12 4 7

0 0 0 0
2 8 2 8

(a) (b)

Tree (a) is an AVL tree; tree (b) is not an AVL tree


Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-16
Rotations
If a key insertion violates the balance requirement at some
node, the subtree rooted at that node is transformed via one of
the four rotations. (The rotation is always performed for a
subtree rooted at an “unbalanced” node closest to the new leaf.)
2 0 2 0
3 2 3 2

1 0 0 -1 0 0
R LR
2 > 1 3 1 > 1 3

0 0
1 2
(a) (c)

Single R-rotation Double LR-rotation

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-17
General case: Single R-rotation

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-18
General case: Double LR-rotation

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-19
AVL tree construction - an example

Construct an AVL tree for the list 5, 6, 8, 3, 2, 4, 7


0 -1 -2 0
L(5)
5 5 5 6
0 -1 > 0 0
6 6 5 8

0
8

1 2 1
6 6 6
1 0 2 0 R (5) 0 0
5 8 5 8 > 3 8

0 1 0 0
3 3 2 5

0
2

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-20
AVL tree construction - an example (cont.)
2 0
6 5
-1 0 LR (6) 0 -1
3 8 > 3 6

0 1 0 0 0
2 5 2 4 8

0
4

-1 0
5 5
0 -2 0 0
3 6 3 7
RL (6)
0 0 1 > 0 0 0 0
2 4 8 2 4 6 8

0
7
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-21

You might also like