0% found this document useful (0 votes)
13 views

UNIT 4 (IQ)

Data structure lab

Uploaded by

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

UNIT 4 (IQ)

Data structure lab

Uploaded by

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

UNIT 4

1. Define the graph and list the representation of graphs.

 A graph is a non-linear kind of data structure made up of nodes or vertices and


edges. The edges connect any two nodes in the graph, and the nodes are also
known as vertices.

 This graph has a set of vertices V= { 1,2,3,4,5} and a set of edges E= { (1,2),(1,3),
(2,3),(2,4),(2,5),(3,5),(4,50 }.

Representation of graphs:

 Adjacency Matrix
 Incidence Matrix
 Adjacency List

2. Find the graph and give the explanation of that graph.

 This graph is complete graph.

 A complete graph is a graph in which each vertex is connected to every other vertex. That
is, a complete graph is an undirected graph where every pair of distinct vertices is
connected by a unique edge.
3. Compare the BFS and DFS.

Parameter
s BFS DFS

BFS stands for Breadth DFS stands for Depth First


Stands for First Search. Search.

BFS is a traversal DFS is also a traversal approach


approach in which we in which the traverse begins at
first walk through all the root node and proceeds
nodes on the same level through the nodes as far as
before moving on to the possible until we reach the node
Definition next level. with no unvisited nearby nodes.

It works on the concept


It works on the concept of LIFO
Approach of FIFO (First In First
(Last In First Out).
used Out).

4. Outline about the Bi-connectivity.

Bi-connected graphs are the graphs which cannot be broken into two
disconnected pieces (graphs) by connecting single edge.

For example:
5. Infer Euler circuits with neat diagram.

Euler Circuit: A closed loop within a graph that visits each edge
exactly once and returns to the starting point.

16 marks:

1. Explain the graph types and graph representation with


suitable examples and neat diagram.

A graph is a non-linear kind of data structure made up of nodes or vertices and edges. The edges
connect any two nodes in the graph, and the nodes are also known as vertices.

This graph has a set of vertices V= { 1,2,3,4,5} and a set of edges E= { (1,2),(1,3),(2,3),(2,4),
(2,5),(3,5),(4,50 }.
Types of graphs:

 Directed Graph,

 Non-directed Graph,

 Null Graph,

 Simple Graph,

 Trivial Graph,

 Complete Graph,

 Cycle Graph,

 Acyclic Graph,

 Connected Graph,

 Disconnected Graph,

 Regular Graph,

 Finite Graph,

 Infinite Graph,

 Pseudo Graph,

 Bipartite

REPRESENTATION OF GRAPH :

In graph theory, a graph representation is a technique to store graph into the memory of
computer.

To represent a graph, we just need the set of vertices, and for each vertex the neighbors of the
vertex (vertices which is directly connected to it by an edge). If it is a weighted graph, then the
weight will be associated with each edge.

There are different ways to optimally represent a graph, depending on the density of its edges,
type of operations to be performed and ease of use.
1. Adjacency Matrix

o Adjacency matrix is a sequential representation.


o It is used to represent which nodes are adjacent to each other. i.e. is there any edge
connecting nodes to a graph.
o In this representation, we have to construct a nXn matrix A. If there is any edge from a
vertex i to vertex j, then the corresponding element of A, ai,j = 1, otherwise ai,j= 0.

Note, even if the graph on 100 vertices contains only 1 edge, we still have to have a 100x100
matrix with lots of zeroes.
o If there is any weighted graph then instead of 1s and 0s, we can store the weight of the
edge.

Example

Consider the following undirected graph representation:

Undirected graph representation

Directed graph represenation

See the directed graph representation:


In the above examples, 1 represents an edge from row vertex to column vertex, and 0 represents
no edge from row vertex to column vertex.

Undirected weighted graph represenation

Pros: Representation is easier to implement and follow.

Cons: It takes a lot of space and time to visit all the neighbors of a vertex, we have to traverse all
the vertices in the graph, which takes quite some time.

2. Incidence Matrix

In Incidence matrix representation, graph can be represented using a matrix of size:

Total number of vertices by total number of edges.


It means if a graph has 4 vertices and 6 edges, then it can be represented using a matrix of 4X6
class. In this matrix, columns represent edges and rows represent vertices.

This matrix is filled with either 0 or 1 or -1. Where,

o 0 is used to represent row edge which is not connected to column vertex.


o 1 is used to represent row edge which is connected as outgoing edge to column vertex.
o -1 is used to represent row edge which is connected as incoming edge to column vertex.

Example

Consider the following directed graph representation.

3. Adjacency List

o Adjacency list is a linked representation.


o In this representation, for each vertex in the graph, we maintain the list of its neighbors. It
means, every vertex of the graph contains list of its adjacent vertices.
o We have an array of vertices which is indexed by the vertex number and for each vertex
v, the corresponding array element points to a singly linked list of neighbors of v.

Example

Let's see the following directed graph representation implemented using linked list:
We can also implement this representation using array as follows:

Pros:

o Adjacency list saves lot of space.


o We can easily insert or delete as we use linked list.
o Such kind of representation is easy to follow and clearly shows the adjacent nodes of
node.

Cons:

o The adjacency list allows testing whether two vertices are adjacent to each other but it is
slower to support this operation.

2. Illustrate the concepts of Breadth-first travels and Depth-first


traversal with neat diagram.
BREADTH-FIRST TRAVERSAL:

Breadth First Search (BFS) algorithm traverses a graph in a breadthward


motion to search a graph data structure for a node that meets a set of
criteria. It uses a queue to remember the next vertex to start a search, when
a dead end occurs in any iteration.

Breadth First Search (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.

As in the example given above, BFS algorithm traverses from A to B to E to F


first then to C and G lastly to D. It employs the following rules.

 Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display


it. Insert it in a queue.
 Rule 2 − If no adjacent vertex is found, remove the first vertex from
the queue.
 Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.

BFS (Routine):

Void BFS (vertex u)


{

Initialize queue Q;

Vistied [u]=1;

Enqueue (u,Q);

While(! Isempty(Q))

u=Dequeue(Q)

print u;

for all vertices v adjacent to u do

if(visited [v]==0)then

Enqueue(v,Q)

Visited[v]=1;

Ste
Traversal Description
p

1 Initialize the queue.


We start from visiting S (starting
2
node), and mark it as visited.

We then see an unvisited


adjacent node from S. In this
example, we have three nodes
3
but alphabetically we choose A,
mark it as visited and enqueue
it.

Next, the unvisited adjacent


4 node from S is B. We mark it as
visited and enqueue it.

Next, the unvisited adjacent


5 node from S is C. We mark it as
visited and enqueue it.
Now, S is left with no unvisited
6 adjacent nodes. So, we dequeue
and find A.

From A we have D as unvisited


7 adjacent node. We mark it as
visited and enqueue it.

At this stage, we are left with no unmarked (unvisited) nodes. But as per the
algorithm we keep on dequeuing in order to get all unvisited nodes. When
the queue gets emptied, the program is over.

DEPTH-FIRST TRAVERSAL:

Depth First Search (DFS) algorithm is a recursive algorithm for searching all
the vertices of a graph or tree data structure. This algorithm traverses a
graph in a depthward motion and uses a stack to remember to get the next
vertex to start a search, when a dead end occurs in any iteration.
As in the example given above, DFS algorithm traverses from S to A to D to
G to E to B first, then to F and lastly to C. It employs the following rules.

 Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display


it. Push it in a stack.
 Rule 2 − If no adjacent vertex is found, pop up a vertex from the
stack. (It will pop up all the vertices from the stack, which do not have
adjacent vertices.)
 Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.

DFS (Routine):

Void DFS(Vertex V)

Visited [V]=True;

For each W adjacent to V

If(! visited [W])

Dfs(W);

}
Ste
Traversal Description
p

1 Initialize the stack.

Mark S as visited and put it onto


the stack. Explore any unvisited
adjacent node from S. We have
2 three nodes and we can pick any
of them. For this example, we
shall take the node in an
alphabetical order.

Mark A as visited and put it onto


the stack. Explore any unvisited
adjacent node from A.
3
Both S and D are adjacent
to A but we are concerned for
unvisited nodes only.
Visit D and mark it as visited and
put onto the stack. Here, we
have B and C nodes, which are
4 adjacent to D and both are
unvisited. However, we shall
again choose in an alphabetical
order.

We choose B, mark it as visited


and put onto the stack.
5 Here B does not have any
unvisited adjacent node. So, we
pop B from the stack.

We check the stack top for


return to the previous node and
6 check if it has any unvisited
nodes. Here, we find D to be on
the top of the stack.

Only unvisited adjacent node is


from D is C now. So we visit C,
7
mark it as visited and put it onto
the stack.
3. Summarize about the Topological Sort and Construct the
following graph using stack model.

Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such
that for every directed edge u-v, vertex u comes before v in the ordering.
Note: Topological Sorting for a graph is not possible if the graph is not a DAG.
Example:
Input: Graph:

Example

Output: 5 4 2 3 1 0

Explanation: The first vertex in topological sorting is always a vertex with an in-degree of 0
(a vertex with no incoming edges). A topological sorting of the following graph is “5 4 2 3 1
0”. There can be more than one topological sorting for a graph. Another topological sorting of
the following graph is “4 5 2 3 1 0”.
Algorithm for Topological Sorting using DFS:

Here’s a step-by-step algorithm for topological sorting using Depth First Search (DFS):
 Create a graph with n vertices and m-directed edges.
 Initialize a stack and a visited array of size n.
 For each unvisited vertex in the graph, do the following:
o Call the DFS function with the vertex as the parameter.
o In the DFS function, mark the vertex as visited and recursively call the DFS
function for all unvisited neighbors of the vertex.
o Once all the neighbors have been visited, push the vertex onto the stack.
 After all, vertices have been visited, pop elements from the stack and append them to the
output list until the stack is empty.
 The resulting list is the topologically sorted order of the graph.

Illustration Topological Sorting Algorithm:

Below image is an illustration of the above approach:


Overall workflow of topological sorting

Step 1:
 We start DFS from node 0 because it has zero incoming Nodes
 We push node 0 in the stack and move to next node having minimum number of adjacent
nodes i.e. node 1.
Step 2:
 In this step , because there is no adjacent of this node so push the node 1 in the stack and
move to next node.

Step 3:
 In this step , We choose node 2 because it has minimum number of adjacent nodes after 0
and 1 .
 We call DFS for node 2 and push all the nodes which comes in traversal from node 2 in
reverse order.
 So push 3 then push 2 .

Step 4:
 We now call DFS for node 4
 Because 0 and 1 already present in the stack so we just push node 4 in the stack and return.

Step 5:

 In this step because all the adjacent nodes of 5 is already in the stack we push node 5 in the
stack and return.

Step 6: This is the final step of the Topological sorting in which we pop all the element from
the stack and print it in that order.

You might also like