Contents
• Terminology
• Graphs as ADTs
• Graphs as ADTs
• Applications of Graphs
Terminology
• Definition:
A set of points that are joined by lines
• Graphs also represent the relationships
among data items
• G = { V , E }; that is, a graph is a set of
vertices and edges
• A subgraph consists of a subset of a
graph’s vertices and a subset of its edges
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
What is a Graph?
• A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
• An edge e = (u,v) is a pair of vertices
• Example:
a b V= {a,b,c,d,e}
E= {(a,b),(a,c),(a,d),
(b,e),(c,d),(c,e),
c (d,e)}
d e
Applications
CS16
• electronic circuits
• networks (roads, flights, communications)
JFK
LAX STL
HNL
DFW
FTL
Terminology:
Adjacent and Incident
• If (v0, v1) is an edge in an undirected
graph,
v0 and v1 are adjacent
The edge (v0, v1) is incident on vertices v0
and v1
• If <v0, v1> is an edge in a directed graph
v0 is adjacent to v1, and v1 is adjacent from
v0
The edge <v0, v1> is incident on v0 and v1
Terminology
• Undirected graphs: edges do not indicate a
direction
• Directed graph, or digraph: each edge has
a direction
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Terminology:
Degree of a Vertex
The degree of a vertex is the number of
edges incident to that vertex
For directed graph,
the in-degree of a vertex v is the number of edges
that have v as the head
the out-degree of a vertex v is the number of edges
that have v as the tail
if di is the degree of a vertex i in a graph G with n vertices
and e edges, the number of edges is
n 1
Why? Since adjacent vertices each
e ( d )/ 2
0
i count the adjoining edge, it will be
counted twice
Examples
0
3
2
0 1 2
3 3
3 1 2 3 3 4 5 6
3G1 1 1 G2 1 1
3
0 in:1, out: 1
directed graph
in-degree
out-degree 1 in: 1, out: 2
2 in: 1, out: 0
G3
Terminology:
Path
• path: sequence of
3 2
vertices v1,v2,. . .vk
such that consecutive
vertices vi and vi+1 are 3
adjacent.
3 3
a b a b
c c
d e d e
abedc bedc
More Terminology
• simple path: no repeated vertices
a b
bec
c
d e
• cycle: simple path, except that the last vertex is the same as
the first vertex
a b
acda
c
d e
Graphs as ADTs
ADT graph operations
Test whether graph is empty.
Get number of vertices in a graph.
Get number of edges in a graph.
See whether edge exists between two given
vertices.
Insert vertex in graph whose vertices have
distinct values that differ from new vertex’s
value.
Graphs as ADTs
ADT graph perform following operations:-
Insert edge between two given vertices in
graph.
Remove specified vertex from graph and any
edges between the vertex and other vertices.
Remove edge between two vertices in graph.
Retrieve from graph vertex that contains given
value.
Adjacency Matrix
Let G=(V,E) be a graph with n vertices.
The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph i
symmetric; the adjacency matrix for a digraph
need not be symmetric
Examples for Adjacency Matrix
0 4
0
0
2 1 5
1 2
3 6
3 1
0 1 1 1 0 1 0
1 0 1 1 7
1 0 1
2 0 1 1 0 0 0 0 0
1 1 0 1 0 0 0 1
0 0 1 0 0 0 0
1 1 1 0
1 0 0 1 0 0 0 0
G2
G1
0 1 1 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 1 0 1 0
symmetric 0 0 0 0 0 1 0 1
0 0 0 0 0 0 1 0
undirected: n2/2
directed: n2
G4
Adjacency Lists (data structures)
Each row in adjacency matrix is represented as an adjacency list.
#define MAX_VERTICES 50
typedef struct node {
int vertex;
struct node *link;
}gnode;
gnode *adj[MAX_VERTICES];
int n=0; /* vertices currently in use */
0 0 4
2 1 5
1 2 3 6
3 7
0 1 2 3 0 1 2
1 0 2 3 1 0 3
2 0 1 3 2 0 3
3 0 1 2 3 1 2
G1 0 4 5
5 4 6
0 1 6 5 7
1 0 2 1
7 6
2
G3 G4
2
An undirected graph with n vertices and e edges ==> n head nodes and 2e list no
Implementing Graphs
FIGURE 20-6 (a) A directed graph and
(b) its adjacency matrix
Implementing Graphs
FIGURE 20-7 (a) A weighted undirected graph and
(b) its adjacency matrix
Implementing Graphs
FIGURE 20-8 (a) A directed graph and
(b) its adjacency list
Implementing Graphs
FIGURE 20-9 (a) A weighted undirected graph and
(b) its adjacency list
Graph Traversal
• Problem: Search for a certain node or
traverse all nodes in the graph
• Depth First Search
Once a possible path is found, continue the
search until the end of the path
• Breadth First Search
Start several paths at a time, and advance
in each one step at a time
Graph Traversals
• Visits all of the vertices that it can reach
Happens if and only if graph is connected
• Connected component is subset of vertices
visited during traversal that begins at given
vertex
Depth-First Search
• Goes as far as possible from a vertex before
backing up
• Recursive algorithm
Depth-First Search
FIGURE 20-10 Visitation order for (a) a depth-first search; (b) a breadth-first
search
Depth-First Search
FIGURE 20-11 A connected graph with cycles
Depth-First Search
FIGURE 20-12 The results of a depth-first traversal,
beginning at vertex a , of the graph in Figure 20-11
Breadth-First Search
• Visits all vertices adjacent to vertex before
going forward
See Figure 20-10b
• Breadth-first search uses a queue
Breadth-First Search
FIGURE 20-13 The results of a breadth-first traversal,
beginning at vertex a, of the graph in Figure 20-11
Applications of Graphs
Topological Sorting
V1, V2, V3, V4 and V1, V3, V2, V4 are legal orderings
Topological Sorting
Degree of a vertex U: the number of edges (U,V)- outgoing edges
Indegree of a vertex U:the number of edges (V,U)-incoming edges
The algorithm for topological sort uses "indegrees" of vertices.
Algorithm
[Link] the indegrees of all vertices
[Link] a vertex U with indegree 0 and print it (store it in the
ordering)
[Link] there is no such vertex then there is a cycle
and the vertices cannot be ordered. Stop.
[Link] U and all its edges (U,V) from the graph.
Update the indegrees of the remaining vertices.
Repeat steps 2 through 4 while there are vertices to be
processed.
Topological Sorting
One possible sorting: V1, V2, V4, V3, V5
Another sorting: V1, V2, V4, V5, V3
Applications of Graphs
• Topological Sorting
FIGURE 20-14 A directed graph without cycles
Applications of Graphs
FIGURE 20-15 The graph in Figure 20-14 arranged
according to the topological orders (a) a, g,
d, b, e, c, f and (b) a, g, b, d, e, f, c
Spanning Trees
• Tree: an undirected connected graph
without cycles
• Observations about undirected graphs
1. Connected undirected graph with n vertices
must have at least n – 1 edges.
2. Connected undirected graph with n vertices,
exactly n – 1 edges cannot contain a cycle
3. A connected undirected graph with n vertices,
more than n – 1 edges must contain at least
one cycle
Spanning Trees
FIGURE 20-20 The DFS spanning tree rooted at vertex a for the graph in
Figure 20-11
Spanning Trees
• DFS spanning tree algorithm
Spanning Trees
• BFS spanning
tree algorithm
Spanning Trees
FIGURE 20-21 The BFS spanning tree rooted at vertex a for the graph in
Figure 20-11
Minimum Spanning Trees
FIGURE 20-22 A weighted, connected, undirected graph
Minimum Spanning Trees
• Minimum spanning tree algorithm
Minimum Spanning Trees
FIGURE 20-23 A trace of primsAlgorithm for the graph in
Figure 20-22 , beginning at vertex a
Minimum Spanning Trees
FIGURE 20-23 A trace of primsAlgorithm for the graph in
Figure 20-22 , beginning at vertex a
Minimum Spanning Trees
FIGURE 20-23 A trace of primsAlgorithm for the graph in
Figure 20-22 , beginning at vertex a
Finding MST using Kruskal’s
algorithm
1. Sort all the edges in non-decreasing order of their
weight.
2. Pick the smallest edge. Check if it forms a cycle
with the spanning tree formed so far. If cycle is not
formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the
spanning tree.
A trace of Kruskal’s Algorithm for the graph beginning at
vertex a