Skip to content

Add matrix_shortest_path_using_depth_first_search.py #7721

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

Conversation

sinsankio
Copy link
Contributor

Describe your change:

finding the shortest path of a non symmetrical matrix (m x n) between given start and end points including consideration of obstacles using depth first search approach.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

finding the shortest path of a
non symmetrical matrix (m x n) between given start and end points including consideration of obstacles using depth first search approach.
@algorithms-keeper algorithms-keeper bot added require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required labels Oct 27, 2022
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

current_shortest_path_distance = math.inf


def find_shortest_path_using_dfs(matrix: list, i: int, j: int, distance: int) -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file path_tracing/matrix_shortest_path_using_depth_first_search.py, please provide doctest for the function find_shortest_path_using_dfs

Please provide descriptive name for the parameter: i

Please provide descriptive name for the parameter: j

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 27, 2022
Added descriptive names for i and j parameters of find_shortest_path_using_dfs function

Added doctests for find_shortest_path_using_dfs function
@algorithms-keeper algorithms-keeper bot added tests are failing Do not merge until tests pass and removed require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required labels Oct 27, 2022
None
>>> find_shortest_path_using)dfs([[1], [-1, 's', 1], [-1, -1, 'e', -1]], 2, 1, 0)
"""
global current_shortest_path_distance
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using global is strongly recommended against. Perhaps return current_shortest_path_distance

None
>>> find_shortest_path_using_dfs([[1, 1, 1], [1, 1,'s'], [1, -1, 'e']], 1, 2, 0)
None
>>> find_shortest_path_using)dfs([[1], [-1, 's', 1], [-1, -1, 'e', -1]], 2, 1, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
>>> find_shortest_path_using)dfs([[1], [-1, 's', 1], [-1, -1, 'e', -1]], 2, 1, 0)
>>> find_shortest_path_using_dfs([[1], [-1, 's', 1], [-1, -1, 'e', -1]], 2, 1, 0)

Comment on lines 28 to 34
>>> find_shortest_path_using_dfs([['s', 1, 1], [1, -1, 1], [-1, 1, 'e']], 0, 0, 0)
None
>>> find_shortest_path_using_dfs([[-1, 1], ['s', 1], [-1, 1], [1, 'e']], 1, 0, 0)
None
>>> find_shortest_path_using_dfs([[1, 1, 1], [1, 1,'s'], [1, -1, 'e']], 1, 2, 0)
None
>>> find_shortest_path_using)dfs([[1], [-1, 's', 1], [-1, -1, 'e', -1]], 2, 1, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no point of these doctests if they return None. All this doctest is really doing is checking for errors, which only needs to be done once.



def find_shortest_path_using_dfs(
matrix: list, row: int, col: int, distance: int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list of what?


if __name__ == "__main__":
shortest_path_distance = helper(matrix)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Comment on lines 98 to 105
matrix = [
[START_POS, VALID_POS, OBSTACLE_POS],
[VALID_POS, VALID_POS, VALID_POS],
[END_POS, OBSTACLE_POS, VALID_POS],
[OBSTACLE_POS, OBSTACLE_POS, OBSTACLE_POS],
]

if __name__ == "__main__":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
matrix = [
[START_POS, VALID_POS, OBSTACLE_POS],
[VALID_POS, VALID_POS, VALID_POS],
[END_POS, OBSTACLE_POS, VALID_POS],
[OBSTACLE_POS, OBSTACLE_POS, OBSTACLE_POS],
]
if __name__ == "__main__":
if __name__ == "__main__":
matrix = [
[START_POS, VALID_POS, OBSTACLE_POS],
[VALID_POS, VALID_POS, VALID_POS],
[END_POS, OBSTACLE_POS, VALID_POS],
[OBSTACLE_POS, OBSTACLE_POS, OBSTACLE_POS],
]

It is only being used inside the if, so that is where it will go

Modified find_shortest_path_using_dfs with proper doctest features
@algorithms-keeper algorithms-keeper bot added the require tests Tests [doctest/unittest/pytest] are required label Oct 27, 2022
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

current_shortest_path_distance = math.inf


def find_shortest_path_using_dfs(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file path_tracing/matrix_shortest_path_using_depth_first_search.py, please provide doctest for the function find_shortest_path_using_dfs

@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Oct 27, 2022
@algorithms-keeper algorithms-keeper bot removed the require tests Tests [doctest/unittest/pytest] are required label Oct 27, 2022
Copy link
Contributor

@tianyizheng02 tianyizheng02 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Like @CaedenPH said, you shouldn't be using global variables, but it would take an unnecessary amount of work to refactor this file to not use them.

  2. Why create a new directory path_tracing? We want to avoid creating new directories unless an algorithm truly doesn't fall under any of the existing directories.

  3. I take issue with the premise of this PR. DFS is an inherently really poor choice of algorithm here because DFS explores as far as possible before backtracking, meaning it needs to traverse every possible path to find the shortest one. If you wish to open a new PR to implement this using BFS instead, I think it'd be much more appropriate.

  4. I kind of feel that this file is more a specific use case of an algorithm (namely DFS) rather than purely implementing an algorithm itself. In any case, I don't think this is the right algorithm for the job anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants