UNIT-3
UNIT-3
3 Have successful career and meet the requirements of Indian and other Multi-National Companies. Higher order Thinking
4 Have exposure to advanced technologies, technical skills and opportunities to work as team members on Understand
multidisciplinary projects.
CO3 Design a memory module and analyze its operation by interfacing with the CPU.
1
Fundamentals of Computer Algorithms – E. Horowitz, Sartaj Saini,
Galgotia Publications
2
Introduction to Algorithms, 4TH Edition, Thomas H Cormen, Charles
E Lieserson, Ronald L Rivest and Clifford Stein, MIT Press/McGraw-
Hill
3
Data Structures and Algorithms in C++, Weiss, 4 th edition, Pearson.
4
Algorithm Design, 1stEdition, Jon Kleinberg and ÉvaTardos, Pearson.
Module3:
Graph and Tree Algorithms: Traversal algorithms: Depth First Search (DFS) and Breadth First Search (BFS);
Shortest path algorithms, Transitive closure, Minimum Spanning Tree, Topological sorting, Network Flow
Algorithm.
[10 hrs] (CO3)
BFS-LEVEL ORDER GRAPH
• BFS is implemented with the help of Queue:Let the traversal begins at Node A which is assigned a distance
value of “1”.
• Now the nodes adjacent to “A” are B,D and C.
• B is visited first and assigned a distance value of “2”.and the nodes adjacent to B are E and D.
• After B,
• D is visited next and the nodes adjacent to D are B,E and C.the node B is already visited so node C will be visited next.
• The nodes adjacent to C are A,B and D which are already visited.
• Hence Node E will be visited and assigned a value of 2
Algo: BST(G,A)
Given a graph”G”.This algo executes BFS beginning at a starting node “A”.
The variable STATUS tells us the current status of a node “Q” which is the name of
the queue.
1)Initialize all nodes to the ready state.
• Set status =0 to all nodes of the graph”G”.
2)Initialize first node and put it in queue “Q”
• A)Insert starting node A in queueQ
• B)Set STATUS=1(Waiting State)for Node A.
3)Repeat steps4 and 5 until queue “Q” is empty
4)A)remove the first nide,say N of Queue
• B) Process Node “N”
5)Add all the neighbours of N in queue
• A) Insert all the neighbours of N in the Queue
• B)Change STATUS=2(Waiting State) of all the neighbours of N
6)Exit
DFS (Depth First Search)
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.
The step by step process to implement the DFS traversal is given as follows -
1. First, create a stack with the total number of vertices in the graph.
2. Now, choose any vertex as the starting point of traversal, and push that vertex into the stack.
3. After that, push a non-visited vertex (adjacent to the vertex on the top of the stack) to the top of the stack.
4. Now, repeat steps 3 and 4 until no vertices are left to visit from the vertex on the stack's top.
5. If no vertex is left, go back and pop a vertex from the stack.
6. Repeat steps 2, 3, and 4 until the stack is empty.
Step 1 - First, push H onto the stack.
STACK: H
Step 2 - POP the top element from the stack, i.e., H, and print
it. Now, PUSH all the neighbors of H onto the stack that are in
ready state.
Print: H]STACK: A
Step 3 - POP the top element from the stack, i.e., A, and print it.
Now, PUSH all the neighbors of A onto the stack that are in
ready state.
Print: A
STACK: B, D
Step 4 - POP the top element from the stack, i.e., D, and print
it. Now, PUSH all the neighbors of D onto the stack that are in
ready state.
Print: D
STACK: B, F
Step 5 - POP the top element from the stack, i.e., F, and print it. Now, PUSH all the neighbors of F onto the stack that are in
ready state.
Print: F
STACK: B
Step 6 - POP the top element from the stack, i.e., B, and print it. Now, PUSH all the neighbors of B onto the stack that are in
ready state.
Print: B
STACK: C
Step 7 - POP the top element from the stack, i.e., C, and print it. Now, PUSH all the neighbors of
C onto the stack that are in ready state
Print: C
STACK: E, G
Step 8 - POP the top element from the stack, i.e., G and PUSH all the neighbors of G onto the
stack that are in ready state.
Print: G
STACK: E
Step 9 - POP the top element from the stack, i.e., E and PUSH all the neighbors of E onto the stack
that are in ready state.
1.Print: E
2.STACK:
ALGO:DFT(G,A)
Given a Graph “G”.“A” is the starting node for traversal. The variable “STATUS” tells us the current status of a node”S” is the name of
Stack.
1) Initialize all nodes to the ready state.
Set STATUS=1 to all node of graph “G”
2) Initialize first node and put it in the stack”s”
A) Push the starting node A onto stack “S”
B) Set STATUS =2(WAITING STATE) for node “A”.
3)Repeat Step 3 & 4 until stack “S” is empty
4) Process the TOP node of stack”S”
A) POP the top node, say N of “S”
B) Process the node “N”
C)Set STATUS=3(Processed state) of node “N”
5)Add all the neighbour of N in stack
A)Push all the neighbours of N onto stack
B) Change STATUS=2(WAITING STATE) of all the neighbours of N
Shortest path algorithm
The shortest path problem is the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the
weights of its constituent edges is minimized.
The problem of finding the shortest path between two intersections on a road map may be modeled as a special case of the
shortest path problem in graphs, where the vertices correspond to intersections and the edges correspond to road segments, each
weighted by the length or distance of each segment.
How to find Shortest Paths from Source to all Vertices using Dijkstra’s Algorithm
Given a weighted graph and a source vertex in the graph, find the shortest paths from the source to all the other vertices in
the given graph.
Note: The given graph does not contain any negative edge.
Examples:
Input: src = 0, the graph is shown below.
Algorithm :
• Create a set sptSet (shortest path tree set) that keeps track of vertices included in the shortest path tree, i.e.,
whose minimum distance from the source is calculated and finalized. Initially, this set is empty.
• Assign a distance value to all vertices in the input graph. Initialize all distance values as INFINITE . Assign the
distance value as 0 for the source vertex so that it is picked first.
• Pick a vertex u that is not there in sptSet and has a minimum distance value.
• Include u to sptSet .
• Then update the distance value of all adjacent vertices of u .
• To update the distance values, iterate through all adjacent vertices.
• For every adjacent vertex v, if the sum of the distance value of u (from source) and weight of edge u-v ,
is less than the distance value of v , then update the distance value of v .
Illustration of Dijkstra Algorithm :
To understand the Dijkstra’s Algorithm lets take a graph and find the shortest path from source to all nodes.
Step 1:
• The set sptSet is initially empty and distances assigned to vertices are {0, INF, INF, INF, INF, INF, INF, INF} where
INF indicates infinite.
• Now pick the vertex with a minimum distance value. The vertex 0 is picked, include it in sptSet . So sptSet becomes
{0}. After including 0 to sptSet , update distance values of its adjacent vertices.
• Adjacent vertices of 0 are 1 and 7. The distance values of 1 and 7 are updated as 4 and 8.
• The following subgraph shows vertices and their distance values, only the vertices with finite distance values
are shown. The vertices included in SPT are shown in green colour.
Step 2:
• Pick the vertex with minimum distance value and not already included in SPT (not in sptSET ). The vertex 1 is picked
and added to sptSet .So sptSet now becomes {0, 1}. Update the distance values of adjacent vertices of 1. The distance
value of vertex 2 becomes 12 .
Step 3:
• Pick the vertex with minimum distance value and not already included in SPT (not in sptSET ). Vertex 7 is
picked. So sptSet now becomes {0, 1, 7}.
• Update the distance values of adjacent vertices of 7. The distance value of vertex 6 and 8 becomes finite ( 15
and 9 respectively).
Step 4:
• Pick the vertex with minimum distance value and not already included in SPT (not in sptSET ). Vertex 6 is
picked. So sptSet now becomes {0, 1, 7, 6} .
• Update the distance values of adjacent vertices of 6. The distance value of vertex 5 and 8 are updated.
• We repeat the above steps until sptSet includes all vertices of the given graph. Finally, we get the following S hortest
Path Tree (SPT).
Transitive Closure
In graph theory, transitive closure is the process of constructing a new graph from an input graph, where the new graph
has an edge between two nodes if there is a directed path between them in the input graph.
Transitive closure is used to answer reachability questions, such as whether it's possible to get from one node to another in
one or more hops. The transitive closure is usually stored as a Boolean matrix, where a value of 1 indicates that a node can
reach another node, and a value of 0 indicates that there is no connection.
Transitive Closure it the reachability matrix to reach from vertex u to vertex v of a graph. One graph is given,
we have to find a vertex v which is reachable from another vertex u, for all vertex pairs (u, v).
The final matrix is the Boolean type. When there is a value 1 for vertex u to vertex v, it means that there is at least one path
from u to v.
Transitive closure of a graph using Floyd Warshall Algorithm
Given a directed graph, determine if a vertex j is reachable from another
vertex i for all vertex pairs (i, j) in the given graph. Here reachable means
that there is a path from vertex i to j. The reach-ability matrix is called the
transitive closure of a graph.
Transitive closure of a graph
1 1 1 1
1 1 1 1
1 1 1 1
0 0 0 1
The graph is given in the form of adjacency matrix say ‘graph[V][V]’ where graph[i][j]
is 1 if there is an edge from vertex i to vertex j or i is equal to j, otherwise graph[i][j] is
0.
Floyd Warshall Algorithm can be used, we can calculate the distance matrix dist[V][V]
using Floyd Warshall, if dist[i][j] is infinite, then j is not reachable from i. Otherwise, j
is reachable and the value of dist[i][j] will be less than V
Minimum Spanning Tree
A spanning tree is defined as a tree-like subgraph of a connected, undirected graph that includes all the vertices of the graph. Or, to say in
Layman’s words, it is a subset of the edges of the graph that forms a tree (acyclic) where every node of the graph is a part of the tree.
The minimum spanning tree has all the properties of a spanning tree with an added constraint of having the minimum possible weights
among all possible spanning trees. Like a spanning tree, there can also be many possible MSTs for a graph.
Properties of a Spanning Tree:
• The spanning tree holds the below-mentioned principles:
• The number of vertices (V) in the graph and the spanning tree is the same.
• There is a fixed number of edges in the spanning tree which is equal to one less than the total number of vertices ( E = V-1 ).
• The spanning tree should not be disconnected, as in there should only be a single source of component, not more than that.
• The spanning tree should be acyclic, which means there would not be any cycle in the tree.
• The total cost (or weight) of the spanning tree is defined as the sum of the edge weights of all the edges of the spanning tree.
• There can be many possible spanning trees for a graph.
Minimum Spanning Tree
• A minimum spanning tree (MST) is defined as a spanning tree that has the minimum weight among all the possible
spanning trees.
• The minimum spanning tree has all the properties of a spanning tree with an added constraint of having the minimum
possible weights among all possible spanning trees. Like a spanning tree, there can also be many possible MSTs for a
graph.
• Algorithms to find Minimum Spanning Tree:
• There are several algorithms to find the minimum spanning tree from a given graph, some of them are listed below:
Kruskal’s Minimum Spanning Tree Algorithm
This is one of the popular algorithms for finding the minimum spanning tree from a connected, undirected graph. This is a
greedy algorithm.
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”.
• In computer science, a topological sort or topological ordering of a directed graph is a linear ordering of its vertices such
that for every directed edge (u,v) from vertex u to vertex v, u comes before v in the ordering.
• For instance, the vertices of the graph may represent tasks to be performed, and the edges may represent constraints that one
task must be performed before another; in this application, a topological ordering is just a valid sequence for the tasks.
• Precisely, a topological sort is a graph traversal in which each node v is visited only after all its dependencies are
visited.
• A topological ordering is possible if and only if the graph has no directed cycles, that is, if it is a directed acyclic graph
(DAG). Any DAG has at least one topological ordering, and algorithms are known for constructing a topological ordering of
any DAG in linear time. Topological sorting has many applications, especially in ranking problems such as feedback arc set.
Topological sorting is also possible when the DAG has disconnected components.
This graph has many valid topological sorts, including:5, 7, 3, 11, 8, 2, 9, 10
(visual left-to-right, top-to-bottom)
3, 5, 7, 8, 11, 2, 9, 10 (smallest-numbered available vertex first)
3, 5, 7, 8, 11, 2, 10, 9 (lexicographic by incoming neighbors)
5, 7, 3, 8, 11, 2, 10, 9 (fewest edges first)
7, 5, 11, 3, 10, 8, 9, 2 (largest-numbered available vertex first)
5, 7, 11, 2, 3, 8, 9, 10 (attempting top-to-bottom, left-to-right)
3, 7, 8, 5, 11, 10, 2, 9 (arbitrary)
• The Ford-Fulkerson algorithm is a widely used algorithm to solve the maximum flow problem in a flow network. The
maximum flow problem involves determining the maximum amount of flow that can be sent from a source vertex to a sink
vertex in a directed weighted graph, subject to capacity constraints on the edges.
• The algorithm works by iteratively finding an augmenting path, which is a path from the source to the sink in the residual
graph, i.e., the graph obtained by subtracting the current flow from the capacity of each edge. The algorithm then increases
the flow along this path by the maximum possible amount, which is the minimum capacity of the edges along the path
Problem
• Given a graph which represents a flow network where every edge has a
capacity. Also, given two vertices source ‘s’ and sink ‘t’ in the graph, find the
maximum possible flow from s to t with the following constraints: Flow on an
edge doesn’t exceed the given capacity of the edge. Incoming flow is equal to
outgoing flow for every vertex except s and t. For example, consider the
following graph The maximum possible flow in the above graph is 23.
Ford-Fulkerson Algorithm
2. While there exists an augmenting path from the source to the sink:
1. Find an augmenting path using any path-finding algorithm, such as breadth-first search or depth-first search.
2. Determine the amount of flow that can be sent along the augmenting path, which is the minimum residual capacity along
the edges of the path.
3. Increase the flow along the augmenting path by the determined amount.