Skip to content

Conversation

@altay9
Copy link
Collaborator

@altay9 altay9 commented May 23, 2021

resolves #56

This question is a version of #282, but this time we are asked to find the shortest path to the target.

Dijkstra's algorithm appears to be the fastest with ~5ms and the way it searches is almost the same as the BFS.

But Dijkstra starts with detecting the node which has the least distance from the starting point.

Priority Queue is a perfect match to poll the closest node.

@ErdemT09
Copy link
Collaborator

Issue: #56

@altay9 altay9 changed the title Adds DFS, BFS and Dijkstra's algorithm solutions. Adds DFS, BFS and Dijkstra's algorithm solutions for "The Maze II" May 23, 2021
altay9 added 2 commits May 23, 2021 23:28
As we use PQ to find the closest node, this method is obsolete now.
Refactors
@altay9
Copy link
Collaborator Author

altay9 commented May 24, 2021

Hello @ErdemT09,

I have done some improvements in Dijkstra implementation.
It is more readable and straightforward now.
The latest commit:

  • removes distance array as it is obsolete after we implement PQ to get the closest node.
  • removes visited array to improve space efficiency, utilizes the maze matrix for this purpose.
  • removes unnecessary distance compare as PQ already does this.


if(maze[x][y]==-1) continue;

maze[x][y] = -1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I reccommend moving -1 to a constant called "VISITED" for readability.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I still suggest doing this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oke, it is done. Thanks for the suggestion.

Comment on lines 27 to 28
if (startPoint[0] < 0)
break;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't understand the function of this statement here. Can you please explain that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is also a resideue. I just removed it.

return (nextX >= 0 && nextY >= 0
&& nextX < maze.length
&& nextY < maze[0].length
&& maze[nextX][nextY] != 1);
Copy link
Collaborator

@ErdemT09 ErdemT09 May 28, 2021

Choose a reason for hiding this comment

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

Why don't we check for this condition unlike in the previous two solutions?
maze[nextX][nextY] == 0

Copy link
Collaborator

@ErdemT09 ErdemT09 May 29, 2021

Choose a reason for hiding this comment

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

I have understood the reason why we don't do this.
In the previous solution, the tiles that we come across are either empty or a wall. If empty, we move through them. If wall, we stop. Here, we have a three valued logic: A tile is either empty but visited, empty or a wall. We should be able to move through an empty tile even if it is visited. At the end, we can't stop and branch off there, but whilst moving to another valid tile, we should be able to pass them.

After all, we start the canPass loop from an already visited tile. This means we have to consider -1 as a valid passable tile value.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the explanation Erdem. Appreciated.

@ErdemT09 ErdemT09 closed this May 29, 2021
@ErdemT09 ErdemT09 deleted the TheMazeII branch May 29, 2021 07:12
@ErdemT09 ErdemT09 restored the TheMazeII branch May 29, 2021 07:13
@ErdemT09 ErdemT09 reopened this May 29, 2021
@ErdemT09
Copy link
Collaborator

I have accidentally deleted the branch. I should have been more careful.

Copy link
Collaborator

@ErdemT09 ErdemT09 left a comment

Choose a reason for hiding this comment

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

Approved. A good question in terms of graph theory.

@altay9 altay9 merged commit e06b204 into master May 29, 2021
@altay9 altay9 deleted the TheMazeII branch May 29, 2021 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

505. The Maze II

3 participants