-
-
Notifications
You must be signed in to change notification settings - Fork 47.7k
Transitive Closure Algorithm #12940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Deepak14Jain
wants to merge
15
commits into
TheAlgorithms:master
Choose a base branch
from
Deepak14Jain:algo/transitiveClosure
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Transitive Closure Algorithm #12940
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b7ec18c
Added transitive closure with tests
314c4e0
updating DIRECTORY.md
Deepak14Jain 57adc57
Merge branch 'master' into algo/transitiveClosure
Deepak14Jain f057f1d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a21eeb3
fix lint issue
4e10940
lint fix
7bd7a06
fix line_to_long issue
637e04a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 79aceae
define return type
d5d97f4
added type hint
82cafab
fix line_too_long lint error
3cb8f77
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2684000
fix line_too_long
cb8f5a2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 10ee21d
Merge branch 'master' into algo/transitiveClosure
Deepak14Jain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
def transitive_closure(graph): | ||
Deepak14Jain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Computes the transitive closure of a directed graph using the Floyd-Warshall algorithm. | ||
|
||
Args: | ||
graph (list[list[int]]): Adjacency matrix representation of the graph. | ||
|
||
Returns: | ||
list[list[int]]: Transitive closure matrix. | ||
|
||
>>> graph = [ | ||
... [0, 1, 1, 0], | ||
... [0, 0, 1, 0], | ||
... [1, 0, 0, 1], | ||
... [0, 0, 0, 0] | ||
... ] | ||
>>> result = transitiveClosure(graph) | ||
>>> for row in result: | ||
... print(row) | ||
[1, 1, 1, 1] | ||
[1, 1, 1, 1] | ||
[1, 1, 1, 1] | ||
[0, 0, 0, 1] | ||
""" | ||
n = len(graph) | ||
ans = [[graph[i][j] for j in range(n)] for i in range(n)] | ||
|
||
# Transtive closure of (i, i) will always be 1 | ||
for i in range(n): | ||
ans[i][i] = 1 | ||
|
||
# Apply floyd Warshall Algorithm | ||
# For each intermediate node k | ||
for k in range(n): | ||
for i in range(n): | ||
for j in range(n): | ||
# Check if a path exists between i to k and | ||
# between k to j. | ||
if ans[i][k] == 1 and ans[k][j] == 1: | ||
ans[i][j] = 1 | ||
|
||
return ans | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.