Lecture 14
Lecture 14
Aaron Bauer
Winter 2014
Announcements
A
C
• If (u,v) ∈ E
– Then v is a neighbor of u, i.e., v is adjacent to u
– Order matters for directed edges
• u is not adjacent to v unless (v,u) ∈ E
Winter 2014 CSE373: Data Structures & Algorithms 11
Examples again
Which would use directed edges? Which would have self-edges?
Which would be connected? Which could have 0-degree nodes?
Kingston 30 Edmonds
Bainbridge 35
Seattle
60
Bremerton
Winter 2014 CSE373: Data Structures & Algorithms 13
Examples
• A cycle is a path that begins and ends at the same node (v0==vn)
Chicago
Seattle
San Francisco
Dallas
Example: [Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle]
Winter 2014 CSE373: Data Structures & Algorithms 15
Path Length and Cost
• Path length: Number of edges in a path
• Path cost: Sum of weights of edges in a path
Example where
P= [Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle]
3.5 Chicago
Seattle
2 2
length(P) = 5
2 Salt Lake City
2.5 cost(P) = 11.5
2.5 2.5
3
San Francisco Dallas
Winter 2014 CSE373: Data Structures & Algorithms 16
Simple Paths and Cycles
Example:
D
A C
Example:
D
A C
Example:
D
A C
C D E F
F
G H
G H
Winter 2014 CSE373: Data Structures & Algorithms 25
Rooted Trees
• We are more accustomed to rooted trees where:
– We identify a unique root
– We think of edges as directed: parent to children
• Because |E| is often much smaller than its maximum size, we do not
always approximate |E| as O(|V|2)
– This is a correct bound, it just is often not tight
– If it is tight, i.e., |E| is Θ(|V|2) we say the graph is dense
• More sloppily, dense means “lots of edges”
– If |E| is O(|V|) we say the graph is sparse
• More sloppily, sparse means “most possible edges missing”
0 1 2 3
D(3) 0 F T F F
A(0) C(2) 1 T F F F
B(1) 2 F T F T
3 F F F F
Winter 2014 CSE373: Data Structures & Algorithms 31
Adjacency Matrix Properties
0 1 2 3
• Running time to:
0 F T F F
– Get a vertex’s out-edges:
– Get a vertex’s in-edges:
1 T F F F
– Decide if some edge exists:
– Insert an edge: F T F T
2
– Delete an edge:
3 F F F F
• Space requirements:
• Space requirements:
• Space requirements:
• Space requirements:
• Space requirements:
• Space requirements:
• Space requirements:
– |V|2 bits
• Space requirements:
– |V|2 bits
0 1 2 3
0 F T F F
1 T F F F
2 F T F T
3 F F F F
Winter 2014 CSE373: Data Structures & Algorithms 40
Adjacency Matrix Properties
0 1 2 3
0 F T F F
1 T F F F
2 F T F T
3 F F F F
Winter 2014 CSE373: Data Structures & Algorithms 41
Adjacency Matrix Properties
1 T F F F
2 F T F T
3 F F F F
Winter 2014 CSE373: Data Structures & Algorithms 42
Adjacency List
D(3) 0 1 /
A(0) C(2) 1 0 /
B(1) 2 3 1 /
3 /
3 /
– Get all of a vertex’s in-edges:
– Insert an edge:
– Delete an edge:
• Space requirements:
– Insert an edge:
– Delete an edge:
• Space requirements:
– Insert an edge:
– Delete an edge:
• Space requirements:
2 F T F T 2 3 1 /
B
3 F F T F 3 /
CSE 417
One example output:
126, 142, 143, 374, 373, 417, 410, 413, XYZ, 415
Winter 2014 CSE373: Data Structures & Algorithms 54
Questions and comments
• …