Take certain edges in Dijkstra (or DFS) - graph

i have a question about Dijkstra and/or DFS.
Let's assume I have a graph, with several nodes and edges. Now i want to find a Path from Node A to Node B. On this way i have to take certain Edges, for example the Edge (C,D).
Edit:
Sorry if it was a little bit unclear. My question is : I want a path from A to B. Is there a Path so that all edges {a,b}, {b,c}... and so on are taken? I'm interested if that's possible with dfs. And if thats also possible with Dijkstra under the same constraint if i want the shortest path from A to B and some edges {a,b}, {b,c} in the graph are needed to be taken. Also the graph is directed.
Help would be really appreciated!

Create a list of the all the edges that you want to make sure must be navigated.
Perform a DFS starting from the source.
Everytime you pass though an edge that's in the list, mark is as "traversed".
When you reach the destination, check if all the edges in the list are marked as traversed.
a.) If yes, goal state reached.
b.) Else, backtrack and unmark each edge in the list while passing through them.

Related

Gremlin: How to obtain outgoing edges and their target vertices in a single query

Given a set of vertices (say, for simplicity, that I start with one: G.V().hasId("something")), I want to obtain all outgoing edges and their target vertices. I know that .out() will give me all target vertices, but without the information about the edges (which have properties on them, too). On the other hand, .outE() will give me the edges but not the target vertices. Can I obtain both in a single Gremlin query?
Gremlin is as much about transforming graph data as it is navigating graph data. Typically folks seem to understand the navigation first which got you to:
g.V().hasId("something").outE()
You then need to transform those edges into the result you want - one that includes the edge data and it's adjacent vertex. One way to do that is with project():
g.V().hasId("something").outE()
project('e','v').
by().
by(inV())
Each by()-modulator supplied to project() aligns to the keys supplied as arguments. The first applies to "e" and the second to "v". The first by() is empty and is effectively by(identity()) which returns the same argument given to it (i.e. the current edge in the stream).
Never mind. Figured this out.
G.V().hasId("something").outE().as("E").otherV().as("V").select("E", "V")

How to find all the paths in a undirected graph touching all the nodes of a given set

I have an undirected graph and i'd like to find all the possible paths in it connecting all the nodes of a given set.
Is it an NP problem? Is there an algorithm to doing it, or a good way to accomplish it? I don't care about the order by which each paths touches the nodes in the set, i just need it to go through each of them.
It's called Hamiltonian Path Problem and it is NP-complete.
For detailed info: https://en.wikipedia.org/wiki/Hamiltonian_path_problem

Coloring a graph using Depth first traversal

I know that for coloring graph nodes, backtracking/brute force is a common solution. But I was wondering if using DFS I can also achieve a solution ?
Backtracking gives you the opportunity to go back and try other color possibility in order to paint all the nodes with N colors
DFS will start from one node and color it, then jump to its neighbor and color it in different color than its neighbors etc ...
I did a search about using this method but I didn't find an algorithm that uses this.
Question: Is using DFS possible for coloring graph nodes. If yes, is it more efficient than backtracking ?
Thank you
I believe there is some confusion when comparing backtracking and DFS wrt vertex coloring. DFS traversal for a graph gives full enumeration of its vertices in a sequence related to its structure. It does not, however, constitute a full enumeration for the vertex coloring problem, which would require taking into account the possible colors of the vertices.
Thus, if I understand correctly, what you have implemented is a greedy heuristic coloring for a graph directed by DFS.
On the other hand, a backtracking/brute force solution as you name it (such as [Randall-Brown 72]) will provide an exact solution for the minimum coloring problem since it considers every possible vertex coloring. Note that DFS traversal could be used to sort the vertices initially (topological sort) and feed that order to the exact solver.

How to find a path from source to destination by adding edges with minimum total weight

I have about 100 atoms in a 3-D space. Each atom is a node. Edges are added between two nodes when they are closer than 0.32 nm with weight equals distance. I want to find a path from source node to destination node. Since the 100 atoms are not fully connected, sometimes I can't find a path.
What I want to do is to add one or more edges to make source and destination connected. Meanwhile, I also want to minimize the total weights of the new added edges. Again, weight is calculated from the two nodes' distance.
It is kind of a reverse problem of minimum cut. Is there any algorithm helps to do this?
Thanks a lot!
It seems like one way would be to make use of a graph search algorithm for finding the shortest path, like Dijkstra's algorithm, and perhaps work from both ends (source and destination).
The only difference is that you can't know if any edge actually exists or not, and so you are creating the graph as you go. So if you start at A, and the nodes of the graph are A, B, C, D, E. Then you need to check if A-B, A-C, A-D, and A-E exists. If only A-B exists, then you check B-C, B-D, and B-E.
This will be O(|V|^2), but will really depend on how many edges get explored.
The same idea applies if you are only interested in adding edges that are longer than 0.32 nm. It's just that the path length calculation changes. Any edges that are less than .32 nm are zero-length, or simply a lot shorter (as they become less important). If that last bit doesn't work, then it gets a bit trickier.

Find All Cycle Bases In a Graph, With the Vertex Coordinates Given

A similar question is posted here.
I have an undirected graph with Vertex V and Edge E. I am looking for an algorithm to identify all the cycle bases in that graph. An example of such a graph is shown below:
Now, all the vertex coordinates are known ( unlike previous question, and contrary to the explanation in the above diagram), therefore it is possible to find the smallest cycles that encompass the whole graph.
In this graph, it is possible that there are edges that don't form any cycles.
What is the best algorithm to do this?
Here's another example that you can take a look at:
Assuming that e1 is the edge that gets picked first, and the arrow shows the direction of the edge.
I haven't tried this and it is rather greedy but should work:
Pick one node
Go to one it's neighbors's
Keep on going until you get back to your starting node, but you're not allowed to visit an old node.
If you get a cycle save it if it doesn't already exist or a subset of those node make up a cycle. If the node in the cycle is a subset of the nodes in another cycle remove the larger cycle (or maybe split it in two?)
Start over at 2 with a new neighbor.
Start over at 1 with a new node.
Comments: At 3 you should of course do the same thing as for step 2, so take all possible paths.
Maybe that's a start? As I said, I haven't tried it so it is not optimized.
EDIT: An undocumented and not optimized version of one implementation of the algorithm can be found here: https://gist.github.com/750015. But, it doesn't solve the solution completely since it can only recognize "true" subsets.

Resources