Lect 28 - 2024
Lect 28 - 2024
aft
Foundations of
Data Structures and Algorithms
Lect 27,28
Sameera Muhamed Salam
Dept of CSIS, BITS Pilani, Hyderabad Campus
Dr
Graph Traversals
Theorem
→
−
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.
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.
▶ 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
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∗
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