Lecture 13
Jianjun Hu
Department of Computer Science and Engineering
University of South Carolina
2009.10.
CSCE350 Algorithms and Data Structure
Problem 3
Design a decrease-by-one algorithm for generating power set of a set of n elements.
{a, b, c}
{} {a} {b} {c} {ab} {ac} {bc} {a b c}
==={a,b}
{} {a} {b} {ab}
==={a, b, c}
{c} {ac} {bc} {abc} -----< added when solving {a, b, c}
Depth-first search (DFS)
Explore graph always moving away from last visited vertex, similar to preorder tree traversals
Pseudocode for Depth-first-search of graph G=(V,E)
)(
0 withmarked is
ajacent toin vertexeach
withmark;1
)(
/////////////////////////////////////////////////////
)(
0 withmarked is
in vertexeach
0
unvisited as label0within vertex eachmark
)(
wdfs
w
vVw
countvcountcount
vdfs
vdfs
v
Vv
count
V
GDFS
if
do for
if
do for
//
ALGORITHM
Example – Undirected Graph
c f
ea
bd
g h
ij
a
c
d f
b
e
g
h
i
j
10,76,1
9,85,2
8,94,41,3
7,103,5
2,6
ga
hc
ifd
jb
e
Stack push/pop
DFS forest
(Tree edge / Back edge)
Input Graph
(Adjacency matrix / linked list
Example – Directed Graph (Digraph)
DFS forest may also contain forward edges: edges to descendants (digraphs only) and cross edges (all the edges that are not tree/back/forward edges)
a b
e f
c d
g h
DFS Forest and Stack
a c
h
g
e
f
b d
8,76,1
7,85,64,2
3,41,3
2,5
ca
deb
gf
h
Stack push/pop
DFS: Notes
DFS can be implemented with graphs represented as:• Adjacency matrices: Θ(V2)• Adjacency linked lists: Θ(V+E)
Yields two distinct ordering of vertices:• preorder: as vertices are first encountered (pushed onto stack)• postorder: as vertices become dead-ends (popped off stack)
Applications:• checking connectivity, finding connected components• checking acyclicity• searching state-space of problems for solution (AI)
Breadth-First Search (BFS)
Explore graph moving across to all the neighbors of last visited vertex
Similar to level-by-level tree traversals
Instead of a stack (LIFO), breadth-first uses queue (FIFO)
Applications: same as DFS
BFS algorithm
bfs(v)count ← count + 1mark v with countinitialize queue with vwhile queue is not empty do
a ← front of queuefor each vertex w adjacent to a do
if w is marked with 0 count ←count + 1mark w with countadd w to the end of the queue
remove a from the front of the queue
BFSBFS((GG))countcount ← ← 00mark each vertex with 0mark each vertex with 0for for each vertexeach vertex vv inin VV do do if if vv is marked with 0 is marked with 0 bfsbfs((vv))
BFS Example – undirected graph
c f
ea
bd
g h
ij
a
dc e
f b
g
h
i
j
10
9
8
7
6
5
4
3
2
1
i
j
h
g
b
f
e
d
c
a
Input Graph
(Adjacency matrix / linked list
BFS forest
(Tree edge / Cross edge)
Queue
Example – Directed Graph
a b
e f
c d
g h
BFS traversal:
BFS Forest and Queue
a c
h
g
e fb d
8
7
6
5
4
3
2
1
d
c
h
g
f
e
b
a
Queue
BFS forest
Breadth-first search: Notes
BFS has same efficiency as DFS and can be implemented with graphs represented as:
• Adjacency matrices: Θ(V2)• Adjacency linked lists: Θ(V+E)
Yields single ordering of vertices (order added/deleted from queue is the same)
Directed Acyclic Graph (DAG)
A directed graph with no cycles
Arise in modeling many problems, eg:• prerequisite structure• food chains
A digraph is a DAG if its DFS forest has no back edge.
Imply partial ordering on the domain
Example:
a b
c
d
e
(a)
a
b
c
d
e
(b)
Topological Sorting
Problem: find a total order consistent with a partial order
Example:
Five courses has the prerequisite relation shown in the left. Find the right order to take all of them sequentially
Note: problem is solvable iff graph is dag
C1
C2 C5
C4
C3
Topological Sorting Algorithms
DFS-based algorithm:• DFS traversal: note the order with which the vertices are
popped off stack (dead end)• Reverse order solves topological sorting• Back edges encountered?→ NOT a DAG!
Source removal algorithm• Repeatedly identify and remove a source vertex, ie, a
vertex that has no incoming edges
Both Θ(V+E) using adjacency linked lists
An Example
C51
C42
C33
C14 C25
The popping-off order:
C5, C4, C3, C1, C2The topologically sorted list:
C2 C1 C3 C5C4
(a) (b) (c)
C1
C2 C5
C4
C3
delete C1 delete C2
delete C3 delete C4 delete C5
C1
C2 C5
C4
C3
C2 C5
C4
C3
C5
C4
C3
C5
C4
C5
Variable-Size-Decrease: Binary Search Trees
Arrange keys in a binary tree with the binary search tree property:
k
<k >k
Example 1:Example 1: 5, 10, 3, 1, 7, 12, 9
Example 2:Example 2: 4, 5, 7, 2, 1, 3, 6
a0
a1
an-2
an-1
. . .
(a)
an-1
an-2
a 1
a 0
...
(b)
Searching and insertion in binary search trees
Searching – straightforward
Insertion – search for key, insert at leaf where search terminated
All operations: worst case # key comparisons = h + 1
lg n ≤ h ≤ n – 1 with average (random files) 1.41 lg n
Thus all operations have:• worst case: Θ(n) • average case: Θ(lgn)
Bonus: inorder traversal produces sorted list (treesort)
Finding Kth smallest number
1) Sort and check
2) Smarter algorithm using idea from Quick sort
p
A[i] ≤ p A[i] p
sIf s<K, then we search right halfOtherwise, we search left half
Next Class
Balanced trees (Transform-and-Conquer)