Graph and Their Representation
Graph and Their Representation
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
• 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.