3. Graph
Many real problems require the data to be presented in a two dimensional plane.
Graph theory is a branch of mathematics that deals with graphs, networks, and their properties.
Graph theory is used in transportation planning, logistics, routing, and cost analysis.
Finding the shortest or fastest route between two points on the map is certainly one of the most commonly
used applications of graph theory.
Graph theory has many applications in transportation planning, including modeling road networks, selecting
efficient routes, and optimizing traffic flow.
A database of links between web pages is called a “web graph”. These graphs are used by search engines
such as Google, Bing, and Yahoo!.
4. Varying Applications (examples)
Computer networks
Distinguish between two chemical compounds with the same molecular
formula but different structures.
Solve shortest path problems between cities.
Scheduling exams and assign channels to television stations.
5. Definitions - Graph
A generalization of the simple concept of a set of dots, links, edges or arcs.
A graph G is a non linear data structure represented as an ordered pair as G=<V,E>.
A graph G is mathematical structure consisting of two sets V and E where V is non-empty set of
vertices and E is a non-empty set of edges.
Representation:
Graph G = (V, E)
Set of vertices denoted by V, or by V(G) and
Set of edges E, or E(G)
Example: Suppose there is a graph G = (V, E), where
V = {a, b, c, d}, and
E = {(a, b), (a, c), (b, c), (c, d)}.
7. Basic Terminology
1. Trivial Graph: A graph consisting only one vertex and no edge.
Example :
2. Null Graph:- A Graph consisting n vertex and no edge.
8. Definitions – Edge Type
3. Directed: Ordered pair of vertices. Represented as (u, v) directed from vertex u to v.
A set of vertices, which are connected with the directed edges.
In the directed graph, the edges have a direction which is associated with the vertices.
A graph consist the direction of the edges then this is called directed graph.
4. Undirected: Unordered pair of vertices. Represented as {u, v}.
A graph which is not directed then it is called undirected graph.
A set of vertices which are connected together by the undirected edges.
All the edges of this graph are bidirectional.
u v
u v
e1
e2
e3
e1
9. Definitions – Edge Type
5. Loop: A loop is an edge whose endpoints are equal.
i.e., An edge joining a vertex to it self is called a loop. Represented as {u, u} = {u}
6. Multiple Edges: A collection of two or more edges joining the same
pair of vertices.
u
10. Definitions – Graph Type
7. Proper edge:- An edge which is not self loop is called proper edge.
8. Simple Graph: A graph does not contain any self loop and multiage.
Representation Example: G(V, E), V = {u, v, w}, E = {{u, v}, {v, w}, {u, w}}
u v
w
Dig A Dig B
12. Definitions – Graph Type
9. Multigraph: A graph does not contain any self loop but contain multiedge is called multigraph.
G(V,E), consists of set of vertices V, set of Edges E and a function f from E to {{u, v}| u, v V, u ≠ v}.
The edges e1 and e2 are called multiple or parallel edges if f (e1) = f (e2).
Representation Example: V = {u, v, w}, E = {e1, e2, e3}
u
v
w
e1
e2
e3
14. Definitions – Graph Type
10. Pseudograph: A graph contain both self loop and multiedge is called pseudograph.
Representation Example: V = {u, v, w}, E = {e1, e2, e3, e4}
u
v
w
e1
e3
e2
e4
15. Definitions – Graph Type
Example:-
Directed Graph: G(V, E), set of vertices V, and set of Edges E, that are ordered pair of elements of V
(directed edges)
Representation Example: G(V, E), V = {u, v, w}, E = {(u, v), (v, w), (w, u)}
u
w
v
16. Definitions – Graph Type
Example:-
11. Directed Multigraph: G(V,E), consists of set of vertices V, set of Edges E and a
function f from E to {{u, v}| u, v V}. The edges e1 and e2 are multiple edges if f(e1) =
f(e2)
Representation Example: V = {u, v, w}, E = {e1, e2, e3, e4} u
u
u
e1
e2
e3
e4
17. Graph Type
Type Edges
Multiple Edges
Allowed ?
Loops Allowed ?
Simple Graph Undirected No No
Multigraph Undirected Yes No
Pseudograph Undirected Yes Yes
Directed Graph Directed No Yes
Directed
Multigraph
Directed Yes Yes
18. Definitions – Graph Type
12. Incidence and adjacency
Two vertices, u and v in an undirected graph G are called adjacent (or neighbors) in G,
if {u, v} is an edge of G.
An edge e connecting u and v is called incident with vertices u and v, or is said to
connect u and v.
The vertices u and v are called endpoints of edge {u, v}.
Here e1 is incident of V1 and V2 and V1 and V2 are adjacent.
V1
V3 V2
e1
e2
e3
19. Terminology – Directed graphs
13. Infinite Graph:- A graph is said to be infinite if it has an infinite number of vertices as well as an
infinite number of edges.
14. Finite Graph:- A graph with finite vertex set is called a finite graph.
20. Terminology – Directed graphs
Parallel Edge :- If there are more than one edge between two nodes then they are known as parallel
edges.
Not a parallel edges because ()u,v) and (v,u) are two different edges
u
u v
v
u v
21. Definitions – Graph Type
15 Digraph Graph:
A graph G = (V, E) with a mapping f such that every edge maps onto some ordered pair of vertices (Vi, Vj)
are called a Digraph. It is also called Directed Graph.
The ordered pair (Vi, Vj) means an edge between Vi and Vj with an arrow directed from Vi to Vj. Here in
the figure: e1 = (V1, V2) e2 = (V2, V3) e4 = (V2, V4)
22. Definitions – Graph Type
16. Labeled Graph:
If the vertices and edges of a graph are labeled with name, date, or weight then it is
called a labeled graph. It is also called Weighted Graph.
23. Definitions – Graph Type
17. Symmetric Digraph:
A digraph in which each pair of vertices have either one edge or no edges
is known as asymmetric digraph.
18 Asymmetric Digraph
A digraph in which each pair of vertices x, y have either no edges or two
edges <x, y> and <y, x>
24. Definitions – Graph Type
Connected and Disconnected Graph:-
A graph is said to be connected if every pair of vertices in the graph is connected. This means that there
is a path between every pair of vertices.
An undirected graph that is not connected is called disconnected.
25. Terminology – Undirected graphs
Degree of Vertex (deg (v)):
The degree of a vertex in an undirected graph is the number of edges incident with it, except that a loop at a vertex
contributes twice to the degree of that vertex.
Pendant :- A vertex is pendant if and only if it has degree one.
A pendant vertex is adjacent to exactly one other vertex.
Pendant Vertex: deg (v) =1
Isolated Vertex : A vertex of degree zero is called isolated.
Isolated Vertex: deg (v) = 0
Representation Example: For V = {u, v, w} , E = { {u, w}, {u, w}, (u, v) }, deg (u) = 2, deg (v) = 1, deg (w) = 1, deg (k) = 0, w and
v are pendant , k is isolated
u
k
w
v
26. deg( e ) = 0
deg( b ) = 6
Find the degree of all the other vertices. deg( a )= deg( c )= deg( f)= deg( g )=
deg( d ) = 1
Terminology – Undirected graphs
27. Terminology – Directed graphs
For the edge (u, v), u is adjacent to v OR v is adjacent from u, u – Initial vertex, v – Terminal vertex
In-degree (deg- (u)): number of edges for which u is terminal vertex
Out-degree (deg+ (u)): number of edges for which u is initial vertex
Note: A loop contributes 1 to both in-degree and out-degree (why?)
Representation Example: For V = {u, v, w} , E = { (u, w), ( v, w), (u, v) }, deg- (u) = 0,
deg+ (u) = 2, deg- (v) = 1, deg+ (v) = 1, and deg- (w) = 2, deg+ (u) = 0
u
w
v
34. Example 2
2. A simple graph G has 24 edges and degree of each vertex is 4. Find the number of vertices.
Given-
Number of edges = 24
Degree of each vertex = 4
Let number of vertices in the graph = n.
Using Handshaking Theorem, we have-
Sum of degree of all vertices = 2 x Number of edges
Substituting the values, we get-
n x 4 = 2 x 24
n = 2 x 6
∴ n = 12
Thus, Number of vertices in the graph = 12
35. Example 3
A graph contains 21 edges, 3 vertices of
degree 4 and all other vertices of degree 2.
Find total number of vertices.
Solution-
Given-
• Number of edges = 21
• Number of degree 4 vertices = 3
• All other vertices are of degree 2
Let number of vertices in the graph = n.
Using Handshaking Theorem, we have-
Sum of degree of all vertices = 2 x Number of edges
Substituting the values, we get-
3 x 4 + (n-3) x 2 = 2 x 21
12 + 2n – 6 = 42
2n = 42 – 6
2n = 36
∴ n = 18
Thus, Total number of vertices in the graph = 18.
36. Example 4
A graph has 24 edges and degree of each vertex is k, then which of the following is possible number of
vertices?
A. 20 B. 15 C. 10 D. 8
Given-
• Number of edges = 24
• Degree of each vertex = k
Let number of vertices in the graph = n.
Using Handshaking Theorem, we have-
Sum of degree of all vertices = 2 x Number of edges
Substituting the values, we get-
n x k = 2 x 24
k = 48 / n
Now,
• It is obvious that the degree of any vertex must be a
whole number.
• So in the above equation, only those values of ‘n’
are permissible which gives the whole value of ‘k’.
Now, let us check all the options one by one-
• For n = 20, k = 2.4 which is not allowed.
• For n = 15, k = 3.2 which is not allowed.
• For n = 10, k = 4.8 which is not allowed.
• For n = 8, k = 6 which is allowed
37. Simple graphs – special cases
Complete graph: Kn, is the simple graph that contains exactly one edge between each
pair of distinct vertices.
Representation Example: K1, K2, K3, K4
K2
K1
K4
K3
• Each vertex is connected with all the remaining vertices through exactly one edge.
38. Simple graphs – special cases
Cycle: A simple graph of ‘n’ vertices (n>=3) and n edges forming a cycle of length ‘n’ is called as a cycle
graph.
In a cycle graph, all the vertices are of degree 2.
Cn, n ≥ 3 consists of n vertices v1, v2, v3 … vn and edges {v1, v2}, {v2, v3}, {v3, v4} … {vn-1, vn}, {vn, v1}
Representation Example: C3, C4
C3 C4
39. Simple graphs – special cases
Cyclic Graph-
A graph containing at least one cycle in it is called as a cyclic graph.
This graph contains two cycles in it.
Therefore, it is a cyclic graph
Acyclic Graph-
A graph not containing any cycle in it is called as an acyclic graph.
This graph do not contain any cycle in it.
40. Simple graphs – special cases
Wheels: Wn, obtained by adding additional vertex to Cn and connecting all vertices to this new vertex by
new edges.
Representation Example: W3, W4
W3 W4
41. Simple graphs – special cases
N-cubes: Qn, vertices represented by 2n bit strings of length n.
Two vertices are adjacent if and only if the bit strings that they represent differ by exactly one bit
positions
Representation Example: Q1, Q2
0
10
1
00
11
Q1
01
Q2
42. Simple graphs – special cases
Regular Graph:-
A graph in which degree of all the vertices is same is called as a regular graph.
If all the vertices in a graph are of degree ‘k’, then it is called as a “k-regular graph“.
n these graphs,
•All the vertices have degree-2.
•Therefore, they are 2-Regular graphs.
https://www.geeksforgeeks.org/regular-graph-in-graph-theory/?ref=rp
43. Bipartite graphs
A bipartite graph, also called a bigraph, is a set of graph vertices decomposed into two disjoint sets such
that no two graph vertices within the same set are adjacent.
If the vertex set V of a graph G can be partitioned into two non empty disjoint subsets X and Y in such way
that each edge of G has one end in X and one end in Y then G is called Bigraph or bipartite graph.
A bipartite graph is a special kind of graph with the following properties-
• It consists of two sets of vertices X and Y.
• The vertices of set X join only with the vertices of set Y.
• The vertices within the same set do not join.
V
x Y
44. Bipartite graphs
The vertices of the graph can be decomposed into two sets.
The two sets are X = {A, C} and Y = {B, D}.
The vertices of set X join only with the vertices of set Y and vice-versa.
The vertices within the same set do not join.
Therefore, it is a bipartite graph
47. Complete Bipartite graphs
A graph G = (V, E) is called a complete bipartite graph if its vertices V can be partitioned into two subsets
V1 and V2 such that each vertex of V1 is connected to each vertex of V2. The
number of edges in a complete bipartite graph is m.n as each of the m vertices is connected to each of the n
vertices.
Km,n is the graph that has its vertex set portioned into two subsets of m and n vertices, respectively There is an
edge between two vertices if and only if one vertex is in the first subset and the other vertex is in the second
subset.
Representation example: K2,3, K3,3
K2,3 K3,3
A bipartite graph where every vertex of set X is joined
to every vertex of set Y is called as complete bipartite
graph
49. Problem-01:
Is the following graph a bipartite graph?
Sol: The given graph may be redrawn as-
This graph consists of two sets of vertices.
The two sets are X = {1, 4, 6, 7} and Y = {2, 3, 5, 8}.
The vertices of set X are joined only with the vertices of set Y
and vice-versa.
Also, any two vertices within the same set are not joined.
This satisfies the definition of a bipartite graph
50. Bipartite graphs
• In any bipartite graph with bipartition X and Y,
Sum of degree of vertices of set X = Sum of degree of vertices of set Y
Maximum Number Of Edges-
Any bipartite graph consisting of ‘n’ vertices can have at most (1/4) x n2 edges.
Maximum possible number of edges in a bipartite graph on ‘n’ vertices = (1/4) x n2.
51. Problem 2
The maximum number of edges in a bipartite graph on 12 vertices is _________?
Solution-
We know, Maximum possible number of edges in a bipartite graph on ‘n’ vertices = (1/4) x n2.
Substituting n = 12, we get-
Maximum number of edges in a bipartite graph on 12 vertices
= (1/4) x (12)2
= (1/4) x 12 x 12
= 36
Therefore, Maximum number of edges in a bipartite graph on 12 vertices = 36.
52. Subgraphs
When edges and vertices are removed from a graph, without removing endpoints of any remaining edges, a smaller
graph is obtained. Such a graph is called a subgraph of the original graph.
A graph G1 = (V1, E1) is called a subgraph of a graph G(V, E) if V1(G) is a subset of V(G) and E1(G) is a subset of E(G)
such that each edge of G1 has same end vertices as in G.
A subgraph of a graph G = (V, E) is a graph H =(V’, E’) where V’ is a subset of V and E’ is a subset of E
Application example: solving sub-problems within a graph
Representation example: V = {u, v, w}, E = ({u, v}, {v, w}, {w, u}}, H1 , H2
u
v w
u
u
w
v v
H1 H2
G
53. Subgraphs
When edges and vertices are removed from a graph, without removing endpoints of any remaining edges, a smaller
graph is obtained. Such a graph is called a subgraph of the original graph.
A subgraph of a graph G = (V, E) is a graph H =(V’, E’) where V’ is a subset of V and E’ is a subset of E
Application example: solving sub-problems within a graph
Representation example: V = {u, v, w}, E = ({u, v}, {v, w}, {w, u}}, H1 , H2
u
v
w w
v
w
u
G1 G2 G
54. Union and Intersection
Given two graphs G1 and G2, the task is to find the union and intersection of the two given graphs,
i.e. (G1 ∪ G2) and (G1 ∩ G2).
55. Complement of Graph
The complement of a graph G is a graph G’ on the same set of vertices as of G such that there will be an
edge between two vertices (v, e) in G’, if and only if there is no edge in between (v, e) in G.
Complement of graph G(v, e) is denoted by G'(v, e’).
Note: The number of vertices remains unchanged in the complement of the graph.
Graph Complement Graph
56. Complement of Graph
Relation between G and G`:
1. Number of vertices in G = Number of vertices in G’.
|V(G)| = |V(G’)|
2. The sum of total number of edges in G and G’ is equal to the total number of edges in a complete
graph.
|E(G)| + |E(G’)|
= C(n,2)
= n(n-1) / 2
57. Problem
Problem-01:
A simple graph G has 10 vertices and 21 edges. Find total number of edges in its complement graph G’.
Solution-
Given-
Number of edges in graph G, |E(G)| = 21
Number of vertices in graph G, n = 10
We know |E(G)| + |E(G’)| = n(n-1) / 2.
Substituting the values, we get-
21 + |E(G’)| = 10 x (10-1) / 2
|E(G’)| = 45 – 21
∴ |E(G’)| = 24
Thus, Number of edges in complement graph G’ = 24.
58. Representation
Graph representation, means the technique to be used to store some graph into the computer's memory.
There are two ways to store Graphs into the computer's memory:
1. Incidence (Matrix): Most useful when information about edges is more desirable than information
about vertices.
2. Adjacency (Matrix/List): Most useful when information about the vertices is more desirable than
information about the edges.
These two representations are also most popular since information about the vertices is often more
desirable than edges in most applications
59. Representation- Incidence Matrix
Representation of the Undirected Graph:
G = (V, E) be an unditected graph. Suppose that v1, v2, v3, …, vn are the vertices and e1, e2, …, em are the edges of
G. Then the incidence matrix with respect to this ordering of V and E is the n x m matrix
M = [m ij], where Can also be used to represent :
Incidence matrix also be used to represent Multiple edges and loops.
Multiple edges: by using columns with identical entries, because these edges are incident with the same pair
of vertices.
Loops: by using a column with exactly one entry equal to 1, corresponding to the vertex that is incident with
the loop
otherwise
0
ith v
incident w
is
e
edge
when
1
m
i
j
ij
63. Representation- Incidence Matrix
Representation of the directed Graph:
If a directed graph G consists of n vertices and m edges, then the incidence matrix is an n x m
matrix C = [cij] and defined by
The number of ones in an incidence matrix is equal to the number of edges in the graph.
65. Representation- Adjacency Matrix
Representation of the Undirected Graph:
A Adjacency Matrix is a N*N binary matrix in which value of [i,j]th cell is 1 if there exists an edge originating
from ith vertex and terminating to jth vertex, otherwise the value is 0.
There is an N x N matrix, where |V| = N , the Adjacenct Matrix (NxN) A = [aij]
For undirected graph
For directed graph
This makes it easier to find subgraphs, and to reverse graphs if needed.
otherwise
0
G
of
edge
an
is
)
v
,
(v
if
1
a
j
i
ij
otherwise
0
G
of
edge
an
is
}
v
,
{v
if
1
a
j
i
ij
66. Representation- Adjacency Matrix
Adjacency is chosen on the ordering of vertices. Hence, there as are as many as n! such matrices.
The adjacency matrix of simple graphs are symmetric (aij = aji) (why?)
When there are relatively few edges in the graph the adjacency matrix is a sparse matrix
Directed Multigraphs can be represented by using aij = number of edges from vi to vj
70. Example
Consider the directed graph shown in fig. Determine its adjacency matrix MA.
The directed graph G consists of five vertices. Therefore,
the adjacency matrix will be a 5 x 5 matrix.
The adjacency matrix of the directed graphs is as follows:
72. Representation- Adjacency List
Adjacency List specify the vertices that are adjacent to each vertex of the graph.
Each node (vertex) has a list of which nodes (vertex) it is adjacent
Example: undirected graph G (V, E)
node Adjacency List
u v , w
v w, u
w u , v
u
v w
78. Graph - Isomorphism
The simple G1 = (V1, E2) and G2 = (V2, E2) are isomorphic if there is a one-to-one and
onto function f from V1 to V2 with the property that a and b are adjacent in G1 if and only if f
(a) and f (b) are adjacent in G2, for all a and b in V1. Such a function f is called isomorphism.
When two graphs G and G` are said to be isomorphic if there is a one-to-one
correspondence between their vertices and edges such that the adjacency of vertices are
preserved.
Such graphs will have the same structure, differencing only in the way of their vertices and
edges are labeled.
79. Graph - Isomorphism
Criteria for checking two graphs are isomorphic or not:-
1. Both graph have same number of Vertices .
2. Both graph have same number of edges.
3. Both graph have an equal number of vertices with a given degree.
4. one-to-one correspondence between the vertices of two graphs.
5. Edge preserving is also satisfied between two graphs (one-to-one correspondence ).
6. Smallest possible cycle length is same for two graphs.
7. Adjacency matrices of two graphs should be same.
81. Graph - Isomorphism
1. Number of vertices are same
2. Number of edges are same
3. An equal number of vertices with
given degree
4. U1=2
5. U2=3
6. U3 =2
7. U4= 3
8. U5= 2
9. U6=2
V1= 2
V2= 2
V3= 3
V4= 2
V5 =3
V6= 2
85. Representation- Incidence Matrix
e1 e2 e3
a 1 0 0
b 1 1 0
c 0 1 1
d 0 0 1
e 0 0 0
f 0 0 0
e4 e5 e6 e7
0 0 0 1
0 0 0 0
0 1 1 0
1 0 0 0
1 1 0 0
0 0 1 1
a b
c d
e
f
e1
e2
e3
e4
e5
e6
e7
86. Isomorphism - revisited
A isomorphic invariant for simple graphs is the existence of a simple circuit of length k ,
k is an integer > 2 (why ?)
Representation example: G1 and G2 are isomorphic since we have the invariants, similarity in degree of
nodes, number of edges, length of circuits
G1 G2
87. Connectivity
Basic Idea: In a Graph Reachability among vertices by traversing the edges
Application Example:
- In a city to city road-network, if one city can be reached from another city.
- Problems if determining whether a message can be sent between two
computer using intermediate links
- Efficiently planning routes for data delivery in the Internet
88. Connectivity
Walk – A walk is a sequence of vertices and edges of a graph i.e. if we traverse a graph then we get a
walk. .
Note: Vertices and Edges can be repeated.
Open walk- A walk is said to be an open walk if the starting and ending vertices are different i.e. the
origin vertex and terminal vertex are different.
Closed walk- A walk is said to be a closed walk if the starting and ending vertices are identical i.e. if a
walk starts and ends at the same vertex, then it is said to be a closed walk.
1->2->3->4->5->3 is an open walk.
1->2->3->4->5->3->1 is a closed walk.
89. Connectivity
2. Trail –
Trail is an open walk in which no edge is repeated.
Vertex can be repeated
3. Circuit –
Traversing a graph such that not an edge is repeated but vertex can be repeated and it is closed also
i.e. it is a closed trail.
Vertex can be repeated.
Edge can not be repeated.
Here 1->2->4->3->6->8->3->1 is a circuit.
90. Connectivity
4. Path –
It is a trail in which neither vertices nor edges are repeated i.e. if we traverse a graph such that we do not
repeat a vertex and nor we repeat an edge. As path is also a trail, thus it is also an open walk.
Vertex not repeated
Edge not repeated
Here 6->8->3->1->2->4 is a Path
91. Connectivity
5. Cycle –
Traversing a graph such that we do not repeat a vertex nor we repeat a edge but the starting and ending
vertex must be same i.e. we can repeat starting and ending vertex only then we get a cycle.
Vertex not repeated
Edge not repeated
Here 1->2->4->3->1 is a cycle
Cycle is a closed path. These can not have repeat
anything (neither edges nor vertices).
92. Connectivity – Path
A Path is a sequence of edges that begins at a vertex of a graph and travels along edges of the graph,
always connecting pairs of adjacent vertices.
Representation example: G = (V, E), Path P represented, from u to v is {{u, 1}, {1, 4}, {4, 5}, {5, v}}
1
u
3
4 5
2
v
93. Connectivity – Path
Definition for Directed Graphs
A Path of length n (> 0) from u to v in G is a sequence of n edges e1, e2 , e3, …, en of G such that f (e1) = (xo, x1),
f (e2) = (x1, x2), …, f (en) = (xn-1, xn), where x0 = u and xn = v. A path is said to pass through x0, x1, …, xn or traverse
e1, e2 , e3, …, en
For Simple Graphs, sequence is x0, x1, …, xn
In directed multigraphs when it is not necessary to distinguish between their edges, we can use sequence of
vertices to represent the path
Circuit/Cycle: u = v, length of path > 0
Simple Path: does not contain an edge more than once
94. Connectivity – Connectedness
Undirected Graph
An undirected graph is connected if there exists is a simple path between every pair of vertices.
Representation Example: G (V, E) is connected since for V = {v1, v2, v3, v4, v5}, there exists a path between
{vi, vj}, 1 ≤ i, j≤ 5
v1
v2
v3
v5
v4
95. Connectivity – Connectedness
Undirected Graph
Articulation Point (Cut vertex): removal of a vertex produces a subgraph with more connected components
than in the original graph. The removal of a cut vertex from a connected graph produces a graph that is not
connected
Cut Edge: An edge whose removal produces a subgraph with more connected components than in the original
graph.
Representation example: G (V, E), v3 is the articulation point or edge {v2, v3}, the number of connected
components is 2 (> 1)
v1
v2
v3
v4
v5
96. Connectivity – Connectedness
Directed Graph
A directed graph is strongly connected if there is a path from a to b and from b to a whenever a and b
are vertices in the graph
A directed graph is weakly connected if there is a (undirected) path between every two vertices in the
underlying undirected path
A strongly connected Graph can be weakly connected but the vice-versa is not true (why?)
97. Connectivity – Connectedness
Directed Graph
Representation example: G1 (Strong component), G2 (Weak Component), G3 is undirected graph representation of
G2 or G1
G2
G1 G3
98. Connectivity – Connectedness
Directed Graph
Strongly connected Components: subgraphs of a Graph G that are strongly connected
Representation example: G1 is the strongly connected component in G
G1
G
99. Counting Paths
Theorem: Let G be a graph with adjacency matrix A with respect to the ordering v1, v2, …, Vn (with directed on
undirected edges, with multiple edges and loops allowed). The number of different paths of length r from Vi to Vj,
where r is a positive integer, equals the (i, j)th entry of (adjacency matrix) Ar.
Proof: By Mathematical Induction.
Base Case: For the case N = 1, aij =1implies that there is a path of length 1. This is true since this corresponds to an edge between
two vertices.
We assume that theorem is true for N = r and prove the same for N = r +1. Assume that the (i, j)th entry of Ar is the number of
different paths of length r from vi to vj. By induction hypothesis, bik is the number of paths of length r from vi to vk.
100. Counting Paths
Case r +1: In Ar+1 = Ar. A,
The (i, j)th entry in Ar+1 , bi1a1j + bi2 a2j + …+ bin anj
where bik is the (i, j)th entry of Ar.
By induction hypothesis, bik is the number of paths of length r from vi to vk.
The (i, j)th entry in Ar+1 corresponds to the length between i and j and the length is r+1. This path is made up of
length r from vi to vk and of length from vk to vj. By product rule for counting, the number of such paths is bik* akj The
result is bi1a1j + bi2 a2j + …+ bin anj ,the desired result.
101. Counting Paths
a ------- b
| |
| |
c -------d
A = 0 1 1 0 A4 = 8 0 0 8
1 0 0 1 0 8 8 0
1 0 0 1 0 8 8 0
0 1 1 0 8 0 0 8
Number of paths of length 4 from a to d is (1,4) th entry of A4 = 8.
102. The Seven Bridges of Königsberg, Germany
The residents of Königsberg, Germany, wondered if it was possible to take a walking
tour of the town that crossed each of the seven bridges over the Presel river exactly
once. Is it possible to start at some node and take a walk that uses each edge exactly
once, and ends at the starting node?
103. The Seven Bridges of Königsberg, Germany
You can redraw the original picture as long as for every edge between nodes i and j in the original you put an edge
between nodes i and j in the redrawn version (and you put no other edges in the redrawn version).
Original:
2
3
4 1
Redrawn:
4
2 3
104. The Seven Bridges of Königsberg, Germany
Has no tour that uses each edge exactly once.
(Even if we allow the walk to start and finish in different places.)
Can you see why?
Euler:
105. Euler - definitions
Eulerian Path is a path in a graph that visits every edge
exactly once.
Is it possible to draw a given graph without lifting pencil
from the paper and without tracing any of the edges more
than once”.
Eulerian Circuit is an Eulerian Path that starts and ends on
the same vertex.
A graph is called Eulerian if it has an Eulerian Cycle .
A graph is called Semi-Eulerian if it has an Eulerian Path.
108. Eulerian Cycle: An undirected graph has Eulerian cycle if following two conditions are true.
All vertices with non-zero degree are connected. We don’t care about vertices with zero degree because they don’t
belong to Eulerian Cycle or Path (we only consider all edges).
All vertices have even degree.
Eulerian Path: An undirected graph has Eulerian Path if following two conditions are true.
Same as condition (a) for Eulerian Cycle.
If zero or two vertices have odd degree and all other vertices have even degree. Note that only one vertex with odd
degree is not possible in an undirected graph (sum of all degrees is always even in an undirected graph)
Note that a graph with no edges is considered Eulerian because there are no edges to traverse.
109. Euler - definitions
An Eulerian path (Eulerian trail, Euler walk) in a graph is a path that uses each edge precisely once. If
such a path exists, the graph is called traversable.
An Eulerian cycle (Eulerian circuit, Euler tour) in a graph is a cycle that uses each edge precisely once. If
such a cycle exists, the graph is called Eulerian (also unicursal).
Representation example: G1 has Euler path a, c, d, e, b, d, a, b
a b
c d e
110. The problem in our language:
Show that is not Eulerian.
In fact, it contains no Euler trail.
111. Euler - theorems
1. A connected graph G is Eulerian if and only if G is connected and has no vertices of odd degree.
2. A connected graph G is has an Euler trail from node a to some other node b if and only if G is
connected and a b are the only two nodes of odd degree.
112. Euler – theorems (=>)
Assume G has an Euler trail T from node a to node b (a and b not necessarily distinct).
For every node besides a and b, T uses an edge to exit for each edge it uses to enter. Thus, the degree of
the node is even.
1. If a = b, then a also has even degree. Euler circuit
2. If a b, then a and b both have odd degree. Euler path
113. Euler - theorems
1. A connected graph G is Eulerian if and only if G is connected and has no vertices of odd degree
a b
c d
e
f
Building a simple path:
{a,b}, {b,c}, {c,f}, {f,a}
Euler circuit constructed if all edges
are used. True here?
114. Euler - theorems
1. A connected graph G is Eulerian if and only if G is connected and has no vertices of odd degree
c d
e
Delete the simple path:
{a,b}, {b,c}, {c,f}, {f,a}
C is the common vertex for this
sub-graph with its “parent”.
115. Euler - theorems
1. A connected graph G is Eulerian if and only if G is connected and has no vertices of odd degree
c d
e
Constructed subgraph may not be connected.
C is the common vertex for this sub-graph
with its “parent”.
C has even degree.
Start at c and take a walk:
{c,d}, {d,e}, {e,c}
116. Euler - theorems
1. A connected graph G is Eulerian if and only if G is connected and has no vertices of odd degree
a b
c d
e
f
“Splice” the circuits in the 2 graphs:
{a,b}, {b,c}, {c,f}, {f,a}
“+”
{c,d}, {d,e}, {e,c}
“=“
{a,b}, {b,c}, {c,d}, {d,e}, {e,c}, {c,f}
{f,a}
117. Euler Circuit
1. Circuit C := a circuit in G beginning at an arbitrary vertex v.
1. Add edges successively to form a path that returns to this vertex.
2. H := G – above circuit C
3. While H has edges
1. Sub-circuit sc := a circuit that begins at a vertex in H that is also in C (e.g., vertex “c”)
2. H := H – sc (- all isolated vertices)
3. Circuit := circuit C “spliced” with sub-circuit sc
4. Circuit C has the Euler circuit.
118. Homework 1
Write a program to obtain Euler Circuits.
◦ Input graphs can be Eulerian, no need for checking “non” Euler graphs
◦ Include a simple user interface to “input” the graph.
◦ Minimum of 10 edges (no more than 15 edges needed)
◦ Simple documentation
◦ Include a sample graph, if needed, to test
◦ Any programming language
◦ Submission on WebCT
119. Hamiltonian Graph
Hamiltonian path (also called traceable path) is a path that visits each vertex exactly once.
A Hamiltonian cycle (also called Hamiltonian circuit, vertex tour or graph cycle) is a cycle that visits each
vertex exactly once (except for the starting vertex, which is visited once at the start and once again at the
end).
A graph that contains a Hamiltonian path is called a traceable graph. A graph that contains a Hamiltonian
cycle is called a Hamiltonian graph. Any Hamiltonian cycle can be converted to a Hamiltonian path by
removing one of its edges, but a Hamiltonian path can be extended to Hamiltonian cycle only if its
endpoints are adjacent.
120. A graph of the vertices of a dodecahedron.
Is it Hamiltonian?
Yes
.
121. This one has a Hamiltonian path, but not a Hamiltonian tour.
Hamiltonian Graph
123. Hamiltonian Graph
Similar notions may be defined for directed graphs, where edges (arcs) of a path or a cycle are required
to point in the same direction, i.e., connected tail-to-head.
The Hamiltonian cycle problem or Hamiltonian circuit problem in graph theory is to find a Hamiltonian
cycle in a given graph. The Hamiltonian path problem is to find a Hamiltonian path in a given graph.
There is a simple relation between the two problems. The Hamiltonian path problem for graph G is
equivalent to the Hamiltonian cycle problem in a graph H obtained from G by adding a new vertex and
connecting it to all vertices of G.
Both problems are NP-complete. However, certain classes of graphs always contain Hamiltonian paths.
For example, it is known that every tournament has an odd number of Hamiltonian paths.
124. Hamiltonian Graph
DIRAC’S Theorem: if G is a simple graph with n vertices with n ≥ 3 such that the degree
of every vertex in G is at least n/2 then G has a Hamilton circuit.
ORE’S Theorem: if G is a simple graph with n vertices with n ≥ 3 such that deg (u) + deg
(v) ≥ n fro every pair of nonadjacent vertices u and v in G, then G has a Hamilton circuit.
125. Shortest Path
Generalize distance to weighted setting
Digraph G = (V,E) with weight function W: E R (assigning real values to edges)
Weight of path p = v1 v2 … vk is
Shortest path = a path of the minimum weight
Applications
◦ static/dynamic network routing
◦ robot motion planning
◦ map/route generation in traffic
1
1
1
( ) ( , )
k
i i
i
w p w v v
126. Shortest-Path Problems
Shortest-Path problems
◦ Single-source (single-destination). Find a shortest path from a given source (vertex s)
to each of the vertices. The topic of this lecture.
◦ Single-pair. Given two vertices, find a shortest path between them. Solution to single-
source problem solves this problem efficiently, too.
◦ All-pairs. Find shortest-paths for every pair of vertices. Dynamic programming
algorithm.
◦ Unweighted shortest-paths – BFS.
127. Optimal Substructure
Theorem: subpaths of shortest paths are shortest paths
Proof (”cut and paste”)
◦ if some subpath were not the shortest path, one could substitute the shorter subpath and create a shorter total
path
128. Negative Weights and Cycles?
Negative edges are OK, as long as there are no negative weight cycles (otherwise paths
with arbitrary small “lengths” would be possible)
Shortest-paths can have no cycles (otherwise we could improve them by removing
cycles)
◦ Any shortest-path in graph G can be no longer than n – 1 edges, where n is the
number of vertices
129. Shortest Path Tree
The result of the algorithms – a shortest path tree. For each vertex v, it
◦ records a shortest path from the start vertex s to v. v.parent() gives a predecessor of v in this shortest
path
◦ gives a shortest path length from s to v, which is recorded in v.d().
The same pseudo-code assumptions are used.
Vertex ADT with operations:
◦ adjacent():VertexSet
◦ d():int and setd(k:int)
◦ parent():Vertex and setparent(p:Vertex)
130. Relaxation
For each vertex v in the graph, we maintain v.d(), the estimate of the shortest path from s, initialized to
at the start
Relaxing an edge (u,v) means testing whether we can improve the shortest path to v found so far by
going through u
5
u v
v
u
2
2
9
5 7
Relax(u,v)
5
u v
v
u
2
2
6
5 6
Relax(u,v)
Relax (u,v,G)
if v.d() > u.d()+G.w(u,v) then
v.setd(u.d()+G.w(u,v))
v.setparent(u)
131. Dijkstra's Algorithm
Non-negative edge weights
Greedy, similar to Prim's algorithm for MST
Like breadth-first search (if all weights = 1, one can simply use BFS)
Use Q, a priority queue ADT keyed by v.d() (BFS used FIFO queue, here we use a PQ, which is re-organized
whenever some d decreases)
Basic idea
◦ maintain a set S of solved vertices
◦ at each step select "closest" vertex u, add it to S, and relax all edges from u
132. Dijkstra’s ALgorithm
Solution to Single-source(single-destination).
Input: Graph G, start vertex s
relaxing
edges
Dijkstra(G,s)
01 for each vertex u G.V()
02 u.setd()
03 u.setparent(NIL)
04 s.setd(0)
05 S // Set S is used to explain the
algorithm
06 Q.init(G.V()) // Q is a priority queue ADT
07 while not Q.isEmpty()
08 u Q.extractMin()
09 S S {u}
10 for each v u.adjacent() do
11 Relax(u, v, G)
12 Q.modifyKey(v)
133. Dijkstra’s Example
0
s
u v
y
x
10
5
1
2 3
9
4 6
7
2
10
5
0
s
u v
y
x
10
5
1
2 3
9
4 6
7
2
Dijkstra(G,s)
01 for each vertex u G.V()
02 u.setd()
03 u.setparent(NIL)
04 s.setd(0)
05 S
06 Q.init(G.V())
07 while not Q.isEmpty()
08 u Q.extractMin()
09 S S {u}
10 for each v u.adjacent() do
11 Relax(u, v, G)
12 Q.modifyKey(v)
134. Dijkstra’s Example
u v
8 14
5 7
0
s
y
x
10
5
1
2 3
9
4 6
7
2
8 13
5 7
0
s
u v
y
x
10
5
1
2 3
9
4 6
7
2
Dijkstra(G,s)
01 for each vertex u G.V()
02 u.setd()
03 u.setparent(NIL)
04 s.setd(0)
05 S
06 Q.init(G.V())
07 while not Q.isEmpty()
08 u Q.extractMin()
09 S S {u}
10 for each v u.adjacent() do
11 Relax(u, v, G)
12 Q.modifyKey(v)
135. Dijkstra’s Example
8 9
5 7
0
u v
y
x
10
5
1
2 3
9
4 6
7
2
8 9
5 7
0
u v
y
x
10
5
1
2 3
9
4 6
7
2
Dijkstra(G,s)
01 for each vertex u G.V()
02 u.setd()
03 u.setparent(NIL)
04 s.setd(0)
05 S
06 Q.init(G.V())
07 while not Q.isEmpty()
08 u Q.extractMin()
09 S S {u}
10 for each v u.adjacent() do
11 Relax(u, v, G)
12 Q.modifyKey(v)
136. Dijkstra’s Algorithm
O(n2) operations
◦ (n-1) iterations: 1 for each vertex added to the distinguished set S.
◦ (n-1) iterations: for each adjacent vertex of the one added to the distinguished set.
Note: it is single source – single destination algorithm
137. Traveling Salesman Problem
Given a number of cities and the costs of traveling from one to the other, what is the cheapest roundtrip
route that visits each city once and then returns to the starting city?
An equivalent formulation in terms of graph theory is: Find the Hamiltonian cycle with the least weight
in a weighted graph.
It can be shown that the requirement of returning to the starting city does not change the computational
complexity of the problem.
A related problem is the (bottleneck TSP): Find the Hamiltonian cycle in a weighted graph with the
minimal length of the longest edge.
138. Planar Graphs
A graph (or multigraph) G is called planar if G can be drawn in the plane with its edges intersecting only at vertices of G,
such a drawing of G is called an embedding of G in the plane.
A graph which can be drawn in the plane so that its edges do not cross is called planare.
Application Example: VLSI design (overlapping edges requires extra layers), Circuit design (cannot overlap wires on board)
Representation examples: K1,K2,K3,K4 are planar, Kn for n>4 are non-planar
K4
139. Planar Graphs
Region of a Graph: Consider a planar graph G=(V,E).A region is defined to be an area of the plane that is
bounded by edges and cannot be further subdivided. A planar graph divides the plans into one or more
regions. One of these regions will be infinite.
Finite Region: If the area of the region is finite, then that region is called a finite region.
Infinite Region: If the area of the region is infinite, that region is called a infinite region. A planar graph
has only one infinite region.
140. Example
Example: Consider the graph shown in Fig. Determine the number of
regions, finite regions and an infinite region.
Solution: There are five regions in the above graph, i.e. r1,r2,r3,r4,r5.
There are four finite regions in the graph, i.e., r2,r3,r4,r5.
There is only one infinite region, i.e., r1
141. Planar Graphs
Properties of Planar Graphs:
1. If a connected planar graph G has e edges and r regions, then r ≤ e.
2. If a connected planar graph G has e edges, v vertices, and r regions, then v-e+r=2.
3. If a connected planar graph G has e edges and v vertices, then 3v-e≥6.
4. A complete graph Kn is a planar if and only if n<5.
5. A complete bipartite graph Kmn is planar if and only if m<3 or n>3.
144. Theorem : Euler's planar graph theorem
For a connected planar graph or multigraph:
v – e + r = 2
number
of vertices
number
of edges
number
of regions
Planar Graphs
145. Planar Graphs
Example of Euler’s theorem
K4
R1
R2
R3
A planar graph divides the plane
into several regions (faces), one
of them is the infinite region.
v= 4, e= 6, r= 4,
V – e + r = 2
R4
146. Planar Graphs
Proof of Euler’s formula: By Induction
Base Case: for G1 , e1 = 1, v1 = 2 and r1= 1
n+1 Case: Assume, rn = en – vn + 2 is true. Let {an+1, bn+1} be the edge that is added to Gn to obtain Gn+1 and we
prove that rn = en – vn + 2 is true. Can be proved using two cases.
R1
v
u
147. Planar Graphs
Corollary 1: Let G = (V, E) be a connected simple planar graph with |V| = v, |E| = e > 2, and r regions.
Then 3r ≤ 2e and e ≤ 3v – 6
Proof: Since G is loop-free and is not a multigraph, the boundary of each region (including the infinite
region) contains at least three edges. Hence, each region has degree ≥ 3.
Degree of region: No. of edges on its boundary; 1 edge may occur twice on boundary -> contributes 2 to
the region degree.
Each edge occurs exactly twice: either in the same region or in 2 different regions
R
an+1
bn+1
149. Planar Graphs
Each edge occurs exactly twice: either in the same region or in 2 different regions
2e = sum of degree of r regions determined by 2e
2e ≥ 3r. (since each region has a degree of at least 3)
r ≤ (2/3) e
From Euler’s theorem, 2 = v – e + r
2 ≤ v – e + 2e/3
2 ≤ v – e/3
So 6 ≤ 3v – e
or e ≤ 3v – 6
150. Planar Graphs
Corollary 2: Let G = (V, E) be a connected simple planar graph then G has a vertex degree that does not
exceed 5
Proof: If G has one or two vertices the result is true
If G has 3 or more vertices then by Corollary 1, e ≤ 3v – 6
2e ≤ 6v – 12
If the degree of every vertex were at least 6:
by Handshaking theorem: 2e = Sum (deg(v))
2e ≥ 6v. But this contradicts the inequality 2e ≤ 6v – 12
There must be at least one vertex with degree no greater than 5
151. Planar Graphs
Corollary 3: Let G = (V, E) be a connected simple planar graph with v vertices ( v ≥ 3) , e edges, and no
circuits of length 3 then e ≤ 2v -4
Proof: Similar to Corollary 1 except the fact that no circuits of length 3 imply that degree of region must
be at least 4.
152. Planar Graphs
Elementary sub-division: Operation in which a graph are obtained by removing an edge {u, v} and adding
the vertex w and edges {u, w}, {w, v}
Homeomorphic Graphs: Graphs G1 and G2 are termed as homeomorphic if they are obtained by
sequence of elementary sub-divisions.
u v u v
w
153. Planar Graphs
Non-Planar Graph:
A graph is said to be non planar if it cannot be drawn in a plane so that no edge cross.
Ref:- https://www.javatpoint.com/planar-and-non-planar-graphs#:~:text=A%20graph%20is%20said%20to,and%20cannot%20be%20further%20subdivided.
154. Planar Graphs
Kuwratoski’s Theorem: A graph is non-planar if and only if it contains a subgraph homeomorephic to K3,3 or K5
Representation Example: G is Nonplanar
a
b
c
j
d
i
e
g
f
k
b
a
c
e
d
f
g
h
G
H K5
e
d
c
b
a
155. Graph Coloring Problem
Graph coloring is an assignment of "colors", almost always taken to be consecutive integers starting from
1 without loss of generality, to certain objects in a graph. Such objects can be vertices, edges, faces, or a
mixture of the above.
Application examples: scheduling, register allocation in a microprocessor, frequency assignment in
mobile radios, and pattern matching
156. Vertex Coloring Problem
Assignment of colors to the vertices of the graph such that proper coloring takes place (no two adjacent vertices are
assigned the same color)
Chromatic number: least number of colors needed to color the graph
A graph that can be assigned a (proper) k-coloring is k-colorable, and it is k-chromatic if its chromatic number is
exactly k.
157. Vertex Coloring Problem
The problem of finding a minimum coloring of a graph is NP-Hard
The corresponding decision problem (Is there a coloring which uses at most k colors?) is NP-complete
The chromatic number for Cn = 3 (n is odd) or 2 (n is even), Kn = n, Km,n = 2
Cn: cycle with n vertices; Kn: fully connected graph with n vertices; Km,n: complete bipartite graph
C5
K4 K2, 3
C4
158. Vertex Covering Problem
The Four color theorem: the chromatic number of a planar graph is no greater than 4
Example: G1 chromatic number = 3, G2 chromatic number = 4
(Most proofs rely on case by case analysis).
G1 G2