1 / 61

Transform & Conquer

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

awildaj
Télécharger la présentation

Transform & Conquer

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. This Week Overview • Introduction • Presort • Binary Search Tree • AVL Tree • Problem reduction

  4. 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

  5. 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)

  6. 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.

  7. Introduction

  8. 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

  9. 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.

  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: O(nlog n) + O(log n) = O(nlog n) Good or bad? Why do we have our dictionaries, telephone directories, etc. sorted?

  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: O(nlog n) + O(n) = O(nlog n) • Brute force algorithm Compare all pairs of elements Efficiency: O(n2)

  12. 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

  13. 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

  14. 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

  15. 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.

  16. Tree Structures

  17. 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.

  18. Binary Tree: example

  19. Binary Tree

  20. Binary Tree: Depth

  21. Selected Samples of Binary Trees

  22. 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.

  23. 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

  24. 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

  25. 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.

  26. 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.

  27. 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;

  28. 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

  29. Insert in BST • Insert Key 52. 52 > 51 Go right. 51 14 72 06 33 53 97 43 13 99 25 64 84

  30. Insert in BST • Insert Key 52. 51 14 72 52 < 72 Go left. 06 33 53 97 43 13 99 25 64 84

  31. Insert in BST • Insert Key 52. 51 14 72 52 < 72 Go left. 06 33 53 97 43 13 99 25 64 84

  32. Insert in BST • Insert Key 52. 51 14 72 06 33 53 97 52 < 53 Go left. 43 13 99 25 64 84

  33. 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

  34. 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.

  35. 51 14 72 06 33 53 97 43 13 99 25 64 84 Search in BST • Search for Key 43.

  36. 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

  37. Search in BST • Search for Key 43. 43 < 51 Go left. 51 14 72 06 33 53 97 43 13 99 25 64 84

  38. Search in BST • Search for Key 43. 51 14 72 43 > 14 Go right. 06 33 53 97 43 13 99 25 64 84

  39. Search in BST • Search for Key 43. 51 14 72 43 > 14 Go right. 06 33 53 97 43 13 99 25 64 84

  40. Search in BST • Search for Key 43. 51 14 72 06 33 53 97 43 > 33 Go right. 43 13 99 25 64 84

  41. Search in BST • Search for Key 43. 51 14 72 06 33 53 97 43 > 33 Go right. 43 13 99 25 64 84

  42. Search in BST • Search for Key 43. 51 14 72 06 33 53 97 43 13 99 25 64 84 43 = 43 FOUND

  43. 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

  44. Search in BST • Search for Key 52. 52 > 51 Go right. 51 14 72 06 33 53 97 43 13 99 25 64 84

  45. Search in BST • Search for Key 52. 51 14 72 52 < 72 Go left. 06 33 53 97 43 13 99 25 64 84

  46. Search in BST • Search for Key 52. 51 14 72 52 < 72 Go left. 06 33 53 97 43 13 99 25 64 84

  47. Search in BST • Search for Key 52. 51 14 72 06 33 53 97 52 < 53 Go left. 43 13 99 25 64 84

  48. 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

  49. 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

  50. Binary Tree: example

More Related