graph-terminology-and

advertisement
Graphs
Chapter 12
Graphs
• A graph is a data structure that consists of a set of vertices and a
set of edges between pairs of vertices
• Edges represent paths or connections between the vertices
• The set of vertices and the set of edges must both be finite and
neither one be empty
• Example:
• A vertex represents an airport
• An edge represents a flight route between two airports and
stores the mileage of the route
SFO
PVD
ORD
LGA
HNL
LAX
DFW
Chapter 12: Graphs
MIA
2
Visual Representation of Graphs
• Vertices are represented as points or labeled circles and
edges are represented as lines joining the vertices
• The physical layout of the vertices and their labeling are
not relevant
Chapter 12: Graphs
3
Graphs
• A graph is a pair (V, E), where
• V is a set of vertices
• E is a set of pairs of vertices, called edges
• Example:
• V = {A, B, C, D, E}
• E = {(A,B), (A,D), (C,E), (D, E)}
Chapter 12: Graphs
4
Edge Types
• Directed edge
• ordered pair of vertices (u,v)
• first vertex u is the origin
• second vertex v is the destination
• e.g., a flight
• Undirected edge
• unordered pair of vertices (u,v)
• e.g., a flight route
Chapter 12: Graphs
ORD
flight
AA 1206
PVD
ORD
849
miles
PVD
5
Directed and Undirected Graphs
• A graph with directed edges is called a directed graph or
digraph
• Example: flight network
• A graph with undirected edges is an undirected graph or
simply a graph
• Example: route network
Chapter 12: Graphs
6
Weighted Graphs
• The edges in a graph may have values associated with
them known as their weights
• A graph with weighted edges is known as a weighted
graph
Chapter 12: Graphs
7
Terminology
• End vertices (or endpoints) of
an edge
• U and V are the endpoints
of a
• Edges incident on a vertex
• a, d, and b are incident on
V
• Adjacent vertices
• U and V are adjacent
• Degree of a vertex
• X has degree 5
• Parallel edges
• h and i are parallel edges
• Self-loop
• j is a self-loop
a
U
V
b
d
X
c
Chapter 12: Graphs
h
e
W
j
Z
i
g
f
Y
8
Terminology (cont.)
• A path is a sequence of vertices in
which each successive vertex is
adjacent to its predecessor
• In a simple path, the vertices and
edges are distinct except that the
first and last vertex may be the
same
• Examples
• P1=(V,X,Z) is a simple path
• P2=(U,W,X,Y,W,V) is a path that
is not simple
a
U
Chapter 12: Graphs
c
V
b
d
P2
P1
X
e
W
h
Z
g
f
Y
9
Terminology (cont.)
• A cycle is a simple path in which
only the first and final vertices
are the same
• Simple cycle
a
• cycle such that all its
vertices and edges are
U
distinct
• Examples
c
• C1=(V,X,Y,W,U,V) is a simple
cycle
• C2=(U,W,X,Y,W,V,U) is a
cycle that is not simple
Chapter 12: Graphs
V
b
d
C2
X
e
C1
g
W
f
h
Z
Y
10
Subgraphs
• 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 the edges of G
• A spanning subgraph of G is a
subgraph that contains all the
vertices of G
Subgraph
Spanning subgraph
Chapter 12: Graphs
11
Connectivity
• A graph is connected if there is
a path between every pair of
vertices
• A connected component of a
graph G is a maximal
connected subgraph of G
Connected graph
Non connected graph with two
connected components
Chapter 12: Graphs
12
Trees and Forests
• A (free) tree is an undirected
graph T such that
• T is connected
• T has no cycles
• A forest is an undirected
graph without cycles
• The connected components
of a forest are trees
Tree
Forest
Chapter 12: Graphs
13
Spanning Trees and Forests
• 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
• A spanning forest of a graph is a
spanning subgraph that is a forest
Graph
Spanning tree
Chapter 12: Graphs
14
The Graph ADT and Edge Class
• Java does not provide a Graph ADT
• In making our own, we need to be able to do the
following
• Create a graph with the specified number of vertices
• Iterate through all of the vertices in the graph
• Iterate through the vertices that are adjacent to a specified
vertex
• Determine whether an edge exists between two vertices
• Determine the weight of an edge between two vertices
• Insert an edge into the graph
Chapter 12: Graphs
15
Vertices and edges
• Represent the vertices by integers from 0 up to |V|-1,
where |V| represents the cardinality of V.
• Define an Edge class containing
• Source vertex
• Destination vertex
• Weight
• An Edge object represents a directed edge
• For undirected edges, use two Edge objects, one in
each direction
Chapter 12: Graphs
16
Edge Class
Chapter 12: Graphs
17
Implementing the Graph ADT
• Because graph algorithms have been studied and
implemented throughout the history of computer science,
many of the original publications of graph algorithms and
their implementations did not use an object-oriented
approach and did not even use abstract data types
• Two representations of graphs are most common
• Edges are represented by an array of lists called
adjacency lists, where each list stores the vertices
adjacent to a particular vertex
• Edges are represented by a two dimensional array,
called an adjacency matrix
Chapter 12: Graphs
18
Adjacency List
• An adjacency list representation of a graph uses an
array of lists
• One list for each vertex
Chapter 12: Graphs
19
Adjacency List
1
1
2
5
2
4
3
4
3
5
2
1
5
5
4
4
2
4
3
1
5
2
• An array (Adj) of lists, one list per vertex
• For each vertex u in V,
• Adj[u] contains all edges incident on u
• Space required Θ(n+m), where n denotes the number of
vertices, and m denotes the number of edges
Chapter 12: Graphs
20
Adjacency List (continued)
Chapter 12: Graphs
21
Adjacency Matrix
• Uses a two-dimensional array to represent a graph
• For an unweighted graph, the entries can be boolean values
• Or use 1 for the presence of an edge
• For a weighted graph, the matrix would contain the weights
1
5
2
4
3
1
2
3
4
5
1 0
1
2
0
3
0
4
1
5
1
0
0
1
0
0
1
1
0
0
1
0
1
1
0
1
1
0
1
0
Chapter 12: Graphs
22
Adjacency Matrix
Chapter 12: Graphs
23
Overview of the Graph Class Hierarchy
Chapter 12: Graphs
24
Class AbstractGraph
Chapter 12: Graphs
25
The ListGraph Class
Chapter 12: Graphs
26
Performance
• n vertices, m edges
• no parallel edges
• no self-loops
• Bounds are “big-Oh”
Adjacency
List
Adjacency
Matrix
Space
n+m
n2
Iterate over incident edges of
v
deg(v)
n
isEdge (v, w)
min(deg(v), deg(w))
1
insert(v, w, o)
1
1
Chapter 12: Graphs
27
Download