0% found this document useful (0 votes)
19 views

Design and Analysis of Algorithms - U2-P1 by S. Sandhya

Uploaded by

Jagathdhathri KR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Design and Analysis of Algorithms - U2-P1 by S. Sandhya

Uploaded by

Jagathdhathri KR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Design and Analysis of

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;

● Example: When implementing Kruskal’s algorithm, we built up a minimum


spanning tree T by adding in one edge at a time. Along the way, we needed to
keep track of the connected components of T; this was achieved using a
disjoint-set data structure
Disjoint sets
- Use of forests in representing sets.
- Assume that elements of the sets are numbered 1,2,3,....n, which in practice
are indices to a symbol table of names.
- Also assume that sets being represented are pairwise disjoint. I.e.

Si And Sj are 2 sets where i ≠ j; then there is no element that is in both Si And Sj .

Example:For example, when n = 10,the elements can be partitioned into three


disjoint sets, S1 = {1,7,8,9}, S2 = {2,5,10}, and S3 = {3,4,6}

- Each set can be represented as a tree.


- In each set children nodes are linked to parent.
Disjoint set Operations- Union and Find Operations
The disjoint-set data structure supports the following operations:

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.

Possible representation of S1 U S2 =>

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

And determines the root of the tree containing i.

● Implement Find(i) by following the indices starting

From i untill we reach a node with a parent value as -1.

Example: Find(6) - starts at 6, 6’s parent is 3. 3 is the root since p[3]=-1.

● 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

Ex: q elements each in a set of its own set. I.e

Si = {i}, 1 ≤ i ≤ q ; then

Initial configuration is forest with q nodes, p[i]=0.


When we process the following sequence of union-find operations
Results a degenerate tree.
Drawback of simple union and find
● Time taken for union is constant.

n-1 unions can be processed in Օ(n) time.

● Each find requires a sequence of parent pointers from that element to root.

Time required to process a find for an element at level is Օ(i),

The total time for n find operations is Օ(Σi=1 i) = Օ(n2).

● To improve the performance of these algorithms by avoiding the creation of


degenerate trees.
Weighted Union
To avoid degenerate trees
We use a weighting rule for union
Operation. Union(i,j)

Weighting rule for union(i,j):

If the no. of nodes in the tree with


root i ≤ the no. of nodes in the tree
With root j , then make j as the parent
Of i;
otherwise make i the parent of j.
Weighted Union
When we use this weighting rule on the sequence

We Obtain the following trees


Weighted Union
Weighted Union
Weighted Union
Weighted Union
● To implement weighting rule , we should know how many nodes are there in
every tree.
● To do this, we maintain a count field in the root of every tree.
● If i is the root node, then count[i]= the no. of nodes in the tree.
● All nodes other than roots of trees have positive number in p field; maintain p
field as negative number for roots.
● The time required to perform a union operation is still Օ(1).
● Maximum time to perform a find operation is determined by the following
lemma.
Weighted Union
Collapsing Find Operation
Collapsing Rule: If j is a node on the path from i to its root and p[i] ≠ root[i], then
set p[j] to root[i].
Collapsing Find
Collapsing Find
Collapsing Find
Collapsing
Find
Collapsing Find

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

● A tree is a connected acyclic( has no cycles) graph.


Connected Components- In Graphs

● A Directed Graph G is said to be Strongly Connected iff for every pair of


distinct vertices u and v in V(G), There is a directed path from u to v and also
from v to u.

● This graph is not strongly connected as there is no path


From 3 to 2.
● A Strongly Connected component is a maximal subgraph that is
Strongly connected.
● Therefore the graph has 2 strongly connected components.
Connected Components and BFS
● If G is a connected undirected graph , then all the vertices will get visited on
the first call to BFS.

● IF G is not connected then atleast 2 calls to BFS are needed.

● So, BFS can be used to determine whether G is connected or not.

● The connected components of a graph can be obtained by BFT.

● 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

Just like BFT, DFT can also be used to


obtain the connected components of a
graph.
Example graphs and
Adjacency Lists
Connected Components and BFS and DFS
Bi-Connected Components
To define Bi-connected components we should first define an articulation point in
the graph.

In this Part we always refer to an undirected graphs.

Articulation Point: A vertex v in a connected graph G is an articulation point if


and only if the deletion of vertex v along with all edges incident to v disconnects
the graph into 2 or more non empty components.
Bi-Connected Components- Articulation Point
Example: Vertex 2 is an articulation point.
Bi-Connected Components
● 2 other articulation points vertex 5 and vertex 3.
● A graph G is Biconnected if and only if it contains no articulation points.
● The following graph is biconnected.
● Presence of Articulation points are undesirable
In many cases. Ex: Communication system.

● We are going to develop an efficient algorithm


To test whether a connected graph is Biconnected.

● 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‫יי‬

● A Maximal biconnected subgraph is a biconnected component.

● Ex1: biconnected components of the following graph


only one biconnected component.
Bi-Connected Components
● Ex2: 5 Bi-connected components
Bi-Connected Components

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

edge(4,10) With respect to articulation point 3


edge(10,9)
edge(1,5) With respect to articulation point 2
edge(6,7) With respect to articulation point 5
Bi-Connected Components
● After the edges (Vi , Vi+1) are added vertex ‘a’ is no longer an articulation
point.

● 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

● DFS spanning trees have a property


that is very useful in identifying
articulation points and biconnected
components.
Bi-Connected Components

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.

For each vertex u, define L[u] as follows

● 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.

● Therefore, To determine articulation points, we need to perform DFS on


Graph G, and visit the nodes of the DF spanning tree in Post order.

● We can perform these 2 operations in parallel.

● 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

You might also like