Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CS3401 ALG UNIT 2 NOTES EduEngg

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

CONNECT WITH US

CONNECT WITH US

WEBSITE: www.eduengineering.net

TELEGRAM: @eduengineering
-

INSTAGRAM: @eduengineering

 Regular Updates for all Semesters


 All Department Notes AVAILABLE
 Handwritten Notes AVAILABLE
 Past Year Question Papers AVAILABLE
 Subject wise Question Banks AVAILABLE
 Important Questions for Semesters AVAILABLE
 Various Author Books AVAILABLE
Downloaded
UNIT 2 -from www.eduengineering.net
GRAPHS: basics, representation,
traversals, and application

Basic concepts

Definition

A graph G(V, E) is a non-linear data structure that consists of node


and edge pairs of objects connected by links.

There are 2 types of graphs:

 Directed
 Undirected

Directed graph

A graph with only directed edges is said to be a directed graph.

Example

The following directed graph has 5 vertices and 8 edges. This graph G
can be defined as G = (V, E), where V = {A,B,C,D,E} and E = {(A,B),
(A,C) (B, E), (B,D), (D, A), (D, E),(C,D),(D,D)}.

Directed Graph

Undirected graph

A graph with only undirected edges is said to be an undirected graph.

Example

The following is an undirected graph.

Undirected Graph

Representation of Graphs
TELEGRAM: @eduengineering
Downloaded from
Graph data structure is www.eduengineering.net
represented using the following
representations.

1. Adjacency Matrix
2. Adjacency List

Adjacency Matrix

 In this representation, the graph can be represented using a


matrix of size n x n, where n is the number of vertices.
 This matrix is filled with either 1’s or 0’s.
 Here, 1 represents that there is an edge from row vertex to
column vertex, and 0 represents that there is no edge from row
vertex to column vertex.

Directed graph representation

Adjacency list

 In this representation, every vertex of the graph contains a


list of its adjacent vertices.
 If the graph is not dense, i.e., the number of edges is less,
then it is efficient to represent the graph through the
adjacency list.

Adjacency List

Graph traversals

 Graph traversal is a technique used to search for a vertex in a


graph. It is also used to decide the order of vertices to be
visited in the search process.
 A graph traversal finds the edges to be used in the search
process without creating loops. This means that, with graph
traversal, we can visit all the vertices of the graph without
getting into a looping path. There are two graph traversal
techniques:

1. DFS (Depth First Search)


2. BFS (Breadth-First Search)

Applications of graphs

TELEGRAM: @eduengineering
Downloaded from: To
1. Social network graphs www.eduengineering.net
tweet or not to tweet. Graphs that
represent who knows whom, who communicates with whom, who
influences whom, or other relationships in social structures. An
example is the twitter graph of who follows whom.
2. Graphs in epidemiology: Vertices represent individuals and
directed edges to view the transfer of an infectious disease
from one individual to another. Analyzing such graphs has become
an important component in understanding and controlling the
spread of diseases.
3. Protein-protein interactions graphs: Vertices represent proteins
and edges represent interactions between them that carry out
some biological function in the cell. These graphs can be used
to, for example, study molecular pathway—chains of molecular
interactions in a cellular process.
4. Network packet traffic graphs: Vertices are IP (Internet
protocol) addresses and edges are the packets that flow between
them. Such graphs are used for analyzing network security,
studying the spread of worms, and tracking criminal or non-
criminal activity.
5. Neural networks: Vertices represent neurons and edges are the
synapses between them. Neural networks are used to understand
how our brain works and how connections change when we learn.
The human brain has about 1011 neurons and close to 1015
synapses.

DFS – Depth First Search


Depth First Search (DFS) 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.

Step Traversal Description

TELEGRAM: @eduengineering
Downloaded
1
from www.eduengineering.net

Initialize the stack.

2 Mark S as visited and put it onto the


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

3 Mark A as visited and put it onto the


stack. Explore any unvisited adjacent
node from A. Both S and D are
adjacent to A but we are concerned
for unvisited nodes only.

4 Visit D and mark it as visited and put


onto the stack. Here, we
have B and C nodes, which are
adjacent to D and both are unvisited.
However, we shall again choose in an
alphabetical order.

5
We choose B, mark it as visited and
put onto the stack. Here B does not
have any unvisited adjacent node. So,
we pop B from the stack.

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

7
Only unvisited adjacent node is
from D is C now. So we visit C, mark it
as visited and put it onto the stack.

Pseudocode of DFS
TELEGRAM: @eduengineering
Downloaded
DFS(G, u) from www.eduengineering.net
u.visited = true
for each v ∈ G.Adj[u]
if v.visited == false
DFS(G,v)

init() {
For each u ∈ G
u.visited = false
For each u ∈ G
DFS(G, u)
}

Complexity of Depth First Search


The time complexity of the DFS algorithm is represented in the form of O(V + E), where V is the
number of nodes and E is the number of edges.

The space complexity of the algorithm is O(V).

Application of DFS Algorithm


1. For finding the path

2. To test if the graph is bipartite

3. For finding the strongly connected components of a graph

4. For detecting cycles in a graph

Breadth First Search


Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and uses a queue 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, 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.

TELEGRAM: @eduengineering
Downloaded
Step Traversal
from www.eduengineering.net
Description

Initialize the queue.

We start from visiting S (starting


node), and mark it as visited.

3 We then see an unvisited adjacent


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

4
Next, the unvisited adjacent node
from S is B. We mark it as visited and
enqueue it.

5
Next, the unvisited adjacent node
from S is C. We mark it as visited and
enqueue it.

6
Now, S is left with no unvisited
adjacent nodes. So, we dequeue and
find A.

7
From A we have D as
unvisited adjacent node. We
mark it as visited and
enqueue it.

TELEGRAM: @eduengineering
Downloaded from www.eduengineering.net
BFS pseudocode
create a queue Q

mark v as visited and put v into Q

while Q is non-empty

remove the head u of Q

mark and enqueue all (unvisited) neighbours of u

BFS Algorithm Complexity


The time complexity of the BFS algorithm is represented in the form of O(V + E), where V is the
number of nodes and E is the number of edges.

The space complexity of the algorithm is O(V).

BFS Algorithm Applications


1. To build index by search index
2. For GPS navigation
3. Path finding algorithms
4. In Ford-Fulkerson algorithm to find maximum flow in a network
5. Cycle detection in an undirected graph
6. In minimum spanning tree

Connected graph , Strongly connected and Bi-Connectivity

Connected Graph Component

A connected component or simply component of an undirected graph is a subgraph in which each


pair of nodes is connected with each other via a path.

TELEGRAM: @eduengineering
Downloaded from www.eduengineering.net

Strongly Connected Graph


The Kosaraju algorithm is a DFS based algorithm used to find Strongly Connected
Components(SCC) in a graph. It is based on the idea that if one is able to reach a vertex v starting
from vertex u, then one should be able to reach vertex u starting from vertex v and if such is the case,
one can say that vertices u and v are strongly connected - they are in a strongly connected sub-
graph.

stack STACK
void DFS(int source) {
visited[s]=true
for all neighbours X of source that are not visited:
DFS(X)
STACK.push(source)
}

CLEAR ADJACENCY_LIST
for all edges e:
first = one end point of e
second = other end point of e
ADJACENCY_LIST[second].push(first)

while STACK is not empty:


source = STACK.top()
STACK.pop()
if source is visited :
continue
else :
DFS(source)

Bi Connectivity Graph
An undirected graph is said to be a biconnected graph, if there are two vertex-disjoint paths
between any two vertices are present. In other words, we can say that there is a cycle between any
two vertices.

We can say that a graph G is a bi-connected graph if it is connected, and there are no articulation
points or cut vertex are present in the graph.

TELEGRAM: @eduengineering
Downloaded from
To solve this problem, we will use www.eduengineering.net
the DFS traversal. Using DFS, we will try to find if there is any
articulation point is present or not. We also check whether all vertices are visited by the DFS or not,
if not we can say that the graph is not connected.

Pseudocode for Bi connectivity


isArticulation(start, visited, disc, low, parent)
Begin
time := 0 //the value of time will not be initialized for next function calls
dfsChild := 0
mark start as visited
set disc[start] := time+1 and low[start] := time + 1
time := time + 1
for all vertex v in the graph G, do
if there is an edge between (start, v), then
if v is visited, then
increase dfsChild
parent[v] := start
if isArticulation(v, visited, disc, low, parent) is true, then
return ture
low[start] := minimum of low[start] and low[v]
if parent[start] is φ AND dfsChild > 1, then
return true
if parent[start] is φ AND low[v] >= disc[start], then
return true
else if v is not the parent of start, then
low[start] := minimum of low[start] and disc[v]
done
return false
End
isBiconnected(graph)
Begin
initially set all vertices are unvisited and parent of each vertices are φ
if isArticulation(0, visited, disc, low, parent) = true, then
return false
for each node i of the graph, do
if i is not visited, then
return false
done
return true
End

Minimum Spanning Tree


A Spanning Tree is a tree which have V vertices and V-1 edges. All nodes in a spanning tree
are reachable from each other.
A Minimum Spanning Tree(MST) or minimum weight spanning tree for a weighted,
connected, undirected graph is a spanning tree having a weight less than or equal to the
weight of every other possible spanning tree. The weight of a spanning tree is the sum of
weights given to each edge of the spanning tree. In short out of all spanning trees of a given
graph, the spanning tree having minimum weight is MST.

Algorithms for finding Minimum Spanning Tree(MST):-


1. Prim’s Algorithm
2. Kruskal’s Algorithm
TELEGRAM: @eduengineering
Downloaded from www.eduengineering.net

Prim’s Algorithm
Prim's algorithm is a minimum spanning tree algorithm that takes a graph as input and finds the
subset of the edges of that graph which
 form a tree that includes every vertex
 has the minimum sum of weights among all the trees that can be formed from the graph

How Prim's algorithm works


It falls under a class of algorithms called greedy algorithms that find the local optimum in the hopes
of finding a global optimum.
We start from one vertex and keep adding edges with the lowest weight until we reach our goal.
The steps for implementing Prim's algorithm are as follows:
1. Initialize the minimum spanning tree with a vertex chosen at random.
2. Find all the edges that connect the tree to new vertices, find the minimum and add it to the
tree
3. Keep repeating step 2 until we get a minimum spanning tree

Example of Prim's algorithm

Start with a weighted graph

Choose a vertex

Choose the shortest edge from this vertex and add it

Choose the nearest vertex not yet in the solution

Choose the nearest edge not yet in the solution, if there are multiple choices, choose one at random

TELEGRAM: @eduengineering
Downloaded from www.eduengineering.net

Prim's Algorithm pseudocode


The pseudocode for prim's algorithm shows how we create two sets of vertices U and V-U. U
contains the list of vertices that have been visited and V-U the list of vertices that haven't. One by
one, we move vertices from set V-U to set U by connecting the least weight edge.
T = ∅;
U = { 1 };
while (U ≠ V)
let (u, v) be the lowest cost edge such that u ∈ U and v ∈ V - U;
T = T ∪ {(u, v)}
U = U ∪ {v}

Prim's Algorithm Complexity


The time complexity of Prim's algorithm is O(E log V).

Kruskal Algorithm

Kruskal's algorithm is a minimum spanning tree algorithm that takes a graph as input and finds the
subset of the edges of that graph which
 form a tree that includes every vertex
 has the minimum sum of weights among all the trees that can be formed from the graph
How Kruskal's algorithm works
It falls under a class of algorithms called greedy algorithms that find the local optimum in the hopes
of finding a global optimum.
We start from the edges with the lowest weight and keep adding edges until we reach our goal.
The steps for implementing Kruskal's algorithm are as follows:
1. Sort all the edges from low weight to high
2. Take the edge with the lowest weight and add it to the spanning tree. If adding the edge
created a cycle, then reject this edge.
3. Keep adding edges until we reach all vertices.

Example of Kruskal's algorithm

Start with a weighted graph

Choose the edge with the least weight, if there are more than 1, choose anyone

TELEGRAM: @eduengineering
Downloaded from www.eduengineering.net

Choose the next shortest edge and add it

Choose the next shortest edge that doesn't create a cycle and add it

Choose the next shortest edge that doesn't create a cycle and add it

Repeat until you have a spanning tree

Kruskal Algorithm Pseudocode


KRUSKAL(G):
A=∅
For each vertex v ∈ G.V:
MAKE-SET(v)
For each edge (u, v) ∈ G.E ordered by increasing order by weight(u, v):
if FIND-SET(u) ≠ FIND-SET(v):
A = A ∪ {(u, v)}
UNION(u, v)
return A

Shortest Path Algorithm

The shortest path problem is about finding a path between vertices in a graph such that the total
sum of the edges weights is minimum.

Algorithm for Shortest Path


1. Bellman Algorithm
2. Dijkstra Algorithm
3. Floyd Warshall Algorithm

Bellman Algorithm
Bellman Ford algorithm helps us find the shortest path from a vertex to all other vertices of a
weighted graph.
It is similar to Dijkstra's algorithm but it can work with graphs in which edges can have negative
weights.

TELEGRAM: @eduengineering
Downloaded
How Bellman Ford's algorithmfrom
works www.eduengineering.net
Bellman Ford algorithm works by overestimating the length of the path from the starting vertex to
all other vertices. Then it iteratively relaxes those estimates by finding new paths that are shorter
than the previously overestimated paths.
By doing this repeatedly for all vertices, we can guarantee that the result is optimized.

Step-1 for Bellman Ford's algorithm

Step-2 for Bellman Ford's algorithm

Step-3 for Bellman Ford's algorithm

TELEGRAM: @eduengineering
Downloaded from www.eduengineering.net

Step-4 for Bellman Ford's algorithm

Step-5 for Bellman Ford's algorithm

Step-6 for Bellman Ford's algorithm


TELEGRAM: @eduengineering
Downloaded from www.eduengineering.net
Bellman Ford Pseudocode
We need to maintain the path distance of every vertex. We can store that in an array of size v,
where v is the number of vertices.
We also want to be able to get the shortest path, not only know the length of the shortest path. For
this, we map each vertex to the vertex that last updated its path length.
Once the algorithm is over, we can backtrack from the destination vertex to the source vertex to find
the path.
function bellmanFord(G, S)
for each vertex V in G
distance[V] <- infinite
previous[V] <- NULL
distance[S] <- 0

for each vertex V in G


for each edge (U,V) in G
tempDistance <- distance[U] + edge_weight(U, V)
if tempDistance < distance[V]
distance[V] <- tempDistance
previous[V] <- U

for each edge (U,V) in G


If distance[U] + edge_weight(U, V) < distance[V}
Error: Negative Cycle Exists

return distance[], previous[]

Bellman Ford's Complexity

Time Complexity

Best Case Complexity O(E)

Average Case Complexity O(VE)

Worst Case Complexity O(VE)

Dijkstra Algorithm

Dijkstra's algorithm allows us to find the shortest path between any two vertices of a graph.
It differs from the minimum spanning tree because the shortest distance between two vertices
might not include all the vertices of the graph.

How Dijkstra's Algorithm works


Dijkstra's Algorithm works on the basis that any subpath B -> D of the shortest path A -> D between
vertices A and D is also the shortest path between vertices B and D.

TELEGRAM: @eduengineering
Downloaded from www.eduengineering.net
Each subpath is the shortest path
Djikstra used this property in the opposite direction i.e we overestimate the distance of each vertex
from the starting vertex. Then we visit each node and its neighbors to find the shortest subpath to
those neighbors.
The algorithm uses a greedy approach in the sense that we find the next best solution hoping that
the end result is the best solution for the whole problem.

Example of Dijkstra's algorithm


It is easier to start with an example and then think about the algorithm.

Start with a weighted graph

Choose a starting vertex and assign infinity path values to all other devices

Go to each vertex and update its path length

TELEGRAM: @eduengineering
Downloaded from www.eduengineering.net

If the path length of the adjacent vertex is lesser than new path length, don't update it

Avoid updating path lengths of already visited vertices

After each iteration, we pick the unvisited vertex with the least path length. So we choose 5 before 7

TELEGRAM: @eduengineering
Downloaded from www.eduengineering.net

Notice how the rightmost vertex has its path length updated twice

Repeat until all the vertices have been visited

Djikstra's algorithm pseudocode


We need to maintain the path distance of every vertex. We can store that in an array of size v,
where v is the number of vertices.
We also want to be able to get the shortest path, not only know the length of the shortest path. For
this, we map each vertex to the vertex that last updated its path length.
Once the algorithm is over, we can backtrack from the destination vertex to the source vertex to find
the path.
A minimum priority queue can be used to efficiently receive the vertex with least path distance.
function dijkstra(G, S)
for each vertex V in G
distance[V] <- infinite
previous[V] <- NULL
If V != S, add V to Priority Queue Q
distance[S] <- 0

while Q IS NOT EMPTY


U <- Extract MIN from Q
for each unvisited neighbour V of U
tempDistance <- distance[U] + edge_weight(U, V)
if tempDistance < distance[V]
distance[V] <- tempDistance
previous[V] <- U
return distance[], previous[]

TELEGRAM: @eduengineering
Downloaded from www.eduengineering.net
Dijkstra's Algorithm Complexity
Time Complexity: O(E Log V)
where, E is the number of edges and V is the number of vertices.
Space Complexity: O(V)

Floyd Warshall Algorithm

Floyd-Warshall Algorithm is an algorithm for finding the shortest path between all the pairs of
vertices in a weighted graph. This algorithm works for both the directed and undirected weighted
graphs. But, it does not work for the graphs with negative cycles (where the sum of the edges in a
cycle is negative).
A weighted graph is a graph in which each edge has a numerical value associated with it.
Floyd-Warhshall algorithm is also called as Floyd's algorithm, Roy-Floyd algorithm, Roy-Warshall
algorithm, or WFI algorithm.
This algorithm follows the dynamic programming approach to find the shortest paths.

How Floyd-Warshall Algorithm Works?


Let the given graph be:

Initial graph
Follow the steps below to find the shortest path between all the pairs of vertices.
1. Create a matrix A0 of dimension n*n where n is the number of vertices. The row and the
column are indexed as i and j respectively. i and j are the vertices of the graph.
Each cell A[i][j] is filled with the distance from the ith vertex to the jth vertex. If there is no
path from ith vertex to jth vertex, the cell is left as infinity.

Fill each cell with the distance between ith and jth vertex

2. Now, create a matrix A1 using matrix A0. The elements in the first column and the first row
are left as they are. The remaining cells are filled in the following way.
Let k be the intermediate vertex in the shortest path from source to destination. In this
step, k is the first vertex. A[i][j] is filled with (A[i][k] + A[k][j]) if (A[i][j] > A[i][k] + A[k][j]).
That is, if the direct distance from the source to the destination is greater than the path
through the vertex k, then the cell is filled with A[i][k] + A[k][j].
TELEGRAM: @eduengineering
Downloaded
In this step, k is vertexfrom www.eduengineering.net
1. We calculate the distance from source vertex to destination vertex
through this vertex

k. Calcula
te the distance from the source vertex to destination vertex through this vertex k

For example: For A1[2, 4], the direct distance from vertex 2 to 4 is 4 and the sum of the
distance from vertex 2 to 4 through vertex (ie. from vertex 2 to 1 and from vertex 1 to 4) is 7.
Since 4 < 7, A0[2, 4] is filled with 4.
3. Similarly, A2 is created using A1. The elements in the second column and the second row are
left as they are.
In this step, k is the second vertex (i.e. vertex 2). The remaining steps are the same as in step

2. Calcula
te the distance from the source vertex to destination vertex through this vertex 2

4. Similarly, A3 and A4 is also created.

Calculat
e the distance from the source vertex to destination vertex through this

TELEGRAM: @eduengineering
Downloaded from www.eduengineering.net

vertex C
alculate the distance from the source vertex to destination vertex through this vertex 4
5. A4 gives the shortest path between each pair of vertices.

Floyd-Warshall Algorithm
n = no of vertices
A = matrix of dimension n*n
for k = 1 to n
for i = 1 to n
for j = 1 to n
Ak[i, j] = min (Ak-1[i, j], Ak-1[i, k] + Ak-1[k, j])
return A

Time Complexity
There are three loops. Each loop has constant complexities. So, the time complexity of the Floyd-
Warshall algorithm is O(n3).

Network Flow
Flow Network is a directed graph that is used for modeling material Flow. There are two different
vertices; one is a source which produces material at some steady rate, and another one is sink which
consumes the content at the same constant speed. The flow of the material at any mark in the
system is the rate at which the element moves.
Some real-life problems like the flow of liquids through pipes, the current through wires and delivery
of goods can be modelled using flow networks.
Definition: A Flow Network is a directed graph G = (V, E) such that
1. For each edge (u, v) ∈ E, we associate a nonnegative weight capacity c (u, v) ≥ 0.If (u, v) ∉ E,
we assume that c (u, v) = 0.
2. There are two distinguishing points, the source s, and the sink t;
3. For every vertex v ∈ V, there is a path from s to t containing v.
Let G = (V, E) be a flow network. Let s be the source of the network, and let t be the sink. A flow in G
is a real-valued function f: V x V→R such that the following properties hold:
Play Video
o Capacity Constraint: For all u, v ∈ V, we need f (u, v) ≤ c (u, v).
o Skew Symmetry: For all u, v ∈ V, we need f (u, v) = - f (u, v).
o Flow Conservation: For all u ∈ V-{s, t}, we need

The quantity f (u, v), which can be positive or negative, is known as the net flow from vertex u to
vertex v. In the maximum-flow problem, we are given a flow network G with source s and sink t, and
we wish to find a flow of maximum value from s to t.
TELEGRAM: @eduengineering
Downloaded
Ford-Fulkerson Algorithm from www.eduengineering.net

Initially, the flow of value is 0. Find some augmenting Path p and increase flow f on each edge of p by
residual Capacity cf (p). When no augmenting path exists, flow f is a maximum flow.
FORD-FULKERSON METHOD (G, s, t)
1. Initialize flow f to 0
2. while there exists an augmenting path p
3. do argument flow f along p
4. Return f

FORD-FULKERSON (G, s, t)
1. for each edge (u, v) ∈ E [G]
2. do f [u, v] ← 0
3. f [u, v] ← 0
4. while there exists a path p from s to t in the residual network Gf.
5. do cf (p)←min?{ Cf (u,v):(u,v)is on p}
6. for each edge (u, v) in p
7. do f [u, v] ← f [u, v] + cf (p)
8. f [u, v] ←-f[u,v]

Example: Each Directed Edge is labeled with capacity. Use the Ford-Fulkerson algorithm to find the
maximum flow.

Solution: The left side of each part shows the residual network Gf with a shaded augmenting path
p,and the right side of each part shows the net flow f.

TELEGRAM: @eduengineering
Downloaded from www.eduengineering.net

Maximum Bipartite Matching


The bipartite matching is a set of edges in a graph is chosen in such a way, that no two edges in that
set will share an endpoint. The maximum matching is matching the maximum number of edges.

When the maximum match is found, we cannot add another edge. If one edge is added to the
maximum matched graph, it is no longer a matching. For a bipartite graph, there can be more than
one maximum matching is possible.

Algorithm

bipartiteMatch(u, visited, assign)


Input: Starting node, visited list to keep track, assign the list to assign node with another node.
Output − Returns true when a matching for vertex u is possible.
Begin
for all vertex v, which are adjacent with u, do
if v is not visited, then
mark v as visited
if v is not assigned, or bipartiteMatch(assign[v], visited, assign) is true, then
assign[v] := u
return true
done
return false
End
maxMatch(graph)
Input − The given graph.
Output − The maximum number of the match.
Begin
initially no vertex is assigned
count := 0
for all applicant u in M, do
make all node as unvisited
if bipartiteMatch(u, visited, assign), then
increase count by 1
done
End

TELEGRAM: @eduengineering
CONNECT WITH US

CONNECT WITH US

WEBSITE: www.eduengineering.net

TELEGRAM: @eduengineering
-

INSTAGRAM: @eduengineering

 Regular Updates for all Semesters


 All Department Notes AVAILABLE
 Handwritten Notes AVAILABLE
 Past Year Question Papers AVAILABLE
 Subject wise Question Banks AVAILABLE
 Important Questions for Semesters AVAILABLE
 Various Author Books AVAILABLE

You might also like