Unit 3 - Basic Search and Traversal Techniques

113
Unit 3 Basic search and Traversal Techniques

Transcript of Unit 3 - Basic Search and Traversal Techniques

Page 1: Unit 3 - Basic Search and Traversal Techniques

Unit 3Basic search and Traversal

Techniques

Page 2: Unit 3 - Basic Search and Traversal Techniques

Topics to be covered…• Binary Tree Traversal

– Inorder

– Preorder

• Search and traversal techniques for Graphs– BFS

– BFT

– DFS

– DFT

• Code optimization,

• AND/OR graphs,

• Game trees

• Biconnected Components and depth first search

Page 3: Unit 3 - Basic Search and Traversal Techniques

Binary Tree Traversal

• It is a way in which each node in the tree is visited exactly once in systematic manner.

• Popular ways of Binary tree traversal1. Preorder Traversal

2. Postorder Traversal

3. Inorder Traversal

Page 4: Unit 3 - Basic Search and Traversal Techniques

Preorder Traversal

The preorder of a non-empty binary tree is

defined as

1. Visit the root node.

2. Traverse the left sub tree in preorder.

3. Traverse the right sub tree in preorder.

4

Page 5: Unit 3 - Basic Search and Traversal Techniques

Preorder traversal of the above binary tree

A B D E I C F G JRoot

5

A

B C

D E F G

J

Root

I

left Right

Page 6: Unit 3 - Basic Search and Traversal Techniques

Algorithm

preorder(BTNODE *tree)

{

if(tree != NULL)

{

printf(“%c”,tree->data); /* step 1*/

preorder(tree->lchild); /*step 2*/

preorder(tree->rchild); /*step 3*/

}

return;

}6

Page 7: Unit 3 - Basic Search and Traversal Techniques

Inorder Traversal

The Inorder of a non-empty binary tree is

defined as

1. Traverse the left sub tree in inorder.

2. Visit the root node.

3. Traverse the right sub tree in inorder.

7

Page 8: Unit 3 - Basic Search and Traversal Techniques

8

A

B C

D E F G

J

Root

I

left Right

Inorder traversal of the above binary tree

D B I E A F C G J

Page 9: Unit 3 - Basic Search and Traversal Techniques

Algorithm

inorder(BTNODE *tree)

{

if(tree != NULL)

{

inorder(tree->lchild); /*step 1*/

printf(“%c”,tree->data); /* step 2*/

inorder(tree->rchild); /*step 3*/

}

return;

}9

Page 10: Unit 3 - Basic Search and Traversal Techniques

Postorder Traversal

The postorder of a non-empty binary tree is

defined as

1. Traverse the left sub tree in postorder.

2. Traverse the right sub tree in postorder.

3. Visit the root node.

10

Page 11: Unit 3 - Basic Search and Traversal Techniques

• Post order traversal of the above binary tree

D I E B F J G C A11

A

B C

D E F G

J

Root

I

left Right

Page 12: Unit 3 - Basic Search and Traversal Techniques

Algorithm

postorder(BTNODE *tree)

{

if(tree != NULL)

{

postorder(tree->lchild); /*step 1*/

postorder(tree->rchild); /*step 2*/

printf(“%c”,tree->data); /* step 3*/

}

return;

}12

Page 13: Unit 3 - Basic Search and Traversal Techniques

Binary Search Tree (BST)

• Tree in which each node has left and right child.

• If traversed in inorder, we get a file in ascending order.

13

Page 14: Unit 3 - Basic Search and Traversal Techniques

• Traverse the tree in inorder, we get

9 122125424348

14

25

12 43

9 21 42 48

Page 15: Unit 3 - Basic Search and Traversal Techniques

SEARCHING

• Start with the root.

• Eg; search 21 in the given tree– Compare 21<25, true. Take left branch.

– At left child second comparison is made. Here 21>12. so take right branch.

– On third comparison, since 21=21, the search is announced successful.

15

Page 16: Unit 3 - Basic Search and Traversal Techniques

Algorithm for SearchingSearch(int key_val, int &found, BTNODE *(&parent)){ BTNODE *p;found=0;parent=NULL;if(tree==NULL){ return;}p=tree; 16

Page 17: Unit 3 - Basic Search and Traversal Techniques

while(p!=null)

{

if(p->data==key_val)

{found=1;

return;

}

if(p->data > key_val)

{parent=p;

p=p->lchild;

}

else

if(p->data < key_val)

{parent=p;

p=p->rchild;

}

}}

Page 18: Unit 3 - Basic Search and Traversal Techniques

Advantages

• Advantage over an array is that all the insertion and deletions and searching are performed efficiently.

18

Page 19: Unit 3 - Basic Search and Traversal Techniques

Insertion in a BST

• To insert an element 23 in an tree shown below

• Search for element 23 in that tree.

• Search causes to reach at the node 21 with no where to go.

• Since 23>21, we simply insert node 23 as right child to node 21.

19

25

12 43

9 21 42 48

25

12 43

9 21 42 48

23

23>21

Page 20: Unit 3 - Basic Search and Traversal Techniques

Algorithm for insertioninsert BST(int num)

{

int found;

BTNODE *temp,*parent;

search_node(num,found,parent);

if(found==1)

printf(“Node already exists”);

else

20

Page 21: Unit 3 - Basic Search and Traversal Techniques

{

temp=(BTNODE*)malloc(sizeof(BTNODE));

temp->data=num;

temp->lchild=temp->rchild=NULL;

if(parent==NULL)

tree=temp;

else

{ if(parent->data > num)

parent->lchild=temp;

else

parent-> rchild=temp;

}}}

Page 22: Unit 3 - Basic Search and Traversal Techniques

Deletion in a BST

Three cases:

1. Node to be deleted has no children.

2. Node to be deleted has exactly one subtree.

3. Node to be deleted has two subtrees.

22

Page 23: Unit 3 - Basic Search and Traversal Techniques

In first situation ,

As there is no children for the node, so that node is deleted with out any further adjustments to the tree.

23

Page 24: Unit 3 - Basic Search and Traversal Techniques

In second case,

The node to be deleted has exactly one child, so this child is moved up the tree to occupy its place.

24

25

12 43

9 21 42 48

23Node to be

deleted

25

12 43

9 23 42 48

After Deletion of the node 21

Before Deletion of the node 21

Page 25: Unit 3 - Basic Search and Traversal Techniques

Third case,

The node to be deleted has two sub trees. The in order successor of the node should be moved up to occupy its

place.

25

25

12 43

9 21 42 48

23

Node to be

deleted42

12 43

9 21 48

23

After Deletion of

the Root node 25Before Deletion of

the Root node 25

Page 26: Unit 3 - Basic Search and Traversal Techniques

C code for deletiondelete_BST(int number, BTNODE *tree)

{

int found;

BTNODE *temp, *parent, *temp_succ;

if(tree==NULL)

{

printf(“ Tree is empty”);

return;

}26

Page 27: Unit 3 - Basic Search and Traversal Techniques

parent=temp=Null;

search_BST(number,parent,temp,tree,found)

if(found==0)

{ printf(“ node to be deleted is not found”);

return;

}

Page 28: Unit 3 - Basic Search and Traversal Techniques

/* first case : The node to be deleted has no children*/

If((temp->lchild == NULL) && (temp->rchild ==NULL))

{

if(parent-.rchild == temp)

{

parent->rchild = NULL;

}

else

{

parent->lchild= NULL;

}

free(temp);

return;

}

28

Page 29: Unit 3 - Basic Search and Traversal Techniques

/* second case: if node to be deleted has only left child */

If(temp->lchild != NULL)&&(temp->rchild == NULL)

{

if(parent->lchild ==temp)

{

parent->lchild =temp->lchild;

}

else

{

parent->rchild = temp->lchild;

}

free(temp);

return;

}

29

Page 30: Unit 3 - Basic Search and Traversal Techniques

/* Third case: if node to be deleted has two children */

If((temp->lchild != NULL) && (temp->rchild != NULL))

{

parent = temp;

temp_succ = temp->rchild;

while(temp_succ != NULL)

{

parent=temp_succ;

temp_succ=temp_succ->lchild;

}

temp->data = temp_succ->data;

temp=temp_succ;

}

}

30

Page 31: Unit 3 - Basic Search and Traversal Techniques

Efficiency of Binary Search Tree Operations

• The timing analysis of searching a binary search

tree, for randomly inserted records is O(log n).

• Worst case behavior of a binary search tree

would occur when records are inserted in sorted

order. In this case the search time is O(n), as the

resulting tree consists all NULL left children.

31

Page 32: Unit 3 - Basic Search and Traversal Techniques

Search and traversal techniques for graphs

• Many graph algorithms require one to systematically examine the nodes and edges of a graph G.

• There are two standard ways that this is done.

– Breadth- first search .

– Depth- first search.

Page 33: Unit 3 - Basic Search and Traversal Techniques

• Breadth first search will use a queue as an auxiliary structure to hold nodes for future processing.

• Depth first search will use a stack.

Page 34: Unit 3 - Basic Search and Traversal Techniques

Breadth-first Search• Expands the frontier between discovered and

undiscovered vertices uniformly across the breadth of the frontier.

– A vertex is “discovered” the first time it is encountered during the search.

– A vertex is “finished” if all vertices adjacent to it have been discovered.

• Colors the vertices to keep track of progress.– White – Undiscovered.

– Gray – Discovered but not finished.

– Black – Finished.

• Colors are required only to reason about the algorithm. Can be implemented without colors.

Page 35: Unit 3 - Basic Search and Traversal Techniques

Example (BFS)

0

r s t u

v w x y

Q: s 0

(Courtesy of Prof. Jim Anderson)

Page 36: Unit 3 - Basic Search and Traversal Techniques

Example (BFS)

1 0

1

r s t u

v w x y

Q: w r 1 1

Page 37: Unit 3 - Basic Search and Traversal Techniques

Example (BFS)

1 0

1 2

2

r s t u

v w x y

Q: r t x 1 2 2

Page 38: Unit 3 - Basic Search and Traversal Techniques

Example (BFS)

1 0

1 2

2

2

r s t u

v w x y

Q: t x v 2 2 2

Page 39: Unit 3 - Basic Search and Traversal Techniques

Example (BFS)

1 0

1 2

2 3

2

r s t u

v w x y

Q: x v u 2 2 3

Page 40: Unit 3 - Basic Search and Traversal Techniques

Example (BFS)

1 0

1 2 3

2 3

2

r s t u

v w x y

Q: v u y 2 3 3

Page 41: Unit 3 - Basic Search and Traversal Techniques

Example (BFS)

1 0

1 2 3

2 3

2

r s t u

v w x y

Q: u y 3 3

Page 42: Unit 3 - Basic Search and Traversal Techniques

Example (BFS)

1 0

1 2 3

2 3

2

r s t u

v w x y

Q: y 3

Page 43: Unit 3 - Basic Search and Traversal Techniques

Example (BFS)

1 0

1 2 3

2 3

2

r s t u

v w x y

Q:

Page 44: Unit 3 - Basic Search and Traversal Techniques

Example (BFS)

1 0

1 2 3

2 3

2

r s t u

v w x y

BF Tree

Page 45: Unit 3 - Basic Search and Traversal Techniques

BFS(G,s)

1. for each vertex u in V[G] – {s}

2 do color[u] white

3 d[u]

4 [u] nil

5 color[s] gray

6 d[s] 0

7 [s] nil

8 Q

9 enqueue(Q,s)

10 while Q

11 do u dequeue(Q)

12 for each v in Adj[u]

13 do if color[v] = white

14 then color[v] gray

15 d[v] d[u] + 1

16 [v] u

17 enqueue(Q,v)

18 color[u] black

white: undiscoveredgray: discoveredblack: finished

Q: a queue of discovered verticescolor[v]: color of vd[v]: distance from s to v[u]: predecessor of v

Page 46: Unit 3 - Basic Search and Traversal Techniques

Applications of BFS

• Used to solve following problems– Testing whether graph is connected.

– Computing the spanning forest of the graph.

– Computing a cycle in graph or reporting that no such cycle exists.

Page 47: Unit 3 - Basic Search and Traversal Techniques

Depth-first Search (DFS)• Explore edges out of the most recently discovered

vertex v.

• When all edges of v have been explored, backtrack to explore other edges leaving the vertex from which v was discovered (its predecessor).

• “Search as deep as possible first.”

• Continue until all vertices reachable from the original source are discovered.

• If any undiscovered vertices remain, then one of them is chosen as a new source and search is repeated from that source.

Comp 122, Fall 2004

Page 48: Unit 3 - Basic Search and Traversal Techniques

Depth-first Search• Input: G = (V, E), directed or undirected. No source

vertex given!

• Output:– 2 timestamps on each vertex. Integers between 1 and 2|

V|.

• d[v] = discovery time (v turns from white to gray)

• f [v] = finishing time (v turns from gray to black)

– [v] : predecessor of v = u, such that v was discovered during the scan of u’s adjacency list.

• Uses the same coloring scheme for vertices as BFS.

Comp 122, Fall 2004

Page 49: Unit 3 - Basic Search and Traversal Techniques

Pseudo-codeDFS(G)

1. for each vertex u V[G]

2. do color[u] white

3. [u] NIL

4. time 0

5. for each vertex u V[G]

6. do if color[u] = white

7. then DFS-Visit(u)Uses a global timestamp time.

DFS-Visit(u)1. color[u] GRAY White vertex u

has been discovered2. time time + 13. d[u] time4. for each v Adj[u]5. do if color[v] = WHITE6. then [v] u7. DFS-Visit(v)8. color[u] BLACK Blacken u;

it is finished.9. f[u] time time + 1

Page 50: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

1/

u v w

x y z

u1 Push u

Stack

Page 51: Unit 3 - Basic Search and Traversal Techniques

Step 1: DFS(G) If suppose G is a given graph

Line 1: For each vertex uE V[G]

do color[u]= white

parent[u]= NIL

That is any vertex from the given graph and assign color white. Time=0;

For each vertex uEv[G] if color[u]= white(true)

Then DFS- VISIT[u]

since above condition checked by if statement is correct therefore now we apply DES VISIT algo for u

Step 2: Apply DFS-VISIT algorithm for u.

Line 1: color[u]=gray

Line 2: discover[u]= time= discover[u]=0

Line 3: time=time+1

time=0+1=1

Line 4: for each vEadj[u]={v,x}

Line 5: if color[v]=white [true]

Line 6: then [v] u [ make u as the parent of u]

Line 7: DFS-Visit(v)

Page 52: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

1/ 2/

u v w

x y z

u1Push [v]

Stack

v2

Page 53: Unit 3 - Basic Search and Traversal Techniques

Line 1: color[v]= gray

Line 2: discover[v]= time= 1

Line 3: time= time+1= 1+1=2 assign time =2 to vertex v

Line 4: now, find adjacent of vertex v.

for each v E adj[u]

adj[v]={y}

Line 5: now, check color of y

if color[y]=white (true)

line 6: make vertex v as a parent of vertex y.

parent[y]=v.

Page 54: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

1/

3/

2/

u v w

x y z

u1

Push [y]

Stack

v2y3

Page 55: Unit 3 - Basic Search and Traversal Techniques

Now find the adjacent vertex for y as

Adj[y]= [x]

Now check the color[x]= white (true)

Make the y as the parent of vertex x.

Again apply DFS_VISIT for vertex x

Set the color[x]= gray

Set discover time as discover time[x]= time= 3

time=time+1=4 assign time = 4 to vertex x

Page 56: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

1/

4/ 3/

2/

u v w

x y z

u1

Push [x]

Stack

v2y3x4

Page 57: Unit 3 - Basic Search and Traversal Techniques

Now again find the adjacent of vertex x.

Adj[x]= v

If the color[v]= white (false)

Then

Color[x]= black.

Finish x= time=time+1

Finish[x]= 4+1=5

Pop the top element from stack i.e, x as we have finished it.

Page 58: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

1/

4/ 3/

2/

u v w

x y z

B

u1

Pop [x]Stack after

v2y3

Page 59: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

1/

4/5 3/

2/

u v w

x y z

B

Page 60: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

1/

4/5 3/6

2/

u v w

x y z

B

Page 61: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

1/

4/5 3/6

2/7

u v w

x y z

B

Page 62: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

1/

4/5 3/6

2/7

u v w

x y z

BF

Page 63: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

1/8

4/5 3/6

2/7

u v w

x y z

BF

Page 64: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

1/8

4/5 3/6

2/7 9/

u v w

x y z

BF

Page 65: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

1/8

4/5 3/6

2/7 9/

u v w

x y z

BF C

Page 66: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

1/8

4/5 3/6 10/

2/7 9/

u v w

x y z

BF C

Page 67: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

1/8

4/5 3/6 10/

2/7 9/

u v w

x y z

BF C

B

Page 68: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

1/8

4/5 3/6 10/11

2/7 9/

u v w

x y z

BF C

B

Page 69: Unit 3 - Basic Search and Traversal Techniques

Example (DFS)

Comp 122, Fall 2004

1/8

4/5 3/6 10/11

2/7 9/12

u v w

x y z

BF C

B

Each vertex has two time stamps•The first stamp records when vertex is first recorded and •The second time stamp records when the search finishes examines adjacency list of vertex.

Page 70: Unit 3 - Basic Search and Traversal Techniques

The order in which the nodes are visited in DFS

Page 71: Unit 3 - Basic Search and Traversal Techniques

Application of DFS

• Testing whether graph is connected.

• Computing a spanning forest of graph.

• Computing a path between two vertices of graph or equivalently reporting that no such path exist.

• Computing a cycle in graph or equivalently reporting that no such cycle exists.

Page 72: Unit 3 - Basic Search and Traversal Techniques

Code Optimization

Page 73: Unit 3 - Basic Search and Traversal Techniques

Introduction

• Criteria for Code-Improving Transformation:

– Meaning must be preserved (correctness)

– Speedup must occur on average.

– Work done must be worth the effort.

• Opportunities:– Programmer (algorithm, directives)

– Intermediate code

– Target code

Page 74: Unit 3 - Basic Search and Traversal Techniques

Peephole Optimizations 1. A Simple but effective technique for locally improving

the target code is peephole optimization,

2. A method for trying to improve the performance of the target program by examining a short sequence of target instructions and replacing these instructions by a shorter or faster sequence whenever possible.

Characteristics of peephole optimization

1. Redundant instruction elimination

2. Flow of control information

3. Algebraic Simplification

4. Use of machine Idioms

Page 75: Unit 3 - Basic Search and Traversal Techniques

Peephole Optimizations

• Constant Folding

x := 32 becomes x := 64

x := x + 32

• Unreachable Code

goto L2

x := x + 1 No need• Flow of control optimizations

goto L1 becomes goto L2

L1: goto L2 No needed if no other L1 branch

Page 76: Unit 3 - Basic Search and Traversal Techniques

Peephole Optimizations

• Algebraic Simplification

x := x + 0 No needed • Dead code

x := 32 where x not used after statement

y := x + y y := y + 32 • Reduction in strength

x := x * 2 x := x + x

x := x << 2

Page 77: Unit 3 - Basic Search and Traversal Techniques

Basic Block Level

1. Common Sub expression elimination

2. Constant Propagation

3. Copy Propagation

4. Dead code elimination

5. …

Page 78: Unit 3 - Basic Search and Traversal Techniques

Simple example: a[i+1] = b[i+1]

•t1 = i+1

•t2 = b[t1]

•t3 = i + 1

•a[t3] = t2

•t1 = i + 1

•t2 = b[t1]

•t3 = i + 1 no longer live

•a[t1] = t2

Common expression can be eliminated

Page 79: Unit 3 - Basic Search and Traversal Techniques

•i = 4

•t1 = i+1

•t2 = b[t1]

•a[t1] = t2

•i = 4

•t1 = 5

•t2 = b[t1]

•a[t1] = t2

Now, suppose i is a constant:

i = 4

t1 = 5

t2 = b[5]

a[5] = t2

i = 4

t2 = b[5]

a[5] = t2

Final Code:

Page 80: Unit 3 - Basic Search and Traversal Techniques

Optimizations on CFG

• Must take control flow into account – Common Sub-expression Elimination

– Constant Propagation

– Dead Code Elimination

– Partial redundancy Elimination

– …

• Applying one optimization may raise opportunities for other optimizations.

Page 81: Unit 3 - Basic Search and Traversal Techniques

Simple Loop Optimizations

• Code Motion

Move invariants out of the loop.

Example:

while (i <= limit - 2)

becomes

t := limit - 2

while (i <= t)

Page 82: Unit 3 - Basic Search and Traversal Techniques

Three Address Code of Quick Sorti = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto (5)

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto (9)

if i >= j goto (23)

t6 = 4 * i

x = a[t6]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

t7 = 4 * I

t8 = 4 * j

t9 = a[t8]

a[t7] = t9

t10 = 4 * j

a[t10] = x

goto (5)

t11 = 4 * I

x = a[t11]

t12 = 4 * i

t13 = 4 * n

t14 = a[t13]

a[t12] = t14

t15 = 4 * n

a[t15] = x

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

Page 83: Unit 3 - Basic Search and Traversal Techniques

Find The Basic Blocki = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto (5)

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto (9)

if i >= j goto (23)

t6 = 4 * i

x = a[t6]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

t7 = 4 * I

t8 = 4 * j

t9 = a[t8]

a[t7] = t9

t10 = 4 * j

a[t10] = x

goto (5)

t11 = 4 * i

x = a[t11]

t12 = 4 * i

t13 = 4 * n

t14 = a[t13]

a[t12] = t14

t15 = 4 * n

a[t15] = x

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

Page 84: Unit 3 - Basic Search and Traversal Techniques

Flow Graphi = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

t6 = 4 * i

x = a[t6]

t7 = 4 * i

t8 = 4 * j

t9 = a[t8]

a[t7] = t9

t10 = 4 * j

a[t10] = x

goto B2

t11 = 4 * i

x = a[t11]

t12 = 4 * i

t13 = 4 * n

t14 = a[t13]

a[t12] = t14

t15 = 4 * n

a[t15] = x

B1

B2

B3

B4

B5 B6

Page 85: Unit 3 - Basic Search and Traversal Techniques

Common Subexpression Elimination

i = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

t6 = 4 * i

x = a[t6]

t7 = 4 * i

t8 = 4 * j

t9 = a[t8]

a[t7] = t9

t10 = 4 * j

a[t10] = x

goto B2

t11 = 4 * i

x = a[t11]

t12 = 4 * i

t13 = 4 * n

t14 = a[t13]

a[t12] = t14

t15 = 4 * n

a[t15] = x

B1

B2

B3

B4

B5 B6

Page 86: Unit 3 - Basic Search and Traversal Techniques

Common Subexpression Elimination

i = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

t6 = 4 * i

x = a[t6]

t8 = 4 * j

t9 = a[t8]

a[t6] = t9

t10 = 4 * j

a[t10] = x

goto B2

t11 = 4 * i

x = a[t11]

t12 = 4 * i

t13 = 4 * n

t14 = a[t13]

a[t12] = t14

t15 = 4 * n

a[t15] = x

B1

B2

B3

B4

B5 B6

Page 87: Unit 3 - Basic Search and Traversal Techniques

Common Subexpression Elimination

i = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

t6 = 4 * i

x = a[t6]

t8 = 4 * j

t9 = a[t8]

a[t6] = t9

a[t8] = x

goto B2

t11 = 4 *i

x = a[t11]

t12 = 4 * i

t13 = 4 * n

t14 = a[t13]

a[t12] = t14

t15 = 4 * n

a[t15] = x

B1

B2

B3

B4

B5 B6

Page 88: Unit 3 - Basic Search and Traversal Techniques

Common Subexpression Elimination

i = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

t6 = 4 * i

x = a[t6]

t8 = 4 * j

t9 = a[t8]

a[t6] = t9

a[t8] = x

goto B2

t11 = 4 * i

x = a[t11]

t12 = 4 * i

t13 = 4 * n

t14 = a[t13]

a[t12] = t14

t15 = 4 * n

a[t15] = x

B1

B2

B3

B4

B5 B6

Page 89: Unit 3 - Basic Search and Traversal Techniques

Common Subexpression Elimination

i = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

t6 = 4 * i

x = a[t6]

t8 = 4 * j

t9 = a[t8]

a[t6] = t9

a[t8] = x

goto B2

t11 = 4 * i

x = a[t11]

t13 = 4 * n

t14 = a[t13]

a[t11] = t14

t15 = 4 * n

a[t15] = x

B1

B2

B3

B4

B5 B6

Page 90: Unit 3 - Basic Search and Traversal Techniques

Common Subexpression Elimination

i = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

t6 = 4 * i

x = a[t6]

t8 = 4 * j

t9 = a[t8]

a[t6] = t9

a[t8] = x

goto B2

t11 = 4 * i

x = a[t11]

t13 = 4 * n

t14 = a[t13]

a[t11] = t14

a[t13] = x

B1

B2

B3

B4

B5 B6

Page 91: Unit 3 - Basic Search and Traversal Techniques

Common Subexpression Elimination

i = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

t6 = 4 * i

x = a[t6]

t8 = 4 * j

t9 = a[t8]

a[t6] = t9

a[t8] = x

goto B2

t11 = 4 * i

x = a[t11]

t13 = 4 * n

t14 = a[t13]

a[t11] = t14

a[t13] = x

B1

B2

B3

B4

B5 B6

Page 92: Unit 3 - Basic Search and Traversal Techniques

Common Subexpression Elimination

i = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

x = a[t2]

t8 = 4 * j

t9 = a[t8]

a[t2] = t9

a[t8] = x

goto B2

t11 = 4 * i

x = a[t11]

t13 = 4 * n

t14 = a[t13]

a[t11] = t14

a[t13] = x

B1

B2

B3

B4

B5 B6

Page 93: Unit 3 - Basic Search and Traversal Techniques

Common Subexpression Elimination

i = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

x = t3

t8 = 4 * j

t9 = a[t8]

a[t2] = t9

a[t8] = x

goto B2

t11 = 4 * i

x = a[t11]

t13 = 4 * n

t14 = a[t13]

a[t11] = t14

a[t13] = x

B1

B2

B3

B4

B5 B6

Page 94: Unit 3 - Basic Search and Traversal Techniques

Common Subexpression Elimination

i = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

x = t3

t9 = a[t4]

a[t2] = t9

a[t4] = x

goto B2

t11 = 4 * i

x = a[t11]

t13 = 4 * n

t14 = a[t13]

a[t11] = t14

a[t13] = x

B1

B2

B3

B4

B5 B6

Page 95: Unit 3 - Basic Search and Traversal Techniques

Common Subexpression Elimination

i = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

x = t3

a[t2] = t5

a[t4] = x

goto B2

t11 = 4 * i

x = a[t11]

t13 = 4 * n

t14 = a[t13]

a[t11] = t14

a[t13] = x

B1

B2

B3

B4

B5 B6

Page 96: Unit 3 - Basic Search and Traversal Techniques

Common Subexpression Elimination

i = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

x = t3

a[t2] = t5

a[t4] = x

goto B2

x = t3

t14 = a[t1]

a[t2] = t14

a[t1] = x

B1

B2

B3

B4

B5 B6

Similarly for B6

Page 97: Unit 3 - Basic Search and Traversal Techniques

Dead Code Eliminationi = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

x = t3

a[t2] = t5

a[t4] = x

goto B2

x = t3

t14 = a[t1]

a[t2] = t14

a[t1] = x

B1

B2

B3

B4

B5 B6

Page 98: Unit 3 - Basic Search and Traversal Techniques

Dead Code Eliminationi = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

a[t2] = t5

a[t4] = t3

goto B2

t14 = a[t1]

a[t2] = t14

a[t1] = t3

B1

B2

B3

B4

B5 B6

Page 99: Unit 3 - Basic Search and Traversal Techniques

Reduction in Strengthi = m - 1

j = n

t1 =4 * n

v = a[t1]

i = i + 1

t2 = 4 * i

t3 = a[t2]

if t3 < v goto B2

j = j – 1

t4 = 4 * j

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

a[t2] = t5

a[t4] = t3

goto B2

t14 = a[t1]

a[t2] = t14

a[t1] = t3

B1

B2

B3

B4

B5 B6

Page 100: Unit 3 - Basic Search and Traversal Techniques

Reduction in Strengthi = m - 1

j = n

t1 =4 * n

v = a[t1]

t2 = 4 * i

t4 = 4 * j

t2 = t2 + 4

t3 = a[t2]

if t3 < v goto B2

t4 = t4 - 4

t5 = a[t4]

if t5 > v goto B3

if i >= j goto B6

a[t2] = t5

a[t4] = t3

goto B2

t14 = a[t1]

a[t2] = t14

a[t1] = t3

B1

B2

B3

B4

B5 B6

Page 101: Unit 3 - Basic Search and Traversal Techniques

And-Or Graphs

• Technique for solving problems that can be decomposed into sub problems

• Links between nodes indicates relations between problems

• Or node: one of its successor node has to be solved

• And node: all of its successor node has to be solved

• Problem can be specified by two thinks: start node, goal nodes.

• Goal nodes: trivial (or primitive) problems

• Cost can be attached to arcs or nodes

Page 102: Unit 3 - Basic Search and Traversal Techniques

And-Or Graphs

• Difference between state-state representation and and or representation.

solution: path vs. tree

Page 103: Unit 3 - Basic Search and Traversal Techniques

Search in and-or graphs

a

b c

e

h i

d gf

Page 104: Unit 3 - Basic Search and Traversal Techniques

Game tree

• A game tree is a directed graph whose nodes are positions in a game and whose edges are moves.

• The complete game tree for a game is the game tree starting at the initial position and containing all possible moves from each position; the complete tree is the same tree as that obtained from the extensive form game representation.

Page 105: Unit 3 - Basic Search and Traversal Techniques

Game tree for tic-tac-toe game

Page 106: Unit 3 - Basic Search and Traversal Techniques

• The diagram shows the first two levels, or plies, in the game tree for tic-tac-toe.

• We consider all the rotations and reflections of positions as being equivalent, so the first player has three choices of move: in the center, at the edge, or in the corner.

• The second player has two choices for the reply if the first player played in the center, otherwise five choices. And so on.

• The number of leaf nodes in the complete game tree is the number of possible different ways the game can be played. For example, the game tree for tic-tac-toe has 26,830 leaf nodes.

Page 107: Unit 3 - Basic Search and Traversal Techniques

Biconnected component• In graph theory, a bi-connected

component (or 2-connected component) is a maximal bi connected subgraph.

• Any connected graph decomposes into a tree of biconnected components called the block tree of the graph.

• The blocks are attached to each other at shared vertices called cut vertices or articulation points. Specifically, a cut vertex is any vertex that when removed increases the number of connected components.

Page 108: Unit 3 - Basic Search and Traversal Techniques

• Each color corresponds to a bi- connected component.

• Multi-colored vertices are cut vertices, and thus belong to multiple biconnected components.

Page 109: Unit 3 - Basic Search and Traversal Techniques

Biconnected Components

• G is Biconnected iff either

(1) G is a single edge, or

(2) for each triple of vertices u,v,w

w-avoiding path from u to v

(equivalently: two disjoint paths from u to v)

∃∃

Page 110: Unit 3 - Basic Search and Traversal Techniques

Example of Biconnected Components of Graph G

• Maximal subgraphs of G which are biconnected.

5

1

8

1

6

2

4

35

7

8

1

2

2

3

4

6

5

7

G

Page 111: Unit 3 - Basic Search and Traversal Techniques

Biconnected Components Meet at Articulation Points• The intersection of two biconnected

components consists of at most one vertex called an Articulation Point.

• Example: 1,2,5 are articulation points1

6

2

4

3

5

7

8

G

Page 112: Unit 3 - Basic Search and Traversal Techniques

Discovery of Biconnected Components via Articulation

Points– find articulation points

then can compute biconnected components:

Algorithm: • During DFS, use auxiliary stack to store visited edges. • Each time we complete the DFS of a tree child of an articulation point, pop all stacked edges• currently in stack • These popped off edges form a biconnected component

Page 113: Unit 3 - Basic Search and Traversal Techniques

• End of unit 3