Topic 6

You might also like

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

Topic VI

Introduction to Graph Theory


Topic outlines
6.1 Introduction to graph theory
6.2 Classification of graphs
6.3 Terminologies of graph
6.4 Types of graph
6.5 Adjacency matrix for graph
6.6 Special types of graph
6.7 Connectivity of graph
6.8 Tree
6.9 Tree traversal
6.10 Spanning Tree
Introduction to graph theory
Classification of graphs

a) Directed and Undirected graphs

b) Connected and Disconnected graphs

c) Weighted and Unweighted graphs


Directed and undirected graphs
● A graph in which every edge is directed is called as directed graph

● A graph in which every edge not is directed is called as undirected graph

For example
Connected and Disconnected graphs
● A connected graph is a graph in which there is a path between every pair of vertices. In
other words, for any two vertices in a connected graph, there exists a sequence of edges
that connects them.

● Disconnected graph is a graph that is not connected. In a disconnected graph, there are
isolated subsets of vertices that have no connection to other subsets.
● For example
Weighted and Unweighted graphs
● A weighted graph is a graph in which each edge is assigned a numerical value called a
weight. The weight represents some kind of measurement or cost associated with
traversing or using that particular edge.
● The weights can represent various quantities depending on the context of the problem.
For example, in a transportation network, the weights may represent the distances between
cities or the travel time required to move from one location to another.
Weighted and Unweighted graphs
● The graph is a unweighted type if every edge does not have an associated numerical
value or weight.
● In an unweighted graph, all edges are considered to have the same cost or weight,
typically indicating a simple presence or absence of a connection.
For example,
Subgraph
● The graph H is said to be a subgraph of G , if and only if every vertex in H is also a
vertex in G, every edge in H is also an edge in G.
● A subgraph is like a smaller version of the original graph, containing a subset of its
vertices and edges.
For example,
Exercise
Find the at least three possible subgraphs for following graphs 𝑮𝟏 and 𝑮𝟐

𝑮𝟏 𝑮𝟐
Types of graph
a) Simple graph
b) Multi graph
c) Pseudo graph
Types of graph
Simple graph
● It is an undirected graph that does not contain any loops or multiple edges.
Note: Loop is an edge that connects a vertex to itself.
Multiple edges are the edges that connects same pair of vertex
● In a simple graph, the edges represent connections between pairs of vertices, and each
edge is unique. That is, there is at most one edge between any two vertices.
For example
Types of graph
Multi graph
● A multigraph graph is graph that has multiple edges between pair of vertices.
● The multigraph does not have any loops.
For example,
Types of graph
Pseudo graph
● A pseudo graph is graph that has multiple edges between pairs of vertices or loops, or
both.
● In pseudo graph both loops and multiple edges are allowed .
For example
Exercise
Classify the following graphs as simple graph, multigraph and pseudo graph

𝑮𝟏
𝑮𝟐 𝑮𝟑
Definitions and Terminologies in Graph theory
Incidence between edges and vertices
● An incident edge refers to an edge that is directly connected to a vertex in a graph.
Then the edge said to be incident to that vertex.
● An incident vertex refers to a vertex that is directly connected to an edge. Then the
vertex is said to be incident to that edges.

For example,
Degree of vertex
● The degree of a vertex in a graph refers to the number of edges incident to that vertex.
In other words, it represents the number of connections that a vertex has with other
vertices in the graph.
● It is denoted by 𝑑(𝑉)
Note: A vertex with loop has a degree of two.
● The degree of a graph is defined as the maximum degree among all vertices in the
graph.

For Example,
𝑑 1 =1 𝑑 2 =2 𝑑 5 =5 𝑑 0 =0

The degree of 𝑑 𝑮𝟏 = 5

𝑮𝟏
Examples
Exercise
Draw any undirected graph with degree 2, 5, and 6
Handshaking theorem
Let 𝐺 = (𝑉, 𝐸) be the undirected graph, then sum of the degrees of all the vertices is equals to
twice the number of edges present in the graph.

Total degree of vertices = 2 𝐸

For example,

Total degree of vertice = 𝑑 𝑣1 + 𝑑 𝑣2 + 𝑑 𝑣3 + 𝑑 𝑣4


= 3 + 1 +2 +2 = 8
Number of edges 𝐸 = 4

Total degree of vertices = 2 × 4 = 8


Exercise
1. How many edges are there in graph with 10 vertices of degree 6.
2. In a simple undirected graph, the sum of the degrees of all vertices is 30. How many edges
does the graph have?
3. In a simple undirected graph, the degrees of four vertices are 3, 4, 2, and 5. How many
edges does the graph have?
4. Find the total degree of following graph
Adjacent vertices and adjacent edges
Order and size of graph
Exercise
Find the size and order of following graph
Adjacency matrix
Exercise
Find the adjacency matrix for following graphs 𝑮𝟏 , 𝑮𝟐

𝑮𝟏
𝑮𝟐
Special types of graphs
Special types of graphs are ,

a) Complete graph
b) Cycle graph
c) Bipartite graph
Special types of graphs
Complete graph
● A graph in which each vertex is connected to every other vertex is called a complete graph.
● If complete graph has 𝑛 number of vertices, then the number of edges is
(𝑛 × (𝑛 − 1)) / 2.
● It is denoted by 𝐾𝑛 , where 𝑛 is number of vertices.
For example,

𝑲𝟑 𝑲𝟓
Special types of graphs
Circle graph
● A circle graph is a simple graph that forms a closed loop or cycle. It consists of a single
cycle of vertices and edges, where each vertex is connected to its adjacent vertices, and the
last vertex is connected back to the first vertex, forming a circular shape.
● A circle graph should have minimum of three vertices with degree 2.
For example,
Bipartite graph
● A simple graph 𝐺 = 𝑉, 𝐸 is called bipartite graph if vertex 𝑉 can be divided into two sets
of vertices 𝑣1 and 𝑣2 in such a way that vertices of set 𝑣1 only joins the vertices of set 𝑣2 .
● In the bipartite graph none of the vertices belonging to the same set join each other.
● It is denoted by 𝐺 = 𝑣1 , 𝑣2 , 𝐸
For example,

𝑣1 = 1,2,3,4,5
𝑣2 = {𝐴, 𝐵, 𝐶, 𝐸}
Example
Check whether the following graph is a bipartite graph or not. If it is a bipartite graph, then
find two sets of vertices
Exercise
Check whether the following graph is a bipartite graph or not. If it bipartite graph, then find
two sets of vertices
Complete Bipartite graph
● A bipartite graph 𝐺 = 𝑣1 , 𝑣2 , 𝐸 is said to be complete bipartite graph if every vertices in
𝑣1 is adjacent to 𝑣2 or every vertices in 𝑣2 is adjacent to 𝑣1 .
● If 𝐺 = 𝑣1 , 𝑣2 , 𝐸 is a complete bipartite graph with 𝑣1 = 𝑚 and 𝑣2 = 𝑛 then 𝐺 =
𝑣1 , 𝑣2 , 𝐸 denoted by, 𝐾𝑚,𝑛
For example,
Exercise
1. Draw any 𝐾3,3 𝑎𝑛𝑑 𝐾3,4 bipartite graph
2. Draw any complete 𝐾1,3 𝑎𝑛𝑑 𝐾2,4 bipartite graph
Different types of routes
1. Walks
2. tails
3. paths
4. cycles
5. circuit
Different types of routes
Walk
A walk is a sequence of vertices and edges where each edge connects adjacent vertices.
The vertices and edges in a walk can be repeated.
For example,
𝒆𝟒
w1 = 𝒗𝟐 − 𝒆𝟑 − 𝒗𝟒 − 𝒆𝟒 − 𝒗𝟏 𝒆𝟑

𝒆𝟓 𝒆𝟔
w𝟐 = 𝒗𝟐 − 𝒆𝟏 − 𝒗𝟔 − 𝒆𝟗 − 𝒗𝟑 − 𝒆𝟕 − 𝒗𝟓 𝒆𝟏 𝒆𝟐

𝒆𝟕
𝒆𝟗
𝒆𝟖
Different types of routes
Different types of walks
Open walk: A walk is said to be an open walk if the starting and ending vertices are different.
Closed walk: A walk is said to be closed walk if the starting and ending vertices are identical.
For example,
(Open walk) 𝒆𝟒
𝒆𝟑
𝒘𝟏 = 𝒗𝟐 − 𝒆𝟑 − 𝒗𝟒 − 𝒆𝟒 − 𝒗𝟏
𝒆𝟓 𝒆𝟔
𝒆𝟐
(Closed walk) 𝒆𝟏
𝒘𝟐 = 𝒗𝟐 − 𝒆𝟏 − 𝒗𝟔 − 𝒆𝟗 − 𝒗𝟑 − 𝒆𝟐 − 𝒗𝟐 𝒆𝟕
𝒆𝟗
𝒆𝟖
Exercise
Find at least two closed walk and open walk for following graphs
Different types of routes
Tails
A tails is a walk in which no edges is repeated, but vertices can be repeated.

For example,
𝒆𝟒
Open tail 𝒆𝟑
1. 𝒕𝟏 = 𝒗𝟐 − 𝒆𝟏 − 𝒗𝟔 − 𝒆𝟗 − 𝒗𝟑 − 𝒆𝟕 − 𝒗𝟓
𝒆𝟓
Closed tail 𝒆𝟐
𝒆𝟔
𝒆𝟏
1. 𝒕𝟐 = 𝒗𝟐 − 𝒆𝟏 − 𝒗𝟔 − 𝒆𝟗 − 𝒗𝟑 − 𝒆𝟐 − 𝒗𝟐
𝒆𝟕
𝒆𝟗
𝒆𝟖
Different types of routes
Circuits
A circuit is a closed tail or closed walk where no edges are repeated.
In circuit, starting vertex and ending vertex should be same.

For example,
𝒆𝟒
𝒄𝟏 = 𝒗𝟐 − 𝒆𝟏 − 𝒗𝟔 − 𝒆𝟗 − 𝒗𝟑 − 𝒆𝟐 − 𝒗𝟐 𝒆𝟑
𝒄𝟐 = 𝒗𝟏 − 𝒆𝟒 − 𝒗𝟒 − 𝒆𝟒 − 𝒗𝟑 − 𝒆𝟔 − 𝒗𝟏
𝒆𝟓 𝒆𝟔
𝒆𝟏 𝒆𝟐

𝒆𝟕
𝒆𝟗
𝒆𝟖
Exercise
Find at least two circuits for following graphs
Different types of routes
Path
A path is a walk in which neither vertices nor edges are repeated.
In path,
● Vertices are not repeated except starting and ending vertex.
● Edges are not repeated
𝒆𝟒
𝒆𝟑
For example,
𝒑𝟏 = 𝒗𝟐 − 𝒆𝟏 − 𝒗𝟔 − 𝒆𝟗 − 𝒗𝟑 𝒆𝟓 𝒆𝟔
𝒆𝟏 𝒆𝟐
𝒑𝟐 = 𝒗𝟏 − 𝒆𝟒 − 𝒗𝟒 − 𝒆𝟓 − 𝒗𝟑
𝒆𝟕
𝒆𝟗
𝒆𝟖
Different types of routes
Cycle
A cycle is a closed path, where the starting and ending vertices are the same, and all other
vertices are distinct. A cycle must have at least three vertices.

For example,
𝒄𝟏 = 𝑨 − 𝑩 – 𝑪 – 𝑨

𝒄𝟏 = 𝑯 − 𝑫 – 𝑩 – 𝑪– 𝑯
Exercise
Find the at least two path and cycle for following graph
Summary
The above definitions can be easily remembered with the help of following chart
Euler Circuits
Let 𝐺 = (𝑉, 𝐸) be the undirected and connected graph, if graph 𝐺 has a circuit such that it
traverse every edge in graph 𝐺 exactly once.
If graph has a Euler circuit, then graph is called as Eulerian Graph.

Theorem: Graph 𝐺 has Euler circuit if and only if


➢ 𝐺 is connected
➢ Every vertices has even degree

For example,
Euler circuit = 𝐴, 𝐷, 𝐸, 𝐴, 𝐶, 𝐸, 𝐹, 𝐶, 𝐵, 𝐴
Euler path
A Euler path is a path in a graph that traverses each edge exactly once. Unlike a Euler circuit,
which is a closed walk that starts and ends at the same vertex, a Euler path starts at one vertex
and ends at another.
Theorem: Graph 𝐺 has Euler path if and only if
➢ 𝐺 is connected
➢ A graph will contain a Euler path if it contains at most two vertices of odd degree.
For example,

Euler path = 𝟒, 𝟑, 𝟎, 𝟐, 𝟏, 𝟎
Euler path = 𝟎, 𝟏, 𝟐, 𝟎, 𝟑, 𝟒
Exercise
Find a Euler circuit and Euler path for following graph
Fleury's algorithm to find Euler circuit and path
Fleury's algorithm
● Start at any vertex if finding a Euler circuit. (If finding a Euler path, start at one of the two
vertices with old degree.)
● Select an arbitrary edge from the current vertex that has not been visited yet. If removing
the selected edge does not disconnect the graph, remove it from the graph and add it to the
Euler circuit or path.
● Move to the other endpoint of the selected edge and set it as the new current vertex.
● Repeat the step 2 until all the edges are visited.
Example
Find a Euler circuit and path using Fleury's algorithm
Example
Find a Euler circuit and Euler path for following graph using Fleury's algorithm
Hamiltonian cycle
A Hamiltonian cycle in connected graph G is a cycle that passes through every vertex of G
exactly once, except for the starting vertex, which is also the ending vertex. In other words, it is
a cycle that visits each vertex of the graph exactly once and returns to the starting vertex.
Theorem: Graph 𝐺 has Hamilton cycle if and only if
➢ 𝐺 is connected
➢ All vertices of graph have minimum of degree 2
➢ Graph does not contain any articulation point
Note: An articulation point, also known as a cut vertex, is a vertex in a graph whose
removal disconnects the graph into two or more separate components
A graph which contains Hamilton cycle is called as Hamiltonian graph.
Hamiltonian cycle
For example,
Hamiltonian cycle = 𝐴, 𝐵, 𝐶, 𝐷, 𝐸, 𝐹, 𝐴
Hamiltonian cycle = 𝐹, 𝐸, 𝐷, 𝐶, 𝐵, 𝐴, 𝐹

𝑮𝟏
Graph 𝑮𝟐 is example of Non-Hamilton graph.

𝑮𝟐
Exercise
Determine whether the graph below is Hamiltonian graph or not. If it is, find a Hamilton cycle.
If it not explain why?
Graph coloring
A colouring of a graph is the assignment of a colour to each vertex of the graph so that no two
adjacent vertices are assigned the same colour.

Chromatic number
The minimum numbers of colors required to colour all the vertices of the given graph is called
as chromatic number. It is dented by 𝑥(𝐺).
For example,
𝑥 𝑮𝟏 = 4
𝑥 𝑮𝟐 = 2

𝑮𝟏 𝑮𝟐
Exercise
1. Find the chromatic number of following graph

2 . Consider a graph with 7 vertices and the following edges: {(1, 2), (1, 3), (2, 3), (2, 4), (3, 4),
(4, 5), (5, 6), (5, 7), (6, 7)}. Determine the minimum number of colors required to color the
graph.
Tree
A simple graph is said to be the tree when it is connected and acyclic. (It does not contain any
cycle)
It is denoted by T.
For example,

𝑻𝟏 𝑻𝟐

𝑇1 is a tree and 𝑇2 is not a tree


Tree
Some properties of tree
● There is a unique path between every two nodes in tree
● A tree with n nodes always has exactly 𝑛 − 1 edges.
● The tree doesn’t contain any cycle.
● The degree of all nodes in tree is always greater than 1 except Degree of leaf node is 1.
Note: leaf node is the node which does not have sub vertices.
Example of Trees
Forest
● A disjoint collection of trees is known as forest. Each component of a forest is tree.
● A forest is disconnected, acyclic graph.
For example
Classification of tree
Rooted tree and unrooted tree
a) Rooted tree
● The rooted tree is a tree graph with a designated node called as root of the tree.
● It is the topmost node and serves as the starting point for traversing the tree.
b) Unrooted tree
● The tree where no node is designated as the root node. The edges in unrooted tree connect
nodes without a root node.
For example,

Unrooted tree Rooted tree


Rooted tree
The rooted tree is a tree graph with a designated node called as root of the tree.
It is the topmost node and serves as the starting point for traversing the tree.

For example,
The node / Vertex 1 is called as root of the tree
Terminologies in rooted tree
Parent and Child:
In a rooted tree, each node has a parent except for the root node. The parent node is the
immediate predecessor of a given node, and the child node are the immediate successors of a
given node.
Sibling:
In a tree, sibling nodes are nodes that share the same parent. They are at the same level of the
tree.
Ancestor and Descendant:
In a rooted tree, an ancestor of a given node is any node on the path from the root to that
node. A descendant of a given node is any node that can be reached by following edges from
that node.
Terminologies in rooted tree
Example:
Node 1 is parent for all other nodes.
Node 5 and 11 are children of node 6

Node 2 and node 6 are siblings

The Ancestors of node 11 is node 6, 7 and 1.


The Descendants of node 7 is node 6,5 and 11
Terminologies in rooted tree
Internal Node:
An internal node is a node in a tree that has one or more child nodes. It is any node that is not
a leaf node.
External node.
An internal node is a node in a tree that has no child nodes. It is also called as a leaf node.

For example,
The node D, E and F are external node

The node B, C and A are internal node


Terminologies in rooted tree
M-ary Tree:
An m-ary tree is a rooted tree in which each internal node can have at most m children. The
term "m" refers to the maximum number of children a node can have.
An m-ary tree is full m-ary tree if very internal node has exactly m children.
Binary tree:
A binary tree is a specific type of m-ary tree where each internal node can have at most two
children. In other words, a binary tree is an m-ary tree where m is equal to 2.
For Example,
𝑻𝟏 is binary tree
𝑻𝟐 full 3-ary tree

𝑻𝟏 𝑻𝟐
Terminologies in rooted tree
Subtree
A subtree is a subset of a tree that is also a tree. It consists of a node and all its descendants.

The tree 𝑻𝟐 is a subtree of tree 𝑻𝟏

𝑻𝟏 𝑻𝟐
Exercise
Find the subtrees of following tree
Terminologies in rooted tree
Level`/ Depth
The depth or level of a node is the number of edges from the root to that node. The root node
is at level 0.
Degree:
• Degree of a node is the total number of children of that node.
• Degree of a tree is the highest degree of a node among all the nodes in the tree.

Height:
• Total number of edges that lies on the longest path from any leaf node to a particular node
is called as height of that node.
• Height of a tree is the height of root node.
• Height of all leaf nodes = 0
Terminologies in rooted tree
Example
Exercise
Based on rooted tree,
1. Find the children of node 𝑗, 𝑛, 𝑜, 𝑝
2. Find the ancestors of node 𝑠, 𝑖 𝑎𝑛𝑑 𝑘
3. Find the sibling of node 𝑞, 𝑟 , 𝑠 𝑎𝑛𝑑 𝑡
4. Find the number of leaves in the rooted tree
5. Find the all the binary tree
6. Find the subtree keeping node b as root.
7. Find the height of rooted tree
Tree transversal
Procedure for systematically visiting every vertex of an ordered tree are called transversal
algorithms. There are three standard ways of traversing a rooted tree.
Depth-First Traversal (DFS):
1. Pre-order
2. In-order
3. Post- order
Pre-order
Pre-order
Def: Let 𝑇 be an ordered rooted tree with root 𝑟. If 𝑇 consists only 𝑟, then 𝑟 is the pre-order
transversal of 𝑇. Otherwise, if If 𝑇 consists subtrees of 𝑇1 , 𝑇2 … . . 𝑇𝑛 at 𝑟 from left to right.
Then the pre-order transversal begins by visiting 𝑟 and continues by traversing 𝑇1 , 𝑇2 till 𝑇𝑛
1. Visit the root 𝑟
2. Transverse the left subtree of 𝑟 in pre-order
3. Transverse the right subtree of 𝑟 in pre-order
Pre-order Example
In-order
In-order
Def: Let 𝑇 be an ordered rooted tree with root 𝑟. If 𝑇 consists only 𝑟, then 𝑟 is the in-order
transversal of 𝑇. Otherwise, if If 𝑇 consists subtrees of 𝑇1 , 𝑇2 … . . 𝑇𝑛 at 𝑟 from left to right.
Then the in-order transversal begins by traversing 𝑇1 in order, then traversing 𝑟. It continues
by traversing 𝑇2 in order, till 𝑇𝑛 .

1. Transverse the left subtree of 𝑟 in in-order


2. Visit the root 𝑟
3. Transverse the right subtree of 𝑟 in in-order
In-order Example
Post-order
In-order
Def: Let 𝑇 be an ordered rooted tree with root 𝑟. If 𝑇 consists only 𝑟, then 𝑟 is the post-order
transversal of 𝑇. Otherwise, if If 𝑇 consists subtrees of 𝑇1 , 𝑇2 … . . 𝑇𝑛 at 𝑟 from left to right.
Then the post-order transversal begins by traversing 𝑇1 post order, then traversing 𝑇2 post
order, till 𝑇𝑛 and ends by traversing root 𝑟.

1. Transverse the left subtree of 𝑟 in post-order


2. Transverse the right subtree of 𝑟 in post-order
3. Visit the root 𝑟
Post-order Example
Exercise
Find the in order , pre order and post order transversal for the following tree
Exercise
Find the in order , pre order
and post order transversal for the
following tree
Spanning trees
Let 𝐺 be a simple graph. A spanning tree of 𝐺 is a subgraph of 𝐺 that is a tree containing
every vertex of 𝐺 with minimum number of edges.
Properties of a spanning tree:
• It contains all the vertices of the original graph.
• It is connected, meaning there is a path between any pair of vertices in the spanning tree.
• It is acyclic, meaning there are no cycles in the spanning tree.
• It has |𝑉| − 1 edges, where |𝑉| is the number of vertices in the original graph
• A complete undirected graph has 𝑛𝑛−1 number of different spanning trees possible
Spanning trees
For example,
Exercise
Find the spanning trees for following tree 𝑻𝟏 and 𝑻𝟐

𝑻𝟏 𝑻𝟐
Minimum Spanning tree
A minimum spanning tree (MST) is a spanning tree of a connected, weighted graph where the
sum of the weights of its edges is minimized.
It is a spanning tree that has the minimum possible total weight among all possible spanning
trees of the graph.
For example,
Minimum Spanning tree Algorithms
Minimum spanning tree (MST) algorithms are used to find the minimum cost spanning tree
in a weighted graph.
Two popular algorithms for finding the MST are:

a) Kruskal’s Algorithm
b) Prim's Algorithm
Minimum Spanning tree Algorithms
Kruskal’s Algorithm
Step 1: Remove the loops and multiple edges comparing the weight (Keep the minimum)
Step 2: Arrange the edges in the increasing order of their weights.
Step 3: Select edge which have minimum weight from the list and add it to the spanning tree if
inclusion of the edge does not form a circuit. If selected edge makes a circuit, then remove it
from list.
Step 4: Repeat the step 3 until all the vertices are connected.
Minimum Spanning tree Algorithms
Example
Minimum Spanning tree Algorithms
Find the minimum spanning tree in following graph using Kruskal’s Algorithm
Minimum Spanning tree Algorithms
Prim’s Algorithm

Step 1: Remove the loops and multiple edges comparing the weight (Keep the minimum)

Step 2: Choose any arbitrary vertex

Step 3: while Adding new, select the edges with minimum weight out of the edges from

already visited vertices. (without forming cycled )

Step 4: Repeat the step 3 until all the vertices are included and minimum spanning tree is

obtained
Example
Example
Find the minimum spanning tree using Prim's algorithms for following graphs

You might also like