Transform & Conquer Lecture 08 ITS033 – Programming & Algorithms

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Advanced Data Structures
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
Chapter 6: Transform and Conquer
COSC 3100 Transform and Conquer
Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
TCSS 343, version 1.1 Algorithms, Design and Analysis Transform and Conquer Algorithms Presorting HeapSort.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Chapter 10: Algorithm Design Techniques
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
1 Divide-and-Conquer Approach Lecture 05 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,
1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
1 State Space of a Problem Lecture 03 ITS033 – Programming & Algorithms Asst. Prof.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Starting at Binary Trees
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
1 Dynamic Programming Topic 07 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Lecture 14 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
COMP 53 – Week Fourteen Trees.
Decrease-and-Conquer Approach
Binary Search Tree (BST)
Chapter 6 Transform-and-Conquer
Section 8.1 Trees.
ITEC 2620M Introduction to Data Structures
Chapter 6 Transform and Conquer.
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
Find in a linked list? first last 7  4  3  8 NULL
Chapter 6: Transform and Conquer
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
CSC 380: Design and Analysis of Algorithms
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Transform & Conquer Lecture 08 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/bunyarit bunyarit@siit.tu.ac.th 02 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 Transform & Conquer: Introduction 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/bunyarit bunyarit@siit.tu.ac.th 02 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.

Introduction

Transform & Conquer: Instance Simplification Lecture 08.2 ITS033 – Programming & Algorithms Transform & Conquer: Instance Simplification 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/bunyarit bunyarit@siit.tu.ac.th 02 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 Transform & Conquer: Representation Change 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/bunyarit bunyarit@siit.tu.ac.th 02 5013505 X 2005

Searching Problem file size (internal vs. external) 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.

Tree Structures

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 Tree: example

Binary Tree

Binary Tree: Depth

Selected Samples of Binary Trees

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 Insert Key 52. Start at root. 52 > 51 Go right. 51 14 72 06 33 53 97 13 25 43 64 84 99

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

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

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

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

Insert in BST Insert Key 52. 51 14 72 No more tree here. INSERT HERE 06 33 53 97 52 < 53 Go left. 13 25 43 52 64 84 99

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

Search in BST Search for Key 52. 51 14 72 06 33 53 97 52 < 53 Go left. 13 25 43 64 84 99 No more tree here. NOT FOUND

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

Binary Tree: example

Tree Structures

In-order Tree Walk What does the following code do? TreeWalk(x) TreeWalk(left[x]); print(x); TreeWalk(right[x]); A: prints elements in sorted (increasing) order This is called an inorder tree walk Preorder tree walk: print root, then left, then right Postorder tree walk: print left, then right, then root

Transform & Conquer: problem reduction Lecture 08.4 ITS033 – Programming & Algorithms Transform & Conquer: problem reduction 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/bunyarit bunyarit@siit.tu.ac.th 02 5013505 X 2005

Problem Reduction This variation of transform-and-conquer solves a problem by a transforming it into different problem for which an algorithm is already available. To be of practical value, the combined time of the transformation and solving the other problem should be smaller than solving the problem as given by another method.

Problem Reduction Problem reduction: If you need to solve a problem, reduce it to another problem that you know how to solve

Reduction to Graph Problems Vertices of a graph typically represent possible states of the problem in question while edges indicate permitted transitions among such states. One of the graph’s vertices represents an initial state, while another represents a goal state of the problem. Such a graph is called a state-space graph. Thus, the transformation just described reduces the problem to the question about a path from the initial-state vertex to a goal-state vertex.

Example A peasant finds himself on a river bank with a wolf, a goat, and a head of cabbage. He needs to transport all three to the other side of the river in his boat. However, the boat has room only for the peasant himself and one other item (either the wolf, the goat, or the cabbage). In his absence, the wolf would eat the goat, and the goat would eat the cabbage. Find a way for the peasant to solve his problem or prove that it has no solution.

Example P, w, g, c stand for the peasant, the wolf, the goat, and the cabbage, respectively; the two bars | | denote the river; there exist two distinct simple paths from the initial state vertex to the final state vertex. Hence, this puzzle has two solutions, each of which requires seven river crossings.

Homework S = {1, 5, 3, 4, 8, 6, 7, 8, 4, 2, 1, 9, 11, 12,15, 14} 1. Construct a Binary Search Tree from the given set S 2. Construct a AVL Tree from the given set S

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

End of Chapter 8 Thank you!