DS Unit 4
DS Unit 4
Graphs
● A Graph is a data structure that consists of a set of nodes
(vertices) and a set of edges that relate the nodes to each
other
G=(V,E)
V(G): a finite set of vertices
E(G): a set of edges (pairs of vertices)
Examples of Graphs
A Graph G(V, E) with 5 vertices (A, B, C, D, E) and six edges ((A,B),
(B,C), (C,E), (E,D), (D,B), (D,A)) is shown in the following figure.
Examples of Graphs
Social Networks:
• Each person is a vertex, and relationships (like friendships) are
the edges. Algorithms can suggest potential friends.
Internet:
•Can be represented as a Graph, with web pages as vertices and
hyperlinks as edges.
TYPES OF GRAPHS
Directed vs. Undirected Graphs
Undirected Graph
● has no orientation (no arrow head)
● A Undirected Graph is when the edges between the vertex pairs
have no direction.
u v
Directed Graph
● has an orientation (has an arrow head)
Complete Graph
A graph in which a
every vertex is
connected to every
other vertex by an
edge.
Cyclic Graph
A graph containing at least one
cycle in it is called as a cyclic
graph.
Acyclic Graph
A graph not containing
any cycle in it is called
as an acyclic graph
Graph terminology
● Adjacent nodes: two nodes are adjacent if they are
connected by an edge
5 is adjacent to 7
7 is adjacent from 5
Types :-
1. Indegree :- The number of
arcs entering the node is called
indegree of that node.
1 2
3 4
5
Adjacency Lists Representation
● Adjacency list is a linked representation.
● 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.
● Each row in adjacency matrix is represented as an
adjacency list.
Graphs: Adjacency List
24
Topological Sort
• Topological Sort is a linear ordering of the
vertices in such a way that if there is an edge
in the DAG going from vertex ‘u’ to vertex ‘v’,
then ‘u’ comes before ‘v’ in the ordering.
• Topological Sorting is possible if and only if the
graph is a Directed Acyclic Graph.
• There may exist multiple different topological
orderings for a given directed acyclic graph.
25
Topological Sort Example
26
Topological Sort Example
Step-01:
• Write in-degree of each vertex-
27
Topological Sort Example
Step-02:
• Vertex-A has the least in-degree.
• So, remove vertex-A and its associated edges.
• Now, update the in-degree of other vertices.
28
Topological Sort Example
Step-03:
• Vertex-B has the least in-degree.
• So, remove vertex-B and its associated edges.
• Now, update the in-degree of other vertices.
29
Topological Sort Example
Step-04:
• There are two vertices with the least in-
degree. So, following 2 cases are possible-
In case-01, In case-02,
•Remove •Remove
vertex-C and its vertex-D and
associated its associated
edges. edges.
•Then, update •Then, update
the in-degree of the in-degree
other vertices. of other
vertices.
30
Topological Sort Example
Step-05:
• There are two vertices with the least in-
degree. So, following 2 cases are possible-
In case-01, In case-02,
•Remove vertex-D since it has the •Remove vertex-C since it has
least in-degree. the least in-degree.
•Then, remove the remaining vertex- •Then, remove the remaining
E. vertex-E.
31
Topological Sort Example
32
33
Topological Sort
34
Graphs Traversal
35
Graphs Traversal-Breadth First Search(BFS)
It is also known as level order traversal.
The Queue data structure that follows first in first out is used for
Breadth First Search traversal.
It begins with a node, then first traverses all its adjacent vertices.
Once all adjacent vertices are visited, then their adjacent are
traversed.
Algorithmic Steps
Step 1 - Define a Queue of size total number of vertices in the graph.
Step 2 - Select any vertex as starting point for traversal. Visit that
vertex and insert it into the Queue.
Step 3 - Visit all the non-visited adjacent vertices of the vertex which is
at front of the Queue and insert them into the Queue.
Step 4 - When there is no new vertex to be visited from the vertex
which is at front of the Queue then delete that vertex.
Step 5 - Repeat steps 3 and 4 until queue becomes empty.
Step 6 - When queue becomes empty, then produce final spanning
tree by removing unused edges from the graph 36
Graphs Traversal-Breadth First Search
37
Graphs Traversal-Breadth First Search
38
Graphs Traversal-Breadth First Search
39
Graphs Traversal-Breadth First Search
40
Graphs Traversal-Breadth First Search
BFS: A D E B C F G
41
Graphs Traversal-Breadth First Search
Routine
Void BFS (vertex v)
{
Visited[v]=TRUE;
For each w adjacent to v;
if (visited[w]==0)
BFS (G,V);
}
42
Graphs Traversal-Depth First Search
DFS is also called Edge Based Traversal because it explores the nodes
along the edge or path.
It uses the Stack data structure which follows Last in First out.
The depth-first search (DFS) algorithm starts with the initial node of
graph G and goes deeper until we find the goal node or the node with
no children.
Algorithmic Steps
Step 1 - Define a Stack of size total number of vertices in the graph.
Step 2 - Select any vertex as starting point for traversal. Visit that vertex
and push it on to the Stack.
Step 3 - Visit any one of the non-visited adjacent vertices of a vertex
which is at the top of stack and push it on to the stack.
Step 4 - Repeat step 3 until there is no new vertex to be visited from the
vertex which is at the top of the stack.
Step 5 - When there is no new vertex to visit then use back tracking and
pop one vertex from the stack.
Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
Step 7 - When stack becomes Empty, then produce final spanning tree by
removing unused edges from the graph 43
Graphs Traversal-Depth First Search
44
Graphs Traversal-Depth First Search
45
Graphs Traversal-Depth First Search
46
Graphs Traversal-Depth First Search
47
Graphs Traversal-Depth First Search
48
Graphs Traversal-Depth First Search
49
Graphs Traversal-Depth First Search
DFS: A B C E D F G 50
Graphs Traversal-Depth First Search
DFS: A B C E D F G
51
Graphs Traversal
Routine
Void DFS(vertex v)
{
Visited[v]=TRUE;
For each w adjacent to v;
if (!visited[w])
DFS (w);
}
52
Minimum spanning tree
Spanning Trees
A subgraph T of a undirected graph G=(V, E) is a
spanning tree of G if it is a tree and contains every
vertex of G
53
Minimum spanning tree
A Minimum Spanning Tree in an undirected connected
weighted graph can be defined as the spanning tree
(among all spanning trees) in which the sum of the
weights of the edge is minimum.
54
Prim's Algorithm
• Prim's Algorithm is a greedy algorithm that is used to
find the minimum spanning tree from a graph.
• Prim's algorithm finds the subset of edges that
includes every vertex of the graph such that the sum
of the weights of the edges can be minimized.
• Prim's algorithm starts with the single node and
explores all the adjacent nodes with all the
connecting edges at every step.
• The edges with the minimal weights causing no
cycles in the graph got selected.
55
Prim's Algorithm
• The steps for implementing Prim's algorithm are as
follows:
Initialize the minimum spanning tree with a
vertex chosen at random.
Finding the edge with the lowest weight that
connects the tree to a vertex not yet in the tree
Adding the edge to the MST if it doesn't create a
closed cycle
Repeating steps 2 and 3 until all vertices are in
the MST
56
Example of Prim's Algorithm
57
Example of Prim's Algorithm
Step 2
• Since B is the last visited, check for the least cost
edge that is connected to the vertex B.
B→A=9
B → C = 16
B → E = 14
• Least cost edge from B is A,
so B → A is the edge added to
the spanning tree.
58
Example of Prim's Algorithm
Step 3
• Since A is the last visited, check for the least cost edge that is
connected to the vertex A
A → C = 22
A→B=9
A → E = 11
• But A → B is already in the
spanning tree, check for the
next least cost edge. Hence,
A → E is added to the spanning
tree.
59
Example of Prim's Algorithm
Step 4
• Since E is the last visited, check for the least cost edge that is
connected to the vertex E.
E → C = 18
E→D=3
Therefore, E → D is added
to the spanning tree.
60
Example of Prim's Algorithm
Step 5
• Since D is the last visited, check for the least cost edge that is
connected to the vertex D.
• D → C = 15
• E→D=3
• Therefore, D → C is
added to the spanning
tree.
63
Example of Kruskal’s Algorithm
Construct a minimum spanning tree using
kruskal’s algorithm for the graph given below −
65
Example of Kruskal’s Algorithm
66
Example of Kruskal’s Algorithm
Step 2: Pick edge 8-2. No cycle is formed, include it.
67
Example of Kruskal’s Algorithm
Step 3: Pick edge 6-5. No cycle is formed, include it.
68
Example of Kruskal’s Algorithm
Step 4: Pick edge 0-1. No cycle is formed, include it.
69
Example of Kruskal’s Algorithm
Step 5: Pick edge 2-5. No cycle is formed, include it.
70
Example of Kruskal’s Algorithm
Step 6: Pick edge 8-6. Since including this edge results in the cycle,
discard it. Pick edge 2-3: No cycle is formed, include it.
71
Example of Kruskal’s Algorithm
Step 7: Pick edge 7-8. Since including this edge results in the cycle,
discard it. Pick edge 0-7. No cycle is formed, include it.
72
Example of Kruskal’s Algorithm
Step 8: Pick edge 1-2. Since including this edge results in the cycle,
discard it. Pick edge 3-4. No cycle is formed, include it.