UNIT - 2 Advanced Algorithm PDF
UNIT - 2 Advanced Algorithm PDF
Page47
MATROIDS:
Matroid is a structure that abstracts and generalizes the notion of linear independence in vector
spaces. Matroid is a pair ⟨X,I⟩⟨X,I⟩ where XX is called ground set and II is set of
all independent subsets of XX. In other words matroid ⟨X,I⟩⟨X,I⟩ gives a classification for each subset
of XX to be either independent or dependent (included in II or not included in II).Of course, we are not
speaking about arbitrary classifications. These 3 properties must hold for any matroid:
1. Empty set is independent.
2. Any subset of independent set is independent.
3. If independent set AA has smaller size than independent set BB, there exist at least one element
in BB that can be added into AA without loss of independency.
These are axiomatic properties of matroid. To prove that we are dealing with matroid, we generally
have to prove these three properties. For example, explicit presentation of matroid on ground
set {x,y,z}{x,y,z} which considers {y,z}{y,z} and {x,y,z}{x,y,z} to be dependent and marked red.
Other sets are independent and marked green in the below diagram.
Examples
There are matroids of different types. There are some examples:
Uniform matroid. Matroid that considers subset SS independent if size of SS is not greater than
some constant kk (|S|≤k|S|≤k). Simplest one, this matroid does not really distinguish elements of
ground set in any form, it only cares about number of taken elements. All subsets of size kk are
bases for this matroid, all subsets of size (k+1)(k+1) are circuits for this matroid. We can also define
some specific cases of uniform matroid.
Trivial matroid. k=0 k=0. Only empty set is considered independent, any element of ground set is
considered dependent (any combination of ground set elements is also considered dependent as a
consequence).
Complete matroid. k=|X| k=|X|. All subsets are considered independent including complete ground
set itself.
Linear (algebra) matroid. Ground set consists of vectors of some vector space. Set of vectors is
considered independent if it is linearly independent (no vector can be expressed as linear
Page48
combination of other vectors from that set). This is the matroid from which whole matroid theory
originates from. Linear bases of vector set are bases of matroid. Any circuit of this matroid is set of
vectors, where each vector can be expressed as combination of all other vectors, but this
combination involves all other vectors in circuit.
Colorful matroid. Ground set consists of colored elements. Each element has exactly one color. Set
of elements is independent if no pair of included elements share a color. Rank of a set is amount of
different colors included into a set. Bases of this matroid are sets that have exactly one element of
each color. Circuits of this matroid are all possible pairs of elements of the same color.
Graphic matroid. This matroid is defined on edges of some undirected graph. Set of edges is
independent if it does not contain a cycle. This type of matroids is the greatest one to show some
visual examples, because it can include dependent subsets of a large size and can be represented on
a picture at the same time. If graph is connected then any basis of this graph is just a spanning tree
of this graph. If graph is not connected then basis is a forest of spanning trees that include one
spanning tree for each connected component. Circuits are simple loops of this graph. Independence
oracle for this matroid type can be implemented with DFS, BFS (start from each vertex in graph and
check that no edge connect a vertex with already visited one) or DSU (keep connected components,
start with disjoint vertices, join by all edges and ensure that each edge connected different
components upon addition). Here is an example of circuit combinations property in graphic matroid:
Truncated matroid. We can limit rank of any matroid by some number kk without breaking
matroid properties. For example, basis of truncated colorful matroid is set of elements that include
no more than kk different colors and all colors are unique. Basis of truncated graphic matroid is
acyclic set of edges that leaves at least (n−k)(n−k) connected components in the graph (where nn is
amount if vertices in a graph). This is possible because third matroid property does not only refer to
bases of matroid, but to any independent set in matroid and when all independent sets with sizes
greater than kk are simultaneously removed, independent sets of size kk become new bases and for
any lesser independent set can still find elements from each basis that can be added.
Matroid on a subset of ground set. We can limit ground set of matroid to its subset without
breaking matroid properties. This is possible because rules of dependence does not rely on specific
elements being in ground set. If we remove an edge from a graph, we will still have a valid graph. If
we remove an element from set (of vectors or colored elements) we will still get a valid set of some
Page49
element of the same type and rules will preserve. Now, we can also define rank of subset in matroid
as a rank of matroid on a ground set limited to this subset.
Expanded matroid. Direct matroid sum. We can consider two matroids as one big matroid
without any difficulties if elements of ground set of first matroid does not affect independence,
neither intersect with elements of ground set of second matroid and vice versa. If we consider two
graphic matroids on two connected graphs, we can unite their graphs together resulting in graph
with two connected components, but it is clear that including some edges in one component have no
effect on other component. This is called direct matroid sum. Formally,
M1=⟨X1,I1⟩M1=⟨X1,I1⟩,
M2=⟨X2,I2⟩M2=⟨X2,I2⟩,
M1+M2=⟨X1∪X2,I1×I2⟩M1+M2=⟨X1∪X2,I1×I2⟩,
Where ×× means cartesian product of two sets. We can unite as many matroids of as many different
types without restrictions.
Greedy Algorithm:
In Greedy Algorithm a set of resources are recursively divided based on the maximum, immediate
availability of that resource at any given stage of execution. To solve a problem based on the greedy
approach, there are two stages
1. Scanning the list of items
2. Optimization.
These stages are covered parallelly, on course of division of the array.
To understand the greedy approach, we need to have a working knowledge of recursion and context
switching. This helps us to understand how to trace the code.
Two conditions define the greedy paradigm
Each stepwise solution must structure a problem towards its best-accepted solution.
It is sufficient if the structuring of the problem can halt in a finite number of greedy steps.
Page50
The greedy paradigm was registered as a different type of optimization strategy in the NIST records
in 2005.
Till date, protocols that run the web, such as the open-shortest-path-first (OSPF) and many other
network packet switching protocols use the greedy strategy to minimize time spent on a network.
In the greedy scan shown here as a tree (higher value higher greed), an algorithm state at value: 40, is
likely to take 29 as the next value. Further, its quest ends at 12. This amounts to a value of 41.
However, if the algorithm took a sub-optimal path or adopted a conquering strategy then 25 would be
followed by 40, and the overall cost improvement would be 65, which is valued 24 points higher as a
suboptimal decision.
Page52
ALGORITHM TO COMPUTE A MAXIMUM WEIGHT MAXIMAL
INDEPENDENT SET:
The Greedy Approach and Divide and conquer algorithms are used to compute a maximum
weight maximal independent set.
Greedy Algorithm: The maximum (weighted) independent set (MIS(MWIS)) is one of the most
important optimization problems. In several heuristic methods for optimization problems,
the greedy strategy is the most natural and simplest one. For MIS, two simple greedy algorithms have
been investigated. One is called GMIN, which selects a vertex of minimum degree, removes it and its
neighbors from the graph and iterates this process on the remaining graph until no vertex remains. (the
set of selected vertices is an independent set). The other is called GMAX, which deletes a vertex of
maximum degree until no edge remains (the set of remaining vertices is an independent set).
Divide/Break: This step involves breaking the problem into smaller sub-problems. Sub-problems
should represent a part of the original problem. This step generally takes a recursive approach to divide
the problem until no sub-problem is further divisible. At this stage, sub-problems become atomic in
nature but still represent some part of the actual problem.
Conquer/Solve: This step receives a lot of smaller sub-problems to be solved. Generally, at this level,
the problems are considered 'solved' on their own.
Merge/Combine: When the smaller sub-problems are solved, this stage recursively combines them
until they formulate a solution of the original problem. This algorithmic approach works recursively
Page53
conquers and merge steps works so close that they appear as one. Below diagram indicates this:
Page54
APPLICATION TO MST:
Tree: A tree is a graph with the following properties:
1. The graph is connected (can go from anywhere to anywhere)
2. There are no cyclic (Acyclic)
Spanning Tree:
Given a connected undirected graph, a spanning tree of that graph is a sub-graph that is a tree and
joined all vertices. A single graph can have many spanning trees.
For Example:
Page55
6. Spanning Tree does not contain cycles.
7. Spanning Tree has (n-1) edges where n is the number of vertices.
Addition of even one single edge results in the spanning tree losing its property of Acyclicity and
elimination of one single edge results in its losing the property of connectivity.
Page56
Methods of Minimum Spanning Tree
There are two methods to find Minimum Spanning Tree
1. Kruskal's Algorithm
2. Prim's Algorithm
Kruskal's Algorithm:
This is an algorithm to construct a Minimum Spanning Tree for a connected weighted graph. It is a
Greedy Algorithm. If the graph is not linked, then it finds a Minimum Spanning Tree.
Analysis:
Where E is the number of edges in the graph and V is the number of vertices, Kruskal's Algorithm
can be shown to run in O(E log E) time, or simply, O(E log V) time, all with simple data structures.
These running times are equivalent because:
o E is at most V2 and log V2= 2 x log V is O (log V).
Page57
o If we ignore isolated vertices, V ≤ 2 E, so log V is O (log E).Thus the total time is
:O (E log E) = O (E log V).
For Example: Find the Minimum Spanning Tree of the following graph using Kruskal's algorithm.
Solution: First we initialize the set A to the empty set and create |v| trees, one containing each vertex
with MAKE-SET procedure. Then sort the edges in E into order by non-decreasing weight.
There are 9 vertices and 12 edges. So MST formed (9-1) = 8 edges
Now, check for each edge (u, v) whether the endpoints u and v belong to the same tree. If they do then
the edge (u, v) cannot be supplementary. Otherwise, the two vertices belong to different trees, and the
edge (u, v) is added to A and the vertices in two trees are merged in by union procedure.
Page58
Steps to find Minimum Spanning Tree using Kruskal's algorithm
Step1: So, first take (h, g) edge Step 2: then (g, f) edge.
Step 3: then (a, b) and (i, g) edges are Step 4: Now, edge (h, i). Both h and i
considered, and the forest becomes vertices are in the same set. Thus it creates
a cycle. So this edge is discarded.
Then edge (c, d), (b, c), (a, h), (d, e), (e, f)
are considered, and the forest becomes.
Step 5: In (e, f) edge both endpoints e and Step 6: After that edge (d, f) and the final
f exist in the same tree so discarded this spanning tree is shown as in dark lines.
edge. Then (b, h) edge, it also creates a
cycle.
Step 7: This step will be required Minimum Spanning Tree because it contains all the 9
vertices and (9 - 1) = 8 edges
e → f, b → h, d → f [cycle will be formed]
Page59
Prim's Algorithm
It is a greedy algorithm. It starts with an empty spanning tree. The idea is to maintain two sets of
vertices:
o Contain vertices already included in MST.
o Contain vertices not yet included.
At every step, it considers all the edges and picks the minimum weight edge. After picking the edge, it
moves the other endpoint of edge to set containing MST.
Example: Generate minimum cost spanning tree for the following graph using Prim's algorithm.
Page60
Solution: In Prim's algorithm, first we initialize the priority Queue Q. to contain all the vertices and the
key of each vertex to ∞ except for the root, whose key is set to 0. Suppose 0 vertex is the root, i.e., r. By
EXTRACT - MIN (Q) procure, now u = r and Adj [u] = {5, 1}.Removing u from set Q and adds it to set
V - Q of vertices in the tree. Now, update the key and π fields of every vertex v adjacent to u but not in
a tree.
Vertex 0 1 2 3 4 5 6
Key 0 ∞ ∞ ∞ ∞ ∞ ∞
Value
Parent NIL NIL NIL NIL NIL NIL NIL
π[3]= 4 π[6]= 4
Vertex 0 1 2 3 4 5 6
Key 0 28 ∞ 22 25 10 ∞
Value
Parent NIL 0 NIL 4 5 0 NIL
Page62
Adj [3] = {4, 6, 2}
4 is already in heap
4 ≠ Q key [6] = 24 now becomes key [6] = 18
Key [2] = ∞ key [6] = 24
w (3, 2) = 12 w (3, 6) = 18
w (3, 2) < key [2] w (3, 6) < key [6]
Now in Q, key [2] = 12, key [6] = 18, key [1] = 28 and parent of 2 and 6 is 3.
π [2] = 3 π[6]=3
Vertex 0 1 2 3 4 5 6
Key 0 28 12 22 25 10 18
Value
Parent NIL 0 3 4 5 0 3
u = EXTRACT_MIN (2, 6)
u=2 [key [2] < key [6]]
12 < 18
Now the root is 2
Adj [2] = {3, 1}
3 is already in a heap
Taking 1, key [1] = 28
w (2,1) = 16
w (2,1) < key [1]
π[1]= 2
Page63
Vertex 0 1 2 3 4 5 6
Key
0 16 12 22 25 10 18
Value
Parent NIL 2 3 4 5 0 3
Now all the vertices have been spanned, Using above the table we get Minimum Spanning Tree.
0→5→4→3→2→1→6
[Because Π [5] = 0, Π [4] = 5, Π [3] = 4, Π [2] = 3, Π [1] =2, Π [6] =1]
Thus the final spanning Tree is
Total Cost = 10 + 25 + 22 + 12 + 16 + 14 = 99
Page64
GRAPH MATCHING:
Graph matching is the problem of finding a similarity between graphs. Graphs are commonly used to
encode structural information in many fields, including computer vision and pattern recognition, and
graph matching is an important tool in these areas. In these areas it is commonly assumed that the
comparison is between the data graph and the model graph.
The case of exact graph matching is known as the graph isomorphism problem. The problem of exact
matching of a graph to a part of another graph is called subgraph isomorphism problem.
The inexact graph matching refers to matching problems when exact matching is impossible, e.g.,
when the numbers of vertices in the two graphs are different. In this case it is required to find the best
possible match. For example, in image recognition applications, the results of image
segmentation in image processing typically produces data graphs with the numbers of vertices much
larger than in the model graphs data expected to match against. In the case of attributed graphs, even if
the numbers of vertices and edges are the same, the matching still may be only inexact.
Two categories of search methods are the ones based on identification of possible and impossible
pairings of vertices between the two graphs and methods which formulate graph matching as
an optimization problem. Graph edit distance is one of similarity measures suggested for graph
matching. The class of algorithms is called error-tolerant graph matching.
Definition
A matching graph is a sub-graph of a graph where there are no edges adjacent to each other. Simply,
there should not be any common vertex between any two edges.
Matching
Let ‘G’ = (V, E) be a graph. A subgraph is called a matching M(G), if each vertex of G is incident
with at most one edge in M, i.e.,deg(V) ≤ 1 ∀ V ∈ G. Which means in the matching graph M(G), the
vertices should have a degree of 1 or 0, where the edges should be incident from the graph G.
Page65
Notation − M(G) The Example:
In a matching,
ifdeg(V) = 1, then (V) is said to be matched
ifdeg(V) = 0, then (V) is not matched.
In a matching, no two edges are adjacent. It is because if any two edges are adjacent, then the degree of
the vertex which is joining those two edges will have a degree of 2 which violates the matching rule.
Maximal Matching
A matching M of graph ‘G’ is said to be maximal if no other edges of ‘G’ can be added to M.
Example
M1, M2, M3 from the above graph are the maximal matching of G.
Maximum Matching
It is also known as largest maximal matching. Maximum matching is defined as the maximal matching
with maximum number of edges.
The number of edges in the maximum matching of ‘G’ is called its matching number.
Page66
For a graph given in the above example, M1 and M2 are the maximum matching of ‘G’ and its
matching number is 2. Hence by using the graph G, we can form only the sub-graphs with only 2
edges maximum. Hence we have the matching number as two.
Perfect Matching
A matching (M) of graph (G) is said to be a perfect match, if every vertex of graph g (G) is incident
to exactly one edge of the matching (M), i.e.,deg(V) = 1 ∀ V
The degree of each and every vertex in the subgraph should have a degree of 1.
Note − Every perfect matching of graph is also a maximum matching of graph, because there is no
chance of adding one more edge in a perfect matching graph.
A maximum matching of graph need not be perfect. If a graph ‘G’ has a perfect match, then the
number of vertices |V(G)| is even. If it is odd, then the last vertex pairs with the other vertex, and
finally there remains a single vertex which cannot be paired with any other vertex for which the degree
is zero. It clearly violates the perfect matching principle.
Example
Page67
Note − The converse of the above statement need not be true. If G has even number of vertices, then
M1 need not be perfect.
Example
It is matching, but it is not a perfect match, even though it has even number of vertices.
A matching in a Bipartite Graph is a set of the edges chosen in such a way that no two edges
share an endpoint. A maximum matching is a matching of maximum size (maximum number of
edges). In a maximum matching, if any edge is added to it, it is no longer a matching. There can be
more than one maximum matching for a given Bipartite Graph.
Hopcroft Karp algorithm is an improvement that runs in O(√V x E) time. Let us define few
terms before we discuss the algorithm.
Free Node or Vertex: Given a matching M, a node that is not part of matching is called free node.
Initially all vertices as free (from first graph of below diagram). In second graph, u2 and v2 are free. In
third graph, no vertex is free.
Matching and Not-Matching edges: Given a matching M, edges that are part of matching are called
Matching edges and edges that are not part of M (or connect free nodes) are called Not-Matching edges.
In first graph, all edges are non-matching. In second graph, (u0, v1), (u1, v0) and (u3, v3) are matching
and others not-matching.
Page68
Alternating Paths: Given a matching M, an alternating path is a path in which the edges belong
alternatively to the matching and not matching. All single edges paths are alternating paths. Examples
of alternating paths in middle graph are u0-v1-u2 and u2-v1-u0-v2.
Augmenting path: Given a matching M, an augmenting path is an alternating path that starts from and
ends on free vertices. All single edge paths that start and end with free vertices are augmenting paths. In
below diagram, augmenting paths are highlighted with blue color. Note that the augmenting path
always has one extra matching edge.
The Hopcroft Karp algorithm is based on below concept.
A matching M is not maximum if there exists an augmenting path. It is also true other way, i.e, a
matching is maximum if no augmenting path exists. So the idea is to one by one look for augmenting
paths. And add the found paths to current matching.
In the initial graph all single edges are augmenting paths and we can pick in any order. In the middle
stage, there is only one augmenting path. We remove matching edges of this path from M and add not-
matching edges. In final matching, there are no augmenting paths so the matching is maximum.
Ford-Fulkerson algorithm
The Ford–Fulkerson method or Ford–Fulkerson algorithm (FFA) is a greedy algorithm that computes
Page69
the maximum flow in a flow network. It is sometimes called a "method" instead of an "algorithm" as
the approach to finding augmenting paths in a residual graph is not fully specified or it is specified in
several implementations with different running times.
Time Complexity: Time complexity of the above algorithm is O(max_flow * E). We run a loop while
there is an augmenting path. In worst case, we may add 1 unit flow in every iteration. Therefore the
time complexity becomes O(max_flow * E).
Page70
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 following
constraints:
a) Flow on an edge doesn’t exceed the given capacity of the edge.
b) Incoming flow is equal to outgoing flow for every vertex except s and t.
For example, consider the following graph.
A matching in a Bipartite Graph is a set of the edges chosen in such a way that no two edges share an
endpoint. A maximum matching is a matching of maximum size (maximum number of edges). In a
maximum matching, if any edge is added to it, it is no longer a matching. There can be more than one
maximum matchings for a given Bipartite Graph.
Why do we care?
There are many real world problems that can be formed as Bipartite Matching.
For example, consider the following problem:
There are M job applicants and N jobs. Each applicant has a subset of jobs that he/she is interested in.
Each job opening can only accept one applicant and a job applicant can be appointed for only one job.
Find an assignment of jobs to applicants in such that as many applicants as possible get jobs.
Page71
Example: Ford-Fulkerson Algorithm for Maximum Flow Problem
Page72
How to implement the above approach?
Let us first define input and output forms. Input is in the form of Edmonds matrix which is a 2D
array ‘bpGraph[M][N]’ with M rows (for M job applicants) and N columns (for N jobs). The value
bpGraph[i][j] is 1 if i’th applicant is interested in j’th job, otherwise 0.
Output is number maximum number of people that can get jobs.
A simple way to implement this is to create a matrix that represents adjacency matrix
representation of a directed graph with M+N+2 vertices. Call the fordFulkerson() for the matrix. This
implementation requires O((M+N)*(M+N)) extra space.
Extra space can be be reduced and code can be simplified using the fact that the graph is bipartite and
capacity of every edge is either 0 or 1. The idea is to use DFS traversal to find a job for an applicant
(similar to augmenting path in Ford-Fulkerson). We call bpm() for every applicant, bpm() is the DFS
based function that tries all possibilities to assign a job to the applicant.
In bpm(), we one by one try all jobs that an applicant ‘u’ is interested in until we find a job, or all
jobs are tried without luck. For every job we try, we do following.
If a job is not assigned to anybody,
We simply assign it to the applicant and return true. If a job is assigned to somebody else say x, then
we recursively check whether x can be assigned some other job. To make sure that x doesn’t get the
same job again, we mark the job ‘v’ as seen before we make recursive call for x. If x can get other
job, we change the applicant for job ‘v’ and return true. We use an array maxR[0..N-1] that stores the
applicants assigned to different jobs.
If bmp() returns true, then it means that there is an augmenting path in flow network and 1 unit
of flow is added to the result in maxBPM().
Each Directed Edge is labeled with capacity. Use the Ford-Fulkerson algorithm to find the maximum
flow.
Page73
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.
Page74
CHARACTERIZATION OF MAXIMUM MATCHING BY
AUGMENTING PATHS:
Augmenting Paths for Matchings:
Definitions: ñ Given a matching M in a graph G, a vertex that is not incident to any edge of M is called
a free vertex w. r. t. M. ñ for a matching M a path P in G is called an alternating path if edges in M
alternate with edges not in M. ñ An alternating path is called an augmenting path for matching M if it
ends at distinct free vertices.
Suppose there is a matching M0 with larger cardinality. Consider the graph H with edge-set
M0M (i.e., only edges that are in either M or M0 but not in both).
Each vertex can be incident to at most two edges (one from M and one from M0). Hence, the
connected components are alternating cycles or alternating path.
As jM0j>jMj there is one connected component that is a path P for which both endpoints are
incident to edges from M0. P is an alternating path.
Algorithmic idea:
As long as we find an augmenting path augment our matching using this path. When we arrive at a
matching for which no augmenting path exists we have a maximum matching.
Page75
Introduction
The blossom algorithm, created by Jack Edmunds in 1961, was the first polynomial time algorithm that
could produce a maximum matching on any graph. Previous to the discovery of this algorithm, there
were no fast algorithms that were able to find a maximum matching on a graph with odd length cycles.
The Hungarian Algorithm came the closest still created a maximum matching on a graph under the
condition that there were no odd length cycles. A matching is a graph that occurs when all vertices are
connected to at most one other vertex through a single edge and therefore the set of all edges in the
original graph do not contain any common vertices. A maximum matching occurs when the number of
edges in the matching graph is maximized.
An example of an augmenting path where matched vertices are orange and unmatched vertices are
black. The edges that make up the matching are blue.
Page76
Finding an augmenting path happens as follows. Starting with a free (not part of the matching) vertex
V, explore all of its neighbors. Either V will discover another free vertex (and thus find an augmenting
path) or it will find a vertex W that is part of the matching. If the latter occurs, mark the edge from V to
W and then extend the path to W's mate X. This will always produce an alternating path starting from
the original vertex to some other matched vertex in the graph. Finally, recurse with vertex X. This
process will continue as long as there is a vertex X that can recursed on.
Page77
The search for an augmenting path already occured as described as steps 1-5 for finding an augmenting
path. The starting point for this potential augmenting path is vertex A
1. The yellow edge connects two nodes that could be part of an alternating path back towards A.
So a blossom is present.
2. All nodes that are part of the cycle are then contracted into the supernode (red).
3. The supernode is then enqueued into Q.
After the nodes are contracted, the search for an augmenting path continues as if the supernode were a
single node. Once the search for an augmenting path is completed (regardless of if it was found or not),
the supernode is then lifted. This lifting means that the supernode once again becomes all of the original
nodes. If an augmented path was found, then there is an augmenting path from the original free node to
another free node with some vertices of the odd length cycle in the path. This augmenting path is then
inverted (just like before) in order to increase the size of the matching by one.
The search for an augmenting path is continued from supernode V as previously described, and it
is found that the augmenting path passes through the supernode.
The Algorithm
Intuitively the algorithm works by repeatedly trying to find augmenting paths (specifically paths
starting and ending with free vertices) in the graph until no more augmenting paths can be found. This
occurs when either all vertices are matched with a mate or when a free vertex is only connected to
matched vertices which recursively only match to other matched vertices.
All Vertices Matched and a maximum Not All Vertices Matched and a
matching maximum matching
Furthermore, the process of finding augmented paths can be slightly modified so that it
simultaneously can check to see if there is a blossom in the graph. This can be accomplished by labeling
each vertex in an alternating manner O (for odd) and E (for even) depending on how far away a vertex
is from the root. To start, label the original free vertex E, and then alternate labeling from there. The
Page78
algorithm that finds augmeted paths will always search for neighboring vertices from a vertex labeled E
(the matched vertex’s mate always is two edges further away from the root) and thus if an adjacent
vertex also has the label E, there is a blossom that contains both of those vertices. This blossom is then
contracted into one supernode with a label E and then the search for augmenting paths continues.
All together, this algorithm starts by finding augmenting paths within any graph while labeling vertices
E or O and then inverting these paths to increase the size of the matching by one. If a blossom is found,
it is contracted into a single node and the augmenting path algorithm continues as normal. This repeats
until all vertices of the graph have been either searched or are already part of the matching set.
Blossoms can be nested within each other, and therefore, supernodes may end up containing
other supernodes.
Time Complexity
Overall, this algorithm takes O(E * V2) time. Every attempt to find an augmenting path takes O(E) time
in order to iterate over all of the edges. Assuming that an attempt to find an augmenting path succeeds,
it will take O(V) time to flip all of the incident edges. Finally, there are O(V) blossoms in any graph and
for each blossom the smaller graph with the supernode may need to be recursed on. This means that the
total time is O(E * V2) or simply O(V4) for a dense graph.
Page79