GRAPH TRAVERSALS & BI-CONNECTED COMPONENTS

Prof. Shaik Naseera Department of CSE JNTUACEK, Kalikiri Objectives • Graph: Definitions and Representations • Traversing Graphs • Depth-First Search on Directed Graphs • Biconnected Components of an Undirected Graph Definition: • Directed Graph A directed graph, or digraph, is a pair G = (V, E) where V is a set whose elements are called vertices, and E is a set of directed edges, or arcs, The vertices are connected by edges, where the edges have a direction associated with them. Definition: Undirected graph • Undirected Graph A undirected graph is a pair G = (V, E) where V is a set whose elements are called vertices, and E is a set of edges, or undirected edges. Vertices are connected by edges, where all the edges are bidirectional. Definitions: Weighted Graph • A weighted graph is a triple (V, E, W) where (V, E) is a graph (directed or undirected) and W is a function from E into R, the reals (integer or rationals). For an edge e, W(e) is called the weight of e. Graph Representation • Adjacency Matrix • Adjacency List Graph Representations using Data Structures • Adjacency Matrix Representation Let G = (V,E), n = |V|, m = |E|, V = {v1, v2, …, vn) G can be represented by an n  n matrix Adjacency Matrix for weight digraph Array of Adjacency Lists Representation • From to Array of Adjacency Lists Representation

from -> to, weight Graph Traversal • graph traversal (also known as graph search) refers to the process of visiting each in a graph. • Types : There are two traversal strategies that provide an efficient way to “visit” each vertex and edge exactly once. Breadth First Search Depth First Search Breadth-first search • Most algorithms for solving problems on a graph examine or process each vertex and each edge. • Breadth-first search: Strategy (for digraph) choose a starting vertex, distance d = 0 vertices are visited in order of increasing distance from the starting vertex, examine all edges leading from vertices (at distance d) to adjacent vertices (at distance d+1) then, examine all edges leading from vertices at distance d+1 to distance d+2, and so on, until no new vertex is discovered

Uses Queue to store the vertices when they are visited. When a new vertex is visited, it is stored in the queue Depth First Search: • visit nodes adjacent to the last visited node first • Uses stack to store the vertices when they are visited. • When a new vertex is visited, it is pushed into the stack and the search continues from there recursively until a dead end is reached/no more unvisited vertex is available. Algorithm for BFS • Algorithm BFs & DFS.docx Example1 Example Example3 Example 4 Example 5 BFS DFS BFS stands for Breadth First DFS stands for Depth First Search. SearchDFS BFS uses Queue data structure DFS uses Stack data structure. for finding the shortest path. BFS can be used to find single In DFS, we might traverse source shortest path in an through more edges to reach a unweighted graph, because in destination vertex from a BFS, we reach a vertex with source. minimum number of edges from a source vertex. BFS is more suitable for DFS is more suitable when searching vertices which are there are solutions away from closer to the given source. source. The Time complexity of BFS is The Time complexity of DFS is O(V + E), where V stands for also O(V + E), where V stands vertices and E stands for for vertices and E stands for edges. edges. • Algorithm BFS & DFS Applications of DFS: Articulation Points and Biconnected Components Articulation Point

• Let G = (V,E) be a connected undirected graph.

Articulation Point: is any vertex of G whose removal results in a disconnected graph. Articulation Point

Articulation Point: is any vertex of G whose removal results in a disconnected graph. Biconnected components

• A graph is biconnected if it contains no articulation points.

• Two edges are cocyclic if they are equal or if there is a simple cycle that contains both edges. (Two different ways of getting from one edge to the other)  This defines an equivalence relation on the edges of the graph

• Biconnected components of a graph are the equivalence classes of cocyclicity relation Biconnected components

• A graph is biconnected if and only if it consists of a single  No articulation points Articulation points and DFS • How to find articulation points? Use the tree structure provided by DFS G is undirected: tree edges and back edges (no difference between forward and back edges, no cross edges)

• Assume G is connected internal vertex u

• Consider an internal vertex u Not a leaf, Assume it is not the root

u • Let v1, v2,…, vk denote the children of u Each is the root of a subtree of DFS

If for some child, there is no back edge from any node in this subtree going to a proper ancestor of u, then u is an Here u is an articulation point articulation point internal vertex u

u • Here u is not an articulation point A back edge from every subtree of u to proper ancestors of u exists What if u is a leaf • A leaf is never an articulation point • A leaf has no subtrees.. What about the root?

• the root is an articulation point if an only if it has two or more children. Root has no proper ancestor There are no cross edges between its subtrees

This root is an articulation point How to find articulation points? • Keep track of all back edges from each subtree? Too expensive Define Low[u] or L[u] • Low[u]: minimum of d[u] and {d[w] | where (v,w) is a back edge and v is a (nonproper) descendent of u.} Computation of L[u] • L[u]=min { dfn[u], min{ L[w] | w is a child of u}, min{ dfn[w] | is a back edge} }

• u is an articulation point iff u has a child w such that L[w]>=dfn[ u ]

Note : L[u] tells the lowest numbered vertex reachable from u other than its parent. example

DFS spanning tree L[1:10] computed by postorder traversal order • L[10]=4 • L[9]=5 • L[6]=8 • L[8]=min{dfn(8),dfn(5),dfn(2)} = min{10,7,6}=6 • L[7]= min{dfn(2), L[8],dfn(7)} = min{6,6,9}=6 • L[5]=min{dfn(5),L(6),L(7)} = min{7,8,6}=6 • L[2]=min{dfn(2),L(5),dfn(1)} = min{6,6,1}=1 • L[3]=min{dfn(3),L(10),L(9),L(2)} = min{3,4,5,1}=1 • L[4]=min{dfn(4),L(3)} = min{2,1}=1 • L[1]=min{dfn(1),L(4)} L[w]>=dfn[u] then u is articulation point = min{1,1}=1 u is parent w is child in dfs tree Computation of L[u] • L[u]=min { dfn[u], min{ L[w] | w is a child of u}, min{ dfn[w] | is a back edge} }

• To compute L[u], follow the post order traversal of the dfs spanning tree. • L[1:10] ={1,1,1,1,6,8,6,6,5,4}

• u is an articulation point iff u has a child w such that L[w]>=dfn[ u ]

Note : L[u] tells the lowest numbered vertex reachable from u other than its parent. Biconnected components Algorithm to construct biconnected graph New Edges • <4,10><10,9> corresponding to articulation point 3 • <1,5> corresponding to the articulation point 2 • <6,7> corresponding to the articulation point 5 • Once Low[u] is computed for all vertices u, we can test whether a nonroot vertex u is an articulation point

• u is an articulation point iff it has a child w for which Low[w] >= d[u] Example problem Gate Questions

21nodes

U is visited before v

THANK YOU