Is there any algorithm to break a Connected Undirected Graph into exactly 2 connected components by deleting Minimum number of vertices.
Example 1: edge list [1-2, 2-3, 3-4], here we can delete vertex number 2 or vertex number 3 to decompose the graph into two connected component.
Example 2: edge list [1-2, 2-5, 2-3, 3-4], here we cannot delete vertex number 2 as it decomposes the graph into 3 connected components (which we don't want) but we can delete vertex number 3 to decompose the graph into two connected components.
You need to look for minimal vertex separator algorithm.
Related
In a directed graph how to efficiently count the number of vertices from which each other vertex in graph is reachable?
If there is no cycle in the graph, there can be only one such vertex and it has an in-degree zero, and there are no other vertices with an in-degree zero. You then have to run a DFS to check if all other vertices are reachable from it. So the answer is either one or zero, depending on the result of DFS.
If there is a cycle, then all the vertices in the cycle have this property or none of them have it.
If you detect a cycle, replace all the vertices in the cycle with one vertex and keep a label for that vertex of how many vertices it represents. Use the same procedure as above. I.e., check in-degrees and run DFS from the new node. The answer will be zero or the label.
Detecting a cycle can be accomplished using a DFS.
There might be several cycles in the graph. In that case you have to eliminate all of them. You can eliminate all of them in one linear pass of DFS, but that's tricky. You could also use Tarjan's algorithm as suggested by btilly in his answer.
Use Tarjan's strongly connected components algorithm to detect all loops then construct a graph with each strongly connected component collapsed to a single node.
Now in this new graph, it is sufficient to look for a vertex with no in edges. If that vertex connects to every other one (verifiable with a breadth first linear search), then everything in the strongly connected component that it came from is in your set, otherwise no vertex is in your set.
I want to solve a problem where is given an undirected graph and its vertices, a value k, where k is the number of vertices that should be in each connected component, and I have to find all connected components of the given graph which has only even numbers vertices. I have to find out how many such connected components are there and save the value of each vertices from each connected components.
Example: K=3 and we are given the following graph:
For that graph we have 2 connected components where all vertices are even numbers. The first connected component is made of the following vertices : 8, 2, 4; and the 2nd connected component is made of the following vertices : 2, 4, 6.
Is there an algorithm for finding the connected components in an undirected graph of a given amount of vertices? Or can anyone help me with an idea on how I should approach the problem? I've tried doing it with the DFS algorithm but I ended up nowhere.
Brute force:
Break the problem into two sub-problems:
1) Identify all connected components (CC) that contain all even numbers, and of arbitrary size. In the example you gave above, there would be only one CC: (8,2,4,6). This can be done in O(e+n) time, where e is the number of edges and n the number of nodes using BFS or DFS. (This is the fast step.)
2) Within each CC from step 1) enumerate all connected subgraphs of size k. This is the slow part. The brute force solution for a CC of size m nodes is O( k* (m choose k)) = O(k m^k) by enumerate all m choose k possible k nodes from m and using k operations to determine if they are connected.
Given that (1) is fast and (2) is slow you are probably not understanding the problem well enough to know how to circumvent the need to compute (2). There may be a faster way to compute (2), what I suggested is probably as close as posisble to brute-force.
For example, I have a matrix like this;
1 2 3 4
2 4 5 1
1 2 4 1
5 3 2 1
and I select 3 nodes from this matrix as random. How can I make graph from these nodes? Is there any algorithm or way to make this happen? I know how to make adjacency matrix from graph but I just can't make the graph from the random matrix right now.
EDIT:
For example, I select row:1 col:1 as first node and row:3 col:1 as second node, it should find the shortest way between first node and second node and make graph of them.
I assume that this 4x4 matrix represents the adjacency matrix from a graph with 4 nodes. In this case, taking 3 nodes randomly, would mean to select 3 random lines and take the corresponding columns, and you have a reduced graph defined by its adjacency list.
EDIT:
According to your edit, each of the 16 matrix item would be a node uniquely identified by its coordinates (i,j). Each of these nodes would be connected at least to his 2 to 4 neighbors. Then some clarifications are needed:
is the value of the item just the label of on of the 16 nodes ?
are the only moves vertical and horitontal or are diagonal allowed ?
are moves bound by the borders or can thy flip over (i.e. The last item of a row is connected with the first) ?
are the cost of each move from one to the next equal or is the cost of each move related to the values of the node traversed ?
You can then easily build the 16*16 ajacency matrix for the 16 items and apply the method of the shortest path.
I am trying to perform testing on my graph class's dijkstras algorithm. To do this, I generate a graph with a couple thousand vertices, and then make the graph connected by randomly adding thousands of edges until the graph is connected. I can then run the search between any two random vertices over and over and be sure that there is a path between them. The problem is, I often end-up with a nearly-dense graph, which because I am using an adjacency list representation, causes my search algorithm to be terribly slow.
Question :
Given a set of vertices V, how do you generate a strongly-connected, directed graph, that has significantly less edges than a dense-graph over the same vertices would have?
I was thinking about simply doing the following :
vertex 1 <--> vertex 2, vertex 2 <--> vertex 3, ..., vertex n-1 <--> vertex n
And then randomly adding like n/10 edges throughout the graph, but this doesn't seem like an optimal way of coming up with random graph structures to test my search algorithms on.
One approach would be to maintain a set of strongly connected components (starting with |V| single-vertex components), and in each iteration, merge some random subset of them into a single connected component by connecting a random vertex of each one to a random vertex of the next one, forming a cycle.
This will tend to generate very sparse graphs, so depending on your use case, you might want to toss in some extra random edges as well.
EDIT: Intuitively I think you'd want to use an exponential distribution when deciding how many components to merge in a single iteration. I don't have any real support for that, though.
I don't know if there is a better way of doing it, but at least this seems to work:
I would add E (directed) edges between random vertices. This will generate several clusters of vertices.
Then, I need to connect those clusters to form a chain of clusters, so ensuring that from a cluster I can reach any other cluster. For this I can label a random vertex of each cluster as the "master"vertex, and join the master vertices forming a loop. Thus, you have a strongly-connected directed graph composed of clusters (not vertices yet). The last master should be connected back to the first master, thus creating a loop.
Now, in order to turn it into a strongly-connected digraph composed of vertices I need to make each cluster a strongly-connected digraph by itself. But this is easy if I run a DFS starting at the master node of a cluster and each time I find a leaf I add an edge from that leaf to its master vertex. Note that the DFS must not traverse outside of the cluster.
I think this may work, though the topology will not be truly random, it will loop like a big loop composed of smaller graphs joined together. But depending on the algorithm you need to test, this may come in handy.
EDIT:
If after that you want to have a more random topology, you can add random edges between vertices of different clusters. That doesn't invalidate the rules and creates more complex paths for your algorithm to traverse.
I have an undirected graph network made of streets and crossings, and I would like to know if there is any algorithm to help me finding closed loops, ie places where I can put buildings.
Any help appreciated, thanks !
Based on comments to my earlier answer:
It seems the graphs are all undirected and planar, i.e. can be embedded in a 2D plane without crossing edges, and one such embedding is given. This embedding will partition the plane. E.g. a figure 8 partitions the plane in three: two "inner" areas and an infinite outer area. An alternative view is that all edges of a node are cyclically ordered. (This is the essential part that allows us to apply graph theory)
A partition is necessarily enclosed by a cycle, but not all cycles may partition a single area. In the trivial case of a figure 8, though, all three areas are directly associated with a distinct cycle.
The input graph can generally be simplified. Some nodes may have only a single edge; they can't contribute to the partitioning and can be removed along with the edge. Other nodes have two edges connecting distinct nodes. Here, the node and the two edges can be replaced by a direct edge connecting the neighbors. I.e. a figure 8 graph can be simplified to two nodes and three edges between them. (This is not a necessary step but helps computation).
Now, each vertex will have two areas to either side (since they're undirected, "left and right" aren't obvious distinctions). So, for |V| vertices, we need to consider 2 * |V| sides. They're in general not distinct. Two adjacent edges (connected to the same node) may border the same area, if they're also adjacent in the cyclic order of edges of that node. Obviously, for nodes with only two edges, the two edges share both areas (which is why we'd eliminated them in the previous step). For nodes with three edges, any two edges share at least one area.
So, here's how to enumerate those areas: Assign a sequential number to all edges and vertices. Assign a direction to each edge so it runs from the lower-numbered edge to the higher. Start with vertex 1, right side, and number this area 1. Trace the boundary edges of this area, assigning the same number 1 to the appropriate sides of its boundary edges. You do this by taking at each node the next adjacent edge in counter-cyclical order. When you get back to your starting point, you know all edges bounding area 1.
You then check the left edge of the first vertex. If it's not part of area 1, then it's area 2, and you apply the same algorithm. Next, check vertex 2, right side and left side, etc. Each time you find an edge and a side that's unnumbered yet, assign the next area number and trace the edges of the newly founded area.
There's a slight problem with determining which area number corresponds to infinity. To see this, take a simple () graph: two edges, two nodes, and two areas (inside and outside). Due to the random numbering of edges and vertices, outside may end up as either 1 or 2. That's unavoidable; in graph theory there's no distinction between inside and outside.
It's a standard function in the Boost Graph library. See this previous answer for details.