I have a problem with graph and subgraph.
I have a directed graph G(V(G), E(G)). Each vertex is either type A or type B. Assume I have a pattern of directed or undirected graph, for example: A->B->A or A-B-A. I want to output all subgraph from graph G that have this pattern.
I have no idea for this. Does anyone have suggestion for an algorithm that can solve this problem?
Related
My teacher didnt specify what kind of graph is this so im confused how to answer some of the questions. Need help to identify if this is directed or undirected graph
An easy way to check that your graph is undirected is, once the adjacency matrix is provided is to notice that it is symmetric, if the graph is directed it cannot be simmetric because it would be an entry which A_i,j different of A_j,i.
I know how to find Articulation points for a undirected graph using DFS variant. But it seems like it is for undirected graph and only looks for back edge. But how to find Articulation points if my graph has forward edge or cross edge.I know i can always run dfs for every node and figure that out but is there any better algorithm.
Definition for Articulation points on directed graph is not unique.
It depends on what kind of connectedness you are considering. There are 3 kind of connections in directed graph
Strongly connected if there is a path from every vertex to every other vertex.
Connected if there is a path between any two nodes, but not in both directions.
Weakly connected if the graph is only connected if the arcs are replaced with undirected arcs.
If you are using second definition of connection, then U can use DFS for finding points.
I am not sure, but I think that if you define connection as Weakly connected then you can use DFS too. But it need to be proved.
Agreed that the definition of articulation is not as clear as in case of undirected graphs. However if we choose two particular nodes: source and sink and call any point that must occur on the path from source to sink articulation point, the definition is clear.
I call them unavoidable points and have got simple solution for finding them on my GitHub
I know A* algorithm can be used in directed graph, can we use it in undirected graph as well?
The A* algorithm is generic for all the graphs. So, yes, you can use it with an undirected graph.
In an undirected graph, all edges are by definition bidirectional. So it's like a directional graph where for every edge, you'd have an edge in the opposite direction. In consequence, if you have an implementation of the algorithm working for directed graphs, you should be able to extend it to undirected graphs according to this principle.
The only difficulty here is to have the appropriate data structure. If implementing the edges with a matrix, you just have to make sure that the matrix is symmetric. If you use adjacency lists, be sure that everytime you add an edge from a to b, the edge of b to a is added, with the same cost factor.
How do you convert (can we convert?) an Undirected graph into a Directed graph.
I am using Jgrapht library
This is simple, just replace the undirected edge between two nodes A and B with two directed edges. One from A->B and the other from B -> A.
I want to get a subgraph of a graph given the vertex to start at. All vertices connected to the starting vertex are considered part of the subgraph that should be returned.
I've already solved this requirement but am curious if there is a more efficient solution. The solution I came up with was to do a DFS of the graph and record every vertex that was encountered in a set, S. Then, I simply took all of the edges from the original graph that were connected to a vertex in S and I built a subgraph from it. The edges in the original graph are stored in a C# Dictionary which I believe is basically a hash.
DFS and BFS do not work because if you have two vertices that both have the same child, BFS or DFS will not traverse one of those edges. Hence the subgraph in that case would contain all of the correct vertices, but be missed some edge pairs.
Is there a better solution than the one I've come up with?
I think a BFS traversal is the most efficient algorithm for this.
If you do a BFS and enqueue all neighbors for each node (i.e. traverse all the edges attached to the current node) and only abort traversal when the current node has already been visited, you avoid the problem you described with "same child" / "missed edges".
a "fast" algorithm that enumerates all induced subgraphs of
given size can be found here:
http://theinf1.informatik.uni-jena.de/~wernicke/motifs-wabi2005.pdf
does this help?