KEMBAR78
Unit 4-PartB of data design and algorithms | PPTX
09/26/2005 GNANESWARA RAO NITTA 1
Welcome to CSE Student Friends
09/26/2012 GNANESWARA RAO NITTA 2
Design and Analysis of
ALGORITHMS:
Basic Traversal and
Search Techniques
— UNIT 4-part B—
Dr.GNANESWARA RAO NITTA
B.E.,M.Tech, PhD,
MISTE,MIEEE,MIEEECS,MACM,MIEE,MIE,MISRS,MCRSI,MCSI,MISTE,MIETE
,MIAPR
Professor
Graph Algorithms Using Depth First
Search
a) Graph Definitions
b) DFS of Graphs
c) Biconnected Components
d) DFS of Digraphs
e) Techniques for Binary Trees,
Graph Terminology
• Graph G=(V,E)
• Vertex set V
• Edge set E pairs of vertices
which
are adjacent
Directed and Undirected Graphs
• G directed
• G undirected
if edges ordered pairs (u,v)
 
u v
if edges unordered pairs {u,v}
 
u v
Proper Graphs and Subgraphs
• Proper graph:
– No loops
– No multi-edges
• Subgraph G‘ of G
G‘ = (V‘, E‘) where
V‘ is a subset of V, E‘ is a subset of E
Paths in Graphs
• Path p
P is a sequence of vertices v0, v1, …, vk
where for i=1,…k, vi-1 is adjacent to vi
Equivalently, p is a sequence of edges
e1, …, ek where for i = 2,…k edges
ei-1, ei share a vertex
V1
V2 Vk-1 Vk
ek
e2
e1
Vo
Simple Paths and Cycles
• Simple path
no edge or vertex repeated,
except possibly vo = vk
• Cycle
a path p with vo = vk where k>1
V1
V2 Vk-1
Vk = Vo
A Connected Undirected Graph
G is if path between
each pair of vertices
connected 
Connected Components of an
Undirected Graph
else G has 2 :
maximal connected subgraphs
connected components

Biconnected Undirected Graphs
G is if two disjoint paths
between each pair of vertices
biconnected 
(or G is single edge)
Size of a Graph
• Graph G = (V,E)
n = |V| = # vertices
m = |E| = # edges
size of G is n+m
4
2 3
1
Representing a Graph by an
Adjacency Matrix
• Adjacency Matrix A
2
A is size n n
1 2 3 4
1 0 1 1 0
1 (i,j) E 2 1 0 1 0
A(i,j)
0 else 3 1 1 0 1
4 0 0 1 0
space cost n -n



 

Adjacency List Representation of a
Graph
• Adjacency Lists Adj (1),…,Adj
(n)
Adj(v) = list of vertices adjacent to v
space cost O(n+m)
1
2
3
4
2
3
1
1
3
3
2 4
Definition of an Undirected Tree
• Tree
T is graph with unique path
between every pair of vertices
n = # vertices
n-1 = # edges
• Forest
– set of trees
Definition of a Directed Tree
• Directed Tree
T is digraph with distinguished vertex root r
such that each vertex reachable from r by a
unique path
Family Relationships:
- ancestors
- descendants
- parent
- child
- siblings
r
leaves have no proper descendants
An Ordered Tree
• Ordered Tree
– is a directed tree with siblings ordered
B
A
C
D
E
F
H I
Preorder Tree Traversal
• Preorder A,B,C,D,E,F,H,I
[1] root (order vertices as pushed on
stack)
[2] preorder left subtree
[3] preorder right subtree
B
A
C
D
E
F
H I
Postorder Tree Traversal
• Postorder B,E,D,H,I,F,C,A
[1] postorder left subtree
[2] postorder right subtree
[3] root (order vertices as popped off
stack)
B
A
C
D
E
F
H I
Spanning Tree and Forest of a
Graph
• T is a spanning tree of graph G if
(1) T is a directed tree with the same vertex
set as G
(2) each edge of T is a directed version of an
edge of G
• Spanning Forest:
forest of spanning trees of connected
components of G
Example Spanning Tree of a Graph
1
6
2
4
3
5
7
8
root
tree edge
back edge
9
10 11
12
cross edge
Classification of Edges of G with
Spanning Tree T
• An edge (u,v) of T is tree edge
• An edge (u,v) of G-T is back edge if u is a
descendent or ancestor of v.
• Else (u,v) is a cross edge
Tarjan’s Depth First Search Algorithm
• We assume a RAM machine model
• Algorithm Depth First Search
graph G (V,E) represented by
adjacency lists Adj(v) for each v V
[0] N 0
[1] all v V (number (v) 0
children (v) ( ) )
[2] all v V do
Input
for do
od
for



 


number (v) 0 DFS(v)
[3] spanning forest defined by children
if then
output

Recursive DFS Procedure
DFS(v)
[1] N N + 1; number (v) N
[2] for each u Adj(v)
number (u) 0
(add u to children (v); DFS(u))
recursive procedure
do
if then
 


Time Cost of Depth First Search
Algorithm on a RAM
• Input size n = |V|, m = |E|
• Theorem
Depth First Search takes total time cost O(n+m)
• Proof
can associate with each edge and vertex
a constant number of operations.
Classification of Edges of G via DFS
Spanning Tree T
1
6
2
4
3
5
7
8
G
1
6
2
4
3
5
7
8
T
Classification of Edges of G via DFS
Spanning Tree T
• Edge notation induced by
• Depth First Search Tree T
u v iff (u,v) is tree edge of T
u v iff u is an ancestor of v
u --- v iff (u,v) is backedge if (u,v) G-T
with either u v or v u

 



 
Classification of Edges of G via DFS
Spanning Tree T (cont’d)
• Note DFS tree T has no cross edges
will number vertices by order
visited in DFS (preorder of T)
Verifying Vertices are Descendants
via Preordering of Tree
• Suppose we preorder number a Tree T
Let Dv = # of descendants of v
• Lemma
u is descendant of v
iff v  u  v + Dv
v
u
Dv
W
*
Testing for Proper Ancestors via
Preordering
• Lemma
If u is descendant of v
and (u,w) is back edge s.t. w  v
then w is a proper ancestor of v
Low Values
• For each vertex v,
define low(v) = min ( {v}  {w | v - - - w} )
• Can prove by induction:
Lemma
can be computed during DFS
in postorder.
*
low(v) min ( {v} {low(w)|v w} {w|v - - - w} )
    
Graph with DFS Numbering and Low
Values in Brackets
7[5]
1[1]
2[2]
4[2]
3[2]
5[1]
8[1]
6[5]
v[low(v)]
Number vertices
by DFS number
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.
5
1
8
1
6
2
4
3
5
7
8
1
2
2
3
4
6
5
7

G
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
6
2
4
3
5
7
8
G
Discovery of Biconnected
Components via Articulation Points
–If can 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
Characterization of an Articulation Point
• Theorem
a is an articulation point iff either
(1) a is root with  2 tree children
or
(2) a is not root but a has a tree child v with
low (v)  a
(note easy to check given low computation)
Proof of Characterization of an
Articulation Point
• Proof
The conditions are sufficient since any
a-avoiding path from v remains in the
subtree Tv rooted at v, if v is a child of a
• To show condition necessary, assume a is
an articulation point.
Characterization of an Articulation
Point (cont’d)
• Case (1)
If a is a root and is articulation point,
a must have  2 tree edges to two distinct
biconnected components.
• Case(2)
If a is not root, consider graph G - {a}
which must have a connected component C
consisting of only descendants of a, and
with no backedge from C to an ancestor
of v. Hence a has a tree child v in C and
low (v)  a
Proof of Characterization of an
Articulation Point (cont’d)
a
v
root
Articulation Point a
is not the root
subtree Tv
C
no
back
edges
• Case (2)
Computing Biconnected Components
• Theorem
The Biconnected Components of G = (V,E)
can be computed in time O(|V|+|E|) using
a RAM
• Proof idea
• Use characterization of Bicoconnected
components via articulation points
• Identify these articulation points dynamically
during depth first search
• Use a secondary stack to store the edges of
the biconnected components as they are
visited
• When an articulation point is discovered ,
pop the edges of this stack off to output a
biconnected component
Biconnected Components Algorithm
[0] initialize a STACK to empty
During a DFS traversal do
[1] add visited edge to STACK
[2] compute low of visited vertex v using Lemma
[3] test if v is an articulation point
[4] if so, for each u  children (v) in order
where low (u) > v
do Pop all edges in STACK
upto and including tree edge (v,u)
Output popped edges as a
biconnected component of G
od
Time Bounds of Biconnected
Components Algorithm
• Time Bounds:
Each edge and vertex can be associated
with 0(1) operations. So time O(|V|+|E|).
Depth First Search in a Directed Graph
• Depth first search tree T of Directed Graph
cross cross
cross
cycle
forward
cycle
1
1
2
3
4
5
6
7
8
2
4
5
6
7
8
G = (V,E)
i = DFS number
v
i
i = postorder
= order vertex
popped off
DFS stack
edge set E partitioned
Classification of Directed Edge (u, v)
via DFS Spanning Tree T
• Tree edge
• Cycle edge
• Forward edge
• Cross edge
*
if v u

if u v in T

otherwise
*
if (u,v) T
but u v in T


Topological Order of Acyclic
Directed Graph
• Digraph G = (V,E) is acyclic if it has
no cycles
1 n
i j
V {v ,...v } satisfies
(v ,v ) E i j
Topological Order

  
Characterization of an Acyclic Digraph
• Proof
Case (1):
• Suppose (u,v)  E is a cycle edge, so v → u.
• But let e1,…ek be the tree edges from v to u.
• Then (u,v), e1,…ek is a cycle.
*
G is acylic iff no cycle edge

Characterization of an Acyclic
Digraph (cont’d)
Case (2):
• Suppose G has no a cycle edge
• Then order vertices in postorder of DFS
spanning forest (i.e. in order vertices are
popped off DFS stack).
• This is a reverse topological order of G.
• So, G can have no cycles.
Note:
Gives an O(|V|+|E|) algorithm
for computing Topological Ordering
of an acyclic graph G = (V,E)
Binary Trees
"A tree may grow a
thousand feet tall, but
its leaves will return to
its roots."
-Chinese Proverb
50
Definitions
8 A tree is an abstract data type
– one entry point, the root
– Each node is either a leaf or an
internal node
– An internal node has 1 or more
children, nodes that can be
reached directly from that
internal node.
– The internal node is said to be
the parent of its child nodes
root node
leaf nodes
internal
nodes
Binary Trees
CS314
51
Properties of Trees
8 Only access point is the root
8 All nodes, except the root, have one parent
– like the inheritance hierarchy in Java
8 Traditionally trees drawn upside down
root
leaves
Binary Trees
CS314
52
Properties of Trees and Nodes
8 siblings: two nodes that
have the same parent
8 edge: the link from one
node to another
8 path length: the number of
edges that must be
traversed to get from one
node to another
root
siblings
edge
path length from root to this
node is 3
Binary Trees
CS314
0
1 2
3
4 5
53
More Properties of Trees
8 depth: the path length from the root of the tree
to this node
8 height of a node: The maximum distance
(path length) of any leaf from this node
– a leaf has a height of 0
– the height of a tree is the height of the root of that
tree
8 descendants: any nodes that can be reached
via 1 or more edges from this node
8 ancestors: any nodes for which this node is a
descendant
Binary Trees
CS314
54
Tree Visualization
D
B C
F
E
A
G H J
I
K L M
N O
Binary Trees
CS314
Clicker 1
8 What is the depth of the node that contains
M on the previous slide?
A. 0
B. 1
C. 2
D. 3
E. 4
Clicker 2 - Same tree, same choices
What is the height of the node
that contains D?
55
Binary Trees
CS314
56
Binary Trees
8 There are many variations on trees but we
will start with binary trees
8 binary tree: each node has at most two
children
– the possible children are usually referred to as
the left child and the right child
parent
left child right child
Binary Trees
CS314
57
Full Binary Tree
8 full binary tree: a binary tree in which each
node has 2 or 0 children
Binary Trees
CS314
Clicker 3
8 What is the maximum height of a full binary
tree with 11 nodes?
A. 3
B. 5
C. 7
D. 10
E. Not possible to have full binary tree with 11
nodes.
CS314 Binary Trees 58
59
Complete Binary Tree
8 complete binary tree: a binary tree in which
every level, except possibly the deepest is
completely filled. At depth n, the height of the
tree, all nodes are as far left as possible
Where would the next node go
to maintain a complete tree?
Binary Trees
CS314
Clicker 4
8 What is the height of a complete binary tree
that contains N nodes?
A. O(1)
B. O(logN)
C. O(N1/2
)
D. O(N)
E. O(NlogN)
8 Recall, order can be applied to any function.
It doesn't just apply to running time.
CS314 Binary Trees 60
61
Perfect Binary Tree
8 perfect binary tree: a binary tree with all leaf
nodes at the same depth. All internal nodes
have exactly two children.
8 a perfect binary tree has the maximum
number of nodes for a given height
8 a perfect binary tree has (2(n+1)
- 1) nodes
where n is the height of the tree
– height = 0 -> 1 node
– height = 1 -> 3 nodes
– height = 2 -> 7 nodes
– height = 3 -> 15 nodes
Binary Trees
CS314
62
A Binary Node class
public class Bnode<E> {
private E myData;
private Bnode<E> myLeft;
private Bnode<E> myRight;
public BNode();
public BNode(Bnode<E> left, E data,
Bnode<E> right)
public E getData()
public Bnode<E> getLeft()
public Bnode<E> getRight()
public void setData(E data)
public void setLeft(Bnode<E> left)
public void setRight(Bnode<E> right)
}
Binary Trees
CS314
63
Binary Tree Traversals
8 Many algorithms require all nodes of a binary tree
be visited and the contents of each node processed
or examined.
8 There are 4 traditional types of traversals
– preorder traversal: process the root, then process all sub
trees (left to right)
– in order traversal: process the left sub tree, process the
root, process the right sub tree
– post order traversal: process the left sub tree, process
the right sub tree, then process the root
– level order traversal: starting from the root of a tree,
process all nodes at the same depth from left to right,
then proceed to the nodes at the next depth.
Binary Trees
CS314
64
Results of Traversals
8 To determine the results of a traversal on a
given tree draw a path around the tree.
– start on the left side of the root and trace around
the tree. The path should stay close to the tree.
12
49 42
5
13
pre order: process when
pass down left side of node
12 49 13 5 42
in order: process when pass
underneath node
13 49 5 12 42
post order: process when
pass up right side of node
13 5 49 42 12
Binary Trees
CS314
65
Clicker 5 - Tree Traversals
D
C
Z
A
G H J
Q L
Binary Trees
What is a the result of a
post order traversal of
the tree to the left?
A. Z C G A Q H L D J
B. Z G C Q L H J D A
C. A C Z G D H Q L J
D. A C D Z G H J Q L
E. None of these
66
Implement Traversals
8 Implement preorder, inorder, and post order
traversal
– Big O time and space?
8 Implement a level order traversal using a
queue
– Big O time and space?
8 Implement a level order traversal without a
queue
– target depth
Binary Trees
CS314
Breadth First Search
Depth First Search
8 from NIST - DADS
8 breadth first search: Any search algorithm that
considers neighbors of a vertex (node), that is,
outgoing edges (links) of the vertex's predecessor
in the search, before any outgoing edges of the
vertex
8 depth first search: Any search algorithm that
considers outgoing edges (links of children) of a
vertex (node) before any of the vertex's (node)
siblings, that is, outgoing edges of the vertex's
predecessor in the search. Extremes are searched
first.
Clicker 6
8 Which traversal of a tree is a breadth first
search?
A. Level order traversal
B. Pre order traversal
C. In order traversal
D. Post order traversal
E. More than one of these
CS314 Binary Trees 68
Breadth First
8 A level order traversal of a tree could be
used as a breadth first search
8 Search all nodes in a level before going
down to the next level
CS314 Binary Trees 69
Breadth First Search of Tree
CS314 Binary Trees 70
C
A G X Z
W
B
Q P O U
K Z
L
M R
Breadth First Search
CS314 Binary Trees 71
C
A G X Z
W
B
Q P O U
K Z
L
M R
search level 0 first
Find Node with B
Breadth First Search
CS314 Binary Trees 72
C
A G X Z
W
B
Q P O U
K Z
L
M R
search level 1
Find Node with B
Breadth First Search
CS314 Binary Trees 73
C
A G X Z
W
B
Q P O U
K Z
L
M R
search level 1
Find Node with B
Breadth First Search
CS314 Binary Trees 74
C
A G X Z
W
B
Q P O U
K Z
L
M R
search level 1
Find Node with B
Breadth First Search
CS314 Binary Trees 75
C
A G X Z
W
B
Q P O U
K Z
L
M R
search level 1
Find Node with B
Breadth First Search
CS314 Binary Trees 76
C
A G X Z
W
B
Q P O U
K Z
L
M R
search level 1 next
Find Node with B
Breadth First Search
CS314 Binary Trees 77
C
A G X Z
W
B
Q P O U
K Z
L
M R
search level 2 next
Find Node with B
Breadth First Search
CS314 Binary Trees 78
C
A G X Z
W
B
Q P O U
K Z
L
M R
search level 2 next
Find Node with B
Breadth First Search
CS314 Binary Trees 79
C
A G X Z
W
B
Q P O U
K Z
L
M R
search level 2 next
Find Node with B
Breadth First Search
CS314 Binary Trees 80
C
A G X Z
W
B
Q P O U
K Z
L
M R
search level 2 next
Find Node with B
Breadth First Search
CS314 Binary Trees 81
C
A G X Z
W
B
Q P O U
K Z
L
M R
search level 2 next
Find Node with B
Breadth First Search
CS314 Binary Trees 82
C
A G X Z
W
B
Q P O U
K Z
L
M R
search level 3 next
Find Node with B
Breadth First Search
CS314 Binary Trees 83
C
A G X Z
W
B
Q P O U
K Z
L
M R
search level 3 next
Find Node with B
Breadth First Search
CS314 Binary Trees 84
C
A G X Z
W
B
Q P O U
K Z
L
M R
search level 3 next
Find Node with B
BFS - DFS
8 Breadth first search typically implemented
with a Queue
8 Depth first search typically implemented with
a stack, implicit with recursion or iteratively
with an explicit stack
8 which technique do I use?
– depends on the problem
CS314 Binary Trees 85
Depth First Search of Tree
CS314 Binary Trees 86
C
A G X Z
W
B
Q P O U
K Z
L
M R
Find B
Depth First Search of Tree
CS314 Binary Trees 87
C
A G X Z
W
B
Q P O U
K Z
L
M R
Find B
Depth First Search of Tree
CS314 Binary Trees 88
C
A G X Z
W
B
Q P O U
K Z
L
M R
Find B
Depth First Search of Tree
CS314 Binary Trees 89
C
A G X Z
W
B
Q P O U
K Z
L
M R
Find B
Depth First Search of Tree
CS314 Binary Trees 90
C
A G X Z
W
B
Q P O U
K Z
L
M R
Find B
Depth First Search of Tree
CS314 Binary Trees 91
C
A G X Z
W
B
Q P O U
K Z
L
M R
Find B
Depth First Search of Tree
CS314 Binary Trees 92
C
A G X Z
W
B
Q P O U
K Z
L
M R
Find B
Depth First Search of Tree
CS314 Binary Trees 93
C
A G X Z
W
B
Q P O U
K Z
L
M R
Find B
Depth First Search of Tree
CS314 Binary Trees 94
C
A G X Z
W
B
Q P O U
K Z
L
M R
Find B
Depth First Search of Tree
CS314 Binary Trees 95
C
A G X Z
W
B
Q P O U
K Z
L
M R
Find B
Depth First Search of Tree
CS314 Binary Trees 96
C
A G X Z
W
B
Q P O U
K Z
L
M R
Find B
Depth First Search of Tree
CS314 Binary Trees 97
C
A G X Z
W
B
Q P O U
K Z
L
M R
Find B
Depth First Search of Tree
CS314 Binary Trees 98
C
A G X Z
W
B
Q P O U
K Z
L
M R
Find B
Depth First Search of Tree
CS314 Binary Trees 99
C
A G X Z
W
B
Q P O U
K Z
L
M R
Find B
Depth First Search of Tree
CS314 Binary Trees 100
C
A G X Z
W
B
Q P O U
K Z
L
M R
Find B
Binary Search Trees 101
Binary Search Trees
6
9
2
4
1 8
<
>
=
Binary Search Trees 102
Ordered Dictionaries
Keys are assumed to come from a
total order.
New operations:
 closestKeyBefore(k)
 closestElemBefore(k)
 closestKeyAfter(k)
 closestElemAfter(k)
Binary Search Trees 103
Binary Search
Binary search performs operation findElement(k) on a
dictionary implemented by means of an array-based
sequence, sorted by key
 similar to the high-low game
 at each step, the number of candidate items is halved
 terminates after O(log n) steps
Example: findElement(7)
1 3 4 5 7 8 9 11 14 16 18 19
1 3 4 5 7 8 9 11 14 16 18 19
1 3 4 5 7 8 9 11 14 16 18 19
1 3 4 5 7 8 9 11 14 16 18 19
0
0
0
0
m
l h
m
l h
m
l h
l=m =h
Binary Search Trees 104
Lookup Table
A lookup table is a dictionary implemented by means of a
sorted sequence
 We store the items of the dictionary in an array-based
sequence, sorted by key
 We use an external comparator for the keys
Performance:
 findElement takes O(log n) time, using binary search
 insertItem takes O(n) time since in the worst case we have to
shift n/2 items to make room for the new item
 removeElement take O(n) time since in the worst case we have
to shift n/2 items to compact the items after the removal
The lookup table is effective only for dictionaries of small
size or for dictionaries on which searches are the most
common operations, while insertions and removals are
rarely performed (e.g., credit card authorizations)
Binary Search Trees 105
Binary Search
Tree
A binary search tree is
a binary tree storing
keys (or key-element
pairs) at its internal
nodes and satisfying
the following property:
 Let u, v, and w be three
nodes such that u is in
the left subtree of v and
w is in the right subtree
of v. We have
key(u)  key(v)  key(w)
External nodes do not
store items
An inorder traversal of
a binary search trees
visits the keys in
increasing order
6
9
2
4
1 8
Binary Search Trees 106
Search
To search for a key k,
we trace a downward
path starting at the
root
The next node visited
depends on the
outcome of the
comparison of k with
the key of the current
node
If we reach a leaf, the
key is not found and
we return
NO_SUCH_KEY
Example:
findElement(4)
Algorithm findElement(k, v)
if T.isExternal (v)
return NO_SUCH_KEY
if k < key(v)
return findElement(k,
T.leftChild(v))
else if k = key(v)
return element(v)
else { k > key(v) }
return findElement(k,
T.rightChild(v)) 6
9
2
4
1 8
<
>
=
Binary Search Trees 107
Insertion
To perform operation
insertItem(k, o), we
search for key k
Assume k is not already in
the tree, and let let w be
the leaf reached by the
search
We insert k at node w and
expand w into an internal
node
Example: insert 5
6
9
2
4
1 8
6
9
2
4
1 8
5
<
>
>
w
w
Binary Search Trees 108
Deletion
To perform operation
removeElement(k), we
search for key k
Assume key k is in the tree,
and let let v be the node
storing k
If node v has a leaf child w,
we remove v and w from
the tree with operation
removeAboveExternal(w)
Example: remove 4
6
9
2
4
1 8
5
v
w
6
9
2
5
1 8
<
>
Binary Search Trees 109
Deletion (cont.)
We consider the case where
the key k to be removed is
stored at a node v whose
children are both internal
 we find the internal node w
that follows v in an inorder
traversal
 we copy key(w) into node v
 we remove node w and its
left child z (which must be a
leaf) by means of operation
removeAboveExternal(z)
Example: remove 3
3
1
8
6 9
5
v
w
z
2
5
1
8
6 9
v
2
Binary Search Trees 110
Performance
Consider a dictionary
with n items
implemented by
means of a binary
search tree of height
h
 the space used is O(n)
 methods findElement ,
insertItem and
removeElement take
O(h) time
The height h is O(n) in
the worst case and
O(log n) in the best
case
Binary Search Trees 111
Thank You

Unit 4-PartB of data design and algorithms

  • 1.
    09/26/2005 GNANESWARA RAONITTA 1 Welcome to CSE Student Friends
  • 2.
    09/26/2012 GNANESWARA RAONITTA 2 Design and Analysis of ALGORITHMS: Basic Traversal and Search Techniques — UNIT 4-part B— Dr.GNANESWARA RAO NITTA B.E.,M.Tech, PhD, MISTE,MIEEE,MIEEECS,MACM,MIEE,MIE,MISRS,MCRSI,MCSI,MISTE,MIETE ,MIAPR Professor
  • 3.
    Graph Algorithms UsingDepth First Search a) Graph Definitions b) DFS of Graphs c) Biconnected Components d) DFS of Digraphs e) Techniques for Binary Trees,
  • 4.
    Graph Terminology • GraphG=(V,E) • Vertex set V • Edge set E pairs of vertices which are adjacent
  • 5.
    Directed and UndirectedGraphs • G directed • G undirected if edges ordered pairs (u,v)   u v if edges unordered pairs {u,v}   u v
  • 6.
    Proper Graphs andSubgraphs • Proper graph: – No loops – No multi-edges • Subgraph G‘ of G G‘ = (V‘, E‘) where V‘ is a subset of V, E‘ is a subset of E
  • 7.
    Paths in Graphs •Path p P is a sequence of vertices v0, v1, …, vk where for i=1,…k, vi-1 is adjacent to vi Equivalently, p is a sequence of edges e1, …, ek where for i = 2,…k edges ei-1, ei share a vertex V1 V2 Vk-1 Vk ek e2 e1 Vo
  • 8.
    Simple Paths andCycles • Simple path no edge or vertex repeated, except possibly vo = vk • Cycle a path p with vo = vk where k>1 V1 V2 Vk-1 Vk = Vo
  • 9.
    A Connected UndirectedGraph G is if path between each pair of vertices connected 
  • 10.
    Connected Components ofan Undirected Graph else G has 2 : maximal connected subgraphs connected components 
  • 11.
    Biconnected Undirected Graphs Gis if two disjoint paths between each pair of vertices biconnected  (or G is single edge)
  • 12.
    Size of aGraph • Graph G = (V,E) n = |V| = # vertices m = |E| = # edges size of G is n+m 4 2 3 1
  • 13.
    Representing a Graphby an Adjacency Matrix • Adjacency Matrix A 2 A is size n n 1 2 3 4 1 0 1 1 0 1 (i,j) E 2 1 0 1 0 A(i,j) 0 else 3 1 1 0 1 4 0 0 1 0 space cost n -n      
  • 14.
    Adjacency List Representationof a Graph • Adjacency Lists Adj (1),…,Adj (n) Adj(v) = list of vertices adjacent to v space cost O(n+m) 1 2 3 4 2 3 1 1 3 3 2 4
  • 15.
    Definition of anUndirected Tree • Tree T is graph with unique path between every pair of vertices n = # vertices n-1 = # edges • Forest – set of trees
  • 16.
    Definition of aDirected Tree • Directed Tree T is digraph with distinguished vertex root r such that each vertex reachable from r by a unique path Family Relationships: - ancestors - descendants - parent - child - siblings r leaves have no proper descendants
  • 17.
    An Ordered Tree •Ordered Tree – is a directed tree with siblings ordered B A C D E F H I
  • 18.
    Preorder Tree Traversal •Preorder A,B,C,D,E,F,H,I [1] root (order vertices as pushed on stack) [2] preorder left subtree [3] preorder right subtree B A C D E F H I
  • 19.
    Postorder Tree Traversal •Postorder B,E,D,H,I,F,C,A [1] postorder left subtree [2] postorder right subtree [3] root (order vertices as popped off stack) B A C D E F H I
  • 20.
    Spanning Tree andForest of a Graph • T is a spanning tree of graph G if (1) T is a directed tree with the same vertex set as G (2) each edge of T is a directed version of an edge of G • Spanning Forest: forest of spanning trees of connected components of G
  • 21.
    Example Spanning Treeof a Graph 1 6 2 4 3 5 7 8 root tree edge back edge 9 10 11 12 cross edge
  • 22.
    Classification of Edgesof G with Spanning Tree T • An edge (u,v) of T is tree edge • An edge (u,v) of G-T is back edge if u is a descendent or ancestor of v. • Else (u,v) is a cross edge
  • 23.
    Tarjan’s Depth FirstSearch Algorithm • We assume a RAM machine model • Algorithm Depth First Search graph G (V,E) represented by adjacency lists Adj(v) for each v V [0] N 0 [1] all v V (number (v) 0 children (v) ( ) ) [2] all v V do Input for do od for        number (v) 0 DFS(v) [3] spanning forest defined by children if then output 
  • 24.
    Recursive DFS Procedure DFS(v) [1]N N + 1; number (v) N [2] for each u Adj(v) number (u) 0 (add u to children (v); DFS(u)) recursive procedure do if then    
  • 25.
    Time Cost ofDepth First Search Algorithm on a RAM • Input size n = |V|, m = |E| • Theorem Depth First Search takes total time cost O(n+m) • Proof can associate with each edge and vertex a constant number of operations.
  • 26.
    Classification of Edgesof G via DFS Spanning Tree T 1 6 2 4 3 5 7 8 G 1 6 2 4 3 5 7 8 T
  • 27.
    Classification of Edgesof G via DFS Spanning Tree T • Edge notation induced by • Depth First Search Tree T u v iff (u,v) is tree edge of T u v iff u is an ancestor of v u --- v iff (u,v) is backedge if (u,v) G-T with either u v or v u        
  • 28.
    Classification of Edgesof G via DFS Spanning Tree T (cont’d) • Note DFS tree T has no cross edges will number vertices by order visited in DFS (preorder of T)
  • 29.
    Verifying Vertices areDescendants via Preordering of Tree • Suppose we preorder number a Tree T Let Dv = # of descendants of v • Lemma u is descendant of v iff v  u  v + Dv v u Dv W *
  • 30.
    Testing for ProperAncestors via Preordering • Lemma If u is descendant of v and (u,w) is back edge s.t. w  v then w is a proper ancestor of v
  • 31.
    Low Values • Foreach vertex v, define low(v) = min ( {v}  {w | v - - - w} ) • Can prove by induction: Lemma can be computed during DFS in postorder. * low(v) min ( {v} {low(w)|v w} {w|v - - - w} )     
  • 32.
    Graph with DFSNumbering and Low Values in Brackets 7[5] 1[1] 2[2] 4[2] 3[2] 5[1] 8[1] 6[5] v[low(v)] Number vertices by DFS number
  • 33.
    Biconnected Components • Gis 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)  
  • 34.
    Example of Biconnected Componentsof Graph G • Maximal subgraphs of G which are biconnected. 5 1 8 1 6 2 4 3 5 7 8 1 2 2 3 4 6 5 7  G
  • 35.
    Biconnected Components Meetat 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 6 2 4 3 5 7 8 G
  • 36.
    Discovery of Biconnected Componentsvia Articulation Points –If can 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
  • 37.
    Characterization of anArticulation Point • Theorem a is an articulation point iff either (1) a is root with  2 tree children or (2) a is not root but a has a tree child v with low (v)  a (note easy to check given low computation)
  • 38.
    Proof of Characterizationof an Articulation Point • Proof The conditions are sufficient since any a-avoiding path from v remains in the subtree Tv rooted at v, if v is a child of a • To show condition necessary, assume a is an articulation point.
  • 39.
    Characterization of anArticulation Point (cont’d) • Case (1) If a is a root and is articulation point, a must have  2 tree edges to two distinct biconnected components. • Case(2) If a is not root, consider graph G - {a} which must have a connected component C consisting of only descendants of a, and with no backedge from C to an ancestor of v. Hence a has a tree child v in C and low (v)  a
  • 40.
    Proof of Characterizationof an Articulation Point (cont’d) a v root Articulation Point a is not the root subtree Tv C no back edges • Case (2)
  • 41.
    Computing Biconnected Components •Theorem The Biconnected Components of G = (V,E) can be computed in time O(|V|+|E|) using a RAM • Proof idea • Use characterization of Bicoconnected components via articulation points • Identify these articulation points dynamically during depth first search • Use a secondary stack to store the edges of the biconnected components as they are visited • When an articulation point is discovered , pop the edges of this stack off to output a biconnected component
  • 42.
    Biconnected Components Algorithm [0]initialize a STACK to empty During a DFS traversal do [1] add visited edge to STACK [2] compute low of visited vertex v using Lemma [3] test if v is an articulation point [4] if so, for each u  children (v) in order where low (u) > v do Pop all edges in STACK upto and including tree edge (v,u) Output popped edges as a biconnected component of G od
  • 43.
    Time Bounds ofBiconnected Components Algorithm • Time Bounds: Each edge and vertex can be associated with 0(1) operations. So time O(|V|+|E|).
  • 44.
    Depth First Searchin a Directed Graph • Depth first search tree T of Directed Graph cross cross cross cycle forward cycle 1 1 2 3 4 5 6 7 8 2 4 5 6 7 8 G = (V,E) i = DFS number v i i = postorder = order vertex popped off DFS stack edge set E partitioned
  • 45.
    Classification of DirectedEdge (u, v) via DFS Spanning Tree T • Tree edge • Cycle edge • Forward edge • Cross edge * if v u  if u v in T  otherwise * if (u,v) T but u v in T  
  • 46.
    Topological Order ofAcyclic Directed Graph • Digraph G = (V,E) is acyclic if it has no cycles 1 n i j V {v ,...v } satisfies (v ,v ) E i j Topological Order    
  • 47.
    Characterization of anAcyclic Digraph • Proof Case (1): • Suppose (u,v)  E is a cycle edge, so v → u. • But let e1,…ek be the tree edges from v to u. • Then (u,v), e1,…ek is a cycle. * G is acylic iff no cycle edge 
  • 48.
    Characterization of anAcyclic Digraph (cont’d) Case (2): • Suppose G has no a cycle edge • Then order vertices in postorder of DFS spanning forest (i.e. in order vertices are popped off DFS stack). • This is a reverse topological order of G. • So, G can have no cycles. Note: Gives an O(|V|+|E|) algorithm for computing Topological Ordering of an acyclic graph G = (V,E)
  • 49.
    Binary Trees "A treemay grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb
  • 50.
    50 Definitions 8 A treeis an abstract data type – one entry point, the root – Each node is either a leaf or an internal node – An internal node has 1 or more children, nodes that can be reached directly from that internal node. – The internal node is said to be the parent of its child nodes root node leaf nodes internal nodes Binary Trees CS314
  • 51.
    51 Properties of Trees 8Only access point is the root 8 All nodes, except the root, have one parent – like the inheritance hierarchy in Java 8 Traditionally trees drawn upside down root leaves Binary Trees CS314
  • 52.
    52 Properties of Treesand Nodes 8 siblings: two nodes that have the same parent 8 edge: the link from one node to another 8 path length: the number of edges that must be traversed to get from one node to another root siblings edge path length from root to this node is 3 Binary Trees CS314 0 1 2 3 4 5
  • 53.
    53 More Properties ofTrees 8 depth: the path length from the root of the tree to this node 8 height of a node: The maximum distance (path length) of any leaf from this node – a leaf has a height of 0 – the height of a tree is the height of the root of that tree 8 descendants: any nodes that can be reached via 1 or more edges from this node 8 ancestors: any nodes for which this node is a descendant Binary Trees CS314
  • 54.
    54 Tree Visualization D B C F E A GH J I K L M N O Binary Trees CS314
  • 55.
    Clicker 1 8 Whatis the depth of the node that contains M on the previous slide? A. 0 B. 1 C. 2 D. 3 E. 4 Clicker 2 - Same tree, same choices What is the height of the node that contains D? 55 Binary Trees CS314
  • 56.
    56 Binary Trees 8 Thereare many variations on trees but we will start with binary trees 8 binary tree: each node has at most two children – the possible children are usually referred to as the left child and the right child parent left child right child Binary Trees CS314
  • 57.
    57 Full Binary Tree 8full binary tree: a binary tree in which each node has 2 or 0 children Binary Trees CS314
  • 58.
    Clicker 3 8 Whatis the maximum height of a full binary tree with 11 nodes? A. 3 B. 5 C. 7 D. 10 E. Not possible to have full binary tree with 11 nodes. CS314 Binary Trees 58
  • 59.
    59 Complete Binary Tree 8complete binary tree: a binary tree in which every level, except possibly the deepest is completely filled. At depth n, the height of the tree, all nodes are as far left as possible Where would the next node go to maintain a complete tree? Binary Trees CS314
  • 60.
    Clicker 4 8 Whatis the height of a complete binary tree that contains N nodes? A. O(1) B. O(logN) C. O(N1/2 ) D. O(N) E. O(NlogN) 8 Recall, order can be applied to any function. It doesn't just apply to running time. CS314 Binary Trees 60
  • 61.
    61 Perfect Binary Tree 8perfect binary tree: a binary tree with all leaf nodes at the same depth. All internal nodes have exactly two children. 8 a perfect binary tree has the maximum number of nodes for a given height 8 a perfect binary tree has (2(n+1) - 1) nodes where n is the height of the tree – height = 0 -> 1 node – height = 1 -> 3 nodes – height = 2 -> 7 nodes – height = 3 -> 15 nodes Binary Trees CS314
  • 62.
    62 A Binary Nodeclass public class Bnode<E> { private E myData; private Bnode<E> myLeft; private Bnode<E> myRight; public BNode(); public BNode(Bnode<E> left, E data, Bnode<E> right) public E getData() public Bnode<E> getLeft() public Bnode<E> getRight() public void setData(E data) public void setLeft(Bnode<E> left) public void setRight(Bnode<E> right) } Binary Trees CS314
  • 63.
    63 Binary Tree Traversals 8Many algorithms require all nodes of a binary tree be visited and the contents of each node processed or examined. 8 There are 4 traditional types of traversals – preorder traversal: process the root, then process all sub trees (left to right) – in order traversal: process the left sub tree, process the root, process the right sub tree – post order traversal: process the left sub tree, process the right sub tree, then process the root – level order traversal: starting from the root of a tree, process all nodes at the same depth from left to right, then proceed to the nodes at the next depth. Binary Trees CS314
  • 64.
    64 Results of Traversals 8To determine the results of a traversal on a given tree draw a path around the tree. – start on the left side of the root and trace around the tree. The path should stay close to the tree. 12 49 42 5 13 pre order: process when pass down left side of node 12 49 13 5 42 in order: process when pass underneath node 13 49 5 12 42 post order: process when pass up right side of node 13 5 49 42 12 Binary Trees CS314
  • 65.
    65 Clicker 5 -Tree Traversals D C Z A G H J Q L Binary Trees What is a the result of a post order traversal of the tree to the left? A. Z C G A Q H L D J B. Z G C Q L H J D A C. A C Z G D H Q L J D. A C D Z G H J Q L E. None of these
  • 66.
    66 Implement Traversals 8 Implementpreorder, inorder, and post order traversal – Big O time and space? 8 Implement a level order traversal using a queue – Big O time and space? 8 Implement a level order traversal without a queue – target depth Binary Trees CS314
  • 67.
    Breadth First Search DepthFirst Search 8 from NIST - DADS 8 breadth first search: Any search algorithm that considers neighbors of a vertex (node), that is, outgoing edges (links) of the vertex's predecessor in the search, before any outgoing edges of the vertex 8 depth first search: Any search algorithm that considers outgoing edges (links of children) of a vertex (node) before any of the vertex's (node) siblings, that is, outgoing edges of the vertex's predecessor in the search. Extremes are searched first.
  • 68.
    Clicker 6 8 Whichtraversal of a tree is a breadth first search? A. Level order traversal B. Pre order traversal C. In order traversal D. Post order traversal E. More than one of these CS314 Binary Trees 68
  • 69.
    Breadth First 8 Alevel order traversal of a tree could be used as a breadth first search 8 Search all nodes in a level before going down to the next level CS314 Binary Trees 69
  • 70.
    Breadth First Searchof Tree CS314 Binary Trees 70 C A G X Z W B Q P O U K Z L M R
  • 71.
    Breadth First Search CS314Binary Trees 71 C A G X Z W B Q P O U K Z L M R search level 0 first Find Node with B
  • 72.
    Breadth First Search CS314Binary Trees 72 C A G X Z W B Q P O U K Z L M R search level 1 Find Node with B
  • 73.
    Breadth First Search CS314Binary Trees 73 C A G X Z W B Q P O U K Z L M R search level 1 Find Node with B
  • 74.
    Breadth First Search CS314Binary Trees 74 C A G X Z W B Q P O U K Z L M R search level 1 Find Node with B
  • 75.
    Breadth First Search CS314Binary Trees 75 C A G X Z W B Q P O U K Z L M R search level 1 Find Node with B
  • 76.
    Breadth First Search CS314Binary Trees 76 C A G X Z W B Q P O U K Z L M R search level 1 next Find Node with B
  • 77.
    Breadth First Search CS314Binary Trees 77 C A G X Z W B Q P O U K Z L M R search level 2 next Find Node with B
  • 78.
    Breadth First Search CS314Binary Trees 78 C A G X Z W B Q P O U K Z L M R search level 2 next Find Node with B
  • 79.
    Breadth First Search CS314Binary Trees 79 C A G X Z W B Q P O U K Z L M R search level 2 next Find Node with B
  • 80.
    Breadth First Search CS314Binary Trees 80 C A G X Z W B Q P O U K Z L M R search level 2 next Find Node with B
  • 81.
    Breadth First Search CS314Binary Trees 81 C A G X Z W B Q P O U K Z L M R search level 2 next Find Node with B
  • 82.
    Breadth First Search CS314Binary Trees 82 C A G X Z W B Q P O U K Z L M R search level 3 next Find Node with B
  • 83.
    Breadth First Search CS314Binary Trees 83 C A G X Z W B Q P O U K Z L M R search level 3 next Find Node with B
  • 84.
    Breadth First Search CS314Binary Trees 84 C A G X Z W B Q P O U K Z L M R search level 3 next Find Node with B
  • 85.
    BFS - DFS 8Breadth first search typically implemented with a Queue 8 Depth first search typically implemented with a stack, implicit with recursion or iteratively with an explicit stack 8 which technique do I use? – depends on the problem CS314 Binary Trees 85
  • 86.
    Depth First Searchof Tree CS314 Binary Trees 86 C A G X Z W B Q P O U K Z L M R Find B
  • 87.
    Depth First Searchof Tree CS314 Binary Trees 87 C A G X Z W B Q P O U K Z L M R Find B
  • 88.
    Depth First Searchof Tree CS314 Binary Trees 88 C A G X Z W B Q P O U K Z L M R Find B
  • 89.
    Depth First Searchof Tree CS314 Binary Trees 89 C A G X Z W B Q P O U K Z L M R Find B
  • 90.
    Depth First Searchof Tree CS314 Binary Trees 90 C A G X Z W B Q P O U K Z L M R Find B
  • 91.
    Depth First Searchof Tree CS314 Binary Trees 91 C A G X Z W B Q P O U K Z L M R Find B
  • 92.
    Depth First Searchof Tree CS314 Binary Trees 92 C A G X Z W B Q P O U K Z L M R Find B
  • 93.
    Depth First Searchof Tree CS314 Binary Trees 93 C A G X Z W B Q P O U K Z L M R Find B
  • 94.
    Depth First Searchof Tree CS314 Binary Trees 94 C A G X Z W B Q P O U K Z L M R Find B
  • 95.
    Depth First Searchof Tree CS314 Binary Trees 95 C A G X Z W B Q P O U K Z L M R Find B
  • 96.
    Depth First Searchof Tree CS314 Binary Trees 96 C A G X Z W B Q P O U K Z L M R Find B
  • 97.
    Depth First Searchof Tree CS314 Binary Trees 97 C A G X Z W B Q P O U K Z L M R Find B
  • 98.
    Depth First Searchof Tree CS314 Binary Trees 98 C A G X Z W B Q P O U K Z L M R Find B
  • 99.
    Depth First Searchof Tree CS314 Binary Trees 99 C A G X Z W B Q P O U K Z L M R Find B
  • 100.
    Depth First Searchof Tree CS314 Binary Trees 100 C A G X Z W B Q P O U K Z L M R Find B
  • 101.
    Binary Search Trees101 Binary Search Trees 6 9 2 4 1 8 < > =
  • 102.
    Binary Search Trees102 Ordered Dictionaries Keys are assumed to come from a total order. New operations:  closestKeyBefore(k)  closestElemBefore(k)  closestKeyAfter(k)  closestElemAfter(k)
  • 103.
    Binary Search Trees103 Binary Search Binary search performs operation findElement(k) on a dictionary implemented by means of an array-based sequence, sorted by key  similar to the high-low game  at each step, the number of candidate items is halved  terminates after O(log n) steps Example: findElement(7) 1 3 4 5 7 8 9 11 14 16 18 19 1 3 4 5 7 8 9 11 14 16 18 19 1 3 4 5 7 8 9 11 14 16 18 19 1 3 4 5 7 8 9 11 14 16 18 19 0 0 0 0 m l h m l h m l h l=m =h
  • 104.
    Binary Search Trees104 Lookup Table A lookup table is a dictionary implemented by means of a sorted sequence  We store the items of the dictionary in an array-based sequence, sorted by key  We use an external comparator for the keys Performance:  findElement takes O(log n) time, using binary search  insertItem takes O(n) time since in the worst case we have to shift n/2 items to make room for the new item  removeElement take O(n) time since in the worst case we have to shift n/2 items to compact the items after the removal The lookup table is effective only for dictionaries of small size or for dictionaries on which searches are the most common operations, while insertions and removals are rarely performed (e.g., credit card authorizations)
  • 105.
    Binary Search Trees105 Binary Search Tree A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying the following property:  Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u)  key(v)  key(w) External nodes do not store items An inorder traversal of a binary search trees visits the keys in increasing order 6 9 2 4 1 8
  • 106.
    Binary Search Trees106 Search To search for a key k, we trace a downward path starting at the root The next node visited depends on the outcome of the comparison of k with the key of the current node If we reach a leaf, the key is not found and we return NO_SUCH_KEY Example: findElement(4) Algorithm findElement(k, v) if T.isExternal (v) return NO_SUCH_KEY if k < key(v) return findElement(k, T.leftChild(v)) else if k = key(v) return element(v) else { k > key(v) } return findElement(k, T.rightChild(v)) 6 9 2 4 1 8 < > =
  • 107.
    Binary Search Trees107 Insertion To perform operation insertItem(k, o), we search for key k Assume k is not already in the tree, and let let w be the leaf reached by the search We insert k at node w and expand w into an internal node Example: insert 5 6 9 2 4 1 8 6 9 2 4 1 8 5 < > > w w
  • 108.
    Binary Search Trees108 Deletion To perform operation removeElement(k), we search for key k Assume key k is in the tree, and let let v be the node storing k If node v has a leaf child w, we remove v and w from the tree with operation removeAboveExternal(w) Example: remove 4 6 9 2 4 1 8 5 v w 6 9 2 5 1 8 < >
  • 109.
    Binary Search Trees109 Deletion (cont.) We consider the case where the key k to be removed is stored at a node v whose children are both internal  we find the internal node w that follows v in an inorder traversal  we copy key(w) into node v  we remove node w and its left child z (which must be a leaf) by means of operation removeAboveExternal(z) Example: remove 3 3 1 8 6 9 5 v w z 2 5 1 8 6 9 v 2
  • 110.
    Binary Search Trees110 Performance Consider a dictionary with n items implemented by means of a binary search tree of height h  the space used is O(n)  methods findElement , insertItem and removeElement take O(h) time The height h is O(n) in the worst case and O(log n) in the best case
  • 111.
    Binary Search Trees111 Thank You