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.
Related
Suppose I have a graph with node weights, for example:
If a node has weight -1, it is "happy". If a node has positive weight, it is "unhappy", and wants to leave the graph.
How do I efficiently calculate which nodes I should kick out of the graph in order to minimize unhappiness (i.e., total graph weight), while making sure that the graph remains connected?
For instance, in this case, I can't get rid of the 10, 8, and 10 nodes, since that would disconnect the graph. The optimal solution in this case seems to be 10 + 8 + 5 = 23.
Small correction : the optimal solution in your example is 2 + 10 + 8 + 5 = 25. Nothing prevents you from removing all but connecting "unhappy" nodes.
On the discussion about bridge : bridge are edges, here you remove nodes. This makes a big difference : when removing edges, the nodes remains, and thus you cannot remove too many of them. When removing nodes, their attached edges are removed too, and you can be much more aggressive. So much that you only need to keep path between clusters of "happy" nodes. Everything else is removed.
How do you do that ?
Replace each cluster of happy nodes (happy nodes that are connected) by one happy node. (keep track of the replaced nodes)
For each edge between an happy and an unhappy node in the starting graph, make an edge between the node representing the cluster of the happy node, and the same unhappy node. The weight is the same as the original weight.
For each pair of happy nodes (representing the original clusters), find the shortest path between them. If this path pass through a third happy node, ignore it. Else, make an edge between the two happy nodes, with a weight equal to the total weight of the shortest path. (keep track of the nodes making the path)
Remove every unhappy node
Find the MST (minimum spanning tree) of the graph. Only keep the selected edges.
Replace each remaining edge of your graph by the path it represents
Replace each happy node by the cluster of happy nodes it represents
I tried to keep this solution in a high language, I can edit it to be more rigorous if needed.
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ł
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).