0% found this document useful (0 votes)
22 views34 pages

Graph and Their Representation

Uploaded by

pooja
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)
22 views34 pages

Graph and Their Representation

Uploaded by

pooja
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/ 34

Graph and their Representation

Matrix reprentation of grapgh


• A graph can be thought of as a data structure that is used to describe
relationships between entities. An entity can be any item that has a
distinctive and independent existence. It could either be an actual physical
object or an abstract idea. For example, an entity can be a person, place or
an organization about which data can be stored.
• In the computing world, graphs have become ubiquitous owing to their
ability to not only provide abstractions to real life but also demonstrate
complicated relationships with ease. As such, a variety of practical
problems can be represented as graphs. For example, a linked structure of
websites can be viewed as a graph.
• Every graph is a set of points referred to as vertices or
nodes which are connected using lines called edges. The
vertices represent entities in a graph. Edges, on the other hand,
express relationships between entities. Hence, while nodes
model entities, edges model relationships in a network graph.
A graph G with a set of V vertices together with a set
of E edges is represented as G= (V, E). Both vertices and
edges can have additional attributes that are used to describe
the entities and relationships. Figure 1 depicts a simple graph
with five nodes and six edges.
• Graphs can broadly be categorized into Undirected (Fig 2a)
or Directed (Fig 2b). An undirected graph is directionless. This means that
the edges have no directions.
• In other words, the relationship is mutual. For example, a Facebook or a
LinkedIn connection. Contrarily, edges of directed graphs have directions
associated with them.
• An asymmetric relationship between a boss and an employee or a teacher
and a student can be represented as a directed graph in data structure.

To understand how an undirected graph can be represented using an
adjacency matrix, consider a small undirected graph with five vertices
(Fig 4). Here, A is connected to B, but B is connected to A as well.
Hence, both the cells i.e., the one with source A destination B and the
other one with source B destination A are marked one. This suffices the
requirement of an undirected edge. Observe that the second entry is at a
mirrored location across the main diagonal.
Matrix of undirected graph
Undirected weighted graph represenation
Representing Graphs

• A graph can be represented using 3 data


structures- adjacency matrix, adjacency list and adjacency
set.
• An adjacency matrix can be thought of as a table with rows
and columns. The row labels and column labels represent the
nodes of a graph.
• An adjacency matrix is a square matrix where the number of
rows, columns and nodes are the same. Each cell of the
matrix represents an edge or the relationship between two
given nodes. For example, adjacency matrix Aij represents the
number of links from i to j, given two nodes i and j.
A B C D E
A 0 0 0 0 1
B 0 0 1 0 0
C 0 1 0 0 1
D 1 0 0 1 1
E 0 1 1 0 0
The adjacency matrix for a directed graph is shown in Fig 3. Observe that it is a square
matrix in which the number of rows, columns and nodes remain the same (5 in this
case).
Each row and column correspond to a node or a vertex of a graph. The cells within the
matrix represent the connection that exists between nodes.
Since, in the given directed graph, no node is connected to itself, all cells lying on the
diagonal of the matrix are marked zero.

F or the rest of the cells, if there exists a directed edge from a given node to another,
then the corresponding cell will be marked one else zero.
To understand how an undirected graph can be represented using an
adjacency matrix, consider a small undirected graph with five vertices
(Fig 4). Here, A is connected to B, but B is connected to A as well. Hence,
both the cells i.e., the one with source A destination B and the other one
with source B destination A are marked one. This suffices the
requirement of an undirected edge. Observe that the second entry is at
a mirrored location across the main diagonal.
In case of a weighted graph, the cells are marked with edge weights
instead of ones. In Fig 5, the weight assigned to the edge connecting
nodes B and D is 3. Hence, the corresponding cells in the adjacency
matrix i.e. row B column D and row D column B are marked 3.
adjacency list
• In adjacency list representation of a graph, every vertex is
represented as a node object. The node may either contain
data or a reference to a linked list.
• This linked list provides a list of all nodes that are adjacent to
the current node. Consider a graph containing an edge
connecting node A and node B.
• Then, the node A will be available in node B’s linked list. Fig 6
shows a sample graph of 5 nodes and its corresponding
adjacency list.
Note that the list corresponding to node E is empty while lists corresponding
to nodes B and D have 2 entries each.
Similarly, adjacency lists for an undirected graph can also be constructed. Fig
7 provides an example of an undirected graph along with its adjacency list
for better understanding.
Adjacency list enables faster search process in comparison to adjacency
matrix. However, it is not the best representation of graphs especially
when it comes to adding or removing nodes. For example, deleting a
node would involve looking through all the adjacency lists to remove a
particular node from all lists. NetworkX library provides a
function adjacency_list () to generate the adjacency list of a given graph.
The following code demonstrates its use.
Adjacency set

The adjacency set mitigates a few of the challenges posed by adjacency


list. Adjacency set is quite similar to adjacency list except for the difference
that instead of a linked list; a set of adjacent vertices is provided.
Adjacency list and set are often used for sparse graphs with few
connections between nodes. Contrarily, adjacency matrix works well for
well-connected graphs comprising many nodes.
Breadth First Search
• Breadth-first search 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. It 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. It selects a single node in a graph and, after that, visits all
the nodes adjacent to the selected node.
Algorithm

Step 1: SET STATUS = 1 (ready state) for each node in G


Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting
state)
Step 3: Repeat Steps 4 and 5 until QUEUE is empty
Step 4: Dequeue a node N. Process it and set its STATUS = 3
(processed state).
Step 5: Enqueue all the neighbours of N that are in the ready state
(whose STATUS = 1) and set
their STATUS = 2
(waiting state)
[END OF LOOP]
Example

Now, let's understand the working of BFS algorithm by using an


example. In the example given below, there is a directed graph
having 7 vertices.
In the above graph, minimum path 'P' can be found by using the BFS that
will start from Node A and end at Node E. The algorithm uses two
queues, namely QUEUE1 and QUEUE2. QUEUE1 holds all the nodes
that are to be processed, while QUEUE2 holds all the nodes that are
processed and deleted from QUEUE1.

Complexity of BFS algorithm

Time complexity of BFS depends upon the data structure used to


represent the graph. The time complexity of BFS algorithm
is O(V+E), since in the worst case, BFS algorithm explores every
node and edge. In a graph, the number of vertices is O(V), whereas
the number of edges is O(E).
The space complexity of BFS can be expressed as O(V), where V
is the number of vertices.
Depth First Search

• DFS is a recursive traversal algorithm for searching all the vertices of a graph or
tree data structure. It starts from the first node of graph G and then goes to further
vertices until the goal vertex is reached.
• DFS uses stack as its backend data structure
• edges that lead to an unvisited node are called discovery edges while the edges
that lead to an already visited node are called block edges.
• Depth First Search Algorithm
• Step 1: STATUS = 1 for each node in Graph G
• Step 2: Push the starting node A in the stack. set its STATUS = 2
• Step 3: Repeat Steps 4 and 5 until STACK is empty
• Step 4: Pop the top node N from the stack. Process it and set its STATUS = 3
• Step 5: Push all the neighbors of N with STATUS =1 into the stack and set their
STATUS = 2
[END OF LOOP]
• Step 6: stop
Working of Depth First Search

Starting node: A
Step 1: Create an adjacency list for the
above graph.
Step 2: Create an empty stack.
Step 3: Push ‘A’ into the stack
Step 4: Pop ‘A’ and push ‘B’ and ‘F’. Mark node ‘A’ as the visited
node
Step 5: pop ‘F’ and push ‘D’. Mark ‘F’ as a visited node.
Step 6: pop ‘D’ and push ‘C’. Mark ‘D’ as a visited node.
Step 7: pop ‘C’ and push ‘E’. Mark ‘C’ as a visited node.
Step 8: pop ‘E’. Mark ‘E’ as a visited node. No new node is left.
Step 9: pop ‘B’. Mark ‘B’ as visited. All the nodes in the graph are visited
now.
BFS stands for Breadth DFS stands for Depth First
First Search. Search.
It a vertex-based It is an edge-based technique
technique to find the because the vertices along
shortest path in a graph. the edge are explored first
from the starting to the end
node.
BFS is a traversal DFS is also a traversal
technique in which all the technique in which traversal
nodes of the same level is started from the root node
are explored first, and and explore the nodes as far
then we move to the next as possible until we reach
level. the node that has no
unvisited adjacent nodes.

Queue data structure is Stack data structure is used


used for the BFS traversal. for the BFS traversal.
BFS is slower than DFS. DFS is faster than BFS.
It is not memory efficient It is memory efficient as it
as it requires more requires less memory than
memory than DFS. BFS.

You might also like