Print all nodes that you can pass through while you are going from one node to another (these two are of course given) - graph

I'm trying to solve a problem in an undirected graph where you have two nodes and you havet to find all of the vertices that are contained in the paths between these two nodes.
I solved it by finding all of the paths and than with joined the results and so I got the solution to the problem. However this wasn't efficient enouhg. Im lookinf for a better solution

Related

What are all the cases in which BFS and DFS produce the same tree on a graph?

I want to know what are the cases in which BFS and DFS produce the same tree from a graph rooted at any node. I know one of the cases is when the graph is already a tree. Is this the only case?
Is it dependent on the way you choose the neighbors of a node? What are some ways that the order of picking the neighbors will make it the same?
First, before delving into a direct answer to the question, I want to take a slight moment to explain DFS and BFS. This is for anyone reading who may not know what "breadth first search" and "depth first search" are.
DFS: Depth-First Search is a tree-traversal algorithm that starts at a root node and explores as far along as possible for each branch before backtracking.
BFS: Breadth-First Search is a tree traversal algorithm that starts at a root node and explores all vertices at the current depth-level before moving on to explore the vertices at the next depth level. This is a top-down approach to create a tree.
Now, onto the question.
I know one of the cases is when the graph is already a tree. Is this the only case?
You are correct in saying this is one of the cases where BFS and DFS of a graph produce the same tree. To be more explicitly clear, when you state that the graph is already a tree, you're hopefully referring to a graph that has one of the three following properties:
A graph with a maximum breadth of 1
A graph that resembles a singly linked list
When you state the tree has to be the same, if isomorphic trees are regarded to as
the "same" by your definition, then any graph that is already a tree (which may also be what you're referring to in your question) will produce the "same" tree for BFS and DFS. Of course, there must be no cycles within the tree, or the graph will result in different outcomes for BFS and DFS.
In all three of these instances, the DFS and BFS of the tree will be the same given that you start at the same proper root node for both traversals. These are the only cases that I know of. However, in some instances, there are other means of obtaining the same tree for DFS and BFS traversal of a given graph if you have the freedom to determine the order you can select "neighbors" of a given node. This leads to the second half of your question...
Is it dependent on the way you choose the neighbors of a node? What are some ways that the order of picking the neighbors will make it the same?
Yes, it is dependent (if this question means what I really think it means), because if you have the freedom for this, then there are instances in which the order of picking neighbors will make it the same. For example, having a tree in which there is at most only one child that also has children, you can obtain the same BFS and DFS by traversing the nodes without children first in the DFS traversal of the graph.
Hopefully this information helps.

Longest path in unweighted undirected graph starting and finishing in the same vertex

I have a problem in which I need to find the longest path. Given an unveighted undirected graph. Starting from a given vertex I need to visit as many vertices as possible and finish in the same one without visiting each of them more then once.
Most of the algorithms I found were for a special case (acyclic, directed etc.). An idea can be to find Hamiltonian cycle for every subset of the vertices (the subset can be generated with backtrack). But I guess there must be a far better algorithm.
As you've discovered, finding the largest cycle involves finding the Hamiltonian cycles of its subgraphs, and thus is NP-complete - unless you're working on some special class of graphs, any solution is going to be exponential in complexity.
A smart brute force approach (e.g. bitmask) is the best efficiency one can get for this type of problem.

Compute automorphism group of graph / check if two graphs are isometric (DAG)

This has to be a well researched problem, but I am struggling researching it.
I started here, but I am looking for algorithms to study and implement.
http://en.wikipedia.org/wiki/Graph_isomorphism_problem
For example, if I have two of these DAGs (Directe Acyclic Graphs), I want to mark/delete one of them because it is just a rotation/reflection of the first. Being in the same automorphism group means they can be rotated/reflected to have the exact same adjacency matrix right?
you can use nauty or saucy algorithm to compute this problem.
This links might be helpful to you. :)
Nauty:
http://cs.anu.edu.au/~bdm/nauty/
http://cs.anu.edu.au/~bdm/nauty/
Saucy:
http://vlsicad.eecs.umich.edu/BK/SAUCY/
There is also a list of ready made tools available (especially for command line in linux based OS), in the saucy page.

Graph Clustering for almost Clustered Graph by removing nodes(vertices)

I want to carry out Graph Clustering in a huge undirected graph with millions of edges and nodes. Graph is almost clustered with different clusters joined together only by some nodes(kind of ambiguous nodes which can relate to multiple clusters). There will be very few or almost no edges between two clusters. This problem is almost similar to finding vertex cut set of a graph, with one exception that graph needs to be partitioned into many components(their number being unknown).(Refer this picture https://docs.google.com/file/d/0B7_3zLD0XdtAd3ZwMFAwWDZuU00/edit?pli=1)
Its almost like different strongly connected components sharing a couple of nodes between them and i am supposed to remove those nodes to separate those strongly connected components. Edges are weigthed but this problem is more like finding structures in a graph, so edge weigths won't be of relevance. (Another way to think about the problem would be to visualize Solid Spheres touching each other at some points with Spheres being those strongly connected components and touching points being those ambiguous nodes)
I am prototyping something, so am quiet short of time to pick up Graph Clustering Algorithms by myself and to select the best possible. Plus i need a solution that would cut nodes and not edges since different clusters share nodes and not edges in my case.
Is there any research paper, blog that addresses this or somewhat related problem? Or can anyone come up with a solution to this problem howsoever dirty.
Since millions of nodes and edges are involved, i would need a MapReduce implementation of the solution. Any inputs, links for that too?
Is there any current open source implementation in MapReduce that can i directly use?
I think this problem is analogous to Finding Communities in online social networks by removing vertices.
Your problem is not so simple. I am afraid that it is related to the clique problem, which is NP complete, so unless you quantify somehow the statement "there are almost no edges between the clusters", your problem might be still very difficult. But what I would do in your shoes, would be to try one dirty, greedy approach, namely regarding the nodes as the following kind of quasi-neural net:
Each vertex I would consider to have inputs, outputs and a sigmoid activation function which convert the input value (sum of inputs) into the output value. The output value, and I consider this important, would not be cloned and sent to all the neighbors, but rather divided evenly between the neighbors. In addition to this, I would define a logarithmic decay of activity in a neuron (self-suppression, suppressive connection to itself), defined by a decay parameter global for the net.
Now, I would start simulation with all the neurons starting from activity 0.5 (activity range is 0 to 1) with very high decay parameter, which would lead to all the neuronst quickly stabilizing in 0 state. I would then gradually decrease the decay parameter until the steady state result would yield the first clique with non-zero stable activity.
The question is what to do next. One possibility is to subtract the found clique from the graph and run the same process again until we find all the cliques. This greedy approach might succeed if your graph is indeed as well behaved (really almost clustered) as you say, but might lead to unexpected results otherwise. Another possibility is to give the found clique a unique clique smell that would be repulsive (mutual suppresion) to other cliques an rerun the algorithm until the second clique is found, give it a different clique smell repulsive to all others etc., until each node has its own assigned smell.
I think this would be as many big ideas as i have about this.
The key is, that since it is probably not possible to solve this problem in the general case (likely NP complete), you need to take use of whatever special properties your graph has. That means you need to play with parameters for a while until the algorithm solves 99% of the cases that you encounter. I don't think that it is possible to give the numerically precise answer to your question without long experimentation with the actual datasets that you encounter.
Since millions of nodes and edges are involved, i would need a MapReduce implementation of the solution. Any inputs, links for that too?
In my experience I doubt if using Map/Reduce over here would be truly advantageous. First 10^6 order of nodes isn't really that large [that too in a non hyper-connected graph, since you are considering clustering], and the over head of using Map/Reduce [unless you already have setup your hardware/software for it] for your problem will not be worth it.
Map/Reduce will work much better, where once you have solved the clustering issue, and then want to process each cluster with similar analysis. Basically when you can break your task into relatively isolated sub-tasks, which can be performed in parallel. This of course can be cascaded to several layers.
In a relatively similar situation, I personally first modelled my graph into a graph database (I used Neo4J, and would recommend it highly) and then ran my analytic and queries on it. You will be surprised as to how white board friendly this solution is, and even massively joined and connected queries will be executed near instantaneously especially at the scale of only a few million nodes. For example, you can do a filtered analysis, based on degrees of separation, followed by listing of commons.

In a graph, how to find the nearest node to a group of nodes?

I have an undirected, unweighted graph, which doesn't have to be planar. I also have a subset of graph's nodes (true subset) and I need to find a node not belonging to the subset, with minimum sum of distances to all nodes in the subset.
So far, I have implemented breath-first search starting from each node in the subset, and the intersection that occurs first is the node I am looking for. Unfortunately, it is running too slow since the graph contains a large number of nodes.
An all-pair shortest path algorithm allows you to find the distance of all nodes to each other in O(V^3) time, see Floyd-warshall. Then summing afterwards will at least be quadratic and I believe worst case cubic as well. It's a very straightforward and not terribly fast way of doing it, but it sounds like it might be an order of magnitude faster than what you're doing right now.

Resources