Dijkstra | small implementation of Dijkstra 's algorithm
kandi X-RAY | Dijkstra Summary
kandi X-RAY | Dijkstra Summary
A small implementation of Dijkstra's algorithm, for single-source shortest paths in a nonnegatively weighted directed graph.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Dijkstra
Dijkstra Key Features
Dijkstra Examples and Code Snippets
Community Discussions
Trending Discussions on Dijkstra
QUESTION
I've been messing with streams in python and been trying to generate the Hamming numbers (all the numbers with prime factors of 2, 3, or 5 only). The standard way for doing so, described by Dijkstra, is to observe that:
- The sequence of Hamming numbers begins with 1.
- The remaining values in the sequence are of the form 2h, 3h, and 5h, where h is any Hamming number.
- h is be generated by outputting the value 1, and then merging together 2h, 3h, and 5h
My implementation is this:
...ANSWER
Answered 2022-Mar-27 at 05:41The further you get out into your stream, the more duplicates you're going to have to be merging. The number 2**4 * 3 **4 = 1296 is going to appear 70 times in your multiple stream (8 choose 4), and your program is going to be spending more time merging duplicates than it is outputting new items.
The further you go out, the more duplication you'r going to be dealing with. There is no reason to expect your program to be linear.
QUESTION
I am trying to use timeit to time two algorithms I have implemented, but I need to construct a graph object and don't want to sort graphs that are already sorted (for time accuracy).
This is the best attempt I have so far at separating the setup from the actual timing. Later I want to call ShortestPathAlgorithm()
with no arguments and print the results in a table to a txt file, so I believe the format has to be something like this. With everything being completed inside the shortestPathAlgorithm
Function.
Can anyone give any pointers on how to do all of this in the nested functions?
...ANSWER
Answered 2022-Mar-26 at 19:33Your understanding is correct. However, there are some issues that could be improved.
Firstly, you usually want to measure only one algorithm at a time. If you measure each algorithm separately you can compare the two and get more accurate data. Also, I would stick to measuring it on the same graph multiple times and then average the time to get one more accurate (you can use copy.deepcopy()
to make duplicates of the graph). Then do this on each of the graphs you have. Since each algorithm may be more efficient on different types of graphs (you would lose this information completely if measured as you propose).
Secondly, since timeit
can repeat the measured operation multiple times but the setup is done only once before any measurement starts (see the docs) you would measure the algorithm on already traversed (or sorted as you put it) graphs. Which you correctly point out.
As a solution, I suggest manually timing the runs using the time.perf_counter_ns()
. The code for measuring the bellmanFord algorithm could look as such:
QUESTION
I wrote this program to solve the dining philosophers problem using Dijkstra's algorithm, notice that I'm using an array of booleans (data->locked
) instead of an array of binary semaphores.
I'm not sure if this solution is valid (hence the SO question).
Will access to the data->locked
array in both test
and take_forks
functions cause data races? if so is it even possible to solve this problem using Dijkstra's algorithm with only mutexes?
I'm only allowed to use mutexes, no semaphores, no condition variables (it's an assignment).
Example of usage:
...ANSWER
Answered 2022-Mar-22 at 06:59Your spin loop while (data->locked[i]);
is a data race; you don't hold the lock while reading it data->locked[i]
, and so another thread could take the lock and write to that same variable while you are reading it. In fact, you rely on that happening. But this is undefined behavior.
Immediate practical consequences are that the compiler can delete the test (since in the absence of a data race, data->locked[i]
could not change between iterations), or delete the loop altogether (since it's now an infinite loop, and nontrivial infinite loops are UB). Of course other undesired outcomes are also possible.
So you have to hold the mutex while testing the flag. If it's false, you should then hold the mutex until you set it true and do your other work; otherwise there is a race where another thread could get it first. If it's true, then drop the mutex, wait a little while, take it again, and retry.
(How long is a "little while", and what work you choose to do in between, are probably things you should test. Depending on what kind of fairness algorithms your pthread implementation uses, you might run into situations where take_forks
succeeds in retaking the lock even if put_forks
is also waiting to lock it.)
Of course, in a "real" program, you wouldn't do it this way in the first place; you'd use a condition variable.
QUESTION
I'm trying to write a recursive function (printPath
) inside another function (dijkstraSSSP
), but it gives me a compiler error.
When I run gcc dijkstra.c WGraph.c PQueue.c
, it compiles just fine. However, when I run dcc -Wall -Werror -std=c11 -o dijkstra dijkstra.c WGraph.c PQueue.c
. I got this error:
ANSWER
Answered 2022-Mar-18 at 17:10Move the definition of printPath
outside of the body of dijkstraSSSP
and, to give it access to pred
, change it to accept two parameters instead of one. The additional parameter should have type int *
and should point to the first element of pred
.
In the call, pass pred
for that new parameter. The array pred
will be automatically converted to a pointer to its first element.
QUESTION
I am trying to find an intuitive explanation as to why we can generalize Dijkstra's Algorithm to find the K shortest (simple) paths from a single source in a directed, weighted graph with no negative edges. According to Wikipedia, the pseudocode for the modified Dijkstra is as follows:
...ANSWER
Answered 2022-Feb-17 at 19:54The Wiki article doesn't specify, but that code will only solve the 'loopy' version of k-shortest-paths, where paths are not required to be simple.
The simple path version of the problem is harder: you'll want to look at something like Yen's algorithm, which does clever filtering to avoid repeated points when generating paths. Yen's algorithm can use Dijkstra's algorithm as a subroutine, but any other shortest-path algorithm can also be used instead.
There is no obvious way to modify Dijkstra's algorithm to solve the k-shortest-simple-paths problem. You'd need to track the paths in the priority queue (which is already done in your posted code), but there's an exponential upper bound on the number of times each vertex can be explored.
Here, if count[u] <= K
puts an upper bound of K+1
on the number of times a vertex can be explored, which works for the non-simple path case. On the other hand, a direct modification of Dijkstra's algorithm for simple paths would, in the worst case, require you to explore a node once for each of the 2^(V-1)
possibilities of which nodes had been previously visited (or possibly a slightly smaller exponential).
QUESTION
The code is compiled, and run without any issue in CLion IDE with g++ compiler in MinGW, however, the same exact code has a compilation error in Visual Studio IDE and with (MSVC compiler)
I believe the error should have to do with the follwoing comparator class:
...ANSWER
Answered 2022-Feb-03 at 21:36Your somewhat odd comparator...
QUESTION
I am trying to find shortest path between graph nodes using Dijkstra's Algorithm, by using the code enclosed in the following article:
https://www.r-bloggers.com/2020/10/finding-the-shortest-path-with-dijkstras-algorithm/
But this code creates the graph from an edgelist. Instead I would like to create the graph from a dataframe like this:
...ANSWER
Answered 2022-Feb-01 at 19:15Perhaps you can try this
QUESTION
What is the time complexity of this particular implementation of Dijkstra's algorithm?
I know several answers to this question say O(E log V) when you use a min heap, and so does this article and this article. However, the article here says O(V+ElogE) and it has similar (but not exactly the same) logic as the code below.
Different implementations of the algorithm can change the time complexity. I'm trying to analyze the complexity of the implementation below, but the optimizations like checking visitedSet
and ignoring repeated vertices in minHeap
is making me doubt myself.
Here is the pseudo code:
...ANSWER
Answered 2021-Dec-22 at 00:38Despite the test, this implementation of Dijkstra may put Ω(E) items in the priority queue. This will cost Ω(E log E) with every comparison-based priority queue.
Why not E log V? Well, assuming a connected, simple, nontrivial graph, we have Θ(E log V) = Θ(E log E) since log (V−1) ≤ log E < log V² = 2 log V.
The O(E + V log V)-time implementations of Dijkstra's algorithm depend on a(n amortized) constant-time DecreaseKey operation, avoiding multiple entries for an individual vertex. The implementation in this question will likely be faster in practice on sparse graphs, however.
QUESTION
I have implemented Dijkstra's algorithm but I have a problem. It always prints the same minimum path while there may be other paths with the same weight.
How could I change my algorithm so that it randomly selects the neighbors with the same weight?
My algorithm is below:
...ANSWER
Answered 2021-Dec-22 at 20:36probably just try something like this
QUESTION
I am having a discussion with a friend if the following will work:
We recently learned in a lecture about Breadth-First-Search. I know that it is a special case of Dijkstra where each edge weight is set to one. Assume now we are given a graph where the edges have integer weights of more than one. Then I would modify this graph by introducing additional vertices and connecting them by edges with weight one, e.g. assume we have an edge of weight 3 connecting the vertices u and v, then I would introduce dummy-vertices d1, d2, remove the edge connecting u and v and instead add edges {u, d1}, {d1, d2}, {d2,v} of weight one.
If I modify my whole graph this way and then apply breadth-first search starting from one of the original vertices, wouldn't this work as well?
Thank you very much in advance!
...ANSWER
Answered 2021-Dec-10 at 08:20Since BFS is guaranteed to return an optimal path on unweighted graphs, and you've created the unweighted equivalent of your original graph, you'll be guaranteed to get the shortest path.
What you lose by doing this over Dijkstra's algorithm is runtime optimality. Now the runtime of your algorithm is dependent on the edge weights, whereas Dijkstra's is only dependent on the number of edges.
This sort of thought experiment is a great way to understand how Dijkstra's algorithm works (eg. how would you modify your algorithm to not require creating a new graph? Or not take 100 steps for an edge with weight 100?). In fact this is probably how Dijkstra discovered the algorithm to begin with.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Dijkstra
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page