1 transform & conquer lecture 08 asst. prof. dr. bunyarit uyyanonvara it program, image and...

61
1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara 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 [email protected] 02 5013505 X 2005 ITS033 – Programming & Algorithms

Upload: sophia-washington

Post on 17-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

1

Transform & Conquer

Lecture 08

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information and Computer Technology

Sirindhorn International Institute of Technology

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

ITS033 – Programming & Algorithms

Page 2: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

2

ITS033Topic 01Topic 01 -- Problems & Algorithmic Problem SolvingProblems & Algorithmic Problem SolvingTopic 02Topic 02 – Algorithm Representation & Efficiency Analysis – Algorithm Representation & Efficiency AnalysisTopic 03Topic 03 - State Space of a problem - State Space of a problemTopic 04Topic 04 - Brute Force Algorithm - Brute Force AlgorithmTopic 05Topic 05 - Divide and Conquer - Divide and ConquerTopic 06Topic 06 -- Decrease and ConquerDecrease and ConquerTopic 07Topic 07 - Dynamics Programming - Dynamics ProgrammingTopic 08Topic 08 -- Transform and ConquerTransform and ConquerTopic 09Topic 09 - Graph Algorithms - Graph AlgorithmsTopic 10Topic 10 - Minimum Spanning Tree - Minimum Spanning TreeTopic 11Topic 11 - Shortest Path Problem - Shortest Path ProblemTopic 12Topic 12 - Coping with the Limitations of Algorithms Power - Coping with the Limitations of Algorithms Power

http://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.siit.tu.ac.th/bunyarit/its033.phpand and http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7

Midterm

Page 3: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

3

This Week Overview Introduction

Presort

Binary Search Tree

AVL Tree

Problem reduction

Page 4: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

4

Transform & Conquer: Introduction

Lecture 08.1

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information and Computer Technology

Sirindhorn International Institute of Technology

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

ITS033 – Programming & Algorithms

Page 5: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

5

Transform and ConquerThis 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)

Page 6: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

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.

Page 7: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

7

Introduction

Page 8: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

8

Transform & Conquer: Instance Simplification

Lecture 08.2

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information and Computer Technology

Sirindhorn International Institute of Technology

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

ITS033 – Programming & Algorithms

Page 9: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

9

Instance simplification - PresortingSolve a problem’s instance by transforming it intoanother simpler/easier instance of the same problem

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

Page 10: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

10

Searching with presortingProblem: 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?

Page 11: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

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)

Page 12: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

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 Afor i 0 to n - 2 do

if A[i]= A[i + 1] return falsereturn true

Page 13: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

13

Transform & Conquer: Representation Change

Lecture 08.3

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information and Computer Technology

Sirindhorn International Institute of Technology

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

ITS033 – Programming & Algorithms

Page 14: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

14

Searching ProblemProblem: 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

Page 15: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

15

Tree Characteristics

§-§- trees trees hierarchical structures that place elements in nodes along hierarchical structures that place elements in nodes along

branches that originate from a root. branches that originate from a root.

NodesNodes in a tree are subdivided into levels in which the topmost in a tree are subdivided into levels in which the topmost level holds the root node. level holds the root node.

Any node in a tree may have multiple successors at the next level. Any node in a tree may have multiple successors at the next level. Hence a tree is a non-linear structure. Hence a tree is a non-linear structure.

Page 16: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

16

Tree Structures

P r e side n t - C E O

P r o duc t io nM a n a ge r

Sa le sM a n a ge r

Sh ip p in gSup e r v iso r

P e r so n n e lM a n a ge r

W a r e h o useSup e r v iso r

P ur c h a sin gSup e r v iso r

H IER A R C H IC A L TR EE S TR U C TU R E

Page 17: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

17

Binary Tree: Formal Definition

A binary treeA binary tree T is a finite set of nodes with one of the following T is a finite set of nodes with one of the following properties:properties:

a.) T is a tree if the set of nodes is empty. a.) T is a tree if the set of nodes is empty. (An empty tree is a tree.) (An empty tree is a tree.)

b.) The set consists of b.) The set consists of a roota root, R, and exactly two distinct binary , R, and exactly two distinct binary trees, trees, the left subtree,the left subtree, TL and TL and the right subtree, the right subtree, TR. TR.

c.) The nodes in T consist of node R and all the nodes in TL and c.) The nodes in T consist of node R and all the nodes in TL and TR. TR.

Page 18: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

18

Binary Tree: exampleBinary Tree: example

Page 19: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

19

Binary TreeBinary Tree

Page 20: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

20

Binary Tree: DepthBinary Tree: Depth

Page 21: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

21

Selected Samples of Binary Trees

A

E

D

C

B

A

F

H

ED

CB

I

T ree ASiz e 9 D ep t h 3

T ree BSiz e 5 D ep t h 4

G

Page 22: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

22

Binary Search Trees

Binary Search TreeBinary Search Tree (BSTs) is a binary tree (BSTs) is a binary tree which has following properties:which has following properties:

Element to the Element to the leftleft of the parent is of the parent is always always smallersmaller than its parents. than its parents.

Element to the Element to the right right of the parent is of the parent is always always greatergreater than its parent. than its parent.

Page 23: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

23

Binary Search TreeArrange keys in a binary tree with the binary search tree property:

K

<K >K

Example: 5, 3, 1, 10, 12, 7, 9Example: 5, 3, 1, 10, 12, 7, 9

Page 24: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

24

Operations of BSTs: Insert

Adds an element x to the tree so that the binary search tree Adds an element x to the tree so that the binary search tree property continues to holdproperty continues to hold

The basic algorithmThe basic algorithm set the Index to Rootset the Index to Root Check if the data where index is pointing is NULL, if so Check if the data where index is pointing is NULL, if so

Insert x in place of NULL, and Insert x in place of NULL, and finish the processfinish the process.. If the data is not NULL then compare the data with inserting If the data is not NULL then compare the data with inserting

itemitem If the inserting item is equal or bigger then traverse to the If the inserting item is equal or bigger then traverse to the

right else traverse to the left.right else traverse to the left. Continue with 2Continue with 2ndnd step again until step again until

Page 25: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

25

Insert Operations: 1st of 3 steps

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

2 5

4 01 2

3 52 0

p aren t

t

(a)St ep 1 : C o m p are 3 2 an d 2 5 .T rav ers e t h e righ t s u b t ree.

Page 26: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

26

Insert Operations: 2nd of 3 steps

2)-2)- Considering 35 to be the root of its own subtree, we compare item 32 with 35 and traverse the left subtree of 35.

(b )St ep 2 : C o m p are 3 2 an d 3 5 .T rav ers e t h e left s u b t ree.

2 5

4 01 2

3 52 0

t

p aren t

Page 27: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

27

Insert Operations: 3rd of 3 steps3)-3)- Create a leaf node with data value 32. Create a leaf node with data value 32. Insert the new node as the left child of node Insert the new node as the left child of node 35.35.

newNode = new Node;newNode = new Node;

parent->left = newNode;parent->left = newNode;

(c)St ep 3 : In s ert 3 2 as left ch ildo f p aren t 3 5

2 5

4 01 2

3 52 0 p aren t

3 2

Page 28: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

28

Insert in BSTInsert Key 52.

51

14 72

06 33 53 97

6425 4313 9984

Start at root.

52 > 51Go right.

Page 29: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

29

Insert Key 52.

51

14 72

06 33 53 97

6425 4313 9984

52 > 51Go right.

Insert in BST

Page 30: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

30

Insert Key 52.

51

14 72

06 33 53 97

6425 4313 9984

52 < 72Go left.

Insert in BST

Page 31: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

31

Insert Key 52.

51

14 72

06 33 53 97

6425 4313 9984

52 < 72Go left.

Insert in BST

Page 32: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

32

Insert Key 52.

51

14 72

06 33 53 97

6425 4313 9984

52 < 53Go left.

Insert in BST

Page 33: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

33

Insert Key 52.

51

14 72

06 33 53 97

6425 4313 9984

No more tree here.INSERT HERE

52 < 53Go left.

52

Insert in BST

Page 34: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

34

Operations of BSTs: Search

looks for an element looks for an element xx within the tree within the tree

The basic algorithmThe basic algorithm Set the index to rootSet the index to root Compare the searching item with the data at indexCompare the searching item with the data at index If If (the searching item is smaller) then traverse to the left (the searching item is smaller) then traverse to the left elseelse

traverse to the right.traverse to the right. Repeat the process untill the index is point to NULL or found Repeat the process untill the index is point to NULL or found

the item.the item.

Page 35: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

35

Search in BSTSearch for Key 43.

51

14 72

06 33 53 97

6425 4313 9984

Page 36: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

36

Search in BSTSearch for Key 43.

51

14 72

06 33 53 97

6425 4313 9984

Start at root.

43 < 51Go left.

Page 37: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

37

Search in BSTSearch for Key 43.

51

14 72

06 33 53 97

6425 4313 9984

43 < 51Go left.

Page 38: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

38

Search in BSTSearch for Key 43.

51

14 72

06 33 53 97

6425 4313 9984

43 > 14Go right.

Page 39: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

39

Search in BSTSearch for Key 43.

51

14 72

06 33 53 97

6425 4313 9984

43 > 14Go right.

Page 40: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

40

Search in BSTSearch for Key 43.

51

14 72

06 33 53 97

6425 4313 9984

43 > 33Go right.

Page 41: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

41

Search in BSTSearch for Key 43.

51

14 72

06 33 53 97

6425 4313 9984

43 > 33Go right.

Page 42: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

42

Search in BSTSearch for Key 43.

51

14 72

06 33 53 97

6425 4313 998443 = 43FOUND

Page 43: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

43

Search in BSTSearch for Key 52.

51

14 72

06 33 53 97

6425 4313 9984

Start at root.

52 > 51Go right.

Page 44: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

44

Search in BSTSearch for Key 52.

51

14 72

06 33 53 97

6425 4313 9984

52 > 51Go right.

Page 45: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

45

Search in BSTSearch for Key 52.

51

14 72

06 33 53 97

6425 4313 9984

52 < 72Go left.

Page 46: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

46

Search in BSTSearch for Key 52.

51

14 72

06 33 53 97

6425 4313 9984

52 < 72Go left.

Page 47: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

47

Search in BSTSearch for Key 52.

51

14 72

06 33 53 97

6425 4313 9984

52 < 53Go left.

Page 48: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

48

Search in BSTSearch for Key 52.

51

14 72

06 33 53 97

6425 4313 9984No more tree here.

NOT FOUND

52 < 53Go left.

Page 49: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

49

Tree Walk

Tree walkTree walk is a series of steps used to traverse a is a series of steps used to traverse a given tree.given tree.

inorder tree walkinorder tree walk – is a tree walk with following – is a tree walk with following steps steps

+ left sub-tree+ left sub-tree

+ middle node+ middle node

+ right sub-tree+ right sub-tree

Page 50: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

50

Binary Tree: exampleBinary Tree: example

Page 51: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

51

Tree Structures

+

e

/

ba

*

dc

-

B IN ARY EXPRES S IO N T REE FO R " a* b + (c -d ) / e "

Page 52: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

52

In-order Tree Walk

What does the following code do?What does the following code do?

TreeWalk(x)TreeWalk(x)

TreeWalk(left[x]);TreeWalk(left[x]);

print(x);print(x);

TreeWalk(right[x]);TreeWalk(right[x]);

A: prints elements in sorted (increasing) orderA: prints elements in sorted (increasing) order This is called an This is called an inorder tree walkinorder tree walk

Preorder tree walkPreorder tree walk: print root, then left, then right: print root, then left, then right Postorder tree walkPostorder tree walk: print left, then right, then root: print left, then right, then root

Page 53: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

53

Transform & Conquer: problem reduction

Lecture 08.4

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information and Computer Technology

Sirindhorn International Institute of Technology

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

ITS033 – Programming & Algorithms

Page 54: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

54

Problem ReductionThis 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.

Page 55: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

55

Problem Reduction Problem reduction: If you need to solve a problem,

reduce it to another problem that you know how to solve

Page 56: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

56

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.

Page 57: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

57

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.

Page 58: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

58

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.

Page 59: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

59

HomeworkS = {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

Page 60: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

60

ITS033Topic 01Topic 01 -- Problems & Algorithmic Problem SolvingProblems & Algorithmic Problem SolvingTopic 02Topic 02 – Algorithm Representation & Efficiency Analysis – Algorithm Representation & Efficiency AnalysisTopic 03Topic 03 - State Space of a problem - State Space of a problemTopic 04Topic 04 - Brute Force Algorithm - Brute Force AlgorithmTopic 05Topic 05 - Divide and Conquer - Divide and ConquerTopic 06Topic 06 -- Decrease and ConquerDecrease and ConquerTopic 07Topic 07 - Dynamics Programming - Dynamics ProgrammingTopic 08Topic 08 -- Transform and ConquerTransform and ConquerTopic 09Topic 09 - Graph Algorithms - Graph AlgorithmsTopic 10Topic 10 - Minimum Spanning Tree - Minimum Spanning TreeTopic 11Topic 11 - Shortest Path Problem - Shortest Path ProblemTopic 12Topic 12 - Coping with the Limitations of Algorithms Power - Coping with the Limitations of Algorithms Power

http://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.siit.tu.ac.th/bunyarit/its033.phpand and http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7

Midterm

Page 61: 1 Transform & Conquer Lecture 08 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

61

End of Chapter 8

Thank you!