Does Dijkstra works for non-negative or positive weights? - graph-algorithm

I know why Dijkstra doesn't work for negative weights but is 0 allowed as a weight?
I think that if two nodes have a 0 weight then the edge connecting those nodes can be eliminated and nodes be merged as one.
Is this right? or I'm missing something?

Yes, if the segment has weight zero, we can delete it, if all the segement between that pair of point are zero, you can delete them all, and merge two point to one!
Hope this post help you!

Related

Identifying nodes with many, but thin connections

I need to identify (and label) nodes with many relationships (say, 10 on a possible scale of 1 to 60) but weak weights for the relationships (say 1 or 2 on a possible scale of 1 to 100). I could write a Cypher query to find them. I don’t need help with that. What I want to ask is, is there a GDS metric for this?
You could use a combination of degree and weighted degree.
If you want to construct such a gds graph, you could use the subgraph option that allows you to filter on mutated properties

Can I use Breadth-First-Search on weighted graphs if I modify it?

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!
Since 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.

TSP Where Vertices Can be Visited Multiple Times

I am looking to solve a problem where I have a weighted directed graph and I must start at the origin, visit all vertices at least once and return to the origin in the shortest path possible. Essentially this would be a classic example of TSP, except I DO NOT have the constraint that each vertex can only be visited once. In my case any vertex excluding the origin can be visited any number of times along the path, if this makes the path shorter. So for example in a graph containing the vertices V1, V2, V3 a path like this would be valid, given that it is the shortest path:
ORIGIN -> V1 -> V2 -> V1 -> V3 -> V1 -> ORIGIN
As a result, I am a bit stuck on what approach to take in order to solve this, as a classic dynamic programming algorithm approach which is usually used to solve TSP problems in exponential time is not suitable.
The typical approach is to create a distance matrix that gives the shortest-path distance between any two nodes. So d(i,j) = shortest path (following the edges of the network) from i to j. This can be done using Dijkstra's algorithm.
Now just solve a classical TSP with distances d(i,j). Your TSP doesn't "know" that the actual route followed might involve visiting a node multiple times. At the same time, it will ensure that the vehicle stops at every node.
Now, as for efficiency: As #Codor points out, TSP is NP-hard and so is your variant of it, so you are not going to find a provably optimal, polynomial-time algorithm. However, there are still many, many good algorithms (both heuristic and exact) for TSP, and most of them should be suitable for your problem. (In general, DP is not the way to go for TSP.)
To answer the question in part, the problem described in the question does not admit a polynomial-time algorithm unless P=NP by the following argument. Clearly, the proposed problem includes instances which are Euclidean. However, no optimal solution to a Euclidean instance has repeated nodes, as such a solution can be improved by deleting additional nodes, using the triangle inequality. However, according to the Wikipedia article on TSP, Euclidean TSP is still NP-hard. This means that any polynomial-time algorithm for the problem in the question would be able to solve the Euclidean TSP to optimality on polynomial time, which is impossible unless P=NP.

Decision Tree, What is Wrong here?

I took a contest two days ago. one of our question is as follows:
decision tree with depth 2 is constructed for two binary feature.
how many features are in hypothesis space that can be shown with the following tree ?
The answer sheet say solution as 16 but the commitment say this
question is removed by reason of wrong answer. Who can add
explanantion why this is removed? which part of answer is wrong?
In this case, you have represented all possible features that can be represented by decision tree. So there are overall 4 possible points in the hypothesis space.
The number of partitions that this tree carves the hypothesis space into is equal to the number of leaves (4). That is also the maximum in this case, since with two binary features the total number of unique inputs is 2^2, or 4.
16? No way. The question is dumb, honestly.
The number of features you have in Decision Tree (DT) correspond to the maximum depth of the tree, which is the maximum amount of questions you can ask the DT in order to model the feature space.
That is a logical consequence of in each node the DT discriminating the feature space according to a feature.

cvMatchTemplate finds match without one

I use opencv and need to find a match on picture which is taken from video flow. Functions cvMatchTemplate() and cvMinMaxLoc() finds the match absolutely correct. But the problem is that when there is no match, opencv finds it anyway. Even on white sheet.
Can anyone say what the problem is? Maybe use another function to detect match or to some to understand that there are no match?
Thank you
cvMatchTemplate ouputs a map of similarity measures/image distances (depends on the method parameter). You want to have a look at the similarity measures and threshold them at a reasonable value (so, only accept a match if the similarity is high/distance is low). cvMinMaxLoc also gives you the values, you can use those for thresholding (look at values for positive and negative samples, and set the threshold in between).

Resources