Design and Analysis of Algorithms - U2-P1 by S. Sandhya
Design and Analysis of Algorithms - U2-P1 by S. Sandhya
Algorithms
UNIT-2
Part-1
Disjoint sets
Disjoint sets
● Disjoint-set data structure is a data structure representing a dynamic
collection of sets S = {S1,...,Sr}.
● In this data structure, each set is represented by a tree, so that each element
points to a parent in the tree. The root of each tree will point to itself. In fact,
we shall use the root of the tree as the name of the set itself;
Si And Sj are 2 sets where i ≠ j; then there is no element that is in both Si And Sj .
1.Disjoint-set union :
If Si and Sj are two disjoint sets, then their union Si U Sj= all elements x
such that x is in Si or Sj.
Thus, Si U Sj = {1,7, 8,9,2,5,10}.
Since we have assumed that all sets are disjoint, we can assume that following
the union of Si and Sj, the sets Si and Sj do not exist independently; that is, they
are replaced by Si U Sj i.e. the collection of sets.
2.Find(i):
Given the element i, find the set containing i. Thus, 4 is in set S3,and 9 is in
setS1.
Data representation of sets
● In addition, each root has a pointer to the set name, then to determine which set
an element is currently in, we follow parent links to the root of its tree and use
the pointer to the set name.
Data representations for S1, S2,S3 may be
Simple Union Algorithm
● In the Simple Union and find algorithms
○ We ignore the set names
○ Identify sets by roots of the tree representing them.
○ Union(i,j) requires 2 trees with roots i and j to be joined.
○ Set elements are numbered 1 to n, using array p[1:n]
○ i th element of this array represents the tree node that contains the element
node.
○ This array element gives the parent pointer of the corresponding tree node.
○ Array representation of S1, S2, S3; Root nodes have parent pointer as -1.
Union Operation
Union: Suppose that we wish to obtain the union of ,Si and S2 .
Since we have linked the nodes from children to parent, we simply make one of the
trees a sub tree of the other.
● To obtain the union of two sets, all that has to be done is to set the parent field
of one of the roots to the other root.
This can be accomplished by keeping a pointer to the root of the tree representing
that set.
Simple Find Algorithm
● Find(i) : Determine the root of the tree containing element i.
● Find Pointer() - is a function that takes a set name
● Simple Union and simple find algorithms are simple. But performance wise
not good
●
Drawback of simple union and find
Some sequence of union and find operations results in degenerate trees
Si = {i}, 1 ≤ i ≤ q ; then
● Each find requires a sequence of parent pointers from that element to root.
The lemma makes use of a function ⍺(p,q) which is related to the functional inverse of
Ackerman’s function A(i,j). These functions are defined as follows
Connected Components- In Graphs
● A connected component (or simply a Component) H of an undirected graph is
a maximal connected subgraph.
● By Maximal we mean that G contains no other subgraph that is both
connected and properly contains H.
● Graph G4 has 2 connected components H1 and H2
● If BFS is modified so that all the newly visited vertices are put on to a list(i.e. If
adjacency lists are used), Then the sub-graph formed by the vertices on this
list will form a connected component. Hence BFT will obtain the connected
components in 𝜭(n+e) time.
Connected Components- can be obtained from BFT or DFT
● In case of graphs that are not biconnected, the algorithm will find all
articulation points.
Bi-Connected Components
● After we determine that a graph G is not biconnected, it is desired to identify a
set of edges whose inclusion makes the graph biconnected.
● To determine such edges we should know the maximal sub graphs of G that
are biconnected.
● G( = יV י, E ) יis a maximal biconnected subgraph of G if and only if G has no
biconnected subgraph G( = ייV יי, E ) ייsuch that V ⊆ יV ייE ⊆ יEיי
Proof:
No edge can be in 2 different biconnected components. ( It requires 2 common
vertices)
● The Graph G can be transformed into biconnected by using the following
edge addition algorithm
Bi-Connected Components
Example: Using the above scheme to transform the the graph G into a
biconnected graph, we need to add the following edges
● Hence, After the edges corresponding to all articulation points are added the
there will not be any articulation points in the graph, Hence the graph is
Biconnected.
● If G has ‘p’ articulation points and ‘b’ bi-connected components, then exactly
‘b-p’ edges are added into G.
Bi-Connected Components
● Now, let’s look at the problem of identifying the articulation points and
bi-connected components of a connected graph G with n>=2 vertices.
● This problem can be efficiently solved by DFS spanning Tree of G.
● The DFS Spanning Tree of G is
Bi-Connected Components
● The numbers outside the vertices are
called the dfn(depth first numbers)s. (
represents the order of visiting)
Tree edges
Back
edges
● Tree edges, back edges
dfn(u)< dfn(v)
Bi-Connected Components
● If this cannot be done for some child w of u, then the deletion of vertex u,
produces 2 non empty components.( one is containing root, other containing
w)
● This observation leads to a simple rule to identify articulation points.
● L[u] is the lowest dfn(depth first number) that can be reached from u using a
path of descendents followed by at most one back edge.
If u is not the root then u is an articulation point iff u has a child w such that
L[w] ≥ dfn[u]
Bi-Connected Components
Example :
Calculating L[u]
values
Bi-Connected
Components
Example :
Calculating L[u]
values
Bi-Connected Components
Example :
Finding Articulation
Points
Bi-Connected Components
Example :
Finding Articulation
Points
Procedure for finding articulation points
● L[u] can be computed if the vertices of the depth first spanning tree are visited
in post order.
● The algorithm for finding the articulation points of a given graph is as follows.
Algorithm for Finding Articulation points
● This algorithm performs DFS of G
● During this newly visited vertex gets
assigned its dfn number.
● At the same time L[i] is computed
for each vertex in the tree.
● Algo assumes that
○ Connected graph G,
○ dfn array
○ L array are global.
● num=1 and is global.
● Intial call to Art is Art(1,0)
● dfn=0 before invocation of Art.
Determining BiConnected Components
● When vertex u has been explored and a return is made from the function,
then L[u] has been computed correctly.
● If w ≠ v, then either (u,w) is a back edge or dfn[w] > dfn[u] > L[u].
● In either case L[u] is correctly updated.
● Once L[1:n] is computed, articulation points can be computed in O(e) time.
● Art algorithm complexity is O(n+e), Articulation points can be determined in
O(n+e) time
● To determine bi-connected components of G,
○ In Art algorithm line 12 if L[w] ≥ dfn[u], then u is either root or an articulation point.
○ All edges encountered during this call will form a bi-connected component.
Algorithm for Determining
BiConnected Components