0% found this document useful (0 votes)
67 views47 pages

DAA Unit-2

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)
67 views47 pages

DAA Unit-2

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/ 47

Unit-2

Graph Traversal Tecniques

Spanning Tree: Let G = (V,E) be an undirected connected


graph. A sub-graph t = (V, E’) of G is a spanning tree t of G iff
t is a tree

1. BFS Spanning tree


2. DFS Spanning tree
1. BFS Spanning tree
1. DFS Spanning tree
Connected Components : Set of vertices which are reachable or is a sub-graph,
in which any two verices are connected by paths and no vertices is connected
with any vertices in the super graph.

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.

A vertex such that its removal increases the number of connected


components in the graph.

A vertex whose removal increases the number of connected


components is called an articulation Point.
First try to understand what is a Bi-connected graph is and how to check if a given graph is
Bi-connected or not.

A graph is said to be Bi-connected if:


1. If it is possible to reach every vertex from every other vertex, by a simple path.
2. Even after removing any vertex the graph remains connected. For example, consider the
graph in the following figure

The given graph is clearly connected. Now try removing the


vertices one by one and observe. Removing any of the vertices
does not increase the number of connected components.
So the given graph is Bi-connected.
6

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.

This can be done by considering the depth first spanning(dfs) tree of G,


because dfs have property that is very useful in identifying APs and biconnected
components.
6

1 5

2 7
4

3 8

1
9
0

Graph G Depth first spanning tree of G


Follow the rules to identify the APs using the dfs tree:
i) the root node of a dfs is an AP iff it has at least two children.
ii) no leaf nodes are the APs.
iii) other than the root and leaf nodes, any other node(let it be u) is an AP iff L[w]
≥dfn[u](w is child of u). Here L[u] is the lowest depth first number that can be
reached from u using a path of descendents followed by at most one back edge, but
this may not true for some u, which leads to calculate L[]s for all nodes and
identify the APs in the graph G if L[w]≥ dfn[u] is true for any child w of u.

So, we have efine the L[]s


L[] = min{dfn[u], min{L[w]| w is a child of u}, min{dfn[w] | (u,w) is a back edge}}
L[] = min{dfn[u], min{L[w]| w is a child of u}, min{dfn[w] | (u,w) is a back edge}}
Start from the leaf nodes to the L[]s.
L[10]= min{dfn[10], min{--}, min{--}}= 4.
L[9]= min{dfn[9], min{--}, min{--}}= 5.
L[6]= min{dfn[6], min{--}, min{--}}= 8.
L[8]= min{dfn[8], min{--}, min{6}}= 6.
L[7]= min{dfn[7], min{6}, min{6}}= 6.
L[5]= min{dfn[5], min{8, 6}, min{6}}= 6.
L[2]= min{dfn[2], min{6}, min{1}}= 1.
L[3]= min{dfn[3], min{4, 5, 1}, min{--}}= 1.
L[4]= min{dfn[4], min{1}, min{--}}= 1.
L[1:10]= {1, 1, 1, 1, 6, 8, 6, 6, 5, 4}
L[1]= min{dfn[1], min{1}, min{--}}= 1.
L values are L[1:10]= {1, 1, 1, 1, 6, 8, 6, 6, 5, 4}

rules to identify the APs using the dfs tree:


i) the root node is not an AP because it has no two children, hence, node-1 is not AP
ii) the leaf nodes 10, 9, 6, 8 are not APs.
iii) For any other nodes, if L[w]≥dfn[u] for any w, then u is an AP.

Node 7, 6≥9 is false, so node 7 is not a AP.


Node 5, 8≥7 if true, so node 8 is a AP
Node 3, 4≥3 and 5≥3, so node 3 is a AP
Node 4, 1≥2 , so node 4 is not a AP
Node 2, 6≥6 , so node 2 is AP
Node 1, 1≥1 , but, root node is an AP iff the root node of a depth first spanning
tree has two children.
Algorithm BiComp(u, v)
//u is a start vertex for depth first search. v is its parent if any in the depth first panning tree. It is assumed that the global array dfn is initially
//zero and that the global variable num is initialized to 1. n is the number of vertices in G
{
dfn[u]:= num; L[u]=num; num= num + 1;
for each vertex w adjacent from u do
{
if ((v ≠ w) and (dfn[w]<dfn[u)) then add (u, w) to the top of a stack s;
if (dfn[w] =0) then
{
BiComp(w, u); // w is unvişited.
L[u]= min(L[u], L[w]);
if (L[w] ≥ dfn[u]) then
{
write ("New bi-component");
repeat
{
Delete an edge from the top of stack s;
Let this edge be (x, y);
write (x, y);
} until (((x, y) = (u, w)) or ((x, y) = (w, u)));
}
}
elseif (w ≠ v) then L[u]=min(L[u], dfn[w]);
}
}
Example 2
Now consider the following graph and find the Aps and Bi-Connected Components.
Sets and Disjoint-sets are special type of data structures, are used in implementing
some algorithms(Kruskal's algorithm).

for example:
SETS : elements of a set should be distinct

Disjoint-set: Si ∩ Sj = Ø
Disjoint-set:

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


Simple FIND and UNION Algorithms
Union operation

Initially, we have n elements are in forest or having n independent sets

Their array representation is


i [1] [2] [3] [4] .... [n]
p -1 -1 -1 -1 .... -1

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

Find(1), Find(2),. . . . . .., Find(n).


The simple Union and Find operations time complexities are O(n) and O(n2) respectively.

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.

Definition: 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].
Theorem: Assume that we start with a forest of trees, each having one node.
Let T be a tree with n nodes created as a result of algorithm WeightedUNION.
The height of T is no greater than log2 n + 1.

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,

An AND/OR graph is a graph which represents a problem-solving process.


The solution involves decomposing the problem into smaller problems.
We then solve these smaller problems.
AND/OR graph corresponding to the laundry problem
Game trees

Complete game tree for Nim Game with n=6


It is estimated that the game tree for chess has >10100 nodes. Even using a computer which
is capable of generating 1011 nodes a second, the complete generation of the game tree for
chess would require more than 1080 years.
Alpha-beta pruning(cutoffs) or min-max process

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

Portion of Game Tree for a Hypothetical Game.


The value of terminal nodes is obtained from the evaluation function(E(x)) for player A.
Rough notes

You might also like