Vous êtes sur la page 1sur 22

Theory of Computer Science

Unit 3

Unit 3
Structure 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 Introduction Objectives Graphs Trees Binary Trees Walks and Paths Directed Graphs Summary Terminal Questions Answers

Trees and Binary Trees

3.1 Introduction
The last three decades have witnessed more interest in Graph Theory, particularly among applied mathematicians and engineers. Graph Theory has a surprising number of applications in many developing areas. The Graph Theory is also intimately related to many branches of mathematics including Group Theory, Matrix Theory, Automata and Combinatorics. Graph Theory serves as a mathematical model for any system involving a binary relation. Objectives: At the end of the unit you would be able to

learn the concept, graph and illustration. know the Trees and Binary Trees. know the weighed graphs and applications. find the spanning tree of weighted graphs define a directed graph
Page No.:

Sikkim Manipal University

Theory of Computer Science

Unit 3

3.2 Graphs
3.2.1 Definitions

A graph G = (V, E) consists of a nonempty set of objects, V = {v1, v2, } called vertices and another set, E = {e1, e2, } of elements called edges such that each edge ek is identified with an unordered pair {vi, vj} of vertices.

The vertices vi, vj associated with edge ek are called the end vertices of ek. An edge associated with a vertex pair {vi, vi} is called a loop (or) selfloop.

If there is more than one edge associated with a given pair of vertices, then these edges are called parallel edges (or) multiple edges.

3.2.2 Example Consider the graph given here. v1 e3 e5 e4 e6 v2 e2 e7 v3 v4

e1

This is a graph with five vertices and seven edges. Here G = (V, E) where V = {v1, v2, v3, v4, v5} and E = {e1, e2, e3, e4, e5, e6, e7}. The identification of edges with the unordered pairs of vertices is given by e1 {v2, v2}, e2 {v2, v4}, e3 {v1, v2}, e4 {v1, v3}, e5 {v1, v3}, e6 {v3, v4}. Here e1 is a loop and e4, e5 are parallel edges. 3.2.3 Definition A graph that does not have self-loop nor even parallel edges is called a
Sikkim Manipal University Page No.:

Theory of Computer Science

Unit 3

simple graph. Graph containing either parallel edges or loops is referred as general graph. 3.2.4 Example It can be observed that the two graphs given in Figures A and B are one and the same. 1 2 4 3 3.2.5 Definition i) If a vertex v is an end vertex of some edge e, then v and e are said to be incident with (or on, or to) each other. ii) Two non-parallel edges are said to be adjacent if they are incident on a common vertex. Two vertices are said to be adjacent if they are the end vertices of the same edge. iii) The number of edges incident on a vertex v is called the degree of v. The degree of a vertex v is denoted by d(v). It is to be noted that a selfloop contributes two to the degree of the vertex. 3.2.6 Theorem The sum of the degrees of the vertices of a graph G is twice the number of edges. That is, )
v i V

1 4

FigA

3
Fig-B

d( v i

= 2e. (Here e is the number of edges).

Proof: (The proof is by induction on the number of edges e). Case-(i): Suppose e = 1. Suppose f is the edge in G with f = uv.
Sikkim Manipal University Page No.:

Theory of Computer Science

Unit 3

Then d(v) = 1, d(u) = 1. Therefore,

Sikkim Manipal University

Page No.:

Theory of Computer Science

Unit 3

d( x
)
xV

= = )
xV \ { u ,v }

d( x

+ d(u) + d(v)

=0 +1 +1=2=2 = 2 (number of edges). Hence the given statement is true for n = 1. Now we can assume that the result is true for e = k - 1. Take a graph G with k edges. Now consider an edge f in G whose end points are u and v. Remove f from G. Then we get a new graph G = G - {f}. Suppose d (v) denotes the degree of vertices v in G . Now for any x {u, v}, we have d(x) = d (x), and d (v) = d(v) - 1, d (u) = d(u) - 1. Now G has k - 1 edges. So by induction hypothesis
* * * * * * *

d
)
v i V

(v

2(k - 1).

Now 2(k - 1) = d = =

v i V

(vi ) =

v i { u ,v }

d* ( v i
)

+ d* (u) + d* (v)

v i { u ,v }

d( v i ) + (d(u) - 1) + (d(v) - 1) d( v i ) + d(u) + d(v) - 2 d


*

v i { u ,v }

vi V

(vi ) -2

2(k - 1) + 2 =

Sikkim Manipal University

Page No.:

Theory of Computer Science


v i V

Unit 3

( v i ) 2k =

v i V

d( v i ) .

Hence, by induction we get that the sum of the degrees of the vertices of the graph G is twice the numbers of edges.

Sikkim Manipal University

Page No.:

Theory of Computer Science

Unit 3

3.2.7 Problem The number of vertices of odd degrees is always even. Solution: We know that the sum of degrees of all the n vertices (say, vi, 1 i n) of a graph G is twice the number of edges (e) of G. So we have

d( v i ) = 2e --------- (i)
i= 1

If we consider the vertices of odd degree and even degree separately, then

d( v i ) = d( v j
)
i= 1 vj iseven

d( v k ) -------- (ii)
v k isodd

Since the L.H.S of (ii) is even (from (i)) and the first expression on the RHS side is even, we have that the second expression on RHS is always even. Therefore,
vkis odd

d(v k ) --------- (iii)

is an even number. In (iii), each d(vk) is odd. The number of terms in the sum must be even to make the sum an even number. Hence the number of vertices of odd degree is even. 3.2.8 Definition A graph H is said to be a sub-graph of a graph G if all the vertices and all the edges of H are in G, and each edge of H has the same end vertices in H as in G. 3.2.9 Example The graphs H and K are subgraphs of graph G.

Sikkim Manipal University

Page No.:

Theory of Computer Science 1 i a b c


2

Unit 3 5

f h 4 1 b e 2 d

4 6

1 i

b
2

6
j

d
3

d e

3
Graph G

Graph K Graph H

Self Assessment Questions 1. Every graph is a itself; of _of G; _of G of

2. A subgraph of a subgraph of G is a 3. A single vertex in a graph G is a

4. A single edge in G together with its end vertices is a G.

3.3 Trees
The concept of a tree plays a vital role in the theory of graphs. First, we introduce the concept of tree, study its properties and some of its applications. Later, we introduce the concept of spanning tree, and study the relationships among circuits and trees. 3.3.1 Definition A connected graph without circuits is called a tree. 3.3.2 Example Trees with one, two, three and four vertices are given.
Sikkim Manipal University Page No.:

Theory of Computer Science

Unit 3

3.3.3 Note

Since a tree is a graph, we have that a tree contains at least one vertex. A tree without any edge is referred to as a null tree. Since we are considering only finite graphs, we have that the trees considered are also finite. A tree is always a simple graph.

3.3.4 Definition A connected graph is said to be minimally connected if the removal of any one edge from the graph provides a disconnected graph. Observation:

A graph G is a tree it is minimally connected. If a graph G contains n vertices, n -1 edges and no circuits, then G is a connected graph. If G is a tree (with two or more vertices), then there exists at least two pendant vertices.

3.3.5 Theorem For a given graph G, with n vertices the following conditions are equivalent: i) G is connected and is circuitless; ii) G is connected and has n 1 edges; iii) G is circuitless and has n 1 edges; iv) There is exactly one path between every pair of vertices in G; v) G is a tree.

3.4 Binary Trees


3.4.1 Definition A tree in which one vertex (called the root) is distinguished from all the other vertices, is called a rooted tree. In a rooted tree, the root is generally marked in a small triangle.
Sikkim Manipal University Page No.:

Theory of Computer Science

Unit 3

3.4.2 Example Distinct rooted trees with four vertices were given in Fig.

Generally, the term tree means trees without any root. However, they are sometimes called free trees or non-rooted trees. A verity of rooted trees (called the Binary rooted trees) is of particular interest (since they are extensively used in the computer search methods, binary identification problems, and variable length binary codes). 3.4.3 Definition A tree in which there is exactly one vertex of degree 2, and all other remaining vertices are of degree one or three, is called a binary tree. i) Clearly, the following graph represents a binary tree (because the only vertex v1 is of degree 2, and all other vertices are of degree either 1 or 3).
v1 v2 v3

v
v6 v7
v10

v5 v11

Sikkim Manipal University

Page No.:

Theory of Computer Science v9 v8 v1 v1

Unit 3

Sikkim Manipal University

Page No.:

Theory of Computer Science

Unit 3

ii) Since the vertex of degree 2 (that is, v1) is distinct from all other vertices, this vertex v1 is the root. iii) In a binary tree, the vertex with degree 2 serves as a root. So every binary tree is a rooted tree. 3.4.4 Properties of Binary trees Property (i): The number of vertices n, in a binary tree is always odd. Property (ii): The number of pendent vertices is n+1 . 2 n+1 )-1 2

Property (iii): Number of vertices of degree 3 is = n - p - 1 = n - ( = n 3 . 2

3.4.5 Example In the above graph, we have that N = 13, p = n+ 1 2 =

13 + 1 14
=

= 7.

2 13 3 n 3 = = 5. 2 2

Therefore, number of vertices of degree 3 is 3.4.6 Definition

A non-pendent vertex in a tree is called an internal vertex. Note: i) The number of internal vertices in a Binary tree is p = the number of pendent vertices. ii) In the above binary tree, the internal vertices are v1, v3, v4, v5, v6, v9. These are 6 (= 7 - 1 = p - 1) in number. n 1 = (p -1) where 2

Sikkim Manipal University

Page No.:

Theory of Computer Science

Unit 3

3.4.7 Definition Let v be a vertex in a binary tree. Then v is said to be at level i if v is at a distance of i from the root. 3.4.8 Example i) A 13-vertex, 4-level binary tree was given in Fig. level 0

level 1

level 2 level 3

level 4 Here the number of vertices at levels 0, 1, 2, 3, 4 is 1, 2, 2, 4 and 4 respectively.

Sikkim Manipal University

Page No.:

Theory of Computer Science

Unit 3

Self Assessment Question 5. Draw the 11-vertex binary trees and also find its path length.

3.5 Walks and Paths


3.5.1 Definition

A finite alternating sequence of vertices and edges (no repetition of edge allowed) beginning and ending with vertices such that each edge is incident with the vertices preceding and following it, is called a walk.

Vertices with which a walk begins and ends are called the terminal vertices of the given walk. The remaining vertices in the walk are called intermediate vertices of the walk. A walk is said to be a closed walk if the terminal points are same.

3.5.2 Example i) From the above definition, it is clear that no edge appears more than once in a walk. ii) Consider the graph given. We can observe that v1av2bv3cv3d v4e v2f v5 is a walk. g b v3 d e v4 v1 c

v2 f

v5

Sikkim Manipal University

Page No.:

Theory of Computer Science

Unit 3

The set of vertices and edges constituting a given walk in a graph G forms a subgraph of G. A walk which is not closed, is called an open walk. [In other words, a walk is said to be an open walk if the terminal points are different]. In the above example, the walk v1av2bv3cv1 is a closed walk and the walk v1av2bv3dv4ev2fv5 is an open walk. 3.5.3 Definition An open walk, in which no vertex appears more than once, is called a path or simple path. (In other words, an open walk is said to be a path if it does not intersects itself. The number of edges in a path is called the length of the path). 3.5.4 Example We can observe that v1av2bv3dv4 is a path, and v1av2bv3cv3dv4ev2f v5 is a walk but not a path (because this walk contains repeated vertex v2). i) Consider the graph given above, the path v1av2bv3dv4 is of length 3. ii) An edge that is not a self-loop is a path of length one. iii) A self-loop is a walk but not a path. iv) Consider a path as a subgraph. With respect to the subgraph, we have that terminal vertices of the path are of degree one and the rest of the vertices (these are called intermediate vertices) are of degree two. For example, consider the path v1av2bv3dv4 (refer the graph given). With respect to this path d(v1) = 1, d(v2) = 2, d(v3) = 2, d(v4) = 1. 3.5.5 Note Consider the graph given below Observe the following. i) If there is no repetition of vertices in a walk, then the walk is a nonintersecting walk.

Sikkim Manipal University

Page No.:

cv 8

Theory of Computer Science

Unit 3

v1 v5

v4

v2

v3

ii) Observe that a walk is intersecting there lies repetition of vertices. iii) v1v5, v5v3, v3v4, v4v5, v5v2 is a walk. 3.5.6 Definition

A collection of trees is called a forest. A tree T is said to be a spanning tree of a connected graph G if T is a subgraph of G and T contains all the vertices of G.

3.5.7 Example Consider the graph G given in Fig A. Graph T (given in Fig B) is a spanning tree of G.
6

c1 b1
7

c2

v5 v7 b5 c7 b6 v1

v6

v1

c3
c

v5

c5

b3

v3 v3
2

v4 v2 Fig B Graph-T

b4

v4 c6

Fig- A Graph-G
Sikkim Manipal University Page No.:

Theory of Computer Science

Unit 3

3.5.8 Note i) Since spanning trees are the largest (with the maximum number of edges) trees among all trees in G, we have that a spanning tree is also called a maximal tree subgraph or maximal tree of G. ii) Spanning is defined only for a connected graph. (since a tree is always connected). However, each component of a disconnected graph, does have a spanning tree. Thus a disconnected graph with k components contains a spanning forest consisting of k spanning trees. 3.5.9 Theorem Every connected graph has at least one spanning tree. Proof: Let G be a connected graph. If G has no circuit, then G is a spanning tree. If G has a circuit, then delete an edge from this circuit. If till leaves the graph connected. If there are more circuits, repeat the operation till an edge from the last circuit is deleted, leaving the graph connected, circuitless, and contains all the vertices of G. Thus the subgraph obtained is a spanning tree of G. Hence every connected graph has at least one spanning tree. 3.5.10 Definitions

A graph G is said to be a weighted graph if all the edges e of G were assigned by a corresponding real number (e). Let T be any spanning tree of a connected graph G. Then the weight of T, that is, (T) is defined as the sum of weights of all branches in T. A spanning tree with the smallest weight in a weighted graph is called a minimal spanning tree.

3.5.11 Note i) Different spanning trees of G may have different weights. Among all the spanning trees of G, one with the smallest weight is of practical significance.

Sikkim Manipal University

Page No.:

Theory of Computer Science

Unit 3

ii) Let G be a graph on n vertices in which every edge has a unit weight . Then all the spanning trees have the same weight of (n -1) units. 3.5.12 Kruskal algorithm for finding shortest spanning tree Step-(i): List all the edges of G in order of non-decreasing weight. Now we select an edge e1 of G such that (e1) is as small as possible and e1 is not a loop. Step-(ii): Select next smallest edge from the set of all remaining edges of G such that the selected edges do not form a circuit with the edges that have already been chosen. Step-(iii): We continue this process of taking smallest edges among those not already chosen, provided no circuit is formed with those, that have been chosen already. [If edges e1, e2, , ei have been chosen, then chose ei+1 from E \ {e1, e2, , ei} in such way that graph with {e1, e2, , ei+1} as small as possible]. Step-(iv): If a graph G has n vertices, then we will stop this process after choosing n - 1 edges. These edges form a subgraph T, which is not cyclic. (Thus T is a shortest spanning tree of G). Self Assessment Questions 7. Consider the following graph: Write the paths, lengths of path, cycles, and also specify whether or not its simple path/closed path. is acyclic and (ei+1) is

Sikkim Manipal University

Page No.:

Theory of Computer Science

Unit 3

3.6 Directed Graphs


3.6.1 Definition A directed graph (or) a digraph G consists of a non-empty set V (the elements of V are normally denoted by v1, v2, ) and a set E (the elements of E are normally denoted by e1, e2, .) and a mapping that maps every element of E onto an ordered pair (vi, vj) of elements from V. The elements of V are called as vertices or nodes or points. The elements of E are called as edges or arcs or lines. If e E and vi, vj V such that

(e) = (vi, vj), then we write e


=

v i v j

. In this case, we say that e is

an edge between vi and vj. (We also say that e is an edge from vi to vj). (We also say that e originates at vi and terminates at vj). (An edge from vi to vj is denoted by a line segment with an arrow directed from vi to vj). 3.6.2 Note i) A directed graph is also refereed to as an oriented graph. ii) Let G be a directed graph and e = vu . 3.6.3 Example The graph given in figure is a digraph with 5 vertices and ten edges.
2

e5 e1 e6 e8 e7 v3 e10
9

e4 v1 e3 e2

v4
Sikkim Manipal University

v5
Page No.:

Theory of Computer Science

Unit 3

Here v5 is the initial vertex and v4 is the terminal vertex for the edge e7. The edge e5 is a self-loop. 3.6.4 Definition

The number of edges incident out of a vertex v is called the out degree of v. The out degree of a vertex v is denoted by d (v).
+

The number of edges incident into v is called the in-degree of v. The indegree of a vertex v is denoted by d (v).
-

Observation: The degree of v is equal to the sum of in-degree and out degree of v, for any vertex v in a graph. (In symbols, we can write as d(v) = d (v) + d (v) for all vertices v). Self Assessment Questions 7. Consider the graph given in Example: 3.6.3. Write the indegrees and outdegrees of the graph.
+ -

3.7 Summary
This Unit is meant for beginning your process of learning Graph Theory. It started with the definition of Graph and illustrated the concepts like incidence, degree, isolated vertex, pendent vertex and null graph. We also discussed the isomorphism between graphs and subgraphs of a given graph with appropriate illustrations. We dealt with a special type of graphs called trees and studied some properties. We considered the binary search tree and their properties. We presented some illustrations on binary search tree, complete binary search trees and directed graphs.

3.8 Terminal Questions


1. Define the terms: Graph, finite graph, infinite graph, incidence, degree, isolated vertex, pendent vertex, null graph. 2. Show that the sum of the degrees of the vertices of a finite graph G is twice the number of edges.
Sikkim Manipal University Page No.:

Theory of Computer Science

Unit 3

3. Show that the number of vertices of odd degree is always even. 4. Show that an infinite graph with finite number of edges must have an infinite number of isolated vertices. 5. Show that the maximum degree of any vertex in a simple graph is (n - 1). 6. Draw all regular binary trees a) with exactly 7 vertices b) with exactly 9 vertices 7. Draw all distinct binary tree with (i) 3 vertices, (ii) 4 vertices. 8. Draw binary search trees for the following lists. i) 18, 44, 2, 5, 73, 45, 14, 6, 8, 10, 20, 11 ii) 2, 1, 5, 6, 8, 9, 7, 3, 4. 7. Define a binary tree, complete binary tree and give examples of each.

3.9 Answers
Self Assessment Questions 1. Every graph is a sub graph of itself; 2. A subgraph of a subgraph of G is a subgraph of G; 3. A single vertex in a graph G is a subgraph of G; and 4. A single edge in G together with its end vertices is a subgraph of G. 5. (i)

level - 0

level -1 level -2 level -3

Sikkim Manipal University

Page No.:

Theory of Computer Science

Unit 3

The path length: 2 + 2 + 3 + 3 + 3 + 3 = 16. (ii) level 0 level 1 level 2 level 3 level 4 level 5 The path length: 1 + 2 + 3 + 4 + 5 + 5 = 20. 6.
Path a-d-c-e-g-j-d-a b-c-e-f-g-j-f-b a-b-a a-d-c-b-a i-i a e-f-g-j-f-b d-b-c-d Length 7 7 2 4 1 0 5 3 Simple (yes/no) no no No Yes Yes Yes No yes Closed (yes/no) yes yes Yes Yes Yes Yes No yes Circuit (yes/no) no yes No Yes Yes No No yes Cycle (yes/no) no no No Yes Yes No No Yes

7. i) Here d (v1) = 3, d (v2) = 1, d (v3) = 1, d (v4) = 1, d (v5) = 4. d (v1) = 1, d (v2) = 2, d (v3) = 4, d (v4) = 3,
+ + + + + + -

d (v5) = 0.
+ -

ii) d(v1) = 4 = 3 + 1 = d (v1) + d (v1) ; d(v2) = 3 = 1 + 2 = d (v2) + d (v2), and so on.

Sikkim Manipal University

Page No.: 64

Vous aimerez peut-être aussi