Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008

Similar presentations


Presentation on theme: "Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008"— Presentation transcript:

1 Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008

2 Recursion

3 Recursive Function Call
a recursion function is a function that either directly or indirectly makes a call to itself. but we need to avoid making an infinite sequence of function calls (infinite recursion)

4 Implementation of Binary Heap Basic Operations
Insert operation Delete operation Build Heap

5 General format for Many Recursive Functions
if (some easily-solved condition) // base case solution statement else // general case recursive function call

6 When a function is called...
a transfer of control occurs from the calling block to the code of the function--it is necessary that there be a return to the correct place in the calling block after the function code is executed; this correct place is called the return address when any function is called, the run-time stack is used--on this stack is placed an activation record for the function call

7 Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 100
FCTVAL result b a Return Address FCTVAL ? result Func(5,0) = ? b Return Address FCTVAL ? result Func(5,1) = ? b a Return Address record for Func(5,0) is popped first with its FCTVAL record for Func(5,1) record for Func(5,2)

8 Tree Tree is a fundamental structure in computer science. Recursive definition: A tree is a root and zero or more nonempty subtrees. Nonrecursive definition: A tree consists of s set of nodes and a set of directed edges that connect pairs of nodes. That is, a connected graph without loop.

9 Traversal Three standard traversal order preorder - V L R
inorder - L V R postorder - L R V Inorder: traverse all nodes in the LEFT subtree first, then the node itself, then all nodes in the RIGHT subtree. Preorder: traverse the node itself first, then all nodes in the LEFT subtree , then all nodes in the RIGHT subtree. Postorder: traverse all nodes in the LEFT subtree first, then all nodes in the RIGHT subtree, then the node itself,

10 Recursive Traversal Implementation
1 2 3 4 5 6 Void PrintPreorder (root) if root != null print(root->data); PrintPreorder(root->left); PrintPreorder(root->right); endif; preorder : inorder : postorder : Void PrintInorder (root) if root != null PrintInorder(root->left); print(root->data); PrintInorder(root->right); endif; Void PrintPostorder (root) if root != null PrintPostorder(root->left); PrintPostorder(root->right); print(root->data); endif; The difference is the order of the three statements in the ‘IF’.

11 Sorting

12 The INSERT operation Insertion is implemented by creating a hole at the next available location and then percolating it up until the new item can be placed in it without violating a heap order. Insert takes constant time on average but logarithmic time in worst case.

13 The DeleteMIN Operation
Deletion of the min involves placing the former last item in a hole that is created at the root. The hole is percolated down the tree through min children until the item can be placed without violating the heap order property.

14 21.3 the buildHeap Operation: Linear-Time Heap construction
The buildHeap operation can be done in linear time by applying a percolate down routine to nodes in reverse order

15 Insertion Sort: Code Moved
template <class Comparable> void insertionSort( vector<Comparable> & a ) { for( int p = 1; p < a.size( ); p++ ) Comparable tmp = a[ p ]; int j; for( j = p; j > 0 && tmp < a[ j - 1 ]; j-- ) a[ j ] = a[ j - 1 ]; a[ j ] = tmp; } Fixed n-1 iterations Worst case i-1 comparisons Searching for the proper position for the new key Move current key to right Insert the new key to its proper position Moved

16 Example of shell sort 5-sort 3-sort 1-sort original 81 94 11 96 12 35
17 95 28 58 41 75 15 5-sort 3-sort 1-sort

17 Merge Sort If List has only one Element, do nothing
Otherwise, Split List in Half Recursively Sort Both Lists Merge Sorted Lists Mergesort(A, l, r) if l < r then q = floor((l+r)/2) mergesort(A, l, q) mergesort(A, q+1, r) merge(A, l, q, r)

18 Binary Merge Sort Given a single file Split into two files 18

19 Binary Merge Sort Merge first one-element "subfile" of F1 with first one-element subfile of F2 Gives a sorted two-element subfile of F Continue with rest of one-element subfiles 19

20 Binary Merge Sort Split again Merge again as before
Each time, the size of the sorted subgroups doubles 20

21 Note we always are limited to subfiles of some power of 2
Binary Merge Sort Last splitting gives two files each in order Last merging yields a single file, entirely in order Note we always are limited to subfiles of some power of 2 21

22 Quicksort Given to sort: 75, 70, 65, 84, 98, 78, 100, 93, 55, 61, 81, 68 Select, arbitrarily, the first element, 75, as pivot. Search from right for elements <= 75, stop at first element <75 Search from left for elements > 75, stop at first element >=75 Swap these two elements, and then repeat this process

23 Quicksort Example When done, swap with pivot
75, 70, 65, 68, 61, 55, 100, 93, 78, 98, 81, 84 When done, swap with pivot This SPLIT operation placed pivot 75 so that all elements to the left were <= 75 and all elements to the right were >75. View code for split() template 75 is now placed appropriately Need to sort sublists on either side of 75

24 Quicksort Note visual example of a quicksort on an array etc. …

25

26 Radix Sort Approach 1. Decompose key C into components C1, C2, … Cd
Component d is least significant, Each component has values over range 0..k

27 Graph

28 Graph Algorithms Graphs and Theorems about Graphs
Graph ADT and implementation Graph Algorithms Shortest paths minimum spanning tree

29 Degree The degree of B is 2. A B C D E F

30 Degree The in degree of 2 is 2 and the out degree of 2 is 3. 1 2 4 5

31 Implementation of a Graph
2 5 1 1 2 2 1 5 3 4 3 3 2 4 5 4 4 2 5 3 5 4 1 2

32 Implementation of a Graph
2 5 1 1 2 2 5 3 4 3 3 4 5 4 4 5 5 5

33 Adjacency-matrix-representation
1 2 1 2 3 4 4 3

34 Adjacency-matrix-representation
1 1 2 3 4 2 4 3

35 Minimum Spanning Tree

36 Algorithms for MST Prim’s Kruskal’s
Grow a MST by adding a single edge at a time Kruskal’s Choose a smallest edge and add it to the forest If an edge is formed a cycle, it is rejected

37 Prim’s greedy algorithm
Start from some (any) vertex. Build up spanning tree T, one vertex at a time. At each step, add to T the lowest-weight edge in G that does not create a cycle. Stop when all vertices in G are touched

38 Kruskal’s Algorithm Choose the smallest edge and add it to a forest
Keep connecting components until all vertices connected If an edge would form a cycle, it is rejected.


Download ppt "Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008"

Similar presentations


Ads by Google