DAA Unit-2
DAA Unit-2
Given an undirected graph, it’s important to find out the number of connected components to
analyze the structure of the graph – it has many real-life applications.
We can use either DFS or BFS for this task.
● Algorithm: Conneted-Componts(G)
● {
● for each vertex VϵN
● flag[V]= -1;
● count=0;
● for(V=0; V<N; V++)
● {
● if(flag[V]= = -1)
● {
● DFS(V,flag)
● count++; Input: given the undirected graph
● } Result: Print the number of connected components
● }
● print(“count”);
● }
● DFS(int V, int flag)
● {
● flag[V] = 1; print(“ V ”);
● for(each adjacent node u of V )do
The key point to observe in the algorithm is that the number of
● if([u] = = -1)
connected components is equal to the number of independent
● DFS(u, flag)
DFS function calls.
● }
Bi-Connected Components(Bi-CC)
A graph is Bi-connected iff it contains no articulation points.
1 5
2 7
4
3 8
1
9
0
Graph G
6
1 5
2 7
4
3 8
1
9
0
Graph G
We have to add the new edges to the original graph to 6
convert the G into bi-connected component.
That we can do if we know the maximal sub-graphs of 5
1
G, that are bi-connected (they are G' = (V', E')). 5
4 2
● A maximal bi-connected sub-graph is a bi-connected 3 2 7
component.
3 3 8
1
9
0
Now, we have to develop an efficiant algorithm to test whether a connected
graph biconnected.
For the case of graphs that are not biconnected, this algorithm will identify all
the articulation points. Once it has been identified that a connected graph is
not biconnected, it may be desirable to determine a set of edges whose
inclusion makes the graph biconnected.
Now, Let us identify an articulation points(APs) and biconnected components of
the connected G with N≥2 vertices.
1 5
2 7
4
3 8
1
9
0
for example:
SETS : elements of a set should be distinct
Disjoint-set: Si ∩ Sj = Ø
Disjoint-set:
Let us process the following sequence of union operations using a simple union algorithm
3 n
2
2 N-1
1
1
Finally, we will get a degenerated tree
Suppose, if we want to perform the following sequence Find operations
Although these two algorithms are very easy to state, but their performance
characteristics are not very good, we can improve the performance of our union and find
algorithms by avoiding the creation of degenerated trees. To do this, we make use of a
weighting rule for Union(i, j).
Definition: Weighted rule Union(i, j):- If the number of nodes in the tree
with root i is less than the number in the tree with root j, then make j the
parent of of i, otherwise make i is the parent of j.
Note: to implement weighted rule, we need to know how many nodes there are in every tree, maintain a
count field in the root of every tree, then count[i] equals the number of nodes in that tree.
Here, avoided the creation of degenerated tree and time required to
process all the n finds is only O(n).
Still we can the improve the performance of FIND operation, using collapsing rule.
According to this theorem the maximum time to process a find is at most O(log n) if there
are n elements in a tree.
Note: Tree generate
using the weighted
union algorithms.
Suppose if we want Find(8) 8-times(sequence of finds), we will see how the
collapsing find algorithm will give the better result than simple FIND.
If Simple FIND is used, find(8) 8-times requires 24 moves.
If we used collapsing find it will required 13 moves only.(3 moves for finding root, 3
moves for collapsing and 7moves requires for remaining 7 finds).
Collapsing Find algorithm doubles the time for individual find. However, it reduces the
worst-case time over a sequence of finds.
AND/OR Graphs,
The alpha value of a max position is defined to be the minimum possible value for that
position. If the value of a min position is determined to be less than or equal to the alpha
value of its parent, then we may stop generation of the remaining children of this min
position.
The beta value of a min position is the maximum possible value for that position. If the value
of a max position is determined to be greater than or equal to the beta value of its parent
node, then we may stop generation of the remaining children of this max position.
Postorder evaluation of a game tree using alpha-beta pruning