Professional Documents
Culture Documents
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.
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.
Section 8 Page 1 of 7
Jennifer Laraya-Llovido
Data Structures & Algorithms
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
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.
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.
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.
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).
Section 8 Page 4 of 7
Jennifer Laraya-Llovido
Data Structures & Algorithms
For a weighted undirected graph, the cost adjacency matrix is defined similarly to that
of a weighted graph.
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.
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