In an undirected, unweighted graph, and I'm trying to print (store in file) all possible connecting paths between given 2 vertices on the graph, not including cycles.
when you consider a complete graph this problem is a NP-complete. because there are "(V-2)!" different paths between 2 vertices.
However,seems it is possible to do it with one of graph traversal (DFS-BFS) algorithm with time complexity of O(|V|+|E|) which is pretty polynomial.
I got confuse about solving a NP-Complete problem in polynomial time?
any idea about what is missing here ?
If you want all possible paths, and the graph has V vertices, and E edges, then the number of paths will be dependent upon the number of connects. Consider a fully connected graph, where every point connects to every other point. Then there are (v-2)! possible paths, right? Well (v-2)! > V+E (much greater).
Related
Is it possible to find all possible directed graphs given a pair of vertices and the information that an edge exists between them? For example if we know the vertices with edge pairs like
1 2
2 3
1 3
The possible directed graphs will be:
1→2, 2→3, 1→3
1→2, 2→3, 3→1
1→2, 3→2, 1→3
1→2, 3→2, 3→1
2→1, 2→3, 1→3
2→1, 2→3, 3→1
2→1, 3→2, 1→3
2→1, 3→2, 3→1
What data-structure to be used here to work with? What can be the working logic?
I was thinking of using adjacency matrix data structure and compute all possible adjacency matrix. Each adjacency matrix will represent a graph. We can use the graph as and when needed for tasks like checking whether cycle is present or not etc.
Apologies that this is more of a discussion than a programming question, but any help will be appreciated
You could maintain one undirected graph data structure G and work with the knowledge that the existence of an edge (u,v) means that there is only one directed edge in a particular instance of digraph possibility D.
If you want to maintain all possible digraphs separately, you would need 2^m many of them, where m is the number of edges. If the vertices and edges are always the same and only the direction is the invariant, then you could maintain 2^m bit-strings, each of length m. Each bit has a 0 or 1 depending on whether the edge (u,v) it corresponds to is u-->v or v<--u. Use the bit string to give direction to the undirected graph suggested above. You just need to generate all 2^m bit strings. Not very efficient... but you can't do better if you need to work with all possibilities.
You could use the bit string to construct a digraph. But, it would be more memory efficient at least to maintain only one bit-string per 'graph' rather than repeating the entire graph data structure with only directional changes. Record bit strings in a hash table: use each edge as a key and then bit value 0/1 depending on direction. Any graph traversal of one of the many possible digraphs D works on undirected G. Then in constant time you can check for incident (undirected) edges of a vertex, which are outgoing/incoming in D. So traversals can occur just as quickly by maintaining only 1 graph object and 1 bit hash table of size 2^m (rather than 2^m graph objects).
if there are 100 graph vertexes, each graph vertex has 4 graph edges toward another graph vertex, and are stored in an array, X. "X(100, 4)" is the array's size, while "X(38, 2)" means the contents of the array at two dimensional index 38, 2.
Is there any simple way to find a way form a given starting graph vertex to another given graph vertex?
It does not have to be the shortest wat, as long as the destination can be reached.
Thanks!
Yes. This is the same as finding a path between two vertices in an undirected graph, and is a thoroughly studied concept in mathematics and computer science. The usual method is a "Depth First Search" (DFS). A suitable algorithm is described here.
Essentially it follows this pattern:
Start with x equal to the start node.
If x is the end node, then we're done.
If we've already visited x then abandon this path.
For each of the nodes y connected to x,
Add x to the current path and set y=x.
Run algorithm from step 2.
Loop to step 4.
That will explore every possible path from x, going as deep down each branch as possible to find the goal or a deadend. Hence "depth first".
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ł
Is there a known algorithm to find all complete sub-graphs within a graph? I have an undirected, graph and I need to find all complete graphs present in that undirected graph.
Is there an existing algorithm for this?
Finding the number of complete subgraphs in an undirected graph is known as the clique problem. It is not solvable in polynomial time, since the number of complete subgraphs itself might be exponential. Therefore, there is not an algorithm that will solve this for graphs of any size in a reasonable amount of time. However, here is one I found that should work for small graphs:
https://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm
I want to generate a sequence of the number of vertices in all graphs which each edge has the same number of leaving edges. I dont have to generate the whole sequence. Let's say the first 50 if exists.
I want:
Input: the number of edges leaving each vertex
Output: a sequence of the number of vertices
So far, I have looked at complete graphs. Complete graphs with n vertices always have n-1 edges leaving each vertex. But there are other kinds of graphs that have this property. For example, some polyhedrons, such as snub dodecahedron and truncated icosidodecahedron have this property.
How should I approach my problem?
I think you mean regular graphs:
http://en.wikipedia.org/wiki/Regular_graph
http://mathworld.wolfram.com/RegularGraph.html
I made a regular graph generator which isn't flawless by the way:
once you generate the nodes, say from 1 to n. You want regularity r.
For node 1, make connections to the following nodes until you reach degree r for node 1.
For node 2 you already have degree 1 (because of node 1), you connect again to further nodes until you reach degree r for node 2 too. And this way till the last node.
The flaw is that you can't define an r-regular graph for any number of nodes. The algorithm mentioned doesn't detect that, so some errors may occur. Also, this isn't a random r-regular graph generator, but instead give one possible solution.
I'm not much of an explainer, so please ask if the description lacks somewhere.