Textual Learning Material - b4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Chapter 3 Graphs

3.1 Introduction
3.2 Graphs
3.3 Euler path and circuits
3.4 Hamiltonian path and circuit
3.5 Represenatation of graph

3.1 Introduction

Graph theory has many powerful applications to a variety of subjects which include
Physics, Chemistry, Economics, Genetics, Engineering, Computer Science nd
Information Technology.. It starts with very simple geometric ideas. In the last three
decades graph theory has established itself as a worthwhile mathematical discipline.

3.1 Graphs

A graph consists of a nonempty set of points or vertices, and a set of edges that link together
the vertices. A simple real world example of a graph would be your house and the corner store.
Where the house and the store are the vertices and the road between them is the edge
connecting the two vertices.

Mathematically, a graph G consists of two parts

(i) A set V= V (G) whose elements are called vertices, points or nodes.
(ii) A collection E=E (G) of unordered pairs of distinct vertices called edges.
To emphasize the two parts of G we write G (V, E).

A multigraph G=G (V, E) also consist of a set of vertices and a set E of edges except that E may
contain multiple edges.

48
Graph (Multigraphs) G=G (V, E) are pictured by diagram in the plane as follows. Each vertex v in
V is represented by a dot (or a small circle) and each edge e={u, v} is represented by a line/curve
which connects its endpoints u and v. In fact, a graph is usually denoted when possible by
drawing its diagram rather than explicitly listing in vertices and edges. The names of the edges
may be omitted if they have no intrinsic meaning. Sometimes the labels on the vertices are
omitted as well if the graphical information is adequate for the discussion. The edges can be
weighted or unweighted. For example, weights can be thought of as the number of blocks
between your house and the corner store. The weights are written on the edge if its weighted
edge.

Figure 1 shows graph G=(V, E) where V consists of vertices A, B, C, D and E and E consists of 6
edges e1={A, B}, e2= {B, C}, e3 ={C, D}, e4={D, E}, e5={E, A} and e6={E, B} and Figure 2 shows
graph G=(V, E) where V consists of vertices A and B and E consists of 2 edges e1={A, B} and e2=
{B, A}. Here Figure 1 is a simple graph and figure 2 is multigraph. Why? Because G contains
multiple edges e1 and e2 which connect the same vertices A and B.

A B
e1
C
e2

e5
e6
E e3
e4
D

Figure 1

e1
A B

e2

49
Figure 2

Graphs are often used to record information about relationships or connections. In a pictorial
representation of graph, the connections are the most important information and generally a
number of pictures may represent the same graph.

An edge in a graph that joins two vertices is said to be incident to both vertices. Furthermore,
the degree of a vertex (generally written as deg (v)) is determined by the number of distinct
edges that are incident to it. A vertex with degree zero is called isolated vertex. A pair of
vertices that determines an edge is adjacent vertices. In figure 1 the degree of vertex A is 2
while the degree of vertex B is 3 and the vertices A and B are the adjacent vertices. In Figure 3 F
is an isolated vertex. The vertex v is said to be even or odd according as deg (v) is even or odd.

A B
e1
e2 C

e5
e6
E e3 F
e4
D

50
Figure 3

Two edges in a graph are termed adjacent if they connect to the same vertex. A loop is an edge
that links a vertex to itself. In figure 3 the vertex D has a loop. A loop contributes 2 to the
degree of vertex. A simple graph is one that contains no loops or parallel edges, where more
than one edge connects two given vertices, whereas a multigraph is a graph that contains
multiple edges. A null graph is one that contains only isolated vertices (ie. no edges). Finally, a
complete graph is a simple graph in which every pair of vertices is adjacent.

A graph can take on many forms: directed or undirected. A directed graph is one in which the
direction of any given edge is defined. Conversely, in an undirected graph you can move in both
directions between vertices. In a directed graph, the indegree and outdegree of a vertex
represent the number of edges that terminates in and originates from a vertex, respectively.
Figure 4 shows directed graph. Table 1 shows the indegree and outdegree of the vertices of
graph of figure 4.

Vertex Indegree Outdegree

A 0 2

51
B 2 1

C 0 2

D 3 0

Table 1

A B

D
C

Figure 4

3.2.1 Paths

A path through a graph is a traversal of consecutive vertices along a sequence of edges. By this
definition, the vertex at the end of one edge in the sequence must also be the vertex at the
beginning of the next edge in the sequence. The vertices that begin and end the path are
termed the initial vertex and terminal vertex, respectively. With the exception of these initial
and terminal vertices, each vertex within the path has two neighboring vertices that must also

52
be adjacent to the vertex. The length of the path is the number of edges that are traversed
along the path.

Repeated edges or vertices within the path are permissible. When there are no repeated
edges in the path of a directed graph, then the path is called a simple path. On the other
hand, an elementary path in a directed graph is one in which there are no repeated
vertices within the path. The path in our road map example is considered a simple path
and also an elementary path.

3.2.2 Circuits

A circuit is a path that begins and ends with the same vertex. This is also called a cycle.
So by removing all the cycles, the path can be considered an elementary path since there
will no longer be any repeated vertices. This means that if a path exists between any two
vertices, an elementary path must also exist between these two vertices. When a simple
directed graph does not contain any cycles is termed acyclic. Figure 5 shows directed
graph cycle.

Figure 5

An edge in an undirected graph is formed by connecting a pair of vertices but no direction is


stipulated for the edge. Therefore, for a path in an undirected graph, either vertex may be
considered as the initial or terminal vertex of the path and the traversal of the vertices along the
path can occur in either direction. However, a cycle in a simple undirected graph is slightly
different from the definition of a cycle for a directed graph due to the lack of direction on the

53
edges. For undirected graphs, the traversal of a set of vertices forward and then backward can
not be considered a cycle. Rather, a simple cycle for an undirected graph must contain at least
three different edges and no repeated vertices, with the exception of the initial and terminal
vertex.

Figure 6

If a vertex is reachable from another vertex then a path exists from the one vertex to the
other vertex. It is assumed that every vertex is reachable from itself. Also, if vertex b is
reachable from vertex a and vertex c is reachable from vertex b, then it follows that
vertex c is reachable from vertex a. The definition of reachability holds true for both
directed and undirected graphs. In the road map example, a driver can reach Kindersley
from Saskatoon, and the same driver can reach Calgary from Kindersley. Therefore,
Calgary is considered by the driver to be reachable from Saskatoon.

a
c

54
Figure 7

3.2.3 Connected Graphs

An undirected graph is considered to be connected if a path exists between all pairs of


vertices thus making each of the vertices in a pair reachable from the other. An
unconnected graph may be subdivided into what are termed connected subgraphs or
connected components of the graph.

The connectedness of a simple directed graph becomes more complex because


direction must be considered. For instance, if vertex a is reachable from vertex b, vertex
a may not be reachable from vertex b. For the road map example when the map is
considered to be a directed graph, it can not be considered a connected graph, because
while Calgary is reachable from Saskatoon, Saskatoon is not reachable from Calgary.

Because of the added complexity, there are three distinct forms of connectedness in
simple directed graphs: weakly connected, unilaterally connected and strongly
connected. A weakly connected graph is where the direction of the graph is ignored and
the connectedness is defined as if the graph was undirected. A unilaterally connected
graph is defined as a graph for which at least one vertex of any pair of vertices is
reachable from the other. A strongly connected graph is one in which for all pairs of
vertices, both vertices are reachable from the other. Figure 8 shows weakly connected
directed graph. And figure 9 shows unilaterally conneceted directed graph.

d
b

Figure 8

55
d
b

Figure 9

3.2 Euler Paths and Circuits

As often happens in mathematics, a simple problem began the creation of a new field of
mathematics: In the 1700's the townspeople of old Konigsberg, East Prussia, wondered if
was possible to take a walk around their town in such a way as to cross each of the seven

bridges exactly once. See figure 10. The bridges are marked a through g.

Figure 10

The great mathematician Euler (pronounced Oiler) began by pointing out that the shapes
and sizes of the land masses and bridges did not matter--so he reduced (in words) figure
10 to the graph shown in figure 11. This simple idea began the study of graph theory!

56
Figure 11

In modern language we now restate the Konigsberg question as: "is there a path which
traverses every edge in this graph exactly once?" Such a path is called an Euler path.
Euler answered the Königsberg question in his 1736 article.

A path in a graph G is called a Euler path if it includes every edge exactly once. An
Euler circuit is an Euler path that is a circuit.

Example: a) An Euler path in figure 12 is : E, D, B, A, C, D.

b) One Euler circuit in the graph of figure 13 is : 5, 3, 2, 1, 3, 4, 5.

E
2 4

D
3
B C

1 5

Figure 12 Figure 13

57
A little experimentation will show that no Euler circuit is possible for the graph in figure
12. In Figure 12 the edge {D,E} must be either the first or the last traveled, because there
is no other way to travel to or from the vertex E. This means that if G has a vertex of
degree 1, there cannot be an Euler circuit, and if there is an Euler path, it must begin or
end at this vertex. A similar argument applies to any vertex v of odd degree, say 2n+1.
We may travel in one of these edges and out on another one n times, leaving one edge
from v untraveled. This last edge may be used for leaving v or arriving at v, but not both,
so a circuit cannot be completed.

Theorem 1:

a) If a graph G has a vertex of odd degree, there can be no Euler circuit in


G.
b) If G is a connected graph and every vertex has even degree, then there is
an Euler circuit in G.

Proof: b) Suppose that there are connected graphs where every vertex has even degree,
but there is no Euler circuit. Choose such a G with the smallest number of edges. G must
have more than one vertex since, if there were only one vertex of even degree, there is
clearly an Euler circuit. Let v be a vertex of G. We first note that there must be a circuit
that begins and ends at v. since the degree of v is at least two and G is connected, there
must be distinct edges between v and vertices a and b (which may be the same vertex).
Since G is connected, there must be a path a, v1, v2, …, b. Then v, a, v1, v2, …, b, v is a
circuit that begins and ends at v. suppose that : v, u,…, v is the longest possible circuit
beginning and ending at v. Since G does not have an Euler circuit, not all edges of G are
used in . Let G’ be the graph formed by deleting the edges of from G. Since is a
circuit, deleting edges reduces the degree of each vertex by 0 or 2, so G’ is also a graph
whose vertices all have even degree. The graph of G may not be connected, but some
piece of it is. We choose the largest connected piece of G’. This piece must have an Euler
circuit ’ because it certainly has fewer edges than G and G was chosen to have the
smallest number of edges without an Euler circuit.

Either ’ contains all the vertices of G or there is some vertex v not in ’. But v must be
adjacent to some v’ in ’ since G is connected and edge {v, v’} is one of the deleted
edges: that is, {v, v’} is an edge of . In either case, some vertex v’ in ’ is also in ,
and we can construct a longer circuit in G by joining and ’ at v’. But this is a
contradiction, because was the longest possible circuit in G. Thus no such G can exist.

We have proved that if G has vertices of odd degree, it is not possible to construct an
Euler circuit for G, but an Euler path may be possible.

Theorem 2:

a) If a graph G has more than two vertices of odd degree, then there can be
no Euler path in G.

58
b) If G is connected and has exactly two vertices of odd degree, there is an
Euler path in G. any Euler path in G must begin at one vertex of odd degree and
end at the other.

Proof: a) let v1, v2, v3 be vertices of odd degree. Any possible Euler path must leave (or
arrive at) each of v1, v2, v3 with no way to return (or leave) since each of these vertices
has odd degree. One vertex of these three vertices may be the beginning of the Euler path
and another the end, but this leaves the third vertex at one end of an untraveled edge.
Thus there is no Euler path.

b) Let u and v be the two vertices of odd degree. Adding the edge {u, v} to g
produces a connected graph G’ all of whose vertices have even degree. By theorem 1
b), there is an Euler circuit ’ in G’. Omitting {u, v} from ’ produces an Euler path
that begins at u (or v) and ends at v (or u).

Fleury’s algorithm produces an Euler circuit for a connected graph with no vertices of
odd degree. An edge {vi, vj} is a bridge in a connected graph G if deleting {vi, vj}
would create a disconnected graph.

Fleury’s Algorithm:

Let G=(V, E) be a connected graph with each vertex of even degree.

1. Select a member v of V as the beginning vertex of the circuit. Let : v


designates the beginning of the path to be constructed.
2. Suppose that : v, u… w, z. Delete {w, z} from E and w from V. If at w
there are several edges, choose one that is not a bridge to the remaining graph,
say {w,z}. Extend to : v, u…w, z and delete {w, z} from E.
3. Repeat step 2 until no edges remain in E.

Example : Using Fleury’s algorithm, one possible Euler circuit for the graph of figure 14
is shown in figure 15.

59
Figure 14

2 3
7
1

6 8
1 4

9 5

Figure 15

3.4 Hamiltonian Paths and Circuits


Given a graph G, a Hamiltonian path for G is a path that contains each vertex exactly
once. Hamiltonian circuit for G is a simple circuit that includes every vertex of G. That
is, a Hamiltonian circuit for G is a sequence of adjacent vertices and distinct edges in
which every vertex of G appears exactly once except for the first vertex, which is also the
last.

Note that for an Euler circuit a graph G must include every vertex of G, it may visit some
vertices more than once and hence may not be a Hamiltonian circuit. On the other hand, a
Hamiltonian circuit for G does not need to include all the edges of G and hence may not
be an Euler circuit.

Example: a) Consider the graph of figure 12. The path e, d, c, a, b is a Hamiltonian path
because it contains each vertex exactly once however there is no Hamiltonian circuit for
this graph.

b) For the graph of Figure 16, the path A, B, C, D, A is a Hamiltonian Circuit.

B C

60

A
Figure 16

Despite the similar sounding definitions of Euler and Hamiltonian circuits, the
mathematics of the two are very different. Any complete graph has Hamiltonian circuits.
It is clear that loops and multiple edges are of no use in finding Hamiltonian circuits,
since loops could not be used and only one edge can be used between any two vertices.

If graph G on n vertices has a Hamiltonian circuit then G must have at least n edges.

Theorem 3: G has a Hamiltonian Circuit if for any two vertices u and v of G that are not adjacent,
the degree of u plus the degree of v is greater than or equal to n.

Corollary: G has a Hamiltonian circuit if each vertex has degree greater than or equal to
n/2.

Proof: The sum of the degree of any two vertices is at least (n/2) + (n/2) = n, so the
hypothesis of theorem 3 hold.

Theorem 4: Let the number of edges of G be m. then G has Hamiltonian circuit if m>=
½(n2-3n+6) where n is the number of vertices.

Proof: Suppose that u and v are any two vertices of G that are not adjacent. We write
deg(u) for the degree of u. Let h be the graph produced by eliminating u and v from G
along with any edges that have u or v as end points. Then H has n-2 vertices and m-
deg(u)-deg(v) edges (one more edge would have been removed if u and v had been
adjacent). The maximum number of edges that H could possibly have is n-2C2. This
happens when there is an edge connecting every distinct pair of vertices. Thus the
number of edges of H is at most
n-2
C2 = (n-2)(n-3)/2 or ½(n2-5n+6)

We then have, m - deg(u) - deg(v) <= ½(n2-5n+6)

Therefore, deg(u) + deg(v) >= m - ½(n2-5n+6)

By the hypothesis of the theorem,

deg(u) + deg(v) >= ½(n2-3n+6) - ½(n2-5n+6) = n

61
Thus the result follows from theorem 1.

The converses of the theorem given above are not true. The conditions given are
sufficient but not necessary for the conclusion. The problem we are considering has a
number of important variations. The edges may have weights. For a weighted a graph the
problem is then to find a Hamiltonian circuit (or path) for which the total sum of weights
in the path is a minimum. A weight can represent distance, cost and the like. For
example, the vertices might represent cities, the edges might represent lines of
transportation and the weight of an edge might represent cost of travel.

3.5 Representation of Graphs

There are several different ways to represent a graph in a computer. Although graphs are
usually shown diagrammatically, this is only possible when the number of vertices and
edges is reasonably small. The graph can be represented using adjacency matrices or
adjacency lists.

3.4.1 Adjacency Matrices

There are several different ways to represent a graph in a computer. Although graphs
are usually shown diagrammatically, this is only possible when the number of vertices
and edges is reasonably small.

Graphs can also be represented in the form of matrices. The major advantage of matrix
representation is that the calculation of paths and cycles can easily be performed using
well known operations of matrices. However, the disadvantage is that this form of
representation takes away from the visual aspect of graphs. It would be difficult to
illustrate in a matrix, properties that are easily illustrated graphically.

Example: Matrix representation of a graph

62
Consider the directed graph G of figure 17 (in which the vertices are ordered as v1, v2, v3,
v4, and v5), and its equivalent adjacency matrix representation :

Figure 17

v1 v2 v3 v4 v5
0 1 0 1 1
v1
0 0 0 1 0
v2
0 0 0 0 1
v3
0 0 0 0 0
v4
As you can see from the abovev5graph,0 if 1a path
0 0 of0length 1 exists from one vertex to
another (ie. the two vertices are adjacent), there must be an entry of 1 in the
corresponding position in the matrix. For example, from the vertex v1, we can reach
vertices v2, v4, and v5. Therefore, we have a corresponding entry of 1 in the matrix in the
first row and the second, fourth and fifth columns.

In general, the number of 1's in the ith row, corresponds to the number of edges leaving
the vertex vi, and the number of 1's in the jth column, corresponds to the number of edges
entering the vertex vj.

Definition of an Adjacency Matrix

An adjacency matrix is defined as follows: Let G be a graph with "n" vertices that are
assumed to be ordered from v1 to vn. The n x n matrix A, in which aij= 1 if there exists a

63
path from vi to vj, aij = 0 otherwise is called an adjacency matrix.

Calculating A Path Between Vertices

As shown in the previous example, the existence of an edge between two vertices vi and
vj is shown by an entry of 1 in the ith row and jth column of the adjacency matrix. This
entry represents a path of length 1 from vi to vj.

To compute a path of length 2, the matrix of length 1 must be multiplied by itself, and
the product matrix is the matrix representation of path of length 2.

Example: Computing A Matrix Representation of Path of Length 2

Using the matrix from the previous example and multiplying it by itself, we obtain the
following new matrix:

v1 v2 v3 v4 v5
0 1 0 1 0
v1
0 0 0 0 0
v2
0 1 0 0 0
v3
The above matrix indicates that we can go0from0 0vertex
0 0 v1 to vertex v2, or from vertex v1
v4
to vertex v4 in two moves. In fact, if we examine the graph, we can see that this can be
done by going through vertex v5 and 0 0 0 1 0
v5 through vertex v2 respectively. We can also reach
vertex v2 from v3, and vertex v4 from v5, all in two moves.

In general, to generate the matrix of path of length n, take the matrix of path of length n-
1, and multiply it with the matrix of path of length 1.

3.4.2 Adjacency Lists


The adjacency list is another form of graph representation in a computer. This structure
consists of a list of all vertices in a given graph. Furthermore, each vertex in the list is
linked to its own (linked) list containing the names of all vertices that are adjacent to
itself.

For example, let's consider a graph of figure 18 consisting of vertices A, B and C, where
the three vertices are in a triangular formation. Vertex A is adjacent to vertices B and C,

64
vertex B is adjacent to vertices A and C, and finally, vertex C is adjacent to vertices A
and B.

Figure18

The resulting list would appear as follows:

The above adjacency list is easy to follow, and clearly illustrates the adjacent nature of
the three vertices. It is most often used when the graph contains a small to moderate
number of edges.

Summary

A graph consists of a nonempty set of points or vertices, and a set of edges that link
together the vertices.
A path through a graph is a traversal of consecutive vertices along a sequence of
edges.
A circuit or a cycle is a path that begins and ends with the same vertex.
There are two types of paths and circuits in agraph, Euler path and circuit and
Hamiltonian paths and circuits.
There are two ways to represent a graph, adjacency matrix and and list.

65

You might also like