Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE) PDF Download

Transitive closure of a graph

Given a directed graph, find out if a vertex j is reachable from another vertex i for all vertex pairs (i, j) in the given graph. Here reachable mean that there is a path from vertex i to j. The reach-ability matrix is called the transitive closure of a graph.
For example, consider below graph
Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE)

Transitive closure of above graphs is
1 1 1 1
1 1 1 1
1 1 1 1
0 0 0 1
The graph is given in the form of adjacency matrix say ‘graph[V][V]’ where graph[i][j] is 1 if there is an edge from vertex i to vertex j or i is equal to j, otherwise graph[i][j] is 0.
Floyd Warshall Algorithm can be used, we can calculate the distance matrix dist[V][V] using Floyd Warshall, if dist[i][j] is infinite, then j is not reachable from I. Otherwise, j is reachable and the value of dist[i][j] will be less than V.
Instead of directly using Floyd Warshall, we can optimize it in terms of space and time, for this particular problem. Following are the optimizations:

  1. Instead of an integer resultant matrix (dist[V][V] in floyd warshall), we can create a boolean reach-ability matrix reach[V][V] (we save space). The value reach[i][j] will be 1 if j is reachable from i, otherwise 0.
  2. Instead of using arithmetic operations, we can use logical operations. For arithmetic operation ‘+’, logical and ‘&&’ is used, and for a min, logical or ‘||’ is used. (We save time by a constant factor. Time complexity is the same though)

Below is the implementation of the above approach

  • C++
    Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE)
    Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE)
    Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE)
  • Java
    Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE)
    Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE)
    Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE)
  • Python
    Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE)
    Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE)
  • C#
    Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE)
    Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE)
    Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE)

Output
Following matrix is transitiveclosure of the given graph
1 1 1 1
0 1 1 1
0 0 1 1
0 0 0 1
Time Complexity: O(V3) where V is number of vertices in the given graph.
See below post for a O(V2) solution.

The document Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Algorithms.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
81 videos|80 docs|33 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Transitive Closure of a Graph - Algorithms - Computer Science Engineering (CSE)

1. What is the transitive closure of a graph?
Ans. The transitive closure of a graph is a directed graph that represents the reachability between every pair of vertices in the original graph. It provides information about all possible paths between any two vertices in the graph.
2. How is the transitive closure of a graph computed?
Ans. The transitive closure of a graph can be computed using the Warshall's algorithm. This algorithm iteratively determines whether there exists a path between every pair of vertices by considering intermediate vertices. It updates the transitive closure matrix until all pairs of vertices have been considered.
3. Why is the transitive closure of a graph important in computer science engineering?
Ans. The transitive closure of a graph has various applications in computer science engineering. It can be used for analyzing the reachability between different nodes in a network, determining the transitive dependencies in a database, solving problems related to graph algorithms, and optimizing queries in databases or knowledge graphs.
4. Does the transitive closure of a graph always exist?
Ans. Yes, the transitive closure of a graph always exists. It may be represented as a matrix or a directed graph. The transitive closure matrix contains information about reachability between every pair of vertices, while the transitive closure graph shows the existence of a path between vertices.
5. What is the time complexity of computing the transitive closure of a graph using Warshall's algorithm?
Ans. The time complexity of computing the transitive closure of a graph using Warshall's algorithm is O(V^3), where V is the number of vertices in the graph. This algorithm requires three nested loops to update the transitive closure matrix. Hence, it has a cubic time complexity.
81 videos|80 docs|33 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Summary

,

mock tests for examination

,

Objective type Questions

,

pdf

,

video lectures

,

Free

,

Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE)

,

ppt

,

practice quizzes

,

study material

,

Viva Questions

,

Sample Paper

,

Exam

,

shortcuts and tricks

,

Semester Notes

,

Important questions

,

Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE)

,

Extra Questions

,

Previous Year Questions with Solutions

,

Transitive Closure of a Graph | Algorithms - Computer Science Engineering (CSE)

,

past year papers

,

MCQs

;