Coloring a graph using Depth first traversal - graph

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.

Related

Does igraph has "has_path" function?

I am trying to port code from python NetworkX to R igraph. In NetworkX there is a function with the name has_path that finds if two vertices have a path. I want to find in an efficient way all the vertices of a graph that don't have an edge between them but they have a path.
I think you can use the code below to check if there is a path from vertex V1 to V2 (the graph can be directed or undirected)
c(!is.infinite(distances(g, V1, V2, mode = "out")))
If you need to check this repeatedly in an undirected graph, simply break it into connected components and check if both vertices are within the same component. This will be very efficient, as the components need to be found only once.
See the components function. It gives you a membership vector. You need to check if the position corresponding to the two vertices has the same value (same component index).
If the graph is directed, the simplest solution is the one posted by #ThomasIsCoding. This is perfectly fine for a one-time check. Speeding up repeated checks is more trouble and warrants its own question.

algorithm for 'generalized' matching in complete graphs

My problem is a generalization of a task solved by [Blossom algorithm] by Edmonds. The original task is the following: given a complete graph with weighted undirected edges, find a set of edges such that
1) every vertex of the graph is adjacent to only one edge from this set (i.e. vertices are grouped into pairs)
2) sum over weights of edges in this set is minimal.
Now, I would like to modify the first goal into
1') vertices are grouped into sets of 3 vertices (or in general, d vertices), and leave condition 2) unchanged.
My questions:
Do you know if this 'generalised' problem has a name?
Do you know about an algorithm solving it in number of steps being polynomial of number of vertices (like Blossom algorithm for an original problem)? I don't see a straightforward generalisation of Blossom algorithm, as it is based on looking for augmenting paths on a graph compressed to a bipartite graph (and uses here Hungarian algorithm). But augmenting paths do not seem to point to groups of vertices different than pairs.
Best regards,
Paweł

graph similarity having multiple edges between two nodes

There are many theories about calculating of graph similarity such as vertex edge overlap, jacard, co-sine, edit distance, signature similarity, lambda distance, deltacon so on. These things are based on single edge of the graph. But there are many graphs having multiple edges in real world.
Given similar two graphs like above, how could we calculate graph similarity?
Using previous graph similarity, there are only 2-dimension vector and the entry is just scalar that is number, but in multiple edge's graph, the entry should be tuple. Because there are one more actions between nodes. For the previous method, it could be called who-knows-whom schem, but latter graph, it could be said who-knows-whom*-how*. I think the previous mothods could be used for the multiple edge's graph easily, so there aren't logic or methods about it.
Thanks in advance!
There is not "the" way yo compute graph similarity.
Depending on your data and problem, very different approaches may be good. In many cases, simply merging the two edges into one makes perfect sense. For example, if I have two roads of capacity x and y to go from A to B - for many analyses this is comparable to having just one rode, with the combined capacity.

Take certain edges in Dijkstra (or DFS)

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.

shortest path search in a map represented as 2d shapes

I have a small library of a few shortest path search algorithms. They were developed for simple undirected graphs (the normal representation - vertices and edges). Now I'd like to somehow apply them on a bit different scenario - where the maps are represented as 2-dimensional shapes, connected by shared edges (edges of the polygons, that is). In this scenario, the search can start/end either at a map object or some point (x,y). What would be the best approach? Try to apply the algorithms onto shapes? or try to extract a 'normal' graph out of the shapes (I have preprocessing time available)? Any advice would be much appreciated, as I'm really not sure which way to go, and I don't have enough time (and skill) to explore many options...
Thanks a lot
What's the "path" you're looking for? A list of the shapes to traverse? (Otherwise you just draw a straight line between start+end points.)
It's easy to preprocess it into a format where the shapes are vertices and are connected by edges when the shapes share a polygon side. Then, just pass it off to your existing library to get the answer.

Resources