Chapter 6 - Graph DS
Chapter 6 - Graph DS
1
Introduction
• Graph is a non-linear data structure which contains a
set of objects known as nodes (or vertices) and a set of
links known as edges (or Arcs).
• Here edges are used to connect the vertices.
3
Graph Terminology
Vertex
• Individual data element of a graph is called as Vertex.
• Vertex is also known as node.
• In the above example graph, A, B, C, D & E are known as
vertices.
Edge
• An edge is a connecting link between two vertices.
• Edge is also known as Arc.
• An edge is represented as (starting Vertex, ending Vertex).
• For example, in the above graph the link between vertices
A and B is represented as (A,B).
4
Graph Terminology
Types of Edge
A. Undirected Edge - An undirected edge is a bidirectional edge.
• If there is undirected edge between vertices A and B then
edge (A, B) is equal to edge (B , A).
5
Graph Terminology
End vertices or Endpoints
• The two vertices joined by edge are called end vertices
(or endpoints) of that edge.
Origin
• If an edge is directed, its first endpoint is said to be the
origin of the edge.
Destination
• If an edge is directed, its first endpoint is said to be the
origin of the edge and the other endpoint is said to be
the destination of that edge.
6
Graph Terminology
Adjacent: If there is an edge between vertices A and B then
both A and B are said to be adjacent.
• In other words, vertices A and B are said to be adjacent if
there is an edge between them.
8
Graph Terminology
Parallel edges or Multiple edges
• If there are two undirected edges with same end vertices
and two directed edges with same origin and destination,
such edges are called parallel edges or multiple edges.
Path
• A path is a sequence of alternate vertices and edges that
starts at a vertex and ends at other vertex such that each
edge is incident to its predecessor and successor vertex.
9
Graph Terminology
Self-loop: Edge (undirected or directed) is a self-loop if its
two endpoints coincide with each other.
Connected Graph
• A connected graph is the one in which some path exists
between every two vertices (u, v) in V.
• There are no isolated nodes in connected graph.
10
Graph representation
• Graph representation means the technique to be used to
store some graph into the computer's memory.
11
1. Sequential representation
• An entry Aij in the adjacency matrix representation of an
undirected graph G will be 1 if an edge exists between
Vi and Vj.
• If an Undirected Graph G consists of n vertices, then the
adjacency matrix for that graph is n x n, and the matrix A
= [Aij] can be defined as -
Aij = 1 {if there is a path exists from Vi to Vj}
Aij = 0 {Otherwise}
• It means that, in an adjacency matrix, 0 represents that
there is no association exists between the nodes, whereas 1
represents the existence of a path between two Vertices.
• If there is no self-loop present in the graph, it means that
the diagonal entries of the adjacency matrix will be 0.
12
1. Sequential representation
• In sequential representation, there is a use of an
adjacency matrix to represent the mapping between
vertices and edges of the graph.
• We can use an adjacency matrix to represent the
undirected graph, directed graph, weighted directed
graph, and weighted undirected graph.
13
Adjacency Matrix for a Directed Graph
• There exist different adjacency matrices for the
directed and undirected graph.
• In a directed graph, an entry Aij will be 1 only when
there is an edge directed from Vi to Vj.
• In a directed graph, edges represent a specific path
from one vertex to another vertex.
• Suppose a path exists from vertex A to another vertex
B; it means that node A is the initial node, while node
B is the terminal node.
14
Adjacency Matrix for a Directed Graph
16
Directed Graph representation
17
2. Linked list representation
• An adjacency list is used in the linked representation
to store the Graph in the computer's memory.
• It is efficient in terms of storage as we only have to
store the values for edges.
• An adjacency list is maintained for each node present
in the graph, which stores the node value and a
pointer to the next adjacent node to the respective
node.
• In this representation, every vertex of a graph contains list
of its adjacent vertices.
• If all the adjacent nodes are traversed, then store the
NULL in the pointer field of the last node of the list.
18
2. Linked list representation
• Adjacency list representation of an undirected graph.
19
2. Linked list representation
• For a directed graph, the sum of the lengths of adjacency
lists is equal to the number of edges present in the graph.
20
3. Incidence Matrix
• In this representation, the graph is represented using
a matrix of size total number of vertices by a total
number of edges.
21
3. Incidence Matrix
• This matrix is filled with 0 or 1 or -1.
• Here,
• 0 represents that the row edge is not connected to
column vertex,
• 1 represents that the row edge is connected as the
outgoing edge to column vertex and
• -1 represents that the row edge is connected as the
incoming edge to column vertex.
22
3. Incidence Matrix
23
Graph Traversal
• Graph traversal is a technique used for searching a
vertex in a graph.
• It is also used to decide the order of vertices is visited
in the search process.
• It finds the edges to be used in the search process
without creating loops.
• That means using graph traversal we visit all the
vertices of the graph without getting into looping
path.
27
BFS: Application
• In web crawlers:
• BFS can be used to create web page indexes.
• It is one of the main algorithms that can be used to index
web pages.
• In Ford-Fulkerson algorithm:
• BFS can be used to find maximum flow in a network
28
BFS: Algorithm
• Step 1: Define a Queue of size total number of vertices in the
graph.
• Step 2: Select any vertex as starting point for traversal. Visit
that vertex and insert it into the Queue.
31
32
33
34
35
BFS: Example 1
Final output:
36
BFS: Example 2
• In the example given below, there is a directed graph
having 7 vertices.
37
DFS (Depth First Search)
• DFS is a recursive algorithm to search all the vertices
of a tree data structure or a graph.
38
DFS: Application
• It can be used to implement the topological sorting
(mainly used for scheduling jobs) .
42
43
44
45
46
47
48
49
DFS: Example 1
Final Output
50
DFS: Example 2
• In the example given below, there is a directed graph
having 8 vertices.
51