Shortest Path Function (Dijkstra's Algorithm) - r

I have a data frame composed of a latitude, longitude, node ID, from NodeID, to Node_ID, length. The from and to node columns are my edges. I can only travel on my edges when trying to find the shortest path. I want to be able to go from a node to another node while minimizing my total length traveled. The output should return every node I have to travel through to get to my destination. I have tried many built in packages like cppRouting and igraph, but I can not get anything to work correctly . Any ideas on how to either create a function or how to use any existing functions to accomplish this? Thank you.

Below are the detailed steps used in Dijkstra’s algorithm to find the shortest path from a single source vertex to all other vertices in the given graph.
Algorithm:
1) Create a set sptSet (shortest path tree set) that keeps track of vertices included in shortest path tree, i.e., whose minimum distance from source is calculated and finalized. Initially, this set is empty.
2) Assign a distance value to all vertices in the input graph. Initialize all distance values as INFINITE. Assign distance value as 0 for the source vertex so that it is picked first.
3) While sptSet doesn’t include all vertices
….a) Pick a vertex u which is not there in sptSet and has minimum distance value.
….b) Include u to sptSet.
….c) Update distance value of all adjacent vertices of u. To update the distance values, iterate through all adjacent vertices. For every adjacent vertex v, if sum of distance value of u (from source) and weight of edge u-v, is less than the distance value of v, then update the distance value of v.
Go through the following link: Printing Paths in Dijkstra’s Shortest Path Algorithm

Related

How to prune a graph given distance K from a node with jgrapht?

I built a graph using jgrapht API. I have a directed graph. Given a node N, I have to create a subgraph with all connected neighbours with distance K.
So basically given a node, and distance K, I have to prune the graph so that only the connected nodes with distance K remains.
I have an idea to implement it by hand.
I can generate the shortest path between all pairs from the list of nodes.
After that, I can get rid of nodes that are beyond the distance K.
However, this would result in the comparison between all the nodes and would like to know whether there is a better way to do this?
Moreover, wondering jgrapht has an API to do this already.
(I have already looked into the API of jgrapht but have not found any such API)
I assume that distance is defined as the length of the shortest path in a weighted graph, where the length of a path is given by the sum of its edge weights. I also assume that it is only required that all neighbors are within a given maxDistance from input vertex N, and that it is not required that two of those neighbors must also be within maxDistance of each other.
The simplest approach involves:
For a given input vertex N, determine all vertices that are at most maxDistance away from N.
Return an induced subgraph on N plus its (indirect) neighbors that are at most maxDistance units away.
public <V,E> Graph<V, E> getSubgraph(Graph<V,E> graph, V source, double maxDistance){
//Compute a shortest. Optionally we can limit the search to vertices that are maxDistance away
DijkstraShortestPath<V,E> ds = new DijkstraShortestPath<>(graph, maxDistance);
ShortestPathAlgorithm.SingleSourcePaths<V, E> shortestPaths = ds.getPaths(source);
//Collect all neighboring vertices that are at most maxDistance units away
Set<V> neighbors = graph.vertexSet().stream().filter(v -> shortestPaths.getWeight(v) <= maxDistance).collect(Collectors.toSet());
//Return an induced subgraph on those vertices
return new AsSubgraph<>(graph, neighbors);
}

Dijkstra algorithm with min-priority queue

I'm trying to implement the dijkstra algorithm with priority queue, but I can't understand how it works. I read many guide on the web but I can't understand this algorithm at all.
My questions are: What is the priority for each node? I think that it is the weight of the incoming edge with the minimum value, but I'm not sure. Is this true?
Second question, when I extract the root of the queue, how does it work if this node is not adjacency with no one of the visited nodes?
You should use priority queue where the vertex with the shortest distance from the starting vertex will get the highest priority. Initially, all vertices will have the shortest distance of infinity and the starting vertex will have the shortest distance 0.
Start by inserting of all vertices (with its edges) from the graph inside the PQ. Remove vertex from the PQ and explore all its edges. Compare the shortest distances with all adjacent vertices and if any distance is less than the shortest distance on the current vertex, update adjacent vertex shortest distance inside the PQ. Continue while PQ is not empty. Vertices which got no edges will finish with the shortest distance of infinity because it is not possible 'get to them' from the starting vertex. However, they will be still removed from the PQ.
Pseudocode
initialize graph
initialize pq
pq.insertAll(graph.getVertices())
while (pq is not empty) {
vertex = pq.remove()
edges = vertex.getEdges()
for all edges {
destination = edge.getDestination()
newDistance = edge.getLength() + vertex.getDistance()
if (newDistance < destination.getDistance()) {
destination.setShortestDistance(newDistance)
pq.update(destination)
}
}
}
MIT OpenCourseWare Links:
Path problems overview
Dijkstra

Graph shortest path?

I am facing which I believe is a kind of shortest path problem on a graph.
I need to find shortest path from node A to B, considering all edges have positive weight for connected vertexes, ∞ for not connected ones.
Vertexes have variable positive weightes.
The cost of a path is the weight of the vertex with maximum weight considering all vertexes involved in that path.
Should I apply Dijkstra in this situation, and if so how, considering that the weight of each Vertex changes depending on the previous vertexes visited?
Can you point me on how to tackle this problem otherwise?
I cant understand if you should consider the weights of the edges,because you said that you want the path with the max/min weight on a vertice possible,from A to B.
My solution for that is to convert every weight on vertex,to a weight on edge , just like in the image:
now you want to find the path from A to B where the the biggest weight on edge is min/max.
you can use MST algotirhm for this,because you dont care about the path lenght,but only the max/min edge cost.

How can I find all 'long' simple acyclic paths in a graph?

Let's say we have a fully connected directed graph G. The vertices are [a,b,c]. There are edges in both directions between each vertex.
Given a starting vertex a, I would like to traverse the graph in all directions and save the path only when I hit a vertex which is already in the path.
So, the function full_paths(a,G) should return:
- [{a,b}, {b,c}, {c,d}]
- [{a,b}, {b,d}, {d,c}]
- [{a,c}, {c,b}, {b,d}]
- [{a,c}, {c,d}, {d,b}]
- [{a,d}, {d,c}, {c,b}]
- [{a,d}, {d,b}, {b,c}]
I do not need 'incomplete' results like [{a,b}] or [{a,b}, {b,c}], because it is contained in the first result already.
Is there any other way to do it except of generating a powerset of G and filtering out results of certain size?
How can I calculate this?
Edit: As Ethan pointed out, this could be solved with depth-first search method, but unfortunately I do not understand how to modify it, making it store a path before it backtracks (I use Ruby Gratr to implement my algorithm)
Have you looked into depth first search or some variation? A depth first search traverses as far as possible and then backtracks. You can record the path each time you need to backtrack.
If you know your graph G is fully connected there is N! paths of length N when N is number of vertices in graph G. You can easily compute it in this way. You have N possibilities of choice starting point, then for each starting point you can choose N-1 vertices as second vertex on a path and so on when you can chose only last not visited vertex on each path. So you have N*(N-1)*...*2*1 = N! possible paths. When you can't chose starting point i.e. it is given it is same as finding paths in graph G' with N-1 vertices. All possible paths are permutation of set of all vertices i.e. in your case all vertices except starting point. When you have permutation you can generate path by:
perm_to_path([A|[B|_]=T]) -> [{A,B}|perm_to_path(T)];
perm_to_path(_) -> [].
simplest way how to generate permutations is
permutations([]) -> [];
permutations(L) ->
[[H|T] || H <- L, T <- permutations(L--[H])].
So in your case:
paths(A, GV) -> [perm_to_path([A|P]) || P <- permutations(GV--[A])].
where GV is list of vertices of graph G.
If you would like more efficient version it would need little bit more trickery.

Node's position in tree as a Feature Vector?

Background
I have a tree of nodes and I'm trying to run some machine learning algorithms to classify them. One of the features I want to use is the position of the nodes in the tree, i.e. closer nodes are likely to be in the same class.
My Problem
I'm representing all the features as a vector of numbers. Any thoughts on how I can represent position in the tree as a vector? So that distance b/n two vectors corresponds to distance between nodes in the tree? (I have a small tree of depth around 5-7 and branching around 2-3)
What I tried
P.S. I read about algorithms to find shortest distance between 2 nodes (finding each one's distance to their closest common ancestor) One idea I found was to have a vector x where each index corresponds to possible ancestors in the tree. Then set x[i] = numbers of levels from that ancestor. The problem with that is- I don't know what to do with nodes that aren't ancestors.
just put the path of the tree as the vector. then simply calculate the length of the difference between the two paths. so for example. 2,3,1,5,3 is one path. and 2,3,3,5,9,5 is another path. so 2,3 they have in common. so the length of the difference is 1,5,3 and 3,5,9,5 which is 7. good luck
So there is actually a very nice way to derive the features that you want; you can do so with MDS.
What MDS does is that it takes a N by N matrix (here N is number of nodes) where entry a_{i,j} is the distance between item i and item j (node i and node j) and for each item i it will return a D (pre-specified) position vector, D_i, such that the distance between D_i and D_j is approximately a_{i,j}.
Thus, we can have your feature vector with a bit of pre-processing. First, find the shortest distance (in hops) for each pair of nodes (you could use Floyd-Warshall) then use the distance matrix as input for MDS and specify the number of dimensions for you're position vector, and MDS will output position vectors of said dimensions.
If you search the web I'm sure you can find open sourced implementations to both Floyd-Warshall and MDS.

Resources