Lecture notes for CSE326

CSE 326: Data Structures:
Graphs
Lecture 19: Monday, Feb 24, 2003
1
Outline
• Graphs:
– Definitions
– Properties
– Examples
• Topological Sort
• Graph Traversals
– Depth First Search
– Breadth First Search
TO DO: READ WEISS CH 9
2
Graphs
Graphs = formalism for representing relationships
between objects
• A graph G = (V, E)
– V is a set of vertices
– E  V  V is a set of edges
Han
Luke
Leia
V = {Han, Leia, Luke}
E = {(Luke, Leia),
(Han, Leia),
(Leia, Han)}
3
Directed vs. Undirected Graphs
• In directed graphs, edges have a specific direction:
Han
Luke
Leia
• In undirected graphs, they don’t (edges are two-way):
Han
Luke
Leia
• Vertices u and v are adjacent if (u, v)  E
4
Weighted Graphs
Each edge has an associated weight or cost.
Clinton
20
Mukilteo
Kingston
30
Bainbridge
35
Edmonds
Seattle
60
Bremerton
There may be more
5
information in the graph as well.
Paths and Cycles
A path is a list of vertices {v1, v2, …, vn} such
that (vi, vi+1)  E for all 0  i < n.
A cycle is a path that begins and ends at the same
node.
Chicago
Seattle
Salt Lake City
San Francisco
Dallas
6
p = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle}
Path Length and Cost
Path length: the number of edges in the path
Path cost: the sum of the costs of each edge
3.5
Chicago
Seattle
2
2
2
Salt Lake City
2.5
2.5
2.5
3
San Francisco
Dallas
length(p) = 5
cost(p) = 11.5
7
Connectivity
Undirected graphs are connected if there is a
path between any two vertices
Directed graphs are strongly connected if
there is a path from any one vertex to any
other
8
Connectivity
Directed graphs are weakly connected if there
is a path between any two vertices, ignoring
direction
A complete graph has an edge between every
pair of vertices
9
Connectivity
A (strongly) connected component is a
subgraph which is (strongly) connected
CC in an undirected graph:
SCC in a directed graph:
10
Distance and Diameter
• The distance between two nodes, d(u,v), is the
length of the shortest paths, or  if there is no path
• The diameter of a graph is the largest distance
between any two nodes
• Graph is strongly connected iff diameter < 
11
An Interesting Graph
• The Web !
• Each page is a vertice
• Each hyper-link is an edge
• How many nodes ?
• Is it connected ?
• What is its diameter ?
12
The Web Graph
13
Other Graphs
• Geography:
– Cities and roads
– Airports and flights (diameter  20 !!)
• Publications:
– The co-authorship graph
• E.g. the Erdos distance
– The reference graph
• Phone calls: who calls whom
• Almost everything can be modeled as a graph !
14
Graph Representation 1:
Adjacency Matrix
A |V| x |V| array in which an element
(u, v) is true if and only if there is an
Han Luke Leia
edge from u to v
Han
Han
Luke
Luke
Leia
Runtime:
iterate over vertices
iterate ever edges
iterate edges adj. to vertex
edge exists?
Leia
Space requirements:
15
Graph Representation 2:
Adjacency List
A |V|-ary list (array) in which each entry
stores a list (linked list) of all adjacent
vertices
Han
Han
Luke
Luke
Leia
Runtime:
iterate over vertices
iterate ever edges
iterate edges adj. to vertex
edge exists?
Leia
space requirements:
16
Graph Density
A sparse graph has O(|V|) edges
A dense graph has (|V|2) edges
Anything in between is either sparsish or densy depending on
17
the context.
Trees as Graphs
• Every tree is a graph
with some restrictions:
– the tree is directed
– there are no cycles
(directed or undirected)
– there is a directed path
from the root to every
node
A
B
C
D
E
F
G
BAD!
I
H
J
18
Directed Acyclic Graphs (DAGs)
DAGs are directed
graphs with no
cycles.
if program call
graph is a DAG,
then all
procedure calls
can be in-lined
main()
mult()
add()
access()
Trees  DAGs  Graphs
read()
19
Application of DAGs:
Representing Partial Orders
check in
airport
reserve
flight
call
taxi
pack
bags
take
flight
taxi to
airport
locate
gate
20
Topological Sort
Given a graph, G = (V, E), output all the
vertices in V such that no vertex is output
before any other vertex with an edge to it.
reserve
flight
call
taxi
taxi to
airport
pack
bags
check in
airport
take
flight
locate
gate
21
Topological Sort
call
taxi
pack
bags
taxi to
airport
reserve
flight
check in
airport
locate
gate
take
flight
22
Topo-Sort Take One
Label each vertex’s in-degree (# of inbound edges)
While there are vertices remaining
Pick a vertex with in-degree of zero and output it
Reduce the in-degree of all vertices adjacent to it
Remove it from the list of vertices
runtime:
23
Topo-Sort Take Two
Label each vertex’s in-degree
Initialize a queue to contain all in-degree zero vertices
While there are vertices remaining in the queue
Remove a vertex v with in-degree of zero and output it
Reduce the in-degree of all vertices adjacent to v
Put any of these with new in-degree zero on the queue
runtime:
Can we use a stack instead of a queue ?
24
Topo-Sort
• So we sort again in linear time:
O(|V|+|E|)
• Can we apply this to normal sorting ?
If we sort an array A[1], A[2], ..., A[n] using
topo-sort, what is the running time ?
25
Recall: Tree Traversals
a
b
f
c
h
g
e
d
k
abfgkcdhilje
i
j
l
26
Depth-First Search
• Pre/Post/In – order traversals are examples of
depth-first search
– Nodes are visited deeply on the left-most branches
before any nodes are visited on the right-most branches
• Visiting the right branches deeply before the left would still be
depth-first! Crucial idea is “go deep first!”
• Difference in pre/post/in-order is how some computation (e.g.
printing) is done at current node relative to the recursive calls
• In DFS the nodes “being worked on” are kept on
a stack
27
DFS
void DFS(Node startNode) {
Stack s = new Stack;
s.push(startNode);
while (!s.empty()) {
x = s.pop();
/* process x */
for y in x.children() do
s.push(y);
}
28
DFS on Trees
a
a
b
b
c
e
d
g
f
h
g
i
j
k
k
l
29
DFS on Graphs
a
a
b
b
c
e
d
g
f
h
g
i
j
k
k
Problem with cycles: may run into an infinite loop
l
30
DFS
void DFS(Node startNode) {
for v in Nodes do
v.visited = false;
Stack s = new Stack;
s.push(startNode);
while (!s.empty()) {
x = s.pop();
x.visited = true;
/* process x */
for y in x.children() do
if (!y.visited) s.push(y);
}
Running time:
31
Breadth-First Search
• Traversing tree level by level from top to bottom
(alphabetic order)
a
b
f
c
h
g
k
e
d
i
j
l
32
Breadth-First Search
BFS characteristics
• Nodes being worked on maintained in a
FIFO Queue, not a stack
• Iterative style procedures often easier to
design than recursive procedures
33
Breadth-First Search
void DFS(Node startNode) {
for v in Nodes do
v.visited = false;
Stack s = new Stack;
void BFS(Node startNode) {
for v in Nodes do
v.visited = false;
Queue s = new Queue;
s.push(startNode);
while (!s.empty()) {
x = s.pop();
x.visited = true;
/* process x */
for y in x.children() do
if (!y.visited) s.push(y);
}
s.enqueue(startNode);
while (!s.empty()) {
x = s.dequeue();
x.visited = true;
/* process x */
for y in x.children() do
if (!y.visited) s.enqueue(y);
}
34
QUEUE
a
bcde
cdefg
defg
efghij
fghij
ghij
hijk
ijk
jkl
kl
l
a
b
f
c
h
g
k
e
d
i
j
l
35
Graph Traversals
DFS and BFS
• Either can be used to determine connectivity:
– Is there a path between two given vertices?
– Is the graph (weakly) connected?
• Important difference: Breadth-first search
always finds a shortest path from the start vertex
to any other (for unweighted graphs)
– Depth first search may not!
36