Professional Documents
Culture Documents
Topics to be covered
Code optimization,
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
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
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
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
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
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*/
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
SEARCHING
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;
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
20
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
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
delete_BST(int number, BTNODE *tree) { int found; BTNODE *temp, *parent, *temp_succ; if(tree==NULL) { printf( Tree is empty); return; }
26
parent=temp=Null; search_BST(number,parent,temp,tree,f ound) if(found==0) { printf( node to be deleted is not found); return; }
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
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 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 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
Testing whether graph is connected. Computing the spanning forest of the graph. Computing a cycle in graph or reporting that no such cycle exists.
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
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.
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
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
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
Meaning must be preserved (correctness) Speedup must occur on average. Work done must be worth the effort.
Opportunities:
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
goto L1
Peephole Optimizations
Algebraic Simplification
x := x + 0 No needed
Dead code
y := y + 32 x := x + x
Reduction in strength
x := x * 2
x := x << 2
Common Sub expression elimination Constant Propagation Copy Propagation Dead code elimination
2.
3.
4.
5.
Final Code:
Optimizations on CFG
Common Sub-expression Elimination Constant Propagation Dead Code Elimination Partial redundancy Elimination
Code Motion
limit - 2)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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
B4
if i >= j goto B6
B1
i=m-1 j=n t1 =4 * n v = a[t1]
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
B4
if i >= j goto B6
B1
i=m-1 j=n t1 =4 * n v = a[t1]
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
B4
if i >= j goto B6
B1
i=m-1 j=n t1 =4 * n v = a[t1]
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
B4
if i >= j goto B6
B1
i=m-1 j=n t1 =4 * n v = a[t1]
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
B4
if i >= j goto B6
B1
i=m-1 j=n t1 =4 * n v = a[t1]
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
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]
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]
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]
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]
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]
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]
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]
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]
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]
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
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
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.
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)
G
2 3 4
1 1 1 2 5 5 6 7 8 2
5 8 3 6 4 7
G
2 3 5 6 8
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