Professional Documents
Culture Documents
1
Outline
2
1 8 7 10
2555 13
HNL 1233 9 9
LAX DFW 1120
MIA
4
Edge Types
Directed edge
ordered pair of vertices (u,v)
first vertex u is the origin flight
ORD AA 1206 PVD
second vertex v is the destination
e.g., a flight
Undirected edge
unordered pair of vertices (u,v) 849
e.g., a flight route ORD PVD
miles
Directed graph
all the edges are directed
e.g., route network
Undirected graph
all the edges are undirected
e.g., flight network
Applications
5
Path
sequence of alternating vertices and
edges
begins with a vertex V
ends with a vertex
a b
P1
each edge is preceded and followed d
by its endpoints U X Z
Simple path P2 h
path such that all its vertices and
c e
edges are distinct W g
Examples
P1=(V,b,X,h,Z) is a simple path f
P2=(U,c,W,e,X,g,Y,f,W,d,V) is a Y
path that is not simple
Terminology (3/3)
8
Cycle
circular sequence of alternating
vertices and edges
V
each edge is preceded and followed a b
by its endpoints
Simple cycle U d X Z
cycle such that all its vertices and C2 h
edges are distinct e C1
c
Examples W g
C1=(V,b,X,g,Y,f,W,c,U,a,) is a
simple cycle f
C2=(U,c,W,e,X,g,Y,f,W,d,V,a,) is Y
a cycle that is not simple
Properties
9
Property 1 Notation
v deg(v) 2m n number of vertices
Proof: each edge is counted m number of edges
twice deg(v) degree of vertex v
Property 2
In an undirected graph with no Example
self-loops and no multiple
n 4
edges
m n (n 1)2 m 6
Update methods
Vertices and edges insertVertex(o): insert a vertex
are positions storing element o
store elements insertEdge(v, w, o): insert an
edge (v,w) storing element o
Accessor methods removeVertex(v): remove
endVertices(e): an array of the vertex v (and its incident edges)
two endvertices of e
removeEdge(e): remove edge e
opposite(v, e): the vertex
opposite of v on e Iterator methods
areAdjacent(v, w): true iff v and incidentEdges(v): edges
w are adjacent incident to v
replace(v, x): replace element at vertices(): all vertices in the
vertex v with x graph
replace(e, x): replace element at edges(): all edges in the graph
edge e with x
Edge List Structure
11
Vertex object u
element a c
reference to position in b d
vertex sequence v w z
Edge object
element
origin vertex object
destination vertex object
u v w z
reference to position in edge
sequence
Vertex sequence
sequence of vertex objects a b c d
Edge sequence
sequence of edge objects
Adjacency List Structure
12
B D E
14
Subgraphs
15
A subgraph S of a graph G
is a graph such that
The vertices of S are a subset
of the vertices of G
The edges of S are a subset of Subgraph
the edges of G
A spanning subgraph of G is
a subgraph that contains all
the vertices of G
Spanning subgraph
Connectivity
16
A graph is connected
if there is a path
between every pair of
vertices
A connected Connected graph
component of a graph
G is a maximal
connected subgraph of
G
A spanning tree of a
connected graph is a spanning
subgraph that is a tree
A spanning tree is not unique
unless the graph is a tree
Spanning trees have Graph
applications to the design of
communication networks
A spanning forest of a graph
is a spanning subgraph that is
a forest
Spanning tree
19
Depth-First Search
Depth-first search (DFS) DFS on a graph with n
is a general technique for vertices and m edges takes
traversing a graph O(nm ) time
A DFS traversal of a graph DFS can be further
G extended to solve other
Visits all the vertices and graph problems
edges of G Find and report a path
Determines whether G is between two given vertices
connected Find a cycle in the graph
Computes the connected Depth-first search is to
components of G
graphs what Euler tour is
Computes a spanning forest
of G to binary trees
DFS Algorithm
20
A
A unexplored vertex
A visited vertex
B D E
unexplored edge
discovery edge C
back edge
A A
B D E B D E
C C
Example (2/2)
22
A A
B D E B D E
C C
A A
B D E B D E
C C
Properties of DFS
23
Property 1
DFS(G, v) visits all the
vertices and edges in the A
connected component of v
Property 2 B D E
The discovery edges
labeled by DFS(G, v)
form a spanning tree of the C
connected component of v
24
Analysis of DFS
L1
B C D
L2
E F
26
Breadth-First Search
27
L0 L0
A A
L1 L1
B C D B C D
E F E F
Example (2/3)
30
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
Example (3/3)
31
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
Properties
32
Notation A
Gs: connected component of s
Property 1 B C D
BFS(G, s) visits all the vertices and
edges of Gs E F
Property 2
The discovery edges labeled by
BFS(G, s) form a spanning tree Ts of L0
Gs A
Property 3 L1
For each vertex v in Li B C D
The path of Ts from s to v has i edges L2
Every path from s to v in Gs has at least i E F
edges
Analysis
33
Biconnected components
L0
A A
L1
B C D B C D
L2
E F E F
DFS BFS
DFS vs. BFS (2/2)
35
L0
A A
L1
B C D B C D
L2
E F E F
DFS BFS
36
References