- 
  Notifications
 You must be signed in to change notification settings 
- Fork 5k
Dijkstra's algorithm #428
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
Dijkstra's algorithm #428
Conversation
should i change something?
@crabman448 checking it out now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all, great work!
It's great to have one of the most famous algorithms written up for our repo.
The readme file needs a fair bit of work. I've left some more details below, but ideally you should give a mid to high level explanation of the interesting bits of the algorithm. Given the fame of this algorithm, there should be lots to write about.
In addition to the code level comments written below, I've got a few RW style guidelines I want to mention:
- omit needless selfreferences. You've got many of those in your implementations
- extensions can give your code better readability for protocol conformances
The tool looks/sounds interesting. I won't be reviewing the code that implements it, but I'm looking forward to playing around with it!
Cheers!
 
 
 Dijkstra Algorithm/README.md
 
 Outdated
 
 
 There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put the credits at the bottom.
 
 
 Dijkstra Algorithm/README.md
 
 Outdated
 
 
 There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dijkstra's algorithm is one of the most famous graph algorithms. Instead of giving a link to wikipedia, we'll want a bigger sample of what this algorithm is about.
Some things to talk about:
- Why it is so often studied
- Real life problems that it solves
- A summary of how the algorithm works
 
 
 Dijkstra Algorithm/README.md
 
 Outdated
 
 
 There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds cool, will check this out.
 
 
 Dijkstra Algorithm/README.md
 
 Outdated
 
 
 There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These screenshots don't give me any context on what it's about. If this is just to showcase your visualization tool, 1 screenshot right below the paragraph where you introduce your tool is enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unnecessary imports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if lhs.hashValue == rhs.hashValue {
 return true
}
return false
is equivalent to
return lhs.hashValue == rhs.hashValue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this an implicitly unwrapped optional (IUO)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Double type can be inferred from Double.infinity, no need to do explicit typing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As with Vertex, let's put Dijkstra in it's own file in the Sources directory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method depending on an IUO (implicitly unwrapped optional) makes me nervous.
For starters, instead of using Vertex!, you can have it as Vertex? and achieve the same looping logic:
while let vertex = currentVertex { ... }
Try to refactor this method based on that.
Thank you for your comments! 👍
I will fix it as soon as possible :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a few more comments on your latest change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for the _ in front of this variable name?
 
 
 Dijkstra Algorithm/README.md
 
 Outdated
 
 
 There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put the About, Demo Video, and Screenshots section at the bottom of this file.
 
 
 Dijkstra Algorithm/README.md
 
 Outdated
 
 
 There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't just copy from Wikipedia. If it helps, you can quote a small paragraph from it, but most of the writing should be in your own words.
What you've put here is the procedure of Dijkstra's algorithm. Before talking about the procedure, you need to talk about what the algorithm is, and what it will solve. I suggest you take some inspiration from this article: https://github.com/raywenderlich/swift-algorithm-club/tree/master/Merge%20Sort
 
 
 Dijkstra Algorithm/README.md
 
 Outdated
 
 
 There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This diagram needs more context for the reader.
Consider that the reader is new and first opens this article. That person wouldn't and shouldn't know what this diagram is. You'll want to place this diagram in between an explanation to give it context.
Will improve it in a couple days. Thank you again! : )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @crabman448,
Great improvements overall, but the explanations still need some work! I've left some comments that I feel would greatly improve this article.
For now, let's focus on polishing the first half of your readme. Some things to keep a note of:
- grammar
- punctuation
- spelling mistakes
There are many of these in the readme. Try to fix those as you're making changes.
 
 
 Dijkstra Algorithm/README.md
 
 Outdated
 
 
 There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename this to "Example" instead. Initialization doesn't mean anything to the reader for this section.
 
 
 Dijkstra Algorithm/README.md
 
 Outdated
 
 
 There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's focus on 1 example, you mentioned that you wanted to find the shortest path from your house to your job.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sentence doesn't make sense.
In the intro, your example was about going from your house to your [job] company office. It'll read much better to refocus the reader on that example.
This diagram is a bit too noisy to start off with. Let's simplify it a bit. For instance, you could start without the weights of the graph:
screen shot 2017年05月01日 at 5 16 53 pm
Explain what the lines are, and what the circles represents.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of jumping straight to a table, gradually warm up the reader by introducing the concept of a weighted graph. This is where you update your diagram with more information:
screen shot 2017年05月01日 at 5 17 56 pm
For simplicity, tell the reader this is only a one way street (directed graph).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before you introduce this table for computing Dijkstra's algorithm, its sometimes helpful to give the reader a concrete goal to think about. Tell them their goal is to find the shortest path from A to E:
screen shot 2017年05月01日 at 5 25 11 pm
Afterwards, you can talk about how to reason about it. That's where the table comes into play.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diagrams here will help a lot. You could do something like this:
screen shot 2017年05月01日 at 5 31 24 pm
This diagram focuses the reader on what you're currently trying to show (the path from A to B), while phasing out everything else. You'll leave the goal (Vertex E) in plain sight to remind the reader of the goal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the rest of the steps, you should use these graphs to showcase the rest of the maneuvers, taking it 1 step at a time:
screen shot 2017年05月01日 at 5 35 16 pm
screen shot 2017年05月01日 at 5 36 39 pm
screen shot 2017年05月01日 at 5 37 12 pm
screen shot 2017年05月01日 at 5 37 44 pm
Thanks a lot again) But I'm not agree with concept, that it would be better to use directed graph. Because, about 2 months ago I was looking for algorithm, that will be possible to use in not directed graph. And I thought, that Dijkstra won't satisfy my requirements, because explanations mostly used directed graphs, but I needed to go in both directions on every edge.
Maybe it would be better to add both directed and not directed graph explanations?
Interesting. Dijkstra's algorithm should be functionally identical for both undirected and directed graphs. I can't picture why you would want to go in both directions for every edge. You'd never want to travel back and forth since that only increases your total distance.
Could you explain why you needed to go in both directions using Dijkstra's?
In my app I have a graph, that is not directed at all. You can go in both directions on every edge. And when I was searching for suitable algorithm, I've seen some explanations with directed graph and I was like: "That won't work in my case, because I have not directed weighted graph". But it would, because it works for not directed graphs to.
In a park, for example, people can go in both directions on footpaths.
Let's take this step by step. If you want to find the shortest path between 2 vertexes in a graph, would you ever want to travel down a single undirected edge more than once?
Probably I won't. But I don't know whether i'll travel up or down an edge. So it doesn't make sense to create directed graph with both directions on every edge.
Can you elaborate on that? One of the most common ways to represent an undirected graph is to use a directed graph where every edge is paired with another edge going in the opposite direction.
But why? It is extra work, isn't it? :)
If I have not directed graph, why should I make it directed?
@crabman448 I took a look at your implementation, here's what I mean:
func setupConnections() {
 // ...
 let neighbor1 = (neighborVertex, randomWeight)
 let neighbor2 = (vertex, randomWeight)
 vertex.neighbors.append(neighbor1)
 neighborVertex.neighbors.append(neighbor2)
}
neighbor1 and neighbor2 are your directed edges. They represent the destination and the weight of the edge. Your undirected graph is built by using a pair of directed edges. Every connection you make is done by adding a pair of edges.
You're right about it being extra work - hence I suggest we base the explanation based on a directed graph.
To create a directed graph, all you need to do is remove one of the pairs:
func setupConnections() {
 // ...
 let neighbor1 = (neighborVertex, randomWeight)
 vertex.neighbors.append(neighbor1)
}
Now your graph is a directed graph, with exactly half as many edges than your previous undirected graph. I recommend reading our tutorial on adjacency lists for more info.
Hmm, got you)
"In an undirected graph, you can treat it as a directed graph that goes both ways."
So, when all graph's edges are bidirectional, then it is still directed graph? And there is no other way to achieve such behaviour?
Yes
An undirected graph is a directed graph where every edge has another edge going in the opposite direction
- All undirected graphs can be viewed as a directed graph.
- A directed graph is undirected if and only if every edge is paired with an edge going in the opposite direction
Things have become clearer)) Thank you again. I will mention this statement in the beginning of the readme.
I'm currently busy with my graduating project in the university. I'll fix description as soon as I will have enough time)
Awesome work. I can see both of you are diligently making progress over many days. Looking forward to see this completed.
I'm back now :)
Hope, that I will finish in a couple of days.
I've added some general concepts about graphs, but I would like to use example with shops and choosing the closest one of them, because it correlates better with Dijkstra's algorithm: Shortest paths from source vertex to all others.
I'll check grammar, punctuation and spelling mistakes soon)
@kelvinlauKL I've checked grammar etc and hope it is ready for merge now :)
Nice. I just did a quick read and it's shaping up very nicely. Great improvements!
I'll do a more thorough review in the next few days.
Woot, I had a chance to read over it. It's much nicer than before. This is ready for a merge.
I think there's still lots of room for improvement in areas like readability, so if you have time, consider spending more time improving the readme files. Thanks @crabman448!
No description provided.