having a bidirectional graph, best way to remove paths that join certain nodes? - algorithm

Imagine i have a bidirectional graph with 4 nodes with the following connections:
0 <-> 2 ; 0 <-> 3 ; 1 <-> 2 ; 1 <-> 3
now imagine i have a group of nodes K (0 and 1), and i want to calculate the minimum amount of connections i have to remove so that those nodes aren't ALL connected.
0 <-> 3 ; 1 <-> 2
this way theres no path that can connect 0 and 1. in fact even if the group of nodes K were something like 10 nodes, 9 could be connected if at least 1 isn't (thats why i used high case for "all" above).
another example would be:
0 <-> 2 ; 0 <-> 3 ; 0 <-> 4 ; 1 <-> 2 ; 1<->3
and a group of nodes K (0, 1, 4) i would only need to remove 1 connection to avoid them ALL connecting
0 <-> 4
I've tried a lot of things on my own, like calculating all paths of the K group and checking for repetitive paths and removing those, but it doesn't work for all cases (like the first one i posted above).
is there an algorithm that can help me with this? i've tried google but i cant find documentation for this type of problem, maybe its not very common.
thanks in advance.

Example 1:
From your graph:
(0,2),(0,3),(1,2),(1,3)
2
/ \
0 1
\ /
3
K(0, 1)
Create a tree like this:
0
/ \
2 3
/ \
1 1
Each branch begins at 0 and ends at 1. If a branch does not reach 1, it's not included. Remove the topmost edges (in case of branching below that point). It doesn't matter if you build the tree from 0 to 1 or from 1 to 0 since the graph is bidirectional.
Example 2:
Graph:
(0,1),(1,2),(2,3)
0 -- 1 -- 2 -- 3
K(1, 2)
Tree:
1
|
2
Remove:
(1,2)
Example 3:
Graph:
(0,2),(0,3),(0,4),(1,2),(1,3)
0
/ | \
2 3 4
\ /
1
K(0, 1, 4)
Tree:
0
/ | \ <-- 2 edges leading to 1; 1 edge leading to 4
2 3 4
| |
1 1
Remove:
(0,4)

You can count the number of edges that each node has. If you disconnect all the edges from a node, you disconnect the graph. So, the minimum amount of connections you have to remove is the number of edges that the vertex with the least edges has.
Say your graph has bidirectional connections 0-1, 0-2, 0-3, 2-3, 3-1.
0 has 3 edges connecting it.
3 has 3 edges connecting it.
1 has 2 edges connecting it.
2 has 2 edges connecting it.
So you should remove 0-2 and 2-3 to disconnect 2 from the graph. Now you can no longer go from 2 to any of the other points.

Related

What heuristic cost is correct? Why my is incorrect? Finding optimal path on graph

I have simple graph and I need to find heuristic costs for this graph.
Graph is (matrix representation):
0 1 2 0 0 0 0
1 0 0 3 3 2 0
3 0 0 2 0 0 0
0 3 1 0 1 0 0
0 3 0 1 0 6 0
0 2 0 0 6 0 2
0 0 0 0 0 2 0
Image:
Values in brackets means heuristic costs of the vertex for current goal vertex.
Green vertex is start and red vertex is goal.
I created this heristic costs matrix:
0 2 6 3 1 9 5
9 0 2 4 6 4 1
1 3 0 5 2 9 4
3 1 5 0 1 7 8
0 6 2 1 0 10 14
2 1 6 3 7 0 5
1 4 3 2 1 3 0
I have to explain this. This matrix represents this: for example goal vertex is 7; we find 7th row in matrix; value in 1st col means heuristic cost from 1 vertex to 7 vertex (7 is goal); value in 5nd col means heurisitc cost from 5 vertex to 7 vertex (7 is goal); if 5 is goal, we will work with 5 row, etc...
This heusristic costs based on nothing. I don't know how to find good heuristic costs. That is the question.
To summarize:
Firstly, my algorithm found wrong path (because of wrong heuristics probably). It found 1-3-4-5 (length 5), but best is 1-2-5 (length 4).
Also, teacher said, that my heuristic costs prevents the algorithm to find good path, but not helps him. I have problems with translating what he said into english, but he said somethink like: "your heuristic mustn't overestimate best path". What does it mean?
So the question: how to find good heuristic costs in my case?
I am going to wrap my comments as an answer.
First, note that "overestimate best path" means that your shortest path from some node v to the goal is of length k, but h(v)=k' such that k'>k. In this case, the heuristic is overestimating the length of the path. A heuristic that does it for 1 or more nodes is called "inadmissible", and A* is not guaranteed to find the shortest path with such a heuristic.
An admissible heuristic function (never overestimating) is guaranteed to provide the optimal path for A*.
The simplest admissible heuristic is h(v) = 0 for all v. Note that in this case, A* will actually behave like Dijsktra's Algorithm (which is basically an uniformed A*).
You can find more informative heuristics, one example is to first pre-process the graph and find the shortest unweighted path from each node to the goal. This can be done efficiently by BFS. Denote this unweighted distance from some v to the goal as uwd(v).
Now, you can create a heuristic which is uwd(v) * MIN_WEIGHT, where MIN_WEIGHT is the smallest edge weight in the graph.

backtracking for graph with adj list

Consider a graph with adjacency list as given below and with 4 vertices and 4 edges.
1 2 3 4
1 0 1 0 1
2 1 0 1 0
3 0 1 0 1
4 1 0 1 0
It is a simple rectangular graph.
In m-colorability graph problem we have to colour the graph with no adjacent nodes of the same color.I am reading a book which says that if the degree of the graph is 'd' then graph can be colored in d+1 ways.But the above graph can be colored in 2 ways which is the degree of the graph.How?
If graph is fully connected then the graph is colorable with d+1 color. In this case it is not fully connected so colors can be less than d+1 which is 3 here and as seen it is 2 in this case.
Note: fully connected means all vertices are connected to each other directly by edge.

Hungarian (Kuhn Munkres) algorithm oddity

I've read every answer here, Wikipedia and WikiHow, the indian guy's lecture, and other sources, and I'm pretty sure I understand what they're saying and have implemented it that way. But I'm confused about a statement that all of these explanations make that is clearly false.
They all say to cover the zeros in the matrix with a minimum number of lines, and if that is equal to N (that is, there's a zero in every row and every column), then there's a zero solution and we're done. But then I found this:
a b c d e
A 0 7 0 0 0
B 0 8 0 0 6
C 5 0 7 3 4
D 5 0 5 9 3
E 0 4 0 0 9
There's a zero in every row and column, and no way to cover the zeros with fewer than five lines, but there's clearly no zero solution. Row C has only the zero in column b, but that leaves no zero for row D.
Do I misunderstand something here? Do I need a better test for whether or not there's a zero assignment possible? Are all these sources leaving out something essential?
You can cover the zeros in the matrix in your example with only four lines: column b, row A, row B, row E.
Here is a step-by-step walkthrough of the algorithm as it is presented in the Wikipedia article as of June 25 applied to your example:
a b c d e
A 0 7 0 0 0
B 0 8 0 0 6
C 5 0 7 3 4
D 5 0 5 9 3
E 0 4 0 0 9
Step 1: The minimum in each row is zero, so the subtraction has no effect. We try to assign tasks such that every task is performed at zero cost, but this turns out to be impossible. Proceed to next step.
Step 2: The minimum in each column is also zero, so this step also has no effect. Proceed to next step.
Step 3: We locate a minimal number of lines to cover up all the zeros. We find [b,A,B,E].
a b c d e
A ---|---------
B ---|---------
C 5 | 7 3 4
D 5 | 5 9 3
E ---|---------
Step 4: We locate the minimal uncovered element. This is 3, at (C,d) and (D,e). We subtract 3 from every unmarked element and add 3 to every element covered by two lines:
a b c d e
A 0 10 0 0 0
B 0 11 0 0 6
C 2 0 4 0 1
D 2 0 2 6 0
E 0 7 0 0 9
Immediately the minimum number of lines to cover up all the zeros becomes 5. This is easy to verify as there is a zero in every row and a zero in every column. The algorithm asserts that an assignment like the one we were looking for in step 1 should now be possible on the new matrix.
We try to assign tasks such that every task is performed at zero cost (according to the new matrix). This is now possible. We find the solution [(A,e),(B,c),(C,d),(D,b),(E,a)].
We can now go back and verify that the solution that we found actually is optimal. We see that every assigned job has zero cost, except (C,d), which has cost 3. Since 3 is actually the lowest nonzero element in the matrix, and we have seen that there is no zero-cost solution, it is clear that this is an optimal solution.

Construct a graph from given node degrees

I have to find which of the following values can be the degrees of an undirected graph with 6 vertices:
a) 3 2 2 2 3 3
b) 4 2 2 2 3 2
c) 5 2 2 2 0 3
d) 5 2 2 2 1 2
I only method I found is to try to draw the graph on a sheet of paper and then check if it is possible.
I just need a hint to start this problem, if possible, in other way than drawing each graph.
The following algorithm decides if a simple graph can be constructed with given node degrees:
sort the degrees in descending order
if the first degree is 0 (i.e.all degrees are 0) then obviously such a graph can be formed (no edges) and you are done.
if the first degree has value d (> 0) then the following d degrees must be greater 0. If not you are done: no such graph can be formed.
take away the first degree (value d) and reduce the following d degrees by one (i.e. draw the requested number of edges from the node with highest degree to the nodes with highest degrees among the remaining ones - see proof below for correctness of this assumption), then continue with step 1 (with now one node less)
example a) (can be rejected because of the odd sum of weights, but also the above algorithms works)
3 2 2 2 3 3
3 3 3 2 2 2
2 2 1 2 2
2 2 2 2 1
1 1 2 1
2 1 1 1
0 0 1
1 0 0
-1 not possible
example c)
5 2 2 2 0 3
5 3 2 2 2 0
2 1 1 1 -1 not possible
example d)
5 2 2 2 1 2
5 2 2 2 2 1
1 1 1 1 0
0 1 1 0
1 1 0 0
0 0 0 ok
What is missing is a proof that if a graph can be drawn with given node degrees, then one of the matching graphs has this property of step 4, i.e. that the node with highest degree is connected with the nodes with next highest degrees.
Let us therefore assume that A is the node with highest degree and that it is connected with a node B whose degree is less then the degree of node C not being connected to A. Since degree(C) > degree(B), there is node D connected to C and not connected to B. Thus, there are edges AB and CD, and there are no edges AC nor BD. So we can replace AB and CD by the edges AC and BD without changing the nodes' degrees.
By repeating this procedure enough times we can make all nodes with the next highest degrees being connected to node with the highest degree.
The handshaking lemma or degree sum formula is necessary and sufficient condition in this case, since we only care that it forms an undirected graph (orientation of the edge doesn't matter, but nothing is said about loop or parallel edges). Therefore, option c and option d are valid 6-vertex undirected graph.
If the question asks for simple undirected graph (loop and parallel edges disallowed), then we need to bring in the algorithm by Havel/Hakimi, which is as described by #coproc.

ACM MIPT - Graph existence puzzle - examples unclear

This is with reference to the 'graph existence' problem - http://acm.mipt.ru/judge/problems.pl?problem=110. Can someone explain why there is no tree in example 1 but there is a tree in example 2? In both examples, vertices 0, 1, 2 and 3 are connected to each other. Here is the problem statement and examples for your reference:
You are given a matrix of distances in a graph.
You should check whether this graph could be a tree or set of trees (forest).
Edge length is 0 or positive integer.
Input: The first line contains number of vertices N.
Next N lines contains matrix (only left bottom triangle of matrix).
Distance -1 corresponds to infinite distance.
Output: Output YES or NO. If YES, then next lines should contains list of edges
of the tree (any tree (forest) with given distance matrix).
Each edge is coded by two identifiers of it's ends.
Vertex identifiers are numbers 0, 1, ..., N-1.
Input#1
4
0
1 0
1 1 0
1 1 1 0
Output#1
NO
Input#2
5
0
1 0
2 1 0
3 2 1 0
-1 -1 -1 -1 0
Output#2
YES
0 1
1 2
2 3
The problem is not very well translated from its Russian original.
The given matrix is not the matrix of edges in the graph as one might conclude, but a distance matrix. Each edge probably has weight of 1, but I am not entirely sure has a nonnegative weight. One has to check if the matrix can be realized by a tree or a forest.
That is in the first example all vertices are connected, but the second example can be realized the graph looks like:
Example 2:
(0) - (1) - (2) - (3) (4)
The graph in example 1 is
Example 1:
(0) - (1) - (2) - (3)
|_____|_____| |
| |___________|
|_________________|

Resources