-
Notifications
You must be signed in to change notification settings - Fork 0
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 algo + network delay time with a ton of comments + prework Qs #10
base: master
Are you sure you want to change the base?
Conversation
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.
thanks in advance for your comments as usual @robot-dreams! I noted some parts i'm particularly unsure about.
Overall time complexity... | ||
O(E+N+E+N^2) = O(N^2+N+2E) | ||
O(E) to build the graph, where E is the number of items in `times` | ||
O(N+E) to do breadth first search on the graph to check whether everything | ||
is connected | ||
O(N^2) to do Dijkstra's algorithm to find the network delay time | ||
|
||
if it is impossible to reach all nodes, | ||
running time is O(E+N+E) = O(2E+N) |
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.
not sure whether my time complexity is correct.
Runtime: 496 ms, faster than 73.14% of Python3 online submissions | ||
for Network Delay Time. | ||
Memory Usage: 15.8 MB, less than 19.89% of Python3 online submissions | ||
for Network Delay Time. |
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.
there's probably a better way to do this since i'm only in the top 27%? can't think of any way right now. probably involves simplifying the first step of building the graph or finding the max time cos i can't think of any other way to simplify dijkstra's and we need dijkstra's because we're trying to find a path from one node to every other node so that we can find the max time.
I've commented almost every line of the function and printed a whole | ||
bunch of info so that it is much more obvious to me how the algorithm | ||
runs, especially the part about updating the dict of costs |
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.
hopefully my comments are actually accurate and not wildly off the mark 😆
* "Best first" when your graph is representing something like a map and there aren't many obstacles in the map. | ||
* A* when your graph is representing something like a map and there are a lot of obstacles in the map. | ||
* A* is the 'best of both worlds' in that it is faster than BFS like "best first" and finds the shortest path like BFS because "best first" doesn't always find the shortest path. | ||
* Not sure why "best first" runs faster than BFS even though BFS finds the more optimal path. There wasn't time complexity analysis in the [article](https://www.redblobgames.com/pathfinding/a-star/introduction.html) |
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.
still confused
* BFS when cost from one vertex to another is constant and you just want to find the shortest path, or to check that all vertices are connected. Note that in this case, the shortest path is also the least costly path since cost from one vertex to another is constant. | ||
* Dijkstra's when cost from one vertex to another is constant and you just want to find the least costly path, or the lowest cost from the starting vertex to every other vertex. | ||
* When you want to find the path from one vertex to just one other vertex... | ||
* "Best first" when your graph is representing something like a map and there aren't many obstacles in the map. |
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.
It's important to note that "best first" won't always give you the correct answer!
* Dijkstra's when cost from one vertex to another is constant and you just want to find the least costly path, or the lowest cost from the starting vertex to every other vertex. | ||
* When you want to find the path from one vertex to just one other vertex... | ||
* "Best first" when your graph is representing something like a map and there aren't many obstacles in the map. | ||
* A* when your graph is representing something like a map and there are a lot of obstacles in the map. |
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.
One key point to note here is that A* requires a good "heuristic function", way to estimate how far you are from your goal.
* "Best first" when your graph is representing something like a map and there aren't many obstacles in the map. | ||
* A* when your graph is representing something like a map and there are a lot of obstacles in the map. | ||
* A* is the 'best of both worlds' in that it is faster than BFS like "best first" and finds the shortest path like BFS because "best first" doesn't always find the shortest path. | ||
* Not sure why "best first" runs faster than BFS even though BFS finds the more optimal path. There wasn't time complexity analysis in the [article](https://www.redblobgames.com/pathfinding/a-star/introduction.html) |
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.
We've left the realm of worst-case / asymptotic analysis. We've instead run into a situation where these different algorithms will do better or worse in different situations.
|
||
### Notes | ||
* Took me quite a while to get Dijkstra's and I had to rewrite the algorithm presented in [shortest path](https://bradfieldcs.com/algos/graphs/dijkstras-algorithm/) with my own variable names, a ton of comments, and a ton of prints for me to see how the graph was first initialised, and how it changed with each step in the algorithm | ||
* I still don't get the implementation of "best first" and A*, but I haven't yet re-implemented them with a ton of comments. Maybe it'll come to me then. Getting through Dijkstra's and Network Delay Time fried my brain |
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.
Did you also find the solution walkthrough video helpful?
|
||
# note says "nodes can get added to the priority queue many times | ||
# only process a vertex the first time we remove it from the priority queue" | ||
# ^ not sure what that means and why it's important we do this |
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.
Consider a node that has multiple incoming edges. Each time you visit that edge, you might add the node to the priority queue again.
"Why not just add it once?"
Because you don't know which edge the shortest path will need.
A---C
\ /
B
Suppose A -> C has weight 100, but A -> B and B -> C both have weight 1.
for source, target, time in times: | ||
graph[source].append((target, time)) | ||
|
||
# check whether it is possible to reach all nodes with breadth first search |
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.
Do you need this extra BFS step here (can you use the output of Dijkstra's algorithm to answer this same question)?
answers could probably be a lot better in many places, but prior to this prework i've never even attempted dijkstra's before, let alone tried to figure out what A* is. ...i think Oz mentioned A* to me ages ago when I first contacted Bradfield about the algos course and other courses and explained why I was interested in algorithms, though, so it's nice to come full circle.
...would be better if i really understood all of these. XD