Data Structures
CSCI 132, Spring 2014
Lecture 38
Graphs
1
Objects and Connections
Many problems are naturally formulated in terms of objects and the
connections between them. For example:
•Airline Routes--What is the fastest (or cheapest) way to get
from one city to another?
•Electrical circuits--Circuit elements are wired together. How
does the current flow?
•Job Scheduling--The objects are tasks that need to be
performed. The connections indicate which jobs should be
done before which other jobs.
•Links between web pages
A graph is a mathematical object that describes such situations.
Algorithms for graphs are fundamental to CS.
Graph Theory is a major branch of combinatorial mathematics.
2
Graphs
A graph is a collection of vertices, V, and edges, E.
An edge connects two vertices.
a
a
d
is the same as:
d
b
c
b
c
Vertices: a, b, c, d
Edges: ab, bc, ac, ad
3
Definitions
A path from one vertex to another is a list of vertices in which
successive vertices are connected by edges in the graph.
a
Example:
A path from a to c could be ac or abc.
b
d
c
A graph is connected if there is a path from every node to every
other node in the graph. The above graph is connected.
a
b
d
c
e
Not
Connected
4
More definitions
A simple path is a path with no vertex repeated.
A simple cycle is a simple path except the first and last vertex is
repeated and, for an undirected graph, number of vertices >= 3.
a
Example:
abca is a cycle
b
d
c
A tree is a graph with no cycles.
a
b
d
c
5
Definitions Continued
A complete graph is a graph in which all edges are present.
A sparse graph is a graph with relatively few edges.
A dense graph is a graph with many edges.
a
d
a
d
a
d
b
c
b
c
b
c
Complete
Dense
Sparse
6
Types of Graphs
An undirected graph has no specific direction between the
vertices.
A directed graph has edges that are "one way". We can go from
one vertex to another, but not vice versa.
A weighted graph has weights associated with each edge. The
weights can represent distances, costs, etc.
a
d
a
d
a
13
b
c
Undirected
b
Directed
c
b
34
d
22
4
19
Weighted
c
7
Representing Graphs as an
Adjacency List
An adjacency list is an array which contains a list for each
vertex. The list for a given vertex contains all the other vertices
that are connected to the first vertex by a single edge.
List
1
1
2
3
5
4
2
3
4
5
Definition: A digraph G consists of a set V, called the vertices of
G, and for all v in V, a subset Av of V, called the set of vertices
8
adjacent to v.
Representing Graphs as an
Adjacency List
An adjacency list is an array which contains a list for each
vertex. The list for a given vertex contains all the other vertices
that are connected to the first vertex by a single edge.
List
2
5
1
1
2
3
5
4
2
1
5
3
2
4
4
2
5
4
3
3
1
2
4
5
Definition: A digraph G consists of a set V, called the vertices of
G, and for all v in V, a subset Av of V, called the set of vertices
9
adjacent to v.
Representing a Graph with an
Adjacency Matrix
An adjacency matrix is a matrix with a row and column for each
vertex. The matrix entry is 1 if there is an edge between the row
vertex and the column vertex.
1
2
3
5
4
1
2
3
4
5
The diagonal may be zero or one.
Each edge is represented twice.
1
0
1
0
0
1
2
1
0
1
1
1
3
0
1
0
1
0
4
0
1
1
0
1
5
1
1
0
1
0
10
Representing Directed Graphs
In directed graphs we only include in the list those vertices that
are pointed to by a given vertex.
List
1
2
3
1
4
5
1
1
2
3
4
5
6
2
2
6
3
4
5
6
3
4
5
6
11
Representing Directed Graphs
In directed graphs we only include in the list those vertices that
are pointed to by a given vertex.
List
1
2
3
4
2
1
4
1
2
3
4
5
6
5
1
0
0
0
0
0
0
2
1
0
0
1
0
0
6
3
0
0
0
0
0
0
4
1
0
0
0
1
0
5
0
1
1
0
0
0
6
0
0
1
0
0
1
2
5
3
5
4
2
5
6
6
4
6
12
Representing Weighted Graphs
In weighted graphs we include the weights of each edge.
1
1
2
2
3
4
1
1
2
3
4
5
6
3
7
5 6
4
5
2
4
1
2
1
6
3
List
5
6
3
4
5
6
13
Representing Weighted Graphs
In weighted graphs we include the weights of each edge.
1
1
2
2
3
4
1
2
3
4
5
6
1
0
0
0
0
0
0
3
7
5 6
4
5
2
2
0
0
3
0
0
1
6
3
0
0
0
0
0
0
List
4
1
0
0
0
4
0
5
0
5
6
0
0
0
6
0
0
7
0
0
1
1
4 1
2
5 5
3
5 6
4
2 3
5
6
2 2
6 7
4 4
6 1
14
Lists vs. Matrices
Speed:
An adjacency matrix provides the fastest way to determine
whether or not a particular edge is present in the graph.
For an adjacency list, there is no faster way than to search the
entire list for the presence of the edge.
Memory:
Normally, an adjacency matrix requires more space (n2 entries).
However, if n is small and the graph is unweighted, an
adjacency matrix only needs 1 bit per entry. The adjacency list
requires at least 1 word per entry (for the address of the next
node of the list). This may offset the advantage of fewer
entries.
15
Breadth First Traversal
Problem:
•Given a graph, G = (V, E), find all the vertices reachable from
some source, s.
•Repeat for all unvisited vertices
Strategy:
•Visit all vertices adjacent to s first.
•Traversal expands outward level by level. (Visit all vertices a
distance of 2 away from s second, then those at distance 3, etc.)
•Keep track of nodes that have been visited already (Otherwise
may visit a vertex twice or end up in an endless cycle).
16
Pseudocode
for
Breadth
First
Traversal
template <int max_size>
void Digraph <max_size>:: breadth_first(void (*visit)(vertex &)) const {
Queue q;
bool visited[max_size];
Vertex v, w, x;
for (all v in G) visited[v] = false;
for (all v in G)
if (!visited[v]) {
q.append(v);
while(!q.empty( )) {
q.retrieve(w);
if(!visited[w]) {
visited[w] = true;
(*visit)(w);
for (all x adjacent to w)
q.append(x);
}
q.serve( );
}
}
}
Example
a
b
c
d
e
f
g
h
We will work through this in class.
18
© Copyright 2026 Paperzz