Graphs

Graphs
 Chapter

12
Graphs
 Overview

Graphs provide a rich and powerful way to
model relations.
Chapter Objectives
 1.
To review basic graph vocabulary and
concepts.
 2.
To develop classes that model various
kinds of graphs.
 3.
To learn some useful algorithms for
manipulating graphs.
Graphs
 Graphs
represent many real-world
problems
 They
do so by showing complex
relationships between objects
Course prerequisites
Course
Prerequisite(s)
Barry has
completed?
Math 101 None
Yes
Math 201 Math 101
Yes
CS 101 None
Yes
CS 102 CS 101; Math 201 No
CS 215 CS 102; Math 211 No
Math 211 Math 101;Math 201 No
CS 247 CS 215; Math 211 No
CS 392 CS 215
No
CS 405 CS 247; CS 392
No
Prerequisites in
Graphical Form
Math 101
Math 211
Math 201
CS 101
CS 247
CS 215
CS 102
CS 405
CS 392
Tables and graphs
 One
is commonly used to represent the
other.
 Both have a two-dimensional quality
 Both can be used to represent
relationships between objects
Graph Terminology
 Graph
- represents relationship among nodes
 Vertex - a node in a graph
 Edge - link connecting 2 nodes (relationship)
 Adjacent - 2 nodes connected by an edge
 Undirected Graph - a graph in which the edges
have no directional component
Terminology (con’t)
 Network
- a graph in which the edges
are weighted
 Directed
graph (digraph) - a graph in
which the edges have directional
components (like the course
prerequisites example)
Terminology (con’t)
 Path
- a sequence of distinct vertices,
each adjacent to the next
 Cycle - an undirected path containing at
least three vertices such that the last
vertex is adjacent to the first
 Connected graph - an undirected graph
in which there is a path from any vertex
to any other vertex
Types of graphs
Terminology (con’t)
 Disconnected
Graph - an undirected
graph in which it is impossible to get to
at least one node from any other vertex
 Free
tree - a connected, undirected
graph with no cycles
examples
Directed cycle - a directed
path which is cyclical
Strongly connected - a digraph in
which there is a directed path from
any vertex to any other vertex
Weakly connected - a digraph in
which it is not possible to go from
any vertex to any other
Graph theory
 Graphs
may be most conveniently
defined in terms of sets.
 First,
we have a set of vertices (V)
 Second,
we have a set of edges (E)
 A graph
is a set of V and E
Adjacency
 If
there is an edge connection two nodes
then they are adjacent.
 Note:
if the edge is directed, then there
is only adjacency one way.
 Let Av
be the set of all vertices adjacent
to a given vertex v
Adjacency
 For
the subsets Av, we can construct the
edges as ordered pairs by the rule:
 The
pair (v,w) is an edge if and only if w
is a member of Av.
An Example Digraph
0
2
1
3
4
Vertices
Each vertex is part of the set V of vertices in the graph
V = {0,1,2,3,4}
0
2
1
3
4
Edges
Each edge can be represented as a pair of vertices
E = {{0,3}, {1,0}, {1,2}, {1,4}, {2,1}, {4,0},{4,2}}
0
2
1
3
4
Graphs and set
theory
 We
can keep track of all the edges in a
graph by keeping track, for all vertices v,
of the set of edges containing v (E).
 Similarly,
we could keep track of the set
A of all vertices adjacent to v.
Pairs adjacent to
vertex 0 (A0)
The vertices adjacent to vertex 0 are the set
A0 = {3}
0
2
1
3
4
New graph definition
 This
leads us to a new definition of a
graph:
 A graph
G consists of a set V, called the
vertices of G, and, for all v in V, a subset
A of V, consisting of the set of vertices
adjacent to v.
Graph definition
G = V + A,
0
2
V = {0,1,2,3,4},
A0 = {3}
A1 = {0,2,4}
A2 = {1}
A3 = {}
A4 = {0,2}
1
3
4
Undirected graphs
 This
definition works with undirected
graphs as well:
 An undirected graph is one which
satisfies the following symmetry
property:
 if w is a member of Av, this implies that
v is a member of Aw, for all v and w
such that v,w are members of V
Undirected Graph
0
1
2
3
4
Vertices
Each vertex is part of the set V of vertices in the graph
V = {0,1,2,3,4}
0
1
2
3
4
Edges
Each edge can be represented as a pair of vertices
E = {{0,1}, {0,3}, {0,4}, {1,2}, {1,4}, {2,4}}
0
1
2
3
4
Pairs adjacent to 0 (A0)
The vertices adjacent to vertex 0 are the set
A0 = {1,3,4}
0
1
2
3
4
Graph definition
G = V + A,
V = {0,1,2,3,4},
0
1
2
3
A0 = {1,3,4}
A1 = {0,2,4}
A2 = {1}
A3 = {0}
A4 = {0,1}
4
How to represent
graphs
 Since
graphs are sets and subsets, they
need a two dimensional structure
 One
 The
dimension is the vertices
other dimension is the edges (or the
vertices adjacent to the one we are on)
What might do
with graphs?
 Neighborhood

For a vertex, find all edges connected to
other vertices
 Edge

queries
member queries
Determine whether an edge is in the graph
Analysis
 If
there are n vertices
 and m edges
 it may take as many as n*m operations
to traverse every relationship
represented in a graph
 Depending on our representation of the
graph we may be able to streamline the
searching process
List representation
 One
method of implementing a graph is
to employ a List of vertices, such that
from each vertex we can have access to
those which are adjacent.
 A list
of linked lists is one method of
doing this.
An Adjacency List
0
1
2
3
4
3
0
4
1
2
0
2
Adjacency Lists
 Linked
implemetation
 Advantages

No wasted memory space
 Disadvantages

More complicated.

Sequential access only.

Search would be O(n) where n is the
number of possible vertices adjacent to any
given vertex.
Contiguous
implementation
 Advantages


Less awkward than linked
Allows for random access
Adjacency Matrix
0
1
2
3
4
0
1
0
0
1
0
1
1
1
1
0
1
2
0
1
1
0
0
3
0
0
0
1
0
4
1
0
1
0
1
Adjacency Tables
 Advantages
Easy to interpret.
A[v,w] is true only if vertex v is
adjacent to vertex w.




If the graph is directed then this indicates
an edge from v to w.
If the graph is undirected then the
adjacency table is symmetric and A[w,v] will
also be true.
Disadvantages
 Mirrored
across the diagonal for
undirected graphs
 Not
sparse.
UAL GraphADT
 Characteristics
 An
Undirected Adjacency List Graph G =
(V,E) stores an undirected graph so
that vertex neighbors can be found
efficiently.
 The number of vertices in the graph, n =
|V|, is fixed when the graph is created.
 The vertices are labeled 0…n-1.
Operations:
vertexSize()




int vertexSize()
Precondition: None.
Postcondtion: None.
Returns: The number of vertices in the
graph, |V|.
edgeSize()
 int



edgeSize()
Precondition: None.
Postcondtion: None.
Returns: The number of edges in
the graph, |E|.
addEdge()


void addEdge(i,j)
Precondition:
0  i  n;0  j  n; (i, j )  E.

Postcondition:
(i, j )  E
nextNeighbor()
 int
nextNeighbor(i)
 Precondition:
0in
 Postcondition: If the current iterator position
is within the neighbor list, it’s advanced to
the next neighbor; if it’s at the end of the
neighbor list, it’s reset to the beginning.
 Returns: The next neighbor of i, or n if at
the end of the list.
DAL Graph ADT
 Characteristics
 A Directed Adjacency
List Graph G =
(V,E) stores an directed graph so that
vertex neighbors can be found
efficiently.


The number of vertices in the graph, n =
|V|, is fixed when the graph is created.
The vertices are labeled 0…n-1.
vertexSize() and
edgeSize()
 int
vertexSize()
 Precondition: None.
 Postcondition: None.
 Returns: The number of vertices |V|.
 int
edgeSize()
 Precondition: None.
 Postcondition: None.
 Returns: The number of edges |E|.
addEdge()
 void

addEdge(i,j)
Precondition:
0  i  n;0  j  n; (i, j )  E.

Postcondition:
(i, j )  E
nextNeighbor()
 int
nextNeighbor(i)
 Precondition: 0  i  n
 Postcondition: If the current iterator
position is within the neighbor list, it’s
advanced to the next neighbor; if it’s at
the end of the neighbor list, it’s reset to
the beginning.
 Returns: The next neighbor of i, or n if
at the end of the list.
UAM Graph ADT
 Characteristics
 An
Undirected Adjacency Matrix Graph
G = (V,E) stores an undirected graph so
that vertex connectivity can be queried
efficiently.


The number of vertices in the graph, n =|V|,
is fixed when the graph is created.
The vertices are labeled 0…n-1.
Operations:
 int
vertexSize()
 Precondition: None.
 Postcondition: None.
 Returns: The number of vertices in graph, |V|
 int
edgeSize()
 Precondition: None.
 Postcondition: None.
 Returns: The number of edges in the graph, |E|
addEdge()
 void
addEdge(i,j)

 Precondition:
0  i  n;0  j  n;{i, j}  E.

 Postcondition:
{i, j}  E
edgeConnected()
 bool



edgeConnected(i,j)
Precondition: 0  i  n;0  j  n.
Postcondition: None.
Returns: True if and only if {i, j}  E.
Note that by convention
{i, i}  E.
DAM Graph ADT
 Characteristics
 A Directed Adjacency
Matrix Graph G =
(V,E) stores an directed graph so that
vertex connectivity can be queried
efficiently.


The number of vertices in the graph, n =
|V|, is fixed when the graph is created.
The vertices are labeled 0…n-1.
Operations:
 int
vertexSize()
 Precondition: None.
 Postcondition: None.
 Returns: The number of vertices in graph, |V|.
 int
edgeSize()
 Precondition: None.
 Postcondition: None.
 Returns: The number of edges in graph, |E|.
addEdge()





void addEdge(i,j)
Precondition:
0  i  n;0  j  n; (i, j )  E.
Postcondition:
{i, j}  E
edgeConnected()
 bool



edgeConnected(i,j)
Precondition: 0  i  n;0  j  n.
Postcondition: None.
Returns: True if and only if (i, j )  E.
Note that by convention
(i, i )  E.
Undirected graph
for Exercise 12-5
0
2
5
1
3
4
6
7
Directed graph for
Exercise 12-6
0
3
1
4
2
5
Adjacency List for
Exercise 12-7
0
1
2
3
4
5
3
0
4
2
2
1
5
3
1
5
1
Adjacency Matrix for
Exercise 12-8
0
1
2
3
4
5
6
0
1
0
1
1
0
1
0
1
0
1
1
0
1
0
1
2
1
1
1
0
0
1
1
3
1
0
0
1
0
0
0
4
0
1
0
0
1
1
0
5
1
0
1
0
1
1
1
6
0
1
1
0
0
1
1