You are on page 1of 7

Data Structures & Algorithms

SECTION 8:
OBJECTIVES: At the end of the session, the student is expected to be able to
1. define and give examples of an undirected graph
2. define and give examples of a directed graph
3. explain important properties of, and concepts related to undirected and directed graphs
4. represent any given graph using adjacency matrices and adjacency lists
5. explain DFS and BFS traversal for graphs and apply the methods to any given graph

DISCUSSION:
We will now consider an extremely useful mathematical structure called a graph. Graphs are used to
model a variety of problems across many disciplines: in engineering, physics, chemistry, operations
research, economics, computer science, and the like. A graph may be used to represent an electrical
circuit, a program flowchart, a communications network, airline routes, a molecular structure, a street
map, etc.
There are many problems related to graphs. Many elegant algorithms have been devised to solve
some of these problems, and we will examine a number of these. However, some graph problems have
proved to be rather ‘difficult’ (‘intractable’ is the proper term.) You will encounter some of these in a later
course.

Some pertinent definitions and related concepts

1. A graph, G = (V, E), consists of a finite, nonempty set of vertices (or nodes), V, and a set of
edges, E. Shown below are six different graphs.

(a) (b) (c) (d) (e) (f)

2. An undirected graph is a graph in which the pair of vertices representing an edge is


unordered, i.e., (i, j) and (j, i) represent the same edge.
1 2 V = { 1, 2, 3, 4}
E = {(1,2), (1,3), (1,4), (2, 3), (2, 4), (3, 4)}
3 4

Section 8 Page 1 of 7
Jennifer Laraya-Llovido
Data Structures & Algorithms

Figure 1. An undirected graph


If (i, j) is an edge in E, then the vertices i and j are said to be adjacent and the edge (I, j) is said
to be incident on the vertices e and j. We require that i <> j (i.e., no edge connects a vertex to
itself).

3. A directed graph or digraph is a graph in which each edge is represented by a ordered pair <i,
j>, where i is the head and j is the tail of the edge. The edges <i, j> and <j, i> are two distinct
edges.

1
2

Figure 2. A directed graph

If <i, j> is an edge in E, then vertex i is said to be adjacent to vertex j and vertex j is said to be
adjacent from vertex i. The edge <i, j> is incident to vertices i and j. We require that i <> j (i.e., there
are no self-loops in E).

A directed graph is complete if every pair of vertices i and j are connected by two edges <i, j> and
<j, i>. It follows that there are n(n-1) directed edges in a complete digraph.

4. A subgraph of an undirected [directed] graph G = (V, E) is an undirected [directed] graph G’ =


(V’, E’) such that V’ V and E’ E.

5. A path from vertex u to vertex w in an undirected [directed] graph G = (V, E) is a sequence of


vertices v0, v1,v2,…,vm-1, vm where v0 ≡ w, such that (v0, v1), (v1, v2) ,…,(vm-1, vm) [(v0, v1), (v1, v2)
,…,(vm-1, vm)] are edges in E. The length of a path is the number of edges in it. A single vertex u
is considered to be a path of length zero from u to itself. A path in which all vertices, except
possibly the first and the last, are distinct, is called a simple path. A simple cycle is a simple
path having the same start and end vertex.

6. Two vertices i and j are said to be connected if there is a path from vertex i to vertex j. In an
undirected graph, this means that there is also a path from vertex j to vertex i. An undirected
graph is said to be connected if there is a path between every pair of distinct vertices I and j. A

Section 8 Page 2 of 7
Jennifer Laraya-Llovido
Data Structures & Algorithms

directed graph is said to be strongly connected if for every pair of distinct vertices i and j there is
a directed path from vertex i to vertex j and from vertex j to vertex i. A connected component in
an undirected graph G is a maximal connected subgraph in G. A strongly connected
component in a directed graph G is a maximal strongly connected component in G.

7. A weighted graph is a graph with weights or costs assigned to its edges. For instance, if the
vertices of a graph represent cities, the cost of an edge connecting two vertices may represent
the cost of the fare by bus, the length of the travel time by plane, etc. (We will assume in what
follows that weights or costs are non-negative.)

8. A spanning tree is a tree which connects all the vertices of a graph. If the graph is weighted, the
cost of a spanning tree is the sum of the weights or costs of the branches (edges) in the
spanning tree. A minimum cost spanning tree is a spanning tree with minimum cost. For a
given weighted graph, it is not necessarily unique.

Representing graphs in computer memory


There are various ways of representing a graph in the memory of a computer. As with other
structures, a principal consideration in determining which representation to use is the kind of
operations to be performed on the graph. Another important consideration is the number of edges
relative to the number of vertices in the graph.

1. Directed graphs: adjacency matrix


A simple and straightforward way of representing a directed graph on n vertices is by using an n
by n matrix, say A, whose elements are defined such that
A(i, j) = 1 if the edge <i, j> exists, 1 ≤ i, j ≤ n
= 0 otherwise
a. The adjacency matrix A can be declared as a Boolean matrix.
b. The diagonal elements are always zero since no self loops are allowed.
c. The number of nonzero elements in A ≤ n(n-1). The upper limit is attained when the
digraph is complete.
d. The number of nonzero elements in row i is the outdegree of vertex i. (This is the
number of arrows emanating from vertex i.)
e. The number of nonzero elements in column j is the indegree of vertex j. (This is the
number of arrows pointing to vertex j.)

Section 8 Page 3 of 7
Jennifer Laraya-Llovido
Data Structures & Algorithms

f. Given any two vertices i and j in the digraph, it takes O(1) time to determine whether
there is an edge <i,j>.
g. The use of an adjacency matrix implies O(n2) space even if the digraph has much
fewer than n2 edges.
h. For a weighted directed graph, the (i, j)th element of the adjacency matrix is the cost
of edge <i, j>, the (i, j)th element is set to a very large value. The resulting matrix is
called a cost adjacency matrix.

2. Directed graphs: adjacency lists


In an adjacency matrix for a digraph G on n vertices, space is allocated for a(n-1) possible edges
(plus space for the disallowed self-loops). Obviously, this means O(n2) space. Further, even
simple operations such as inputting the matrix elements for G or determining the number of
edges in G also take O(n2) time. When the number of edges in G are much smaller than n2, a
representation in which only those edges that are actually present in G are represented can lead
to savings in both allocated space and processing time.

In the adjacency list representation for a digraph G on n vertices, a sequential table (vetor)
of size n, say LIST, is maintained such that for any vertex I in G, LIST(i) points to the list of
vertices adjacent from i. Each such adjacency list is maintained as a linked list, where each node
in the list has structure, say, (NODE, NEXT). If no vertices are adjacent from vertex i, LST(i) is
set to null.

In direct contrast to the adjacency matrix representation, where it takes constant time to
determine whether there is an edge <i, j>, it may now take O(n) time to perform the same
operation since there could be O(n) vertices in the list for vertex i.

For a weighted digraph, we could represent each node in the list using the node structure
(NODE, COST, NEXT).

3. Undirected graphs: adjacency matrix


The adjacency matrix A for an undirected graph is symmetric, i.e., aij = aji, since if (i, j) is
an edge, so is (j, i). This means that only the lower diagonal elements are required to define the
graph; the upper diagonal elements may be left as “don’t cares.”

Section 8 Page 4 of 7
Jennifer Laraya-Llovido
Data Structures & Algorithms

For an undirected graph G on n vertices, the number of nonzero elements in A ≤ n(n-1)/2.


The upper limit is attained when G is complete.

For a weighted undirected graph, the cost adjacency matrix is defined similarly to that
of a weighted graph.

4. Undirected graphs: adjacency lists


Each edge (i, j) is represented twice: once in the adjacency list for vertex i and once in the
adjacency list for vertex j.
For some applications, it might be desirable to add a new field, say LINK, to link the two
instances of an undirected edge. If α is the address of the node for vertex j in the adjacency list
for vertex i, and if β is the address of the node for vertex i in the adjacency list for vertex j, then
we set LINK(α) Å β and LINK(β)Åα.
For a weighted, undirected graph, we could use the node structure (NODE, COST, NEXT), or
(LINK, NODE, COST, NEXT) as the case may be.

GRAPH TRAVERSALS
As with binary trees, many algorithms on graphs require that we ‘visit’ the vertices of a graph in
a systematic manner. However, a traversal method for a graph differs in many important respects
from that for a binary tree:

1. A graph does not have the equivalent of a root node from which a traversal method can
naturally commence. This means that traversal of a graph can start at any vertex. However, not
all the vertices in the graph is reachable from this start vertex, leaving certain vertices unvisited.
Hence, traversal must be restarted from another, as yet unvisited, vertex. This procedure is
repeated until all vertices in the graph are visited.
2. There is no natural order among the vertices adjacent to (or from) the most recently visited
vertex to indicate which one is to be visited next. Which node is actually next depends largely
on the way the graph is represented.
3. In a graph, a vertex maybe adjacent to (or from) several vertices from which it could be
encountered again. It is therefore necessary to ensure that a vertex, which has already been
visited, is not visited anew. One simple way to do this is to associate with each vertex a mark
bit which is initially set to 0 and subsequently set to 1 when the vertex is visited.

Section 8 Page 5 of 7
Jennifer Laraya-Llovido
Data Structures & Algorithms

Section 8 Page 6 of 7
Jennifer Laraya-Llovido
Data Structures & Algorithms

There are two general methods to traverse a graph, namely, Depth First Search (DFS) and
Breadth First Search (BFS).
1. Depth First Search
Given a graph, with all vertices initially marked unvisited, depth first search proceeds as follows:

a. [Select start vertex] Select an unvisited vertex. If no such vertex can be found, DFS
terminates.
b. Mark start vertex visited.
c. [Process adjacent vertex] Select an unvisited vertex, say vertex u, adjacent to (or from)
start vertex, mark it visited, and initiate a DFS with u as start vertex. If no such vertex
can be found, go to Step (a).
d. [More adjacent unvisited vertices?] Go to Step (c).

This technique is called depth first search because it continues searching (or visiting) in the
forward, or deeper direction a far as possible from the most recently visited vertex, before
visiting the next adjacent vertex.

2. Breadth First Search


Given a graph, with all vertices initially marked unvisited, breadth first search proceeds as
follows:

a. Select some unvisited vertex, say vertex i, and mark it visited. Then, search as broadly
as possible from i by visiting all vertices adjacent to i. Continue the search by next
visiting the vertices adjacent to these latter vertices, searching as broadly a s possible
from each such vertex. Continue searching in this fashion until all vertices reachable
from vertex i are visited.
b. Repeat (a) until all vertices in the graph are visited.

Section 8 Page 7 of 7
Jennifer Laraya-Llovido

You might also like