0% found this document useful (0 votes)
5 views28 pages

Lect 28 - 2024

Uploaded by

OPM Rocky
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)
5 views28 pages

Lect 28 - 2024

Uploaded by

OPM Rocky
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/ 28

BITS F232

aft
Foundations of
Data Structures and Algorithms
Lect 27,28
Sameera Muhamed Salam
Dept of CSIS, BITS Pilani, Hyderabad Campus

Dr
Graph Traversals

BITS F232 Sameera M S 2


Depth First Search in an undirected graph
▶ Used for finding
▶ Path from one vertex to another
▶ Testing Connectivity
▶ To compute Spanning tree
▶ To compute cycle in G
▶ Searching starts from a Starting node s
▶ Mark it as visited
▶ select one arbitrary edge e incident on s
▶ let u be the other endpoint of e. Mark u as visited
▶ select an arbitrary edge e incident on u
▶ let v be the other endpoint of e
▶ If v is already visited, backtrack to u to find another unvisited vertex from v
▶ Repeat this process untill we reach a dead end (a vertex whose all neighbours are
visited)
▶ Then we backtrack along the visited nodes until we find a vertex with an unvisited
neighbour or we reach the start vertexSameera
BITS F232
s. M S 3
DFS: Pseudo code

BITS F232 Sameera M S 4


Recursive DFS

BITS F232 Sameera M S 5


DFS: Running Time
Theorem
Let G be an undirected graph on which a DFS Traversal starting from a vertex s has
been performed. Then the traversal visits all the vertices in the connected component
of s and the discovery edges forms a spanning tree of the connected component of s

▶ DFS is called exactly once on each vertex


▶ Each edge is visited exactly twice
▶ If ms is the number of edges in the connected component of s then DFS starting
at s takes O(ms ) time provided
▶ incidentEdges(v ) takes O(degree(v )) time
▶ hasNext() and nextEdge() returned by incidentEdges(v ) each takes O(1) time
▶ opposite(v , e) takes O(1) time
▶ Which implementation satisfies all these conditions?

BITS F232 Sameera M S 6


DFS: Running Time
Theorem
Let G be an undirected graph on which a DFS Traversal starting from a vertex s has
been performed. Then the traversal visits all the vertices in the connected component
of s and the discovery edges forms a spanning tree of the connected component of s

▶ DFS is called exactly once on each vertex


▶ Each edge is visited exactly twice
▶ If ms is the number of edges in the connected component of s then DFS starting
at s takes O(ms ) time provided
▶ incidentEdges(v ) takes O(degree(v )) time
▶ hasNext() and nextEdge() returned by incidentEdges(v ) each takes O(1) time
▶ opposite(v , e) takes O(1) time
▶ Which implementation satisfies all these conditions?
▶ Adjacency List
▶ Marking a vertex can be done in O(1) time.
BITS F232 Sameera M S 7
DFS: Running Time

DFS: Running Time


Let G be a graph with n vertices and m edges represented with adjacency list
structure. A DFS traversal of G can be performed in O(m + n) time. There exists a
O(m + n) time for the following problems
▶ Testing whether G is connected
▶ Computing Spanning forest of G
▶ Computing connected components of G
▶ Computing path between two vertices in G (if exists)
▶ Computing a cycle in G (if exists)

BITS F232 Sameera M S 8


Biconnected Components
▶ cut vertex or seperating vertex: A vertex whose removal disconnects the graph
▶ Bridge or seperating edge: An edge whose removal disconnects the graph
▶ Biconnected Component of G: satisfies any one of the following
▶ A subgraph of G which is biconnected and adding any additional vertices or edges
would force it to stop being biconnected
▶ A seperating edge and its end points

▶ Let G be a connected graph. The


following are equivalent
▶ G is biconnected
▶ For any 2 vertices of G, there is a
simple cycle containing them
▶ G does not have any seperation
edges or seperation vertices
Biconnected Components are important in Computer networks where vertices are
routers and edges represents connections.
BITS F232 Sameera M S 9
Breadth-First Search
▶ BFS is less adventurous than DFS
▶ Proceeds in rounds and subdivides vertices in to levels

BITS F232 Sameera M S 10


Discovery edges, Tree Edges and Back Edges
If you orient edges along the direction in which they are explored,edges in graph can be
divided as the following
▶ Discovery edges: Edges used to discover new vertices
▶ Tree edges: Edges in the DFS/BFS tree
▶ Back edges: current vertex to already visited vertex (DFS)
▶ Cross edges: current vertex to already visited vertex (BFS)

Theorem

BITS F232 Sameera M S 11


Running Time

BITS F232 Sameera M S 12


Compairing DFS and BFS

▶ BFS is better for finding shortest path


▶ BFS produces spanning tree such that all non tree edges are cross edges
▶ DFS produces spanning tree such that all non tree edges are back edges
▶ DFS is good to find disjoint paths between vertices

BITS F232 Sameera M S 13


Directed graphs and Reachability



we represent a directd graph or a digraph as G


▶ Given two vertices u and v in a digraph(Directed graph) G , we say u reaches v or


v is reachable from u if G has a directed path from u to v


▶ G is strongly connected if, for any two pair of vertices u and v
▶ u reaches v and v reaches u

− →

▶ G is acyclic if G does not contain directed cycles


▶ Transitive Closure (G ∗ ):

− −

▶ All vertices of G are vertices in G ∗

→∗ →

▶ uv is an edge in G whenever G has adirected path from u to v.

BITS F232 Sameera M S 14


Directed graph:Example

BITS F232 Sameera M S 15


Interesting Problems in Digraphs

Trvaersing a diagraph can be done using BFS and DFS. Directed DFS partitioned
edges into following
▶ Tree edges or discovery edges
▶ non tree edges
▶ Back edges: Connects a vertex to its ancestor in the tree
▶ Forward edges: Connects a vertex to its descendant in the tree
▶ Cross edges: Connects a vertex to vertex which is neither its ancestor nor its
descendant in the tree
▶ There will not be any forwarded edges after traversing in a BFS Manner.
BITS F232 Sameera M S 16
Testing for Strong Connectivity

▶ Can check for strong connectivity faster than O(n(n + m)) time using two DFS.

BITS F232 Sameera M S 17


Testing for Strong Connectivity

▶ Can check for strong connectivity faster than O(n(n + m)) time using two DFS.


▶ Do DFS from an arbitraty vertex s in G

− →

▶ If there is any vertex v in G that is not visited and is not reachable from s then G is
not strongly connected
▶ Reverse direction of all edges
▶ Do another DFS starting at s


▶ If every vertex is visited by second DFS also then G is strongly connected.
▶ Running time is O(n + m) time
BITS F232 Sameera M S 18
Dynamic Programming
▶ Like the divide-and-conquer method
▶ Solves problems by combining the solutions to subproblems
▶ Divide-and-conquer algorithms partition the problem into independent
subproblems, solve the subproblems recursively, and then combine their solutions
to solve the original problem.
▶ In contrast, dynamic programming is applicable when the subproblems are not
independent, that is, when subproblems share subsubproblems.
▶ A dynamic-programming algorithm solves every subsubproblem just once and then
saves its answer in a table, thereby avoiding the work of recomputing the answer
every time the subsubproblem is encountered.
▶ Dynamic programming is typically applied to optimization problems.
▶ In such problems there can be many possible solutions.
▶ We wish to find a solution with the optimal (minimum or maximum) value. We
call such a solution an optimal solution to the problem.
BITS F232 Sameera M S 19
Dynamic Programming

BITS F232 Sameera M S 20


Transitive closure using Dynamic Programming

▶ Constructing using series of rounds




▶ Number the vertices in G as v1 , v2 , . . . , vn

− →

▶ We start with G 0 = G

− →

▶ In each step k we start G k = G k−1


▶ Add edge (vi , vj ) if G k−1 contains (vi , vk ) and (vk , vj )

Lemma

− →

For i − 1, 2, . . . , n, disgraph G k has an edge (vi , vj ), iff G has a directed path from vi
to vj whose intermediate vertices are in the set {v1 , v2 , . . . , vk }. In particular,

− →

Gn = G∗

BITS F232 Sameera M S 21


Floyd-Warshall Algorithm

Running Time
▶ Adjacency Matrix: n DFS takes O(n3 ) time
▶ Adjacency List: O(n(n + m)). For dense graph O(n3 ) time
BITS F232 Sameera M S 22
Example

BITS F232 Sameera M S 23


Example

BITS F232 Sameera M S 24


Example

BITS F232 Sameera M S 25


References:

▶ Michael T. Goodrich and Roberto Tamassia, “ Algorithm Design Foundations,


Analysis, and Internet Examples”, Wiley Student Edition.
▶ Jon Kleinberg and Eva Tardos, “ Algorithm Design”, Pearson Publishers.
▶ Thomas H. Chorman, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein, “
Introduction to algorithms”, MIT Press Publishers.
▶ Sanjoy Dasgupta, Umesh Vazirani, Christos Papadimitriou, “ Algorithms”,
McGraw-Hill Education Publishers.

BITS F232 Sameera M S 26


Discussion Time!

BITS F232 Sameera M S 27


BITS F232 Sameera M S 28

You might also like