Lecture 22
Lecture 22
Graph Algorithms
Interval Trees
The problem: maintain a set of intervals
E.g., time intervals for a scheduling program:
7
10
5
4
17
15
19
18 21
23
10
17
19
query interval
[14,16] [15,18]
[16,19] [15,18] or [17,19]
[12,14] NULL
[17,19]
23
[5,11]
18
[4,8]
8
[21,23]
23
[15,18]
18
[7,10]
10
Note that:
x high
Review:
Correctness of IntervalSearch()
Key idea: need to check only 1 of nodes 2
children
Case 1: search goes right
Show that overlap in right subtree, or no overlap at all
Correctness of IntervalSearch()
Case 1: if search goes right, overlap in the right
Review:
Correctness of IntervalSearch()
Case 2: if search goes left, overlap in the left
Fibonacci heaps
Heap structure that supports efficient merge heap op
Requires amortized analysis techniques
Graphs
A graph G = (V, E)
V = set of vertices
E = set of edges = subset of V V
Thus |E| = O(|V|2)
Graph Variations
Variations:
A connected graph has a path from every vertex to
every other
In an undirected graph:
Edge (u,v) = edge (v,u)
No self-loops
In a directed graph:
Edge (u,v) goes from vertex u to vertex v, notated uv
Graph Variations
More variations:
A weighted graph associates weights with either
same vertices
E.g., the call graph in a program (a function can get
Graphs
We will typically express running times in
Representing Graphs
Assume V = {1, 2, , n}
An adjacency matrix represents the graph as a
n x n matrix A:
A[i, j] = 1 if edge (i, j) E (or weight of edge)
= 0 if edge (i, j) E
1
d
b
4
c
2
3
4
??
1
a
d
2
b
4
c
require?
A: O(V2)
What is the minimum amount of storage needed by
an adjacency matrix representation of an undirected
graph with 4 vertices?
A: 6 bits
Undirected graph matrix is symmetric
No self-loops dont need diagonal
vertices adjacent to v
Example:
Adj[1] = {2,3}
Adj[2] = {3}
Adj[3] = {}
Adj[4] = {3}
out-degree(v) = |E|
takes (V + E) storage (Why?)
For undirected graphs, # items in adj lists is
degree(v) = 2 |E| (handshaking lemma)
also (V + E) storage
So: Adjacency lists take O(V+E) storage
Graph Searching
Given: a graph G = (V, E), directed or undirected
Goal: methodically explore every vertex and
every edge
Ultimately: build a tree on the graph
Pick a vertex as the root
Choose certain edges to produce a tree
Note: might also build a forest if graph is not
connected
Breadth-First Search
Explore a graph, turning it into a tree
One vertex at a time
Expand frontier of explored vertices across the
etc.
Breadth-First Search
Again will associate vertex colors to guide the
algorithm
White vertices have not been discovered
All vertices start out white
vertices
Breadth-First Search
BFS(G, s) {
initialize vertices;
Q = {s}; // Q is a queue (duh); initialize to s
while (Q not empty) {
u = RemoveTop(Q);
for each v u->adj {
if (v->color == WHITE)
v->color = GREY;
v->d = u->d + 1;
v->p = u;
What does v->d
Enqueue(Q, v);
What does v->p
}
u->color = BLACK;
}
}
represent?
represent?
Q: s
Q: w
Q: r
Q:
Q: x
Q: v
Q: u
Q: y
Q:
source node
Shortest-path distance (s,v) = minimum number of