A graph is a collection of two sets V and E where V is a finite non‒empty set of vertices and E is a finite non‒empty set of edges.
Data Structures using
C PlusPlus
Chapter 7: Graphs
Introduction
to Graphs
A
graph is a collection of two sets V and E where V is a finite non‒empty set of
vertices and E is a finite non‒empty set of edges.
• Vertices are nothing but the nodes in
the graph.
•
Two adjacent vertices are joined by edges.
•
Any graph is denoted as G = (V, E}.
•
For example :

G
= {{V1, V2, V3 ,V4, V5,
V6},
{E1,
E2, E3, E4, E5, E6, E7
}}

1.
Graph is a non‒linear data structure.
2.
It is a collection of vertices/nodes and edges.
3.
Each node can have any number of edges.
4.
There is no unique node called root in graph.
5.
A cycle can be formed.
6.
Applications : For finding shortest path in networking graph is used
1.
Tree is a non‒linear data structure.
2.
It is a collection of nodes and edges.
3.
General trees consist of the nodes having any number of child nodes. But in
case of binary trees every node can have at the most two child nodes.
4.
There is a unique node called root in trees.
5. There will not be any cycle.
6.
Applications : For game trees, decision trees, the tree is used.
•
Basically graphs are of two types –
1.
Directed graphs
2. Undirected graphs.
•
In the directed graph the directions are shown on the edges. As shown in the Fig. 7.1.2, the edges
between the vertices are ordered. In this type of graph, the edge E1
is in between the vertices V1 and V2. The V1
is called head and the V2 is called the tail. Similarly for V1
head the tail is V3 and so on.

•
We can say E1 is the set of (V1, V2) and not
of (V2, V1). ́ ́
•
Similarly, in an undirected graph, the edges are not ordered. Refer the Fig.
7.1.3 for clear understanding of undirected graph. In this type of graph the
edge E1 is set of (V1, V2) or (V2,
V1).

•
Similarly the object shown in the Fig. 7.1.4 is a multi‒graph.

If
an undirected graph of n vertices consists of n(n‒1)/2
number of edges then that graph is
called a complete graph.

For example :
The graph shown in Fig 7.1.5 is a complete graph.
Here
n = 4
e = n(n‒1) / 2 = 4(3) / 2 = 6

Theorem :
A complete graph with n vertices has n(n‒1) / 2 number of edges.
Proof :
This can be proved by principle of mathematical induction.
Basis of induction :
This theorem is true for n = 1 i.e. a graph with single node because when n =
1, total number of edges = 1(1‒1)/2 = 0.
For instance :

•
Induction hypothesis : We assume
that for k vertices it is true that total number of edges = k(k‒1) / 2
•
Inductive step : We have to show
that the theorem is also true for (n + 1) vertices.
•
Let, G(n) be the graph with n vertices. If we add one more vertex to G(n) then
to make this graph complete, we need to add edges to this newly added vertex from
all the previous n vertices then total number of edges in G (n + 1) will be
Edges = Edges in G(n) + n = [ n(n‒1) / 2 ] + n = [ n(n‒1)+2n ] / 2
=
(n2‒n+2n) / 2 = (n2+n)
/ 2

Total
edges in G(n + 1) = n(n+1) / 2
If
we put n + 1= k then n = k ‒ 1. Hence we can rewrite equation (7.1.1) as
(k‒1)k / 2
i.e. k(k−1) / 2

This is true by induction hypothesis. Hence
total number of edges in G (n+1) graph is

[n(n‒1)
/ 2 ]+ n = [n(n+1)] / 2 is true.
•
Thus it is proved by principle of mathematical induction that total number of edges
in complete graph is n(n‒1) / 2
A
subgraph G' of graph G is a graph such that the set of vertices and set of
edges of G' are proper subset of the set of edges of G.

An
undirected graph is said to be connected if for every pair of distinct vertices
Vi and Vj in V(G) there is an edge Vi to to Vj
in G.

Note
that there is path from any two vertices.
For example:
•
V1 ‒ V2
•
V2
‒ V1
•
V3 – V1
•
V4 ‒ V3 ‒ V1
•
V1 ‒ V3
•
V2 ‒ V1 ‒ V3
•
V3 ‒ V1 ‒ V2
•
V4 – V2
•
V1 ‒ V4
•
V2 ‒ V4
•
V3 ‒ V4
•
V4 ‒ V3
Weighted graph
A
weighted graph is a graph which consists of weights along its edges.
For example :

Path:
A
path is denoted using sequence of vertices and there exists an edge from one
vertex to the next vertex.

Cycle :
A
closed walk through the graph with repeated vertices, mostly having the same
starting and ending vertex is called a cycle.

Component :
The
maximal connected subgraph of a graph is called component of a graph.
For example :
Following are 3 components of a graph.

•
The degree of vertex is the number of edges associated with the vertex.
•
In‒degree of a vertex is the number of edges that incident to that vertex. Out‒degree
of the vertex is total number of edges that are going away from the vertex.

Self loop :
Self
loop is an edge that connects the same vertex, to itself.

Data Structures using C PlusPlus: Chapter 7: Graphs : Tag: Data Structure, C++ Programing : Data Structures using C++ Program - Introduction to Graphs
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Electron Devices
EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Circuits and Network Analysis
EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Drawing - Laboratory
ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus - Laboratory
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Devices and Circuits Laboratory
EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation