You are on page 1of 113

Unit 3 Basic search and Traversal Techniques

Topics to be covered

Binary Tree Traversal

Inorder Preorder BFS BFT DFS DFT

Search and traversal techniques for Graphs


Code optimization,

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 traversal
1. 2. 3.

Preorder Traversal Postorder Traversal Inorder Traversal

Preorder Traversal
The preorder of a non-empty binary tree is defined as
Visit the root node. Traverse the left sub tree in preorder. Traverse the right sub tree in preorder.

1.

2.

3.

Root

A C

B D E F

Preorder traversal of the above binary tree A B D E I C F Right GJ


left
Root

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

Inorder Traversal
The Inorder of a non-empty binary tree is defined as
Traverse the left sub tree in inorder. Visit the root node. Traverse the right sub tree in inorder.

1.

2.

3.

A B D E F C G

Inorder traversal of the above binary tree

DBIEAFCGJ
left
Root

Right

Algorithm
inorder(BTNODE *tree) { if(tree != NULL) { inorder(tree->lchild); /*step 1*/ printf(%c,tree->data); inorder(tree->rchild); } return;
9

/* step 2*/ /*step 3*/

Postorder Traversal
The postorder of a non-empty binary tree is defined as
Traverse the left sub tree in postorder. Traverse the right sub tree in postorder. Visit the root node.

1.

2.

3.

10

A B D E F C G

Post order traversal of the above binary tree Right


left
Root

DIEBFJGCA
11

Algorithm
postorder(BTNODE *tree) { if(tree != NULL) { postorder(tree->lchild); /*step 1*/ postorder(tree->rchild); /*step 2*/ printf(%c,tree->data); } return;
12

/* step 3*/

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

25 12 9 21 43

42

48

Traverse the tree in inorder, we get 9 122125424348


14

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

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

Algorithm for Searching

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;

Advantages

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

18

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

21

42

48

21

42

48

23>21
23

19

insert BST(int num) { int found;

Algorithm for insertion

BTNODE *temp,*parent; search_node(num,found,parent); if(found==1) printf(Node already exists); else

20

{ temp=(BTNODE*)malloc(sizeof(BTNODE) ); temp->data=num; temp->lchild=temp->rchild=NULL; if(parent==NULL) tree=temp; else { if(parent->data > num)

Deletion in a BST
Three cases:
1.

Node to be deleted has no children. Node to be deleted has exactly one subtree. Node to be deleted has two subtrees.

2.

3.

22

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

In second case,
The node to be deleted has exactly one child, so this child is moved up the tree to occupy its place.
25 12 43 12 25 43

21

42

48

23

42

48

Node to be deleted

23

After Deletion of the node 21

Before Deletion of the node 21 24

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.
Node to be deleted 12 25 43 12 42 43

21

42

48

21

48

23

23

Before Deletion of the Root node 25

After Deletion of the Root node 25 25

delete_BST(int number, BTNODE *tree) { int found; BTNODE *temp, *parent, *temp_succ; if(tree==NULL) { printf( Tree is empty); return; }
26

C code for deletion

parent=temp=Null; search_BST(number,parent,temp,tree,f ound) if(found==0) { printf( node to be deleted is not found); return; }

/* 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;
28

/* 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; }
29

/* 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

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

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.

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

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. White Undiscovered. Gray Discovered but not finished. Black Finished.

Colors the vertices to keep track of progress.


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

Example (BFS)
(Courtesy of Prof. Jim Anderson) r s 0 t u

Q: s 0

Example (BFS)
r 1 s 0 t u

1 w

Q: w r 1 1

Example (BFS)
r 1 s 0 t 2 u

1 w

2 x

Q: r t x 1 2 2

Example (BFS)
r 1 s 0 t 2 u

2 v

1 w

2 x

Q: t x v 2 2 2

Example (BFS)
r 1 s 0 t 2 u 3

2 v

1 w

2 x

Q: x v u 2 2 3

Example (BFS)
r 1 s 0 t 2 u 3

2 v

1 w

2 x

3 y

Q: v u y 2 3 3

Example (BFS)
r 1 s 0 t 2 u 3

2 v

1 w

2 x

3 y

Q: u y 3 3

Example (BFS)
r 1 s 0 t 2 u 3

2 v

1 w

2 x

3 y

Q: y 3

Example (BFS)
r 1 s 0 t 2 u 3

2 v

1 w

2 x

3 y

Q:

Example (BFS)
r 1 s 0 t 2 u 3

2 v

1 w

2 x

3 y

BF Tree

BFS(G,s) 1. for each vertex u in V[G] {s} 2 3 4 5 6 7 8 9 11 12 13 14 d[s] 0 [s] nil Q enqueue(Q,s) do u dequeue(Q) for each v in Adj[u] do if color[v] = white then color[v] gray do color[u] white d[u] [u] nil color[s] gray white: undiscovered gray: discovered black: finished Q: a queue of discovered vertices color[v]: color of v d[v]: distance from s to v [u]: predecessor of v

10 while Q

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.

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 Comp 122, Fallsource and search is as a new 2004

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 us adjacency list.

Uses the same coloring scheme for vertices as BFS.


Comp 122, Fall 2004

Pseudo-code
DFS(G) 1. for each vertex u V[G] 2. 3. do color[u] white [u] NIL DFS-Visit(u) 1. color[u] GRAY White vertex u has been discovered 2. time time + 1 3. d[u] time 4. for each v Adj[u] 5. do if color[v] = WHITE 6. then [v] u 7. DFS-Visit(v) 8. color[u] BLACK Blacken u; it is finished. 9. f[u] time time + 1

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.

Example (DFS)
u 1/ v w

1 x y z

u Stac k

Push u

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

Example (DFS)
u 1/ v 2/ w

2 1 x y z

v u Stac k

Push [v]

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 parent[y]=v. (true) line 6: make vertex v as a parent of vertex y.

Example (DFS)
u 1/ v 2/ 3 2 1 z y v u Stac k Push [y] w

3/ x y

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 x assign time = 4 to vertex

Example (DFS)
u 1/ v 2/ 4 3 2 1 z x y v u Stac k Push [x] w

4/ x

3/ y

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.

Example (DFS)
u 1/ B v 2/ 3 2 1 z y v u Stack afterPop [x] w

4/ x

3/ y

Example (DFS)
u 1/ B v 2/ w

4/5 x

3/ y z

Example (DFS)
u 1/ B v 2/ w

4/5 x

3/6 y z

Example (DFS)
u 1/ B v 2/7 w

4/5 x

3/6 y z

Example (DFS)
u 1/ F 4/5 x B v 2/7 w

3/6 y z

Example (DFS)
u 1/8 F 4/5 x B v 2/7 w

3/6 y z

Example (DFS)
u 1/8 F 4/5 x B v 2/7 w 9/

3/6 y z

Example (DFS)
u 1/8 F 4/5 x B v 2/7 C w 9/

3/6 y z

Example (DFS)
u 1/8 F 4/5 x B v 2/7 C w 9/

3/6 y

10/ z

Example (DFS)
u 1/8 F 4/5 x B v 2/7 C w 9/

3/6 y

10/ z

Example (DFS)
u 1/8 F 4/5 x B v 2/7 C w 9/

3/6 y

10/11
z

Example (DFS)
u 1/8 F 4/5 x B v 2/7 C w 9/12

3/6 y

10/11
z

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.
Comp 122, Fall 2004

The order in which the nodes are visited in DFS

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.

Code Optimization

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

Peephole Optimizations
1.

A Simple but effective technique for locally improving the target code is peephole optimization, 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. 2. 3. 4.

2.

Redundant instruction elimination Flow of control information Algebraic Simplification Use of machine Idioms

Peephole Optimizations

Constant Folding

x := 32 x := x + 32

becomes

x := 64

Unreachable Code

goto L2 x := x + 1

No need becomes goto L2

Flow of control optimizations

goto L1

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 x := x + x

Reduction in strength

x := x * 2

x := x << 2

Basic Block Level


1.

Common Sub expression elimination Constant Propagation Copy Propagation Dead code elimination

2.

3.

4.

5.

Common expression can be eliminated


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 a[t1] = t2 no longer live

Now, suppose i is a constant:

i=4 t1 = i+1 t2 = b[t1] a[t1] = t2

i=4 t1 = 5 t2 = b[t1] a[t1] = t2

i=4 t1 = 5 t2 = b[5] a[5] = t2

Final Code:

i=4 t2 = b[5] a[5] = t2

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.

Simple Loop Optimizations

Code Motion

Move invariants out of the loop. Example: while (i <=


becomes

limit - 2)

t := limit - 2 while (i <= t)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Three Address Code of Quick i=m-1 Sort 16 t7 = 4 * Ij j=n 17 t8 = 4 *


t1 =4 * n v = a[t1] i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto (5) j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto (9) if i >= j goto (23) t6 = 4 * i x = a[t6] 18 19 20 21 22 23 24 25 26 27 28 29 30 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

Find The Basic Block


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 i=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=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto (9) if i >= j goto (23) t6 = 4 * i x = a[t6] 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Flow Graph
B5
t6 = 4 * i x = a[t6] t7 = 4 * i t8 = 4 * j t9 = a[t8] a[t7] = t9

B6
t11 = 4 * i x = a[t11] t12 = 4 * i t13 = 4 * n t14 = a[t13] a[t12] = t14 t15 = 4 * n a[t15] = x

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

t10 = 4 * j a[t10] = x goto B2

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Common Subexpression Elimination


B5
t6 = 4 * i x = a[t6] t7 = 4 * i t8 = 4 * j t9 = a[t8] a[t7] = t9

B6
t11 = 4 * i x = a[t11] t12 = 4 * i t13 = 4 * n t14 = a[t13] a[t12] = t14 t15 = 4 * n a[t15] = x

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

t10 = 4 * j a[t10] = x goto B2

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Common Subexpression Elimination


B5
t6 = 4 * i

B6
t11 = 4 * i x = a[t11] t12 = 4 * i t13 = 4 * n t14 = a[t13] a[t12] = t14 t15 = 4 * n a[t15] = x

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

x = a[t6] t8 = 4 * j t9 = a[t8] a[t6] = t9 t10 = 4 * j a[t10] = x goto B2

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Common Subexpression Elimination


B5
t6 = 4 * i

B6
t11 = 4 *i x = a[t11] t12 = 4 * i t13 = 4 * n t14 = a[t13] a[t12] = t14 t15 = 4 * n a[t15] = x

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

x = a[t6] t8 = 4 * j t9 = a[t8] a[t6] = t9 a[t8] = x goto B2

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Common Subexpression Elimination


B5
t6 = 4 * i

B6
t11 = 4 * i x = a[t11] t12 = 4 * i t13 = 4 * n t14 = a[t13] a[t12] = t14 t15 = 4 * n a[t15] = x

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

x = a[t6] t8 = 4 * j t9 = a[t8] a[t6] = t9 a[t8] = x goto B2

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Common Subexpression Elimination


B5
t6 = 4 * i

B6
t11 = 4 * i x = a[t11] t13 = 4 * n t14 = a[t13] a[t11] = t14 t15 = 4 * n a[t15] = x

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

x = a[t6] t8 = 4 * j t9 = a[t8] a[t6] = t9 a[t8] = x goto B2

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Common Subexpression Elimination


B5
t6 = 4 * i

B6
t11 = 4 * i x = a[t11] t13 = 4 * n t8 = 4 * j t9 = a[t8] a[t6] = t9 a[t8] = x goto B2 t14 = a[t13] a[t11] = t14 a[t13] = x

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

x = a[t6]

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Common Subexpression Elimination


B5
t6 = 4 * i

B6
t11 = 4 * i x = a[t11] t13 = 4 * n t8 = 4 * j t9 = a[t8] a[t6] = t9 a[t8] = x goto B2 t14 = a[t13] a[t11] = t14 a[t13] = x

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

x = a[t6]

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Common Subexpression Elimination


B5
x = a[t2]

B6
t11 = 4 * i x = a[t11] t13 = 4 * n t14 = a[t13] a[t11] = t14 a[t13] = x

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

t8 = 4 * j t9 = a[t8] a[t2] = t9 a[t8] = x goto B2

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Common Subexpression Elimination


B5
x = t3 t8 = 4 * j t9 = a[t8] a[t2] = t9 a[t8] = x goto B2

B6
t11 = 4 * i x = a[t11] t13 = 4 * n t14 = a[t13] a[t11] = t14 a[t13] = x

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Common Subexpression Elimination


B5
x = t3 t9 = a[t4] a[t2] = t9 a[t4] = x goto B2

B6
t11 = 4 * i x = a[t11] t13 = 4 * n t14 = a[t13] a[t11] = t14 a[t13] = x

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Common Subexpression Elimination


B5
x = t3 a[t2] = t5 a[t4] = x goto B2

B6
t11 = 4 * i x = a[t11] t13 = 4 * n t14 = a[t13] a[t11] = t14

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

a[t13] = x

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Common Subexpression Elimination


B5
x = t3 a[t2] = t5 a[t4] = x goto B2

B6
x = t3 t14 = a[t1] a[t2] = t14 a[t1] = x

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

Similarly for B6

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Dead Code Elimination


B5
x = t3 a[t2] = t5 a[t4] = x goto B2

B6
x = t3 t14 = a[t1] a[t2] = t14 a[t1] = x

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Dead Code Elimination


B5
a[t2] = t5 a[t4] = t3 goto B2

B6
t14 = a[t1] a[t2] = t14 a[t1] = t3

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1]

Reduction in Strength
B5
a[t2] = t5 a[t4] = t3 goto B2

B6
t14 = a[t1] a[t2] = t14 a[t1] = t3

B2
i=i +1 t2 = 4 * i t3 = a[t2] if t3 < v goto B2

B3
j=j1 t4 = 4 * j t5 = a[t4] if t5 > v goto B3

B4
if i >= j goto B6

B1
i=m-1 j=n t1 =4 * n v = a[t1] t2 = 4 * i t4 = 4 * j

Reduction in Strength
B5
a[t2] = t5 a[t4] = t3 goto B2

B6
t14 = a[t1] a[t2] = t14 a[t1] = t3

B2

t2 = t2 + 4 t3 = a[t2] if t3 < v goto B2

B3

t4 = t4 - 4 t5 = a[t4] if t5 > v goto B3

B4
if i >= j goto B6

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

And-Or Graphs

Difference between state-state representation and and or representation. solution: path vs. tree

Search in and-or graphs


a b d e h f i c g

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.

Game tree for tic-tac-toe game

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

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

Each color corresponds to a bi- connected component. Multi-colored vertices are cut vertices, and thus belong to multiple biconnected components.

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)

Example of Biconnected Components of Graph G

Maximal subgraphs of G which are biconnected .

G
2 3 4

1 1 1 2 5 5 6 7 8 2

5 8 3 6 4 7

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

G
2 3 5 6 8

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

End of unit 3

You might also like