610 likes | 625 Views
Transform & Conquer. ITS033 – Programming & Algorithms. Lecture 08. Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University
E N D
Transform & Conquer ITS033 – Programming & Algorithms Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University http://www.siit.tu.ac.th/bunyaritbunyarit@siit.tu.ac.th02 5013505 X 2005
ITS033 Midterm Topic 01-Problems & Algorithmic Problem Solving Topic 02 – Algorithm Representation & Efficiency Analysis Topic 03 - State Space of a problem Topic 04 - Brute Force Algorithm Topic 05 - Divide and Conquer Topic 06-Decrease and Conquer Topic 07 - Dynamics Programming Topic 08-Transform and Conquer Topic 09 - Graph Algorithms Topic 10 - Minimum Spanning Tree Topic 11 - Shortest Path Problem Topic 12 - Coping with the Limitations of Algorithms Power http://www.siit.tu.ac.th/bunyarit/its033.php and http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7
This Week Overview • Introduction • Presort • Binary Search Tree • AVL Tree • Problem reduction
Transform & Conquer: Introduction Lecture 08.1 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University http://www.siit.tu.ac.th/bunyaritbunyarit@siit.tu.ac.th02 5013505 X 2005
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)
Introduction • Transform-and-conquer • Methods work as two-stage procedures. • (1) Transformation stage: the problem’s instance is modified to be, for one reason or another, more amenable to solution. • (2) Conquering stage, solve it.
Transform & Conquer: Instance Simplification Lecture 08.2 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University http://www.siit.tu.ac.th/bunyaritbunyarit@siit.tu.ac.th02 5013505 X 2005
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.
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: O(nlog n) + O(log n) = O(nlog n) Good or bad? Why do we have our dictionaries, telephone directories, etc. sorted?
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: O(nlog n) + O(n) = O(nlog n) • Brute force algorithm Compare all pairs of elements Efficiency: O(n2)
Example 1 • Checking element uniqueness in an array ALGORITHM PresortElementUniqueness(A[0..n - 1]) //Solves the element uniqueness problem by sorting the array first //Input: An array A[0..n - 1] of orderable elements //Output: Returns “true” if A has no equal elements, “false” // otherwise Sort the array A for i 0 to n - 2 do if A[i]= A[i + 1] return false return true
Transform & Conquer: Representation Change Lecture 08.3 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University http://www.siit.tu.ac.th/bunyaritbunyarit@siit.tu.ac.th02 5013505 X 2005
Searching Problem Problem: Given a (multi)set S of keys and a search key K, find an occurrence of K in S, if any • Searching must be considered in the context of: • file size (internal vs. external) • dynamics of data (static vs. dynamic) • Dictionary operations (dynamic data): • find (search) • insert • delete
Tree Characteristics §- trees • hierarchical structures that place elements in nodes along branches that originate from a root. • Nodes in a tree are subdivided into levels in which the topmost level holds the root node. • Any node in a tree may have multiple successors at the next level. Hence a tree is a non-linear structure.
Binary Tree: Formal Definition • A binary tree T is a finite set of nodes with one of the following properties: • a.) T is a tree if the set of nodes is empty. (An empty tree is a tree.) • b.) The set consists of a root, R, and exactly two distinct binary trees, the left subtree, TL and the right subtree, TR. • c.) The nodes in T consist of node R and all the nodes in TL and TR.
Binary Search Trees • Binary Search Tree (BSTs) is a binary tree which has following properties: • Element to the left of the parent is always smaller than its parents. • Element to the right of the parent is always greater than its parent.
Binary Search Tree Arrange keys in a binary tree with the binary search tree property: K <K >K Example: 5, 3, 1, 10, 12, 7, 9
Operations of BSTs: Insert • Adds an element x to the tree so that the binary search tree property continues to hold • The basic algorithm • set the Index to Root • Check if the data where index is pointing is NULL, if so Insert x in place of NULL, and finish the process. • If the data is not NULL then compare the data with inserting item • If the inserting item is equal or bigger then traverse to the right else traverse to the left. • Continue with 2nd step again until
Insert Operations: 1st of 3 steps 1)- The function begins at the root node and compares item 32 with the root value 25. Since 32 > 25, we traverse the right subtree and look at node 35.
Insert Operations: 2nd of 3 steps 2)- Considering 35 to be the root of its own subtree, we compare item 32 with 35 and traverse the left subtree of 35.
Insert Operations: 3rd of 3 steps 3)- Create a leaf node with data value 32. Insert the new node as the left child of node 35. newNode = new Node; parent->left = newNode;
Insert in BST Start at root. • Insert Key 52. 52 > 51 Go right. 51 14 72 06 33 53 97 43 13 99 25 64 84
Insert in BST • Insert Key 52. 52 > 51 Go right. 51 14 72 06 33 53 97 43 13 99 25 64 84
Insert in BST • Insert Key 52. 51 14 72 52 < 72 Go left. 06 33 53 97 43 13 99 25 64 84
Insert in BST • Insert Key 52. 51 14 72 52 < 72 Go left. 06 33 53 97 43 13 99 25 64 84
Insert in BST • Insert Key 52. 51 14 72 06 33 53 97 52 < 53 Go left. 43 13 99 25 64 84
52 Insert in BST • Insert Key 52. 51 14 72 No more tree here. INSERT HERE 06 33 53 97 52 < 53 Go left. 43 13 99 25 64 84
Operations of BSTs: Search • looks for an element x within the tree • The basic algorithm • Set the index to root • Compare the searching item with the data at index • If (the searching item is smaller) then traverse to the left else traverse to the right. • Repeat the process untill the index is point to NULL or found the item.
51 14 72 06 33 53 97 43 13 99 25 64 84 Search in BST • Search for Key 43.
Search in BST Start at root. • Search for Key 43. 43 < 51 Go left. 51 14 72 06 33 53 97 43 13 99 25 64 84
Search in BST • Search for Key 43. 43 < 51 Go left. 51 14 72 06 33 53 97 43 13 99 25 64 84
Search in BST • Search for Key 43. 51 14 72 43 > 14 Go right. 06 33 53 97 43 13 99 25 64 84
Search in BST • Search for Key 43. 51 14 72 43 > 14 Go right. 06 33 53 97 43 13 99 25 64 84
Search in BST • Search for Key 43. 51 14 72 06 33 53 97 43 > 33 Go right. 43 13 99 25 64 84
Search in BST • Search for Key 43. 51 14 72 06 33 53 97 43 > 33 Go right. 43 13 99 25 64 84
Search in BST • Search for Key 43. 51 14 72 06 33 53 97 43 13 99 25 64 84 43 = 43 FOUND
Search in BST Start at root. • Search for Key 52. 52 > 51 Go right. 51 14 72 06 33 53 97 43 13 99 25 64 84
Search in BST • Search for Key 52. 52 > 51 Go right. 51 14 72 06 33 53 97 43 13 99 25 64 84
Search in BST • Search for Key 52. 51 14 72 52 < 72 Go left. 06 33 53 97 43 13 99 25 64 84
Search in BST • Search for Key 52. 51 14 72 52 < 72 Go left. 06 33 53 97 43 13 99 25 64 84
Search in BST • Search for Key 52. 51 14 72 06 33 53 97 52 < 53 Go left. 43 13 99 25 64 84
Search in BST • Search for Key 52. 51 14 72 06 33 53 97 52 < 53 Go left. 43 13 99 25 64 84 No more tree here. NOTFOUND
Tree Walk • Tree walk is a series of steps used to traverse a given tree. • inorder tree walk – is a tree walk with following steps + left sub-tree + middle node + right sub-tree