Graph

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

5.

2 GRAPH
GRAPH

• A graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also
referred to as vertices. The edges are lines or arcs that connect any two nodes in the graph. The graph
consists of a finite set of vertices(or nodes) and set of edges which connect a pair of nodes
• In the graph, the
set of vertices are V = {0,1,2,3,4} , and the
set set of edges are E = {01, 12, 23, 34, 04, 14, 13}.
APPLICATION OF GRAPH

• Graph are used to solve many real-life problems.


1. It is used to represent networks. The networks may include paths in a city or telephone network or
circuit network.
2. It is used in social networks like LinkedIn, Facebook. For example, on Facebook, each person is
represented with a vertex(or node). Each node is a structure and contains information like person id,
name, gender, locale, etc.
GRAPH AND ITS REPRESENTATIONS

• A graph is a data structure that consists of the following components:


1. A finite set of vertices also called nodes.
2. A finite set of ordered pairs of the form (u, v) called the edge.
• The pair is ordered because (u, v) is not the same as (v, u) in case of a directed graph (digraph).
• The pair of the form (u, v) indicates that there is an edge from vertex u to vertex v.
• The edges may contain weight/value/cost.
GRAPH DATA STRUCTURE
• A graph is a pictorial representation of a set of objects where some pairs of objects are connected by
links. The interconnected objects are represented by points termed as vertices, and the links that connect
the vertices are called edges.
• Formally, a graph is a pair of sets (V, E), where Vis the set of vertices and Eis the set of edges,
connecting the pairs of vertices.
GRAPH DATA STRUCTURE IMPORTANT TERMS

Graph Data Structure Basic Operations:


•Add Vertex. Adds a vertex to the graph.
•Add Edge. Adds an edge between the two vertices of the graph.
•Display Vertex. Displays a vertex of the graph.

Graph Representations
1.Adjacency Matrix
2.Adjacency List
ADJACENCY MATRIX

Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D
array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j. The
adjacency matrix for the undirected graph is always symmetric. Adjacency Matrix is also used to represent
weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with weight w.
Pros and Cons of Adjacency Matrix

Pros
• Representation is easier to implement and follow.
• Removing an edge takes O(1) time.
• Queries like whether there is an edge from vertex ‘u’ to vertex ‘v’ are efficient
and can be done O(1).
Cons
• Consumes more space O(V^2).
• Even if the graph is sparse(contains less number of edges), it consumes the
same space.
• Adding a vertex is O(V^2) time.
ADJACENCY LIST
An array of lists is used in the adjacency list type of graph representation. The size of the array is
equal to the number of vertices.
Let the array be an array[].
• An entry array[i] represents the list of vertices adjacent to theith vertex.
• The representation can also be used to represent a weighted graph.
• The weights of edges can be represented as lists of pairs.
BREADTH FIRST SEARCH OR BFS FOR A
GRAPH

• The Breadth First Search (BFS) algorithm is used to search a graph data structure for a node that
meets a set of criteria. It starts at the root of the graph and visits all nodes at the current depth level
before moving on to the nodes at the next depth level.

• Breadth-First Traversal (or Search) for a graph is similar to the Breadth-First Traversal of a tree. The
only catch here is, that, unlike trees, graphs may contain cycles, so we may come to the same node
again. To avoid processing a node more than once, we divide the vertices into two categories:
• Visited and
• Unvisited

• A boolean visited array is used to mark the visited vertices. For simplicity, it is assumed that all vertices
are reachable from the starting vertex. BFS uses a queue data structure for traversal.
HOW DOES BFS WORK?

• Starting from the root, all the nodes at a particular level are visited first and then the nodes of the next
level are traversed till all the nodes are visited.
• To do this a queue is used. All the adjacent unvisited nodes of the current level are pushed into the queue
and the nodes of the current level are marked visited and popped from the queue.
Step1: Initially queue and visited arrays are empty.

Queue and visited arrays are empty initially.


Step2: Push node 0 into queue and mark it visited.

Push node 0 into queue and mark it visited.


Step 3: Remove node 0 from the front of queue and visit the unvisited
neighbours and push them into queue.

Remove node 0 from the front of queue and visited the unvisited
neighbours and push into queue.
Step 4: Remove node 1 from the front of queue and visit the
unvisited neighbours and push them into queue.

Remove node 1 from the front of queue and visited the unvisited
neighbours and push
Step 5: Remove node 2 from the front of queue and visit the unvisited
neighbours and push them into queue

Remove node 2 from the front of queue and visit the unvisited neighbours and
push them into queue.
Step 6: Remove node 3 from the front of queue and visit the unvisited neighbours and
push them into queue.
As we can see that every neighbours of node 3 is visited, so move to the next node that
are in the front of the queue.

Remove node 3 from the front of queue and visit the unvisited neighbours and push them into
queue.
Steps 7: Remove node 4 from the front of queue and visit the unvisited neighbours and push them
into queue.
As we can see that every neighbours of node 4 are visited, so move to the next node that is in the
front of the queue.

Remove node 4 from the front of queue and visit the unvisited neighbours and push them into
queue.

Now, Queue becomes empty, So, terminate these process of iteration.


DEPTH FIRST SEARCH OR DFS FOR A
GRAPH

• Depth First Traversal (or DFS) for a graph is similar to


Depth First Traversal of a tree. The only catch here is, that, unlike trees, graphs
may contain cycles (a node may be visited twice). To avoid processing a node
more than once, use a boolean visited array. A graph can have more than one DFS
traversal.
HOW DOES DFS WORK?

• Depth-first search is an algorithm for traversing or searching tree or graph data


structures. The algorithm starts at the root node (selecting some arbitrary node
as the root node in the case of a graph) and explores as far as possible along
each branch before backtracking.
Step1: Initially stack and visited arrays are empty.
Step 2: Visit 0 and put its adjacent nodes which are not visited yet
into the stack
Step 3: Now, Node 1 at the top of the stack, so visit node 1 and pop it from the stack and put
all of its adjacent nodes which are not visited in the stack.
Step 4: Now, Node 2 at the top of the stack, so visit node 2 and pop it from the stack and put all of its
adjacent nodes which are not visited (i.e, 3, 4) in the stack.
Step 5: Now, Node 4 at the top of the stack, so visit node 4 and pop it from the stack
and put all of its adjacent nodes which are not visited in the stack.
Step 6: Now, Node 3 at the top of the stack, so visit node 3 and pop it from the stack
and put all of its adjacent nodes which are not visited in the stack.

Now, Stack becomes empty, which means we have visited all the nodes and our DFS traversal
ends.

You might also like