0% found this document useful (0 votes)
11 views

Graphs Data Structure

Basics about Graph Data Structure

Uploaded by

censoredsongs123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Graphs Data Structure

Basics about Graph Data Structure

Uploaded by

censoredsongs123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Graphs Data Structure

By – Shreevatsa PU and Noor Hafsa


What is Graph data structure?
• A graph is a non-linear data structure that consists of vertices (also
known as nodes) and edges.
• The vertices represent the objects, and the edges represent the
relationships between the objects.
• Graphs are used to represent real-life situations that have a many-to-
many relationship.
Graph terminologies
• Vertex (V): A single node in the graph
• Edge (E): A line connecting two vertices

• A graph G consists of two sets


a) a finite, nonempty set of vertices V(G)
b) a finite, possible empty set of edges E(G)
c) G(V,E) represents a graph
Graph terminologies
• Complete Graph: In a complete graph, every node is connected to
every other node.
• Incomplete Graph: A graph in which not all nodes are connected to
every other node.

• Directed Graph (Digraph): In a directed graph, edges have a direction


and connect two nodes in only one direction.
• Undirected Graph: In an undirected graph, edges do not have a
direction and connect two nodes in both directions.
0 0 0

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)

• If (v0, v1) is an edge in an undirected graph,


• v0 and v1 are adjacent
• The edge (v0, v1) is incident on vertices v0 and v1
• If <v0, v1> is an edge in a directed graph
• v0 is adjacent to v1, and v1 is adjacent from v0
• The edge <v0, v1> is incident on v0 and v1
Contd.
• Restrictions on graphs –
a) A graph may not have an edge from a vertex , v, back to itself. Hence
edges of the form (v,v) or <v,v> are illegal. Hence we obtain self-
edges or self-loops and graphs with such edges are called graphs
with self-edges.
b) A graph may not have multiple occurrences of the same edge.
Hence, we obtain a multigraph.
Contd.

0 2 1 3

1
2

Self edge Multigraph


Contd.
• A subgraph of G is a graph G’ such that V(G’) is a subset of V(G) and
E(G’) is a subset of E(G)
• A path from vertex vp to vertex vq in a graph G, is a sequence of
vertices, vp, vi1, vi2, ..., vin, vq, such that (vp, vi1), (vi1, vi2), ..., (vin, vq)
are edges in an undirected graph
• The length of a path is the number of edges on it
0 0 0 1 2 0

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.

You might also like