Академический Документы
Профессиональный Документы
Культура Документы
Analysis of Algorithms
Spring 2013
Lecture 4
Prof. Swarat Chaudhuri
Q3: Analyzing an algorithm
Here is an algorithm:
For i=1, 2, , n
For j = i+1, 2, , n {
Add up entries A[i] through A[j]
Store result in B[i,j]
}
}
2
The lower bound is the more interesting one
Consider the times during the execution of the algorithm when i n/4
and j 3n/4.
3
Graphs
3.1 Basic Definitions and Applications
Undirected Graphs
V = { 1, 2, 3, 4, 5, 6, 7, 8 }
E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 }
n=8
m = 11
5
Some Graph Applications
6
World Wide Web
Web graph.
Node: web page.
Edge: hyperlink from one page to another.
cnn.com
hbo.com
sorpranos.com
7
9-11 Terrorist Network
8
Ecological Food Web
Reference: http://www.twingroves.district96.k12.il.us/Wetlands/Salamander/SalGraphics/salfoodweb.giff
9
Graph Representation: Adjacency Matrix
1 2 3 4 5 6 7 8
1 0 1 1 0 0 0 0 0
2 1 0 1 1 1 0 0 0
3 1 1 0 0 1 0 1 1
4 0 1 0 1 1 0 0 0
5 0 1 1 1 0 1 0 0
6 0 0 0 0 1 0 0 0
7 0 0 1 0 0 0 0 1
8 0 0 1 0 0 0 1 0
10
Graph Representation: Adjacency List
1 2 3
2 1 3 4 5
3 1 2 5 7 8
4 2 5
5 2 3 4 6
6 5
7 3 8
8 3 7
11
Paths and Connectivity
12
Cycles
Def. A cycle is a path v1, v2, , vk-1, vk in which v1 = vk, k > 2, and the
first k-1 nodes are all distinct.
cycle C = 1-2-4-5-3-1
13
Trees
14
Rooted Trees
Rooted tree. Given a tree T, choose a root node r and orient each edge
away from r.
root r
parent of v
child of v
15
Phylogeny Trees
16
GUI Containment Hierarchy
Reference: http://java.sun.com/docs/books/tutorial/uiswing/overview/anatomy.html
17
3.2 Graph Traversal
Connectivity
s-t shortest path problem. Given two nodes s and t, what is the length
of the shortest path between s and t?
Applications.
Facebook.
Maze traversal.
Erdos number.
Kevin Bacon number.
Fewest number of hops in a communication network.
19
Breadth First Search
s L1 L L
BFS algorithm. 2 n-1
L0 = { s }.
L1 = all neighbors of L0.
L2 = all nodes that do not belong to L0 or L1, and that have an edge
to a node in L1.
Li+1 = all nodes that do not belong to an earlier layer, and that have
an edge to a node in Li.
20
Implementing BFS
21
Breadth First Search
Property. Let T be a BFS tree of G = (V, E), and let (x, y) be an edge of
G. Then the level of x and y differ by at most 1.
L0
L1
L2
L3
22
Breadth First Search: Analysis
Pf.
Easy to prove O(n2) running time:
at most n lists Li
each node occurs on at most one list; for loop runs n times
when we consider node u, there are n incident edges (u, v),
and we spend O(1) processing each edge
23
Connected Component
24
Q1: Finding connected components
25
Connected Component
R
s
u v
26
Q2: Flood Fill
Flood fill. Given lime green pixel in an image, change color of entire
blob of neighboring lime pixels to blue.
27
Flood Fill
Flood fill. Given lime green pixel in an image, change color of entire
blob of neighboring lime pixels to blue.
28
Flood Fill
Flood fill. Given lime green pixel in an image, change color of entire
blob of neighboring lime pixels to blue.
Node: pixel.
Edge: two neighboring lime pixels.
Blob: connected component of lime pixels.
recolor lime green blob to blue
29
Depth-first search
Use recursion
DFS(u):
Mark u as Explored and add u to R
For each edge (u,v) incident to u
If v is not marked Explored then
Recursively call DFS(v)
30
Depth-first search
31
Q3: BFS and DFS trees
Prove that G = T.
32
Answer
(*) Since T is a BFS tree, the distance of the two nodes from u in T can
differ at most by one.
33
Q4: Finding a cycle
Run BFS from an arbitrary node s, and obtain a BFS tree T. If every
edge of G appears in the tree, then G = T and there is no cycle.
34