Week 7 - Graph Traversals
Week 7 - Graph Traversals
WARNING
Directed edge
– ordered pair of vertices (u, v) QF 413
SYD MEL
– u is the origin/tail
– v is the destination/head
– e.g., a flight
870 km
SYD MEL
Undirected edge
– unordered pair of vertices (u, v)
– e.g., a two-way road
cslab1a cslab1b
Electronic circuits
– Printed circuit board math.brown.edu
– Integrated circuit
Transportation networks cs.brown.edu
– Highway network
brown.edu
– Flight network
qwest.net
Computer networks att.net
– Internet
– Web
cox.net
Modeling John
V
A simple path is one where all vertices
are distinct
U X Z
Examples
– (V, X, Z) is a simple path
– (U, W, X, Y, W, V) is a path that is W
not simple
Y
A (simple) path from s to t is also
called an s-t path.
Examples W
– (V, X, Y, W, U, V) is a simple cycle
– (U, W, X, Y, W, V, U) is a cycle
that is not simple Y
A connected component of a
graph G is a maximal connected Connected graph
subgraph of G
Fact: ∑v in V deg(v) = 2m
Notation Example: K4
n number of vertices n=4
m number of edges m=6
Δ maximum degree max deg = 3
Undirected
Graph
alternatives
degree(v)
incidentEdges(v)
Edge sequence V
– sequence edges
– edge object keeps track of u v w z
its position in the sequence
– Edge object points to the two
vertices it connects e f g h
a b
§ n vertices, m edges
§ no parallel edges
Edge Adjacency Adjacency
§ no self-loops
List List Matrix
Space O(n + m) O(n + m) O(n2)
incidentEdges(v) O(m) O(deg(v)) O(n)
getEdge(u, v) O(m) O(min(deg(u), deg(v))) O(1)
insertVertex(x) O(1) O(1) O(n2)
insertEdge(u, v, x) O(1) O(1) O(1)
removeVertex(v) O(m) O(deg(v)) O(n2)
removeEdge(e) O(1) O(1) O(1)
A systematic and structured way of visiting all the vertices and all
the edges of a graph
C
C
return parent
A unexplored vertex A
A visited vertex
B D E
unexplored edge
DFS edge C
back edge
A A
B D E B D E
C C
A A
B D E B D E
C C
A A
B D E B D E
C C
The University of Sydney Page 25
DFS main function performance
return parent
# visit neighbors of u
O(deg(u)) time not counting for v in G.incident(u) do
work done in recursive calls to if not visited[v] then
DFS_visit parent[v] ← u
DFS_visit(v)
B D E
Trivial O(m2) time algorithm: For each edge (u,v) in E, remove (u,v)
and check using DFS if G is still connected, put back (u,v)
A level d&u
B
A 0 0
B B 1 0
A D E
C 3 0
D 2 0
C D
E 3 3
C E
The University of Sydney Page 33
Breadth-First Search (BFS)
s L1 L2 Lk
L0
A
A unexplored vertex
A visited vertex L1 B C D
unexplored edge
BFS edge E F
cross edge
L0 L0
A A
L1 L1
B C D B C D
E F E F
L0 L0
A A
L1 L1
B C D B C D
L2
E F E F
L0 L0
A A
L1 L1
B C D B C D
L2 L2
E F E F
The University of Sydney Page 37
Example (cont.)
L0 L0
A A
L1 L1
B C D B C D
L2 L2
E F E F
L0
A
L1
B C D
L2
E F
The University of Sydney Page 38
Properties
The additional attributes about the vertices (seen and parent) can
be associated directly via Vertex class or we can use an external
map data structure
A A L0
B C D B C D L1
E F E F L2
DFS BFS
The University of Sydney Page 43
DFS vs. BFS (cont.)
A A L0
B C D B C D L1
E F E F L2
DFS BFS
The University of Sydney Page 44
Terminology (Undirected graphs)