In my implementation I need to find 4 non intersecting spanning trees in a 2D torus. Assuming all the links are bidirectional. and Bidirectional links are not intersecting.
Ex: my 2D torus is of 3 * 3
| | |
--0-1-2--
| | |
--3-4-5--
| | |
--6-7-8--
| | |
So, each link here is actually representing a bidirectional link for example there are two edges between 0 and 3. one from 0 towards 3 and one from 3 towards 0.
In output I should get 4 non intersecting spanning trees.
Although I have thought of some algorithm but it fails somehow.
ALgorithm:
I have the torus represented in the form of adjacency matrix. And a link between 0 and 3 are represented by 1 in adjacency matrix at position AM[0][3] and AM[3][0]
Node consist of (node_number, parent,weight)
Intially I maintain a priorList in which all nodes are set to their (node_number, -1, INT_MAX) but root of spanning tree which is set to (node_number, -1, 0)
Extract node from priorList with minimum weight, such that its parent does not have already a child. (In first iteration it would always be root). This I do by checking in the set of already extracted nodes in MST.
If found then update all its neighbours other than the nodes already extracted from priorList.
here by updating I mean update their parents, if already discovered.
Now add the extracted node in 1st step into the MST.
Go to step 1 until priorList is not empty.
It will give 1 MST but will get stuck while getting the 2nd MST from the same Adjacency matrix which was left as output while calculation of 1st MST(By which I mean if I had already used some edges in the 1st MST then I would have removed that edges from the Matrix).
Remember all 4 spanning trees should start from the same given root, and try if I can somehow use variant of Prim's algorithm(http://en.wikipedia.org/wiki/Prim%27s_algorithm).
I don't know any other algorithm as of now, but lets see if someone can help.
Related
I want to compute the level of each node in a directed graph. I'm currently applying a depth-first search algorithm on vertices that have no incoming edges. Considering the graph below, for instance:
The expected result is:
Vertex | Level
1 | 0
2 | 1
3 | 2
4 | 1
5 | 3
6 | 4
In this particular case, if we start by applying DFS on 4, then all results for vertices 4, 3, 5 and 6 are going to be wrong, since 1 has level 0. I've tried to always consider the greatest result for each one of the nodes, so in this case the results for 3, 5, and 6 are replaced when applying DFS on 1. It works, but I can't find a way to correctly compute the level of vertex 4.
I'm working only with directed acyclic graphs.
I'm not including any code here because it is a pretty straightforward DFS implementation and I'm not struggling implementation-wise.
Any hint would be much appreciated.
You can compute the levels starting from each vertex without having an incoming edge. Then you can store the maximum value for each vertex until the end. For eg :- Vertex 3 will have values 1 and 2 when traversed from starting points vertex 1 and vertex 4 respectively. At last, you can update the vertices not having the incoming edge(number on child -1). If there's a situation where there multiple children of such a vertex, then you might want to select the child with maximum number on it for replacement and then run the algorithm from that vertex again to see if changes the numbers assigned to any of the other children.
I am trying to create an algorithm to calculate the total resistance for a given an undirected graph with weighted edges. The algorithm will also be given a starting node and an ending node, which will represent the terminals connected to the power supply. For example, the graph on the top with starting node 1 and ending at node 6 will represent this circuit
(3)
|
|6
3 |
(2)------(4)
| |
|5 |1
| |
| 3 | 1
(1)------(5)------(6)
As you would realize, the 6 ohm resistor doesn't really matter in this context as current wouldn't flow through it if a voltage was applied between nodes 1 and 6. So, I figured that I should first of all 'trim' this graph. Here is the explanation of this process:
Trimming a graph is basically cutting off the parts of the graph which cannot be contained in a path between the starting node and the ending node which passes over any node at most once.
4 4
| /|
| / |
2--3 2--3 2--3
| | | | | |
| | | | | |
1--5--6 1--5--6 1--4--5
(1) (2) (3)
For example, in graph (1), node 4 should be trimmed because in any path between 1 and 6, visiting node 4 means visiting node 3 at least twice as there is no path from node 4 which doesn't visit node 3 again. If this graph is trimmed, it will become graph (3). However, if graph (2) is trimmed, it won't change because all nodes can be visited on a path from 1 to 6, including node 4.
So, how can I devise an algorithm that trims a graph with given starting and ending nodes?
EDIT: So, I have learned about the maxflow / mincut problem, and it seems like this could be used for the solution of this problem. I haven't tried it yet, but I will post another edit if I can manage to do it using flows.
Find the tree of Biconnected components.
Any node that is not on the path from s to t in the tree (or part of the component that is on the path) can be removed.
The time complexity is linear.
Biconnected component: https://en.wikipedia.org/wiki/Biconnected_component
Just remove all nodes with only one edge (not counting for the two ending nodes).
But this does not account for the nodes "beyond" the ending nodes. If in your original graph we want to calculate the resistance between nodes 5 and 6 then we do not need any other node (nodes 1, 2, 3 and 4 are all irrelevant).
To remove really all nodes that are irrelevant you have to find all different paths between the two ending nodes, and remove all nodes that are not part of any of those paths.
This can be a possible solution for this:
If starting from a node (say v) if we can reach both the source and terminal nodes with no vertices in common then that node has to be considered for the calculation of the total resistance.
By this I mean, suppose one of the path from v to source is (v, a1, a2, a3, a4, ..., source)
And the path from v to terminal be (v, b1, b2, b3, ..., terminal)
Then if the intersection of these gives zero elements, then the node v can not be trimmed. Otherwise it can be left out.
So we can have the following two solutions for this:
1. Do a depth-first search from each vertex and check if the paths have some common vertex or not.
Find all the bridges in the graph. You can look for it here: https://en.wikipedia.org/wiki/Bridge_(graph_theory)
The way to find all the bridges in a graph is given here: https://www.geeksforgeeks.org/bridge-in-a-graph/
If after removing a bridge-edge, the source and terminal vertices are in the same connected component then all the nodes on the other side of connected component can be trimmed from the graph.
For example: In the graph (1), there is only one bridge 3-4.
If we remove that we get two connected components (1,2,3,5,6) and (4). We can see that both 1 and 6 are in the same connected component, hence all other connected component nodes (here only node numbered 4) can be trimmed.
I've spending many hours on this HackerRank problem and I'm confused about what the prompt means. It says
You are given a tree (a simple connected graph with no cycles). The
tree has nodes numbered from to N.
Find the maximum number of edges you can remove from the tree to get a forest such that each connected component of the forest contains
an even number of vertices.
https://www.hackerrank.com/challenges/even-tree
and it has a graph that displays an example.
I'm confused about why it says
Find the maximum number of edges
Doesn't it mean minimum?
Because if I've trying to maximize the number of edges to remove from
1
/ | \
3 6 2
/ | / \
4 8 7 5
/ \
9 10
to make it into forests of all even counts then I would keep severing it from the top until I'm at
8
/
9
which is a single forest with an even count.
What the heck am I missing about this? Can someone help clarify?
Find the maximum number of edges you can remove from the tree to get a forest such that each connected component of the forest contains an even number of vertices
Note that when you remove an edge from tree, you increase number of connected components by one.
So if you remove m edges, number of connected component of the forest is m+1
This question now asks you to find maximum such m such that all the connected component will have even number of vertices.
If it had been minimum number of edges, answer would have been trivial (0), i.e don't remove any edge!
Hint
For solving this, for each vertex v, maintain count of vertices in subtree rooted at v. If this is even, then you can make it as different connected component.
I'm trying to work on this problem...
Below mentioned is one algorithm..i figured out..
Input a graph
select a vertex with highest degree of matching with all the other nodes.
Remove the edges that are incident on this node.
Add the selected vertex and its edge to a set X.
Return X
Where X returns the minimum set of vertices that are required for a vertex cover.Is this way correct...?
Thanks
To select a vertex with highest degree can't guarantee to give the best solution. For example,
you have a tree with 7 vertices, edges are listed as follows:
1 2 // (1,2) is connected.
1 3
1 4
2 5
3 6
4 7
The minimum vertex cover is {2,3,4}, however, based on you greedy approach, you will choose {1} first, then you will choose at least 3 vertices to covered the left 3 edges.
Indeed, there is a greedy algorithm to solve the vertex cover problem for a tree, that is you find a leaf at each step (since the input is a tree, you can always find such leaf unless there is no edge left), then select the neighbor of the leaf to the vertex cover set X. Return X as the minimum vertex cover when the graph is empty. The complexity is O(E) when E = V-1 so that we can say it is a linear solution.
I want to design a algorithm. In a directed graph G=(V,E), and each arc has a numerical weight. The algorithm needs to return a set S of arcs of maximum weight so that no two arcs in A have the same tail. Assume that it has at least 7 nodes and 10 arcs. Could anyone give some hints about this algorithm?
You say that your arcs are not allowed to have the same tail. So I would divide the set of arcs into several "bins" which are determined by the tail of the arc. I.e. you take each arc, look at its tail, and put it into the corresponding bin.
Consider the graph consistign of the following arcs:
(1->2)
(1->3)
(2->1)
(2->4)
(3->2)
Then, we have something as follows:
bin 1 | 2 | 3 | 4
arc 2 3 | 1 4 | 2 | (empty)
weight .. .. | .. .. | .. |
It is now clear, that we can take at most one arc from each bin. In order to maximize the sum, we can pick always the one with the largest weight in each bin.
Edit: Note that your algorithm does not need to do this whole bin-thing. It can just walk across all arcs and update the solution dynamically.