UNIT 4 (IQ)
UNIT 4 (IQ)
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
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
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:
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
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
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
Example
3. Adjacency List
Example
Let's see the following directed graph representation implemented using linked list:
We can also implement this representation using array as follows:
Pros:
Cons:
o The adjacency list allows testing whether two vertices are adjacent to each other but it is
slower to support this operation.
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.
BFS (Routine):
Initialize queue Q;
Vistied [u]=1;
Enqueue (u,Q);
While(! Isempty(Q))
u=Dequeue(Q)
print u;
if(visited [v]==0)then
Enqueue(v,Q)
Visited[v]=1;
Ste
Traversal Description
p
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.
DFS (Routine):
Void DFS(Vertex V)
Visited [V]=True;
Dfs(W);
}
Ste
Traversal Description
p
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.
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.