Graphs Data Structure
Graphs Data Structure
1 2 1 2
1
3
3 4 5 6
G1 2
G2
G3
Complete graph Incomplete graph
• V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
• V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
• V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>}
Contd.
• For undirected graph with n vertices, the maximum number of edges is n(n-1)/2
• For directed graph with n vertices, the maximum
number of edges is n(n-1)
0 2 1 3
1
2
1 2 1 2 3 1 2
3
3
G1 (i) (ii) (iii) (iv)
0 0 0 0
0
1 1 1
1
2 2
(i) (ii) (iii) (iv)
2
Contd.
• A simple path is a path in which all vertices,
except possibly the first and the last, are distinct
• A cycle is a simple path in which the first and
the last vertices are the same
• In an undirected graph G, two vertices, v0 and v1, are connected if
there is a path in G from v0 to v1
• An undirected graph is connected if, for every
pair of distinct vertices vi, vj, there is a path
from vi to vj
0 0
1 2 1 2
3
3 4 5 6
G1
G2
Tree (Acyclic graph)
Contd.
• A connected component of an undirected graph
is a maximal connected subgraph.
• A tree is a graph that is connected and acyclic.
• A directed graph is strongly connected if there
is a directed path from vi to vj and also
from vj to vi.
• A strongly connected component is a maximal subgraph that is
strongly connected.
H1 0 H2 4
2 1 5
3 6
Not connected
Not strongly connected
0
0 2
1
2
G3
Contd.
• The degree of a vertex is the number of edges incident to that vertex
• For directed graph,
• the in-degree of a vertex v is the number of edges
that have v as the head
• the out-degree of a vertex v is the number of edges
that have v as the tail
• if di is the degree of a vertex i in a graph G with n vertices and e edges, the
number of edges is
n 1
e ( d )/ 2
0
i
3 0
0 2
1 2
3 1 23 3 3
3 4 5 6
3
G1 3 1 1 G2 1 1
0 in:1, out: 1
Directed graph
In-degree
Out-degree 1 in: 1, out: 2
2 in: 1, out: 0
Applications of Graph data structure
• Computer Networks: Graphs are used to model computer networks, including the internet, local area
networks (LANs), and wide area networks (WANs).
• Transportation Networks: Graphs can be used to model transportation networks, such as road
networks, airline routes, and subway systems.
• Social Networks: Graphs are used to model social networks, such as online social networks like
Facebook, LinkedIn, and Twitter.
• Geographic Information Systems (GIS): Graphs can be used to model geographic information, such as
roads, rivers, and other geographical features.
• Graph Algorithms: Graphs are the basis for many algorithms in computer science, including
pathfinding, shortest path, minimum spanning tree, and maximum flow algorithms.