Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design and Analysis of Algorithms

Similar presentations


Presentation on theme: "Design and Analysis of Algorithms"— Presentation transcript:

1 Design and Analysis of Algorithms
Dr. Maheswari Karthikeyan 25/02/2012, Lecture5

2 Quick Sort Sorting in linear time

3

4 Sort an array A[p…r] Divide
A[p…q] A[q+1…r] Sort an array A[p…r] Divide Partition the array A into 2 subarrays A[p..q] and A[q+1..r], such that each element of A[p..q] is smaller than or equal to each element in A[q+1..r] Need to find index q to partition the array

5 Conquer Combine A[p…q] A[q+1…r] ≤
Recursively sort A[p..q] and A[q+1..r] using Quicksort Combine Trivial: the arrays are sorted in place No additional work is required to combine them The entire array is now sorted

6 Alg.: QUICKSORT(A, p, r) if p < r then q  PARTITION(A, p, r) QUICKSORT (A, p, q-1) QUICKSORT (A, q+1, r) Initially: p=1, r=n Recurrence: T(n) = T(q) + T(n – q) + f(n)

7 PARTITION (A,p,r) x ← A[r] i ← p-1 for j ← p to r-1 do if A[j] <= x then i ← i +1 exchange A[i] <-> A[j] exchange A[i+1] <-> A[r] return i+1

8 p j r i 23 6 45 9 11 34 12 5

9 p j r i 23 6 45 9 11 34 12 5 r p j i 23 6 45 9 11 34 12 5

10 p j r i 23 6 15 9 11 4 12 5 r p j i 23 6 15 9 11 4 12 5 p j r i 23 6 15 9 11 4 12 5

11 p j r i 23 6 15 9 11 4 12 5 r p j i 23 6 15 9 11 4 12 5 p j r i 23 6 15 9 11 4 12 5 p j r i 23 6 15 9 11 4 12 5

12 p j r i 23 6 15 9 11 4 12 5

13 p j r i 23 6 15 9 11 4 12 5 j p r i 23 6 15 9 11 4 12 5

14 p j r i 23 6 15 9 11 4 12 5 j p r i 23 6 15 9 11 4 12 5 j p i r 23 6 15 9 11 4 12 5

15 p j r i 23 6 15 9 11 4 12 5 j p r i 23 6 15 9 11 4 12 5 j p i r 23 6 15 9 11 4 12 5 j p i r 4 6 15 9 11 23 12 5

16 Quicksort( A,1, 1) Quicksort( A,3,8)
p i j r 4 6 15 9 11 23 12 5 p i q j r 4 5 15 9 11 23 12 6 Quicksort( A,1, 1) Quicksort( A,3,8)

17 Sort the array 2,10,3,15,6,9,4,13,25,11

18 Sort the array 2,10,3,15,6,9,4,13,25,11 2 10 3 15 6 9 4 13 25 11

19 Sort the array 2,10,3,15,6,9,4,13,25,11 2 10 3 15 6 9 4 13 25 11 2 10 3 6 9 4 11 13 25 15 2 10 3 6 9 4

20 Sort the array 2,10,3,15,6,9,4,13,25,11 2 3 4 6 9 10 6 9 10 2 3 2 3 4 6 9 10

21 Sort the array 2,10,3,15,6,9,4,13,25,11 2 3 4 6 9 10 11 13 25 15 13 15 25 2 3 4 6 9 10 11 13 15 25

22 Worst Case Partitioning
2 1 3 (n2) Worst-case partitioning One region has one element and the other has n – 1 elements Maximally unbalanced Recurrence: q=1 T(n) = T(1) + T(n – 1) + n, T(1) = (1) T(n) = T(n – 1) + n =

23 Best Case Partitioning
Partitioning produces two regions of size n/2 Recurrence: q=n/2 T(n) = 2T(n/2) + (n) T(n) = (nlgn) (Master theorem)

24 Binary Tree ADT Tree: A directed, acyclic structure of linked nodes.
directed : Has one-way links between nodes. acyclic : No path wraps back around to the same node twice. Binary tree: One where each node has at most two children. Recursive definition: A tree is either: empty (null), or a root node that contains: data, a left subtree, and a right subtree. (The left and/or right subtree could be empty.) 7 6 3 2 1 5 4 root

25 Binary Tree ADT Applications of Binary Tree
Expression Tree ( Parse Tree in Compilers) Decision trees (AI) Huffman Coding Tree

26 Types of Binary Tree Full - Every node has exactly two children in all levels, except the last level. Nodes in last level have 0 children Complete - Full up to second last level and last level is filled from left to right Other - not full or complete

27 Types of Binary Tree Full, Complete or other? not binary A C D F I E B
G H T M L O K J Q N not binary

28 Types of Binary Tree A D F I B R S P G H T M L O K N

29 Types of Binary Tree A D F I B Q S R G H T M L P K N O

30 Types of Binary Tree A D F I B Q R G H M L P K N O

31 Terminology node: an object containing a data value and left/right children root: topmost node of a tree leaf: a node that has no children branch: any internal node; neither the root nor a leaf parent: a node that refers to this one child: a node that this node refers to sibling: a node with a common parent subtree: the smaller tree of nodes on the left or right of the current node height: length of the longest path from the root to any node level or depth: length of the path from a root to a given node 7 6 3 2 1 5 4 root height = 3 level 1 level 2 level 3

32 Binary Tree Traversal Traversal: An examination of the elements of a tree. A pattern used in many tree algorithms and methods Preorder Traversal Each node is processed before any node in either of its subtrees Inorder Traversal Each node is processed after all nodes in its left subtree and before any node in its right subtree Postorder Traversal Each node is processed after all nodes in both of its subtrees

33 Preorder Traversal A D F I B R S P G H T M L O K N 1 2 11 3 8 12 13 14
Visit the root Visit Left subtree Visit Right subtree 2 11 3 8 12 13 14 4 5 9 10 6 7 15 16

34 Inorder Traversal A D F I B R S P G H T M L O K N 10 1 1 6 12 1 2 8 11
Visit Left subtree Visit the root Visit Right subtree 1 6 12 1 2 8 11 16 14 1 4 7 9 3 5 13 15

35 Postorder Traversal A D F I B R S P G H T M L O K N 16 9 15 2 5 8 10
Visit Left subtree Visit Right subtree Visit the root 9 15 2 5 8 10 14 13 1 3 4 2 6 7 2 3 11 12

36 Represent ion of Binary Tree ADT
A binary tree can represented using - Linked List - Array Note : Array is suitable only for full and complete binary trees

37 Height and the number of nodes in a Binary Tree
If the height of a binary tree is h then the maximum number of nodes in the tree is 2h -1 If the number of nodes in a complete binary tree is n, then 2h - 1 = n 2h = n + 1 h = log(n+1) O(logn)


Download ppt "Design and Analysis of Algorithms"

Similar presentations


Ads by Google