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
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
3
This Week Overview Introduction
Presort
Binary Search Tree
AVL Tree
Problem reduction
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
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)
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
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
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.
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?
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 Afor i 0 to n - 2 do
if A[i]= A[i + 1] return falsereturn true
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
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
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.
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
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.
18
Binary Tree: exampleBinary Tree: example
19
Binary TreeBinary Tree
20
Binary Tree: DepthBinary Tree: Depth
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
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.
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
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
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.
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
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
28
Insert in BSTInsert Key 52.
51
14 72
06 33 53 97
6425 4313 9984
Start at root.
52 > 51Go right.
29
Insert Key 52.
51
14 72
06 33 53 97
6425 4313 9984
52 > 51Go right.
Insert in BST
30
Insert Key 52.
51
14 72
06 33 53 97
6425 4313 9984
52 < 72Go left.
Insert in BST
31
Insert Key 52.
51
14 72
06 33 53 97
6425 4313 9984
52 < 72Go left.
Insert in BST
32
Insert Key 52.
51
14 72
06 33 53 97
6425 4313 9984
52 < 53Go left.
Insert in BST
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
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.
35
Search in BSTSearch for Key 43.
51
14 72
06 33 53 97
6425 4313 9984
36
Search in BSTSearch for Key 43.
51
14 72
06 33 53 97
6425 4313 9984
Start at root.
43 < 51Go left.
37
Search in BSTSearch for Key 43.
51
14 72
06 33 53 97
6425 4313 9984
43 < 51Go left.
38
Search in BSTSearch for Key 43.
51
14 72
06 33 53 97
6425 4313 9984
43 > 14Go right.
39
Search in BSTSearch for Key 43.
51
14 72
06 33 53 97
6425 4313 9984
43 > 14Go right.
40
Search in BSTSearch for Key 43.
51
14 72
06 33 53 97
6425 4313 9984
43 > 33Go right.
41
Search in BSTSearch for Key 43.
51
14 72
06 33 53 97
6425 4313 9984
43 > 33Go right.
42
Search in BSTSearch for Key 43.
51
14 72
06 33 53 97
6425 4313 998443 = 43FOUND
43
Search in BSTSearch for Key 52.
51
14 72
06 33 53 97
6425 4313 9984
Start at root.
52 > 51Go right.
44
Search in BSTSearch for Key 52.
51
14 72
06 33 53 97
6425 4313 9984
52 > 51Go right.
45
Search in BSTSearch for Key 52.
51
14 72
06 33 53 97
6425 4313 9984
52 < 72Go left.
46
Search in BSTSearch for Key 52.
51
14 72
06 33 53 97
6425 4313 9984
52 < 72Go left.
47
Search in BSTSearch for Key 52.
51
14 72
06 33 53 97
6425 4313 9984
52 < 53Go left.
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.
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
50
Binary Tree: exampleBinary Tree: example
51
Tree Structures
+
e
/
ba
*
dc
-
B IN ARY EXPRES S IO N T REE FO R " a* b + (c -d ) / e "
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
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
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.
55
Problem Reduction Problem reduction: If you need to solve a problem,
reduce it to another problem that you know how to solve
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.
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.
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.
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
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
61
End of Chapter 8
Thank you!