0% found this document useful (0 votes)
4 views51 pages

Chapter 6 - Graph DS

Chapter 6 discusses graph data structures, defining key concepts such as vertices, edges, and types of edges. It explains various graph representations including adjacency matrices, linked lists, and incidence matrices, as well as traversal techniques like Breadth First Search (BFS) and Depth First Search (DFS). The chapter highlights applications and algorithms related to graph traversal, emphasizing their importance in computer science.

Uploaded by

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

Chapter 6 - Graph DS

Chapter 6 discusses graph data structures, defining key concepts such as vertices, edges, and types of edges. It explains various graph representations including adjacency matrices, linked lists, and incidence matrices, as well as traversal techniques like Breadth First Search (BFS) and Depth First Search (DFS). The chapter highlights applications and algorithms related to graph traversal, emphasizing their importance in computer science.

Uploaded by

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

Chapter 6

Graph Data Structure

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.

• Graph is a collection of vertices and arcs in which


vertices are connected with arcs

• Generally, a graph G is represented as G = ( V , E ),


where V is set of vertices and E is set of edges.
2
Introduction
• A Graph G(V, E) with 5 vertices (A, B, C, D, E) and
6 edges ((A,B), (B,C), (C,E), (E,D), (D,B), (D,A)) is
shown in the following figure.

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).

B. Directed Edge - A directed edge is a unidirectional edge.


• If there is directed edge between vertices A and B then edge
(A , B) is not equal to edge (B , A).

C. Weighted Edge - A weighted edge is a edge with value (cost)


on it.

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.

Incident: Edge is said to be incident on a vertex if the vertex


is one of the endpoints of that edge.

Outgoing Edge: A directed edge is said to be outgoing edge


on its origin vertex.

Incoming Edge: A directed edge is said to be incoming edge


on its destination vertex.
7
Graph Terminology
Degree: Total number of edges connected to a vertex is
said to be degree of that vertex.

Indegree: Total number of incoming edges connected to a


vertex is said to be indegree of that vertex.

Outdegree: Total number of outgoing edges connected to


a vertex is said to be outdegree of that vertex.

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.

Simple Graph: A graph is said to be simple if there are no


parallel and self-loop edges.

Cycle: A cycle can be defined as the path which has no


repeated edges or vertices except the first and last vertices.

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.

• There are 3 ways to store Graphs into the computer's


memory:
1. Sequential representation (Adjacency matrix
representation)
2. Linked list representation (Adjacency list
representation)
3. Incidence Matrix

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

• Consider the below-directed graph and try to construct the


adjacency matrix of it.

• In the above graph, we can see there is no self-loop, so the


diagonal entries of the adjacent matrix are 0.
15
Adjacency Matrix for a Weighted Directed Graph
• It is similar to an adjacency matrix representation of a
directed graph except that instead of using the '1' for the
existence of a path, here we have to use the weight
associated with the edge.
• The weights on the graph edges will be represented as the
entries of the adjacency matrix.

16
Directed Graph representation

Undirected 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.

• Adjacency list representation of a directed 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.

• Now, consider the weighted directed graph, and let's see


the adjacency list representation of that 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.

• That means graph with 4 vertices and 6 edges is


represented using a matrix of size 4x6.

• In this matrix, rows represent vertices and columns


represents 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

• For example, consider the following directed graph


representation...

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.

• There are two graph traversal techniques:


1. BFS (Breadth First Search)
2. DFS (Depth First Search)
24
BFS (Breadth First Search)
• BFS is a graph traversal algorithm that starts
traversing the graph from the root node and explores
all the neighboring nodes.

• Then, it selects the nearest node and explores all the


unexplored nodes.

• While using BFS for traversal, any node in the graph


can be considered as the root node.

• There are many ways to traverse the graph, but


among them, BFS is the most commonly used
approach.
25
BFS (Breadth First Search)
• BFS is a recursive algorithm to search all the vertices
of a tree or graph data structure.
• BFS puts every vertex of the graph into two
categories - visited and non-visited.
• BFS selects a single node in a graph and, after that,
visits all the nodes adjacent to the selected node.
• BFS traversal of a graph produces a spanning tree as
final result. Spanning Tree is a graph without loops.
• BFS uses Queue data structure to keep track of the
unvisited nodes.
26
BFS: Application
• For GPS Navigation:
• BFS can be used to find the neighboring locations from a
given source location.
• In a peer-to-peer network:
• BFS can be used as a traversal method to find all the
neighboring nodes.
• In Path finding algorithms:
• BFS is used to determine the shortest path and minimum
spanning tree.
• In most Torrent clients, such as BitTorrent, uTorrent, etc.
employ this process to find "seeds" and "peers" in the
network.

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

• For Cycle detection in an undirected graph.


• BFS can be used to detect cycles in a graph.
• If a node is visited twice during the traversal, it indicates
the presence of a cycle.

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.

• Step 3: Visit all the non-visited adjacent vertices of the vertex


which is at front of the Queue and insert them into the
Queue.
• Step 4: When there is no new vertex to be visited from the
vertex which is at front of the Queue then delete that vertex.

• Step 5: Repeat steps 3 and 4 until queue becomes empty.

• Step 6: When queue becomes empty, then produce final


spanning tree by removing unused edges from the graph.
29
BFS: Algorithm
• BFS algorithm starts at the tree root
and explores all nodes at the present
depth prior to moving on to the
nodes at the next depth level.

• From the above given example, BFS


algorithm traverses from S to A to
B to C first then to D, E and F lastly
to G 30
BFS: Example 1

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.

• The DFS algorithm starts with the initial node of


graph and goes deeper until we find the goal node or
the node with no children.

• DFS uses Stack data structure to keep track of the


unvisited nodes.

• DFS traversal of a graph produces a spanning tree as


final result.

38
DFS: Application
• It can be used to implement the topological sorting
(mainly used for scheduling jobs) .

• It can be used to find the paths between two vertices.

• It can also be used to detect cycles in the graph.

• It is used for one solution puzzles.

• It is used to determine if a graph is bipartite or not.

• It can be used in backtracking algorithms.


39
DFS: Algorithm
• Step 1: Define a Stack of size total number of vertices in the
graph.
• Step 2: Select any vertex as starting point for traversal. Visit
that vertex and push it on to the Stack
• Step 3: Visit any one of the non-visited adjacent vertices of a
vertex which is at the top of stack and push it on to the stack.

• Step 4: Repeat step 3 until there is no new vertex to be visited


from the vertex which is at the top of the stack.
• Step 5: When there is no new vertex to visit then
use backtracking and pop one vertex from the stack.

• Step 6: Repeat steps 3, 4 and 5 until stack becomes Empty.

• Step 7: When stack becomes Empty, then produce final


spanning tree by removing unused edges from the graph. 40
DFS: Algorithm
• DFS algorithm traverses a
graph in a depth ward motion.

• From the above given example,


DFS algorithm traverses from
S to A to D to G to E to B first,
then to F and lastly to C.
41
DFS: Example 1

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

You might also like