Data Structures
11th Week
Chapter 6 Graphs
6.1 The Graph Abstract Data Type
6.2 Elementary Graph Operations
6.3 Minimum Cost Spanning Trees
Chapter 6 Graphs
The Graph Abstract Data Type
A graph, G, consists of two sets : a finite, nonempty set of
vertices, and a finite, possibly empty set of edges.
V(G) and E(G) represent the sets of vertices and edges of G, respectively.
Alternately, we may write G = (V, E) to represent a graph.
Undirected graph is one in which the pair of vertices
representing any edge is unordered.
(v0, v1), (v1, v0) represent the same edge.
Directed graph is one in which we represent each edge as a
directed pair of vertices.
<v0, v1> represents an edge in which v0 is the tail and v1 is the head.
3
the length of a path is the number of edges on it.
simple path : path in which all vertices, except possibly the first
and the last, are distinct.
cycle : simple path in which the first and the last vertices are the
same.
connected
In an undirected graph G, two vertices, v0 and v1, are connected if there is
a path in G from v0 to v1.
connected component : maximal connected subgraph
tree : graph that is connected and a cyclic (it has no cycles).
The degree of a vertex is the number of edges incident to the
vertex.
4
Sample Graphs
0
1
0
0
1
2
3
G1
V(G1) = {0, 1, 2, 3}
V(G2) = {0, 1, 2, 3, 4, 5, 6}
V(G3) = {0, 1, 2}
3
2
4
5
G2
1
6
2
G3
E(G1) = {(0,1), (0,2), (0,3), (1,2), (1,3), (2,3)}
E(G2) = {(0,1), (0,2), (1,3), (1,4), (2,5), (2,6)}
E(G3) = {<0,1>, <1,0>, <1,2> }
5
restriction on graphs :
1. A graph may not have an edge from a vertex, i, back to itself.
self loops.
2. A graph may not have multiple occurrences of the same edge.
if we allow, multigraph.
1
0
2
6
3
Definitions
complete graph
graph that has the maximum number of edges.
adjacent, incident
subgraph
if (v0, v1) is an edge in an undirected graph, then the vertices v0 and v1 are
adjacent and the edge (v0, v1) is incident on vertices v0 and v1
a subgraph of G is a graph G’ such that V(G’ ) V(G) and E(G’) E(G)
path
a path from vertex vp to vertex vq in a graph, G, is a sequence of vertices,
vp, vi1, vi2, … , vin , vq such that (vp, vi1), (vi1, vi2), … , (vin , vq) are edges in
an undirected graph.
7
Graph Representations
Adjacency Matrix
The adjacency matrix of G is a two-dimensional n n array, say adj_mat.
If the edge (vi, vj) ( <vi, vj> for a digraph) is in E(G), adj_mat[i][j] = 1.
If there is no edge in E(G), adj_mat[i][j] = 0.
ex)
0
0
1
2
3
0
1
2
3
0
0
1
1
1
1
1
0
1
1
2
1
1
0
1
3
1
1
1
0
1
2
8
0
1
2
0
0
1
0
1
1
0
0
2
0
1
0
Adjacency Lists
replace the n rows of the adjacency matrix with n linked lists, one for each
vertex in G.
Ex)
headnodes
0
1
2
3
vertex link
0
1
2
3
1
0
0
0
2
2
1
1
0
1
2
1 null
0
2 null
0
1
null
2
9
3
3
3
2
null
null
null
null
Adjacency Multilists
maintaining the lists as multilists, that is, lists in which nodes are shared
among several lists, facilitates this operation.
marked
vertex1
vertex2
path1
path2
ex)
headnodes
0
1
2
0
1
2
3
3
The lists are : vertex 0 : N1 N2 N3
vertex 1 : N1 N4 N5
vertex 2 : N2 N4 N6
vertex 3 : N3 N5 N6
10
N1
0
1
N2
N4
edge(0,1)
N2
0
2
N3
N4
edge(0,2)
N3
0
3
null
N5
edge(0,3)
N4
1
2
N5
N6
edge(1,2)
N5
1
3
null
N6
edge(1,3)
N6
2
3
null
null
edge(2,3)
Depth First Search
Algorithm
1. begin the search by visiting the start vertex, v.
2. select an unvisited vertex, w, from v’s adjacency list and carry out a depth
first search on w. (using recursion)
11
Depth First Search Function
#define FALSE 0
#define TRUE 1
short int visited[MAX_VERTICES];
void dfs(int v)
{
/* depth first search of a graph beginning with
vertex v. */
node_pointer w;
visited[v]=TRUE;
printf(“5d”,v);
for (w = graph[v]; w; w = w->link)
if( !visited[w->vertex] )
dfs(w->vertex);
}
12
ex)
V0
V1
V3
V2
V4
V7
V5
V6
0
1
2
3
4
5
6
7
1
0
0
1
1
2
2
3
2
3
5
7
7
7
7
4
null
4 null
6 null
null
null
null
null
Depth first search order : v0, v1, v3, v7, v4, v5, v2, v6
13
5
6 null
Breadth First Search
Algorithm
1. starts at vertex v and marks it as visited.
2. visits each of the vertices on v’s adjacency list.
3. when we have visited all the vertices on v’s adjacency list, we visit all the
unvisited vertices that are adjacent to the first vertex on v’s adjacency list.
using queue.
14
Connected Components
whether or not an undirected graph is connected?
Calling either dfs(0) of bfs(0) and then determining if there are any
unvisited vertices.
Listing the connected components of a graph.
void connected(void)
{
/* determine the connected components of a graph */
int i;
for ( i = 0; i < n; i++ )
if ( !visited[i] ) {
dfs(i);
printf(“\n”);
}
}
15
Biconnected Components And
Articulation Points
Definition
An articulation point is a vertex v of G such that the deletion of v,
together with all edges incident on v, produces a graph, G’ that has at least
two connected components.
A biconnected graph is a connected graph that has no articulation points.
A biconnected component of a connected undirected graph is a maximal
biconnected subgraph, H, of G. The maximal biconnected subgraph
means that G contains no other subgraph that is both biconnected and
properly contains H.
16
ex)
0
8
1
2
9
7
3
0
8
1
7
1
7
5
2
4
3
5
5
4
6
6
(b)Biconnected components
(a) Conneted graph
3
articulation point : 1, 3 ,5 ,7
17
9
7
Spanning Trees
A spanning tree is any tree that consists solely of edge in G and
that include all vertices in G.
Use either dfs or bfs to create a spanning tree.
depth first spanning tree
breath first spanning tree
V0
V0
V1
V3
V2
V4
V5
V1
V6
V3
V2
V4
V5
V7
V7
depth first spanning tree
breadth first spanning tree
18
V6
Minimum Cost Spanning Trees
Definition
cost of a spanning tree of a weighted undirected graph : the sum of the
costs(weights) of the edges in the spanning tree
Minimum cost spanning tree : a spanning tree of least cost
Three algorithms for minimum cost spanning tree
Kruskal’s, Prim’s, Sollin’s algorithms
All three use an greedy method
Greedy method
At each stage, make a decision that is the best decision (using the criterion)
Since we cannot change this decision later, we make sure that the decision
will result in a feasible solution
Typical criterion : least cost, highest profit criterion
19
Minimum Cost Spanning Trees (Cont’d)
Greedy method
At each stage, make a decision that is the best decision (using the
criterion).
Since we cannot change this decision later, we make sure that the decision
will result in a feasible solution.
Typical criterion : least cost, highest profit criterion
Constraints for minimum cost spanning tree algorithms
1. Must use only edges within the graph.
2. Must use exactly n-1 edges.
3. May not use edges that would produce a cycle.
20
Kruskal’s Algorithm
Methods
build a minimum cost tree T by adding edges to T one at a time
selects the edges for inclusion in T in nondecreasing order of their cost
an edge is added in T if it does not form a cycle
graph G is connected and has n>0 vertices, exactly n-1 edges will be
selected
21
(Ex) Kruskal’s Algorithm
0
0
28
10
10
1
10
5
6
25
1
1
16
14
5
0
2
6
2
5
6
24
18
4
4
12
22
4
3
12
3
(a)
3
(b)
(c)
0
0
10
14
6
0
10
1
5
10
1
14
5
6
2
1
16
14
5
2
6
16
2
25
4
4
12
12
22
3
3
(d)
2
(e)
22
4
12
22
3
(f)
© Copyright 2026 Paperzz