GTU Graph Theory
GTU Graph Theory
❖ INTRODUCTION
✓ Graph theory plays an important role in several areas of computer science such as switching
theory and logical design, artificial intelligence, formal languages, computer graphics,
operating systems, compiler writing, information organization and retrieval.
❖ GRAPH
✓ A graph G =< V, E, ∅ > consists of a nonempty set V called the set of nodes (points, vertices)
of the graph, E is said to be the set of edges of the graph and ∅ is a mapping from the set of
edges E to a set of ordered or unordered pairs of elements of V.
✓ 𝐕 = 𝐕ሺ𝐆ሻ = ሼv1 , v2 , v3 , … ሽ = The set of nodes(vertices/points/dots/junctions).
✓ 𝐄 = 𝐄ሺ𝐆ሻ = ሼe1 , e2 , e3 , … ሽ = The set of edges(branch/line/arc).
✓ The elements of V are called nodes/vertices of a graph G and the elements of E are called
edges of a graph G.
𝒗𝒊 𝒆𝒌 𝒗𝒋
v v v
NOTE:
✓ Throughout, we shall assume that the sets V and E of a graph G are finite.
✓ Any edge e can be made by one OR two nodes ሺu, uሻ OR ሺu, vሻ respectively.
𝒆
𝒆
𝒖 𝒗
❖ ADJACENT NODES 𝒖
✓ If two nodes u and v are joined by an edge e then u and v are said to be adjacent nodes.
❖ INCIDENT EDGE
✓ An edge e ∈ E (directed/undirected) which joins the nodes u and v is said to be incident to
the nodes u and v.
𝑢 𝑒 𝑣
v v
❖ LOOP(SLING)
✓ An edge e of a graph G that joins a node u to itself is called a loop. A loop is 𝑒
an edge e = ሺu, uሻ.
𝑢
❖ PARALLEL EDGES
𝑒1
✓ If two nodes of a graph are joined by more than one edge then 𝑣
𝑢 𝑒2
these edges are called parallel edges/multiple edges.
𝑒3
𝑣2 𝑒1
𝑒2
𝑒1 𝑒2 𝑒1
𝑣1 𝑒6 𝑣3 𝑣1 𝑣2
𝑒5
𝑣1 𝑣3
𝑒2 𝑒3
𝑒4
𝑣4
❖ UNDIRECTED EDGE
✓ In a graph G an edge ‘e’ which is associated with an unordered pair 𝑒
𝑢 𝑣
ሺu, vሻ of nodes ‘u’ and ‘v’ is called undirected edge of graph G.
❖ UNDIRECTED GRAPH
𝑣7
𝑣2
✓ A graph in which every edge is 𝑣8
undirected is called an 𝑒2 𝑒5
undirected graph. 𝑒1 𝑣5
𝑣3
𝑣1
𝑒3
𝑒7
𝑒6
𝑒4
𝑒8 𝑣6
𝑒9
𝑣4
𝑣9
𝑒10
❖ DISTINCT EDGES
✓ The two possible edges between a pair of nodes which are opposite
in direction which are known as distinct edges.
❖ MULTI GRAPH
✓ Any graph which contains some parallel edges is called a
multigraph.
❖ SIMPLE GRAPH
✓ A graph which has neither loop nor parallel edges is called a simple
graph.
❖ WEIGHTED GRAPH 𝑣2
✓ A graph in which weighted are assigned to every edge is called
20 10
a weighted graph.
𝑣1 𝑣3
5
❖ ISOLATED NODE
✓ In a graph a node which is not adjacent to any other node is called an isolated node.
❖ NULL GRAPH
✓ A graph containing only isolated nodes is called a null graph.
✓ The set of edges in the null graph is empty.
NOTE:
1
✓ It can happen that two diagrams which look
1 2
entirely different but both may represent the
same graph.
4 3 2 3
T 2 Define with example: adjacent nodes, initiating node, terminating node and isolated
node.
T 3 Define with example: incident edges, loop, parallel edges, directed edges, undirected
edges and distinct edges.
H 4 Define with example: directed graph, undirected graph, mixed graph, multi graph,
simple graph, weighted graph and null graph.
❖ ISOMORPHIC GRAPH
✓ A graph G1 = ሺV1 , E1 ሻ is said to be isomorphic to the graph G2 = ሺV2 , E2 ሻ if there exists a
bijection between the set of nodes V1 and V2 and a bijection between the set of edges E1 and
E2 such that if e is an edge with end nodes u and v in G1 then the corresponding edge e′ has
its end nodes u′ and v ′ in G2 which correspond to u and v respectively. If such pair of
bijections exist then it is called a graph isomorphism and it is denoted by G1 ≅ G2.
✓ According to the definition of isomorphism we note that any two nodes in one graph which
are joined by an edge must have the corresponding nodes in the other graph also joined by
an edge and hence a one to one correspondence exists between the edges as well.
3
2
𝑢2 𝑢1
1
𝐺1 𝐺2
1 𝑣1
3 𝑣3
𝑣2
2
𝐺1 𝐺2
✓ Here g1 and g 2 both has same number of nodes and edges but g1 is not isomorphic to g 2 . I.e.,
G1 ≇ G2 because the edge < 3, 1 >↛< v1 , V3 >.
NOTE:
✓ The concept of isomorphism also defines in undirected graphs with the same definition given
for directed graphs.
a
1
5
0 e b
2
4
3
d c
𝐺1 𝐺2
𝑣7
𝑣2
𝑣8
𝑣5
𝑣3
𝑣1
𝑣6
𝑣4
𝑣9
𝐝ሺ𝐯𝐢 ሻ = 𝟐𝐞.
𝐢=𝟏
✓ In above graph
9
dሺvi ሻ = dሺv1 ሻ + dሺv2 ሻ + dሺv3 ሻ + dሺv4 ሻ + dሺv5 ሻ + dሺv6 ሻ + dሺv7 ሻ + dሺv8 ሻ + dሺv9 ሻ
i=1
= 4 + 3 + 1 + 6 + 2 + 1 + 0 + 0 + 3 = 20 = 2ሺ10ሻ = 2e.
❖ 𝐑𝐄𝐒𝐔𝐋𝐓
✓ In any undirected graph G, number of odd nodes must be even.
✓ In above graph there are 4(even) number of nodes.
❖ INDEGREE
✓ Let G be a directed graph then for any node v in G, the number of edges which have v as their
terminal node is called the indegree of the node v.
OR
✓ In a directed graph G, the number of edges directed towards node v is called indegree of a
node v. It is denoted by d− ሺvሻ.
NOTE: 𝑣4
✓ In above example,
4 4 4
❖ SUBGRAPHS
✓ Let G and H be two graphs. Then H is said to be a subgraph of G if VሺHሻ ⊆ VሺGሻ & EሺHሻ ⊆ EሺGሻ.
Here G is called super graph of H.
G H
✓ Here H is subgraph of G.
NOTE:
✓ The graph G as well as the null graph obtained from G by deleting all the edges of G are
subgraphs of G. Other subgraphs of G can be obtained by deleting certain nodes and edges of
G.
❖ NODE DELETED SUBGRAPH
✓ The graph obtained by deletion of a node v from a given graph G is called node deleted
subgraph of G. It is denoted by G − ሼvሽ.
❖ EDGE DELETED SUBGRAPH
✓ The graph obtained by deletion of an edge e from a given graph G is called edge deleted
subgraph of G. It is denoted by G − ሼeሽ.
❖ CONVERSE (REVERSAL/DIRECTIONAL DUAL) OF A DIGRAPH
෩ =< V, E
✓ The converse of a digraph G =< V, E > to be a digraph G ෩ > in which the relation E
෩ is
෩ is obtained from G by simply reversing the
the converse of the relation E. The diagram G
෩ is also called the reversal or directional dual of a
directions of the edges in G. The converse G
digraph G.
EXAMPLE:
𝑣2 𝑣2
𝑣1 𝑣3 𝑣1 𝑣3
𝑣4 𝑣4
G 𝐺෨
C 2 Draw all possible different simple digraphs having three nodes up to isomorphism.
Show that there is only one digraph with no edges, one with one edge, four with two
edges, four with three edges, four with four edges, one with five edges and one with
six edges. Assume that there are no loops.
T 3 Define with example: degree of a node, odd node, even node, pendant node and
isolated node for undirected graph.
T 4 Define with example: indegree, outdegree and total degree for directed graph.
C 5 Show that the sum of indegrees of all the nodes of a simple digraph is equal to the sum
of outdegrees of all its nodes and that this sum is equal to the number of edges of the
graph.
T 7 Consider the following graphs: Determine the degree of each node and verify
Handshaking theorem.
𝑣5 𝑣1
𝑣1 𝑣2
𝑣4 𝑣2 𝑣5
𝑣3
𝑣6 𝑣3
𝑣5 𝑣4
𝑣3 𝑣4
𝑣1 𝑣2
C 8 Check whether the following pair of graphs G & H are isomorphic or not with
description.
(A).
G H
(B).
G H
(C).
G H
𝑣3 𝑒3 𝑣4
𝑒4
G
NOTE:
✓ In this method we introduce some additional terminology associated with a simple digraph.
❖ PATH OF A GIVEN GRAPH
✓ Let G = ሺV, Eሻ be a simple digraph. Consider a sequence of edges of G such that the terminal
vertex of any edge in the sequence is the initial vertex of next edge. Such a sequence is called
a path of the graph G.
✓ A path is said to traverse through the nodes appearing in the sequence originating in the
initial node of the first edge and ending in the terminal node of the last edge in the sequence.
❖ LENGTH OF PATH
✓ The number of edges appearing in the sequence of a path is called the length of the path.
❖ SIMPLE PATH (EDGE SIMPLE)
✓ A path in a diagraph in which all the edges are distinct is called a simple path (edge simple).
❖ ELEMENTARY PATH (NODE SIMPLE)
✓ A path in a digraph in which all the nodes through which it traverses are distinct is called an
elementary path (node simple).
4 3
❖ SIMPLE CYCLE
✓ A cycle is called simple if its path is simple path. i.e., no edge in the cycle appears more than
once in the path.
❖ ELEMENTARY CYCLE
✓ A cycle is called elementary if it does not traverse through any node more than once.
NOTE:
✓ In a cycle the initial node appears at least twice even if it is an elementary cycle.
NOTE:
✓ It is assumed that d < u, u > = 0 for any node u.
❖ PROPERTIES OF REACHABILITY
✓ If v is reachable from u then d < u, v > satisfies the following properties:
1. d < u, v > ≥ 0.
2. d < u, u ≥ 0.
3. d < u, v > +d < v, w > ≥ d < u, w >. (Triangle inequality)
NOTE:
✓ If v is not reachable from u then it is customary to write d < u, v >= ∞.
✓ If v is reachable from u and u is reachable from v then d < u, v > is not necessarily equal to
d < v, u >.
𝑣1 𝑣2 𝑣5 𝑣6 𝑣9
𝑣10
𝑣4 𝑣3 𝑣7 𝑣8
✓ In the above graph all the reachable sets are given as below
Rሺv1 ሻ = Rሺv2 ሻ = Rሺv3 ሻ = Rሺv4 ሻ = Rሺv5 ሻ = ሼv1 , v2 , v3 , v4 , v5 , v6 ሽ
Rሺv6 ሻ = ሼv6 ሽ, Rሺv7 ሻ = ሼv6 , v7 ሽ, Rሺv8 ሻ = ሼv6 , v7 , v8 ሽ, Rሺv9 ሻ = ሼv9 ሽ
Rሺv10 ሻ = ሼv10 ሽ, Rሺv5 , v8 , v9 , v10 ሻ = V = Rሺv1 , v8 , v9 , v10 ሻ
❖ NODE BASE
✓ In a digraph G = ሺV, Eሻ, a subset X ⊆ V is called a node base if its reachable set is V and no
proper subset of X has this property.
✓ In the above graph the set ሼv1 , v8 , v9 , v10 ሽ is a node base and similarly the set ሼv5 , v8 , v9 , v10 ሽ
is a node base.
❖ CONNECTEDNESS
✓ An undirected graph is said to be connected if for any pair of nodes of the graph the two nodes
are reachable from one another.
❖ WEAKLY CONNECTED
✓ A digraph is said to be weakly connected(connected) if it is connected as an undirected graph
in which the direction of the edge is neglected. i.e., if the graph when treated as an undirected
graph is connected.
❖ STRONGLY CONNECTED
✓ If for any pair of nodes of the graph both the nodes of the pair are reachable from one another
then the graph is called strongly connected.
❖ UNILATERALLY CONNECTED
✓ A simple digraph is said to be unilaterally connected if for any pair of nodes of the graph at
least one of the node of the pair is reachable from the other node.
A B C
)
✓ The digraph in figure(A) is strongly connected, (B) is weakly connected but not unilaterally
connected while (C) is unilaterally connected but not strongly connected.
❖ STRONG, WEAK AND UNILATERAL COMPONENTS OF A GRAPH
✓ For a simple digraph, a maximal strongly connected subgraph is called a strong component.
Similarly, a maximal unilaterally connected subgraph is called a unilateral component and
maximal weakly connected subgraph is called a weak component.
✓ For the digraph given in above figure 3 4
6
ሼ1,2,3ሽ, ሼ4ሽ, ሼ5ሽ, ሼ6ሽ are the strong components.
ሼ1,2,3,4,5ሽ, ሼ6ሽ are the unilateral components.
ሼ1,2,3,4,5,6ሽ is the weak component because
the graph is weakly connected. 2 1 5
𝑣2 𝑣3
𝑣2 𝑣3
C 3 Prove that in a simple digraph, the length of any elementary path is less than or equal
to n-1, where n is the number of nodes in the graph. Similarly, the length of any
elementary cycle does not exceed n.
H 5 Find the reachable set of ሼv1 , v4 ሽ, ሼv4 , v5 ሽ and ሼv3 ሽ for the digraph given in example-
2.
C 6 Find the reachable set for all the nodes in the following
𝑣1 𝑣2
digraph.
𝑣4
𝑣3
C 7 Find a node base for each of the digraphs given in the examples 1 and 2.
H 9 Find the strong, weak and unilateral component for the digraph given in the example
2.
NOTE:
✓ The sum of all 1’s in a row indicates the outdegree of the corresponding node.
✓ The sum of all 1’s in a column indicates the indegree of the corresponding node.
EXAMPLE:
✓ From the adjacency matrix(A) of the above digraph we can calculate outdegree, indegree and
total degree of each nodes which as follow.
0 otherwise
✓ That is P0 [i, j] = 1 if there is an edge from vi to vj .
P1 [i, j] = 1 if there is a simple path from vi to vj which does not use any other vertex except
possibly v1 .
P2 [i, j] = 1 if there is a simple path from vi to vj which does not use any other vertices except
possibly v1 and v2 . And so on.
✓ Observe that the first matrix P0 = A is the adjacency matrix of G. Furthermore, since G has
only m vertices, the last matrix Pm = P is the path matrix of G.
✓ Warshall observed that Pk [i, j] = 1 can occur only if one of the following two cases occurs:
1. There is a simple path from vi to vj which does not use any other vertices except possibly
v1 , v2 , … , vk−1 ; hence Pk−1 [i, j] = 1.
2. There is a simple path from vi to vk and a simple path from vk to vj where each simple path
does not use any other vertices except possibly v1 , v2 , … , vk−1 . Hence Pk−1 [i, k] =
1 & Pk−1 [k, j] = 1.
𝑣4 𝑣3
4 3
𝑣5
𝑣2
𝑣4 𝑣3
𝑣4 𝑣5 𝑣8 Level 3
Leaf node 𝑣4 𝑣5 𝑣8 Level 3
𝑣10 𝑣2 𝑣10 𝑣9
𝑣2 𝑣9 𝑣3 𝑣7 Level 2
𝑣3 𝑣7 Level 2
𝑣1 𝑣1 𝑣6 Level 1
𝑣6 Level 1
Branch
node 𝑣0 Level 0
𝑣0 Root Level 0
Undirected tree
Directed tree
𝑣4 𝑣5 𝑣8
𝑣0
𝑣1
𝑣2 𝑣10 𝑣9
𝑣3 𝑣7 𝑣2
𝑣3
𝑣1 𝑣4
𝑣6 𝑣5
𝑣6
𝑣7
𝑣0
𝑣8
𝑣9
Figure 1
𝑣10
Method C
ሺ𝑣0 ሺ𝑣1 ሺ𝑣2 ሻ ሺ𝑣3 ሺ𝑣4 ሻሺ𝑣5 ሻሻሻሺሺ𝑣6 ሺ𝑣7 ሺ𝑣8 ሻሻሺ𝑣9 ሻሺ𝑣10 ሻሻሻ
Method B
𝑣7 𝑣8
𝑣9
𝑣5
𝑣2 𝑣𝑣103
𝑣4
𝑣3 𝑣6
𝑣1
𝑣0
Method A
❖ BINARY TREE
✓ In a directed tree the outdegree of every
node is less than or equal to 2 then the
tree is called binary tree.
EXAMPLE: Figure A shows binary tree.
❖ FULL (COMPLETE)BINARY TREE
✓ If the outdegree of every node is exactly A B
equal to 2 or 0 then the tree is called a full
or complete binary tree.
EXAMPLE: Figure B shows binary
tree.
❖ M-ARY TREE
✓ In a directed tree the
outdegree of every node is
less than or equal to m then
the tree is called m-ary tree.
EXAMPLE: Figure C shows 3-ary
tree.
C D
0 1 0 1
00 01 10 11
00 10 11
0 1
0 1 0 1
01 10 11
C D
✓ Figure A shows a binary tree, B shows a full binary tree and C shows all four possible
arrangements of sons of a node in a binary tree. The binary trees shown in figure A and D are
distinct positional trees although they are not distinct ordered trees. In a positional binary
tree, every node is uniquely represented by a string over the alphabet ሼ0, 1ሽ, the root being
represented by an empty string. Any son of a node u has a string which is prefixed by the
string of u. The string of any terminal node is not prefixed to the string of any other node. The
2 3 4 5
9 10 11
Given tree
6 7 8
1
2
3 4 5
3
2
6
4
10 11 7 8 5
6 7 8 9
Stage 1
10
11
H 1 Define with example: acyclic graph, tree, directed tree, forest, root, leaf node, branch
node and level of a node.
H 3 Define with example: binary tree, complete binary tree, m-ary tree and complete m-
ary tree.
B E
B E
F
C D C D F
H G
A
❖ TREE TRAVERSAL
✓ Tree traversal is a procedure by which each node is processed exactly once in some
systematic manner.
❖ PRE-ORDER TRAVERSAL
✓ Process the root node.
Traverse the left subtree in pre-order.
Traverse the right subtree in pre-order.
❖ IN-ORDER TRAVERSAL
✓ Traverse the left subtree in in-order.
Process the root node.
Traverse the right subtree in in-order.
❖ POST ORDER TRAVERSAL
✓ Traverse the left subtree in post-order.
Traverse the right subtree in post-order.
Process the root node.
EXAMPLE:
✓ The pre-order, in-order and post-order traversals of the tree given in following table which
process the nodes in the following order:
❖ ALGORITHM PREORDER:
✓ Given a binary tree whose root node address is given by a variable T and whose node
structure is the same as previously described, this algorithm traverses the tree in preorder.
An auxiliary stack S is used and TOP is the index of the top element of S. P is a temporary
variable which denotes when we are in the tree.
1. [Initialize] If T = NULL, then Exit (the tree has no root and therefore is not a proper binary
tree); otherwise set P ← T and TOP ← 0.
2. [Visit node, stack right branch address and go left] Process node P. If RLINKሺPሻ ≠ NULL,
then set TOP ← TOP + 1 and S[TOP] ← RLINKሺPሻ. Set P ← LLINKሺPሻ.
3. [End of chain?] If P ≠ NULL, then go to step 2.
4. [Unstack a right branch address] If TOP=0, then Exit; otherwise set P ← S[TOP], TOP ←
TOP − 1, and go to step 2.
✓ In the second and third steps of the algorithm, we visit and process a node. The address of
the right branch of such a node, if it exists, is stacked and a chain of left branches is followed
until this chain ends. At this point we enter step 4 and delete from the stack the address of