Negative weight edges - algorithm

Full question: Argue that if all edge weights of a graph are positive, then any subset of edges that connects all vertices and has minimum total weight must be a tree. Give an example to show that the same conclusion does not follow if we allow some weights to be nonpositive.
My answer: Since the edges connects all vertices, it must be a tree. In a graph, you can remove one of the edges and still connect all the vertices. Also, negative edges can be allowed in a graph (e.g. Prim and Kruskal's algorithms).
Please let me know if there's a definite answer to this and explain to me how you got the conclusion. I'm a little bit lost with this question.

First off, a tree is a type of graph. So " In a graph, you can remove one of the edges and still connect all the vertices" isn't true. A tree is a graph without cycles - i.e., with only one path between any two nodes.
Negatives weights in general can exist in either a tree or a graph.
The way to approach this problem is to show that if you have a graph that connects all components, but is NOT a tree, then it is also not of minimum weight (i.e., there is some other graph that does the same thing, with a lower total weight.) This conclusion is only true if the graph contains only positive edges, so you should also provide a counterexample - a graph which is NOT a tree, which IS of minimum weight, and which IS fully connected.

With non-negative weights, adding an edge to traverse from one node to another always results in the weight increasing, so for minimum weight you always avoid that.
If you allow negative weights, adding an edge may result in reducing the weight. If you have a cycle with negative weight overall, minimum weight demands that you stay in that cycle infinitely (leading to infinitely negative weight for the path overall).

Related

Graph marking with lives

You're given an non directed graph with integer node weights and edge weights.
A node is "mark-able" if its node weight is non negative, and marking a node will cause all its neighbors node weights to decrease by the edge weight of the edge connecting the two.
If a marked node's node weight goes below 0, it's automatically unmarked (and the decrease of its neighbors edge weights is also undone).
Solve for a largest possible set of marked nodes.
Potentially easier problems:
Limit the edge weights to be positive.
Limit the edge weights to all be 1.
Solve for the size of the solution set instead of the set itself.
Is this problem solvable in polynomial time? What's the best solution?
Actually, this sounds like a generalization of the minimum vertex cover problem, which is known to be NP-complete.
Indeed, consider a graph where weights of vertices are 0 and weights of edges are 1.
For each edge, we can mark at most one of its endpoints: otherwise, both their weights will become negative.
This property also goes backwards, meaning that any set of markings that follows the "single marked endpoint" property is also a solution.
This means that we want to mark the largest possible set of vertices so that each edge is connected to at most one marked vertex.
In turn, this means that we want to find the smallest possible set of unmarked vertices so that each edge is connected to at least one unmarked vertex.
Considering there are no isolated vertices, it sounds like the unmarked vertices are the minimum vertex cover.

Linear algorithm to make edge weights unique

I have a weighted graph, and I want to compute a new weighting function for the graph, such that the edge weights are distinct, and every MST in the new graph corresponds to an MST in the old graph.
I can't find a feasible algorithm. I doubled all the weights, but that won't make them distinct. I also tried doubling the weights and adding different constants to edges with the same weights, but that doesn't feel right, either.
The new graph will have only 1 MST, since all edges are distinct.
Very simple: we multiply all weights by a factor K large enough to ensure that our small changes cannot affect the validity of an MST. I'll go overboard on this one:
K = max(<sum of all graph weights>,
<quantity of edges>)
+ 1
Number the N edges in any order, 0 through N-1. To each edge weight, add the edge number. It's trivial to show that
the edge weights are now unique (new difference between different weights is larger than the changes);
any MST in the new graph maps directly to a corresponding MST in the
old one (each new path sum is K times the old one, plus a quantity smaller than K -- the comparison (less or greater than) on any two paths cannot be affected).
Yes, this is overkill: you can restrict the value of K quite a bit. However, making it that large reduces the correctness proofs to lemmas a junior-high algebra student can follow.
We definitely cannot guarantee that all of the MSTs in the old graph are MSTs in the new graph; a counterexample is the complete graph on three vertices where all edges have equal weights. So I assume that you do not require the construction to give all MSTs, as that is not possible in the general case.
Can we always make it so that the new graph's MSTs are a subset of the old graph's? This would be easy if we could construct a graph without a MST. Of course, that doesn't make any sense and is impossible, since all graphs have at least one MST. Is it possible to change edge weights so that only one of the old graph's MSTs is an MST for the new graph? I propose that this is possible in general.
Determine some MST of the old graph.
Construct a new graph with the same edges and vertices, but with weights assigned as follows:
if the edge in the new graph belongs to the MST determined in step 1, give it a unique weight between 1 and n, the number of edges in the graph.
otherwise, give the edge in the new graph a unique weight greater than or equal to n^2, the square of the number of edges in the graph.
Without proof, it seems like this should guarantee that only the nominated MST from the old graph is an MST of the new graph, and therefore every MST in the new graph (there is just the one) is an MST in the old graph.
Now, one could ask whether we can do the deed with additional restrictions:
Can you do it if you only want to change the values of edges that are not unique in the old graph?
Can you do it if you want to keep relative weights the same for edges which were unique in the old graph?
One could even pose optimization problems:
What is the minimum number of edge weights that must be changed to guarantee it?
What is the weighting with minimum distance (according to some metric) from the old weighting that guarantees it?
What is the weighting that minimizes the average change while also guaranteeing it?
I am hesitant to attempt these, what I believe to be much more difficult, problems.

Tricky algorithm for finding alternative route in graph with few added edges

Okay, so I found this a bit tricky.
Basically, you have a directed graph (let's call it the base graph), that has some leaves and a node with 0 indegree that is called root. It may contain cycles.
From that base graph, a tree has been made, that contains the root and all leaves, and some connection between them. The nodes and edges that are not needed to connect the root to the leaves are left out.
Now imagine one or more edges in the tree "break", and can no longer be used. The problem now is to
a) If possible, find an alternative route(s) to the disconnected node(s), introducing as few previously unused edges from the base graph as possible.
b) If not possible, select which edges to "repair", repairing as few edges as possible to get all leaves connected again.
This is supposed to represent an electrical grid, and the breaks are power outages.
If just one edge is broken, it is easy enough. But say you have a graph with 100 leaves, 500 edges, and 50 edges break. Now to find which combination of adding previously unused edges from the base graph, and if necessary repairing some edges, to connect all leaves, seems like a very hard problem.
I imagined one could do some sort of brute force, where ALL combinations of unused edges, from using 1 to all of them, are tested. Or if repairs are needed, testing ALL combinations of repairs with all combinations of new edges. When the amount of edges get high, this seems to me very very inefficient.
My question is, does anyone have any smart ideas to how this could be done in a more efficient way? I hope I explained it well enough.
This is an NP-hard problem, and I'll explain why. Imagine that you have three layers of nodes: the root node, a layer of intermediate connecting nodes, and then a layer of leaf nodes. Edges go from root to intermediate nodes, and from an intermediate node to some subset of leaf nodes. Suppose you have some choice of intermediate nodes and edges to leaf nodes that gives you a connected tree graph, where each intermediate node has an edge to only one leaf node. Now imagine all edges in the reduced graph are removed. Then to find the minimum number of edges needed to add to repair the graph, this is equivalent to finding the minimum number of remaining intermediate nodes whose edges cover all of the leaf nodes. This is equivalent to the set cover problem for the leaf nodes http://en.wikipedia.org/wiki/Set_cover_problem and is NP-hard. Thus there is almost certainly no fast algorithm for your problem in the worst case (unless P = NP). Maybe if you bound the number of edges that are removed, you can come up with a polynomial time algorithm where the exponent in the polynomial depends (hopefully weakly) on how many edges were removed.
Seems like the start to a good efficient heuristic/solution would be to weight the edges. A couple simple approaches (not the most space efficient) as to how you could weight the edges based on the total number of edges are listed below.
If using any number of undamaged edges is better than using a single alternative edge and using any number of alternative edges is better than a single damaged edge.
Undamaged edge: 1
Alternative edge: E
Damaged edge: E^2
In the case of 100 vertices and 500 edges, alternative edges would be weighted as 500, while damaged edges would be weighted as 250000.
If using any number of undamaged edges is better than using a single alternative edge or a single damaged edge.
Undamaged edge: 1
Alternative/damaged edge: E
In the case of 100 vertices and 500 edges, alternative/damaged edges would be weighted as 500.
It seems like you then try a number of approaches to find either the exact solution or a heuristic result. The main suggestion I have for an algorithm is below.
Find the directed minimium spanning tree. If you use the weighting listed above, then I believe the result is optimum if I'm understanding things correctly.
Although, if you have intermediate nodes (nodes that are neither the root or a leaf), then this is likely to result in an overestimating heuristic. In which case, you might be able to get around it by running all pairs all shortest paths first and use the path costs for that as input for the directed minimium spanning tree algorithm, but that's probably a heuristic as well.

NP-Complete? Optimal graph embedding for a graph with specific constraints

I have a grid based graph, where nodes and edges occupy cells. Edges can cross, but cannot travel on top of each other in the same direction.
Lets say I want to optimize the graph so that the distance covered by edges is minimized.
I am currently using A* search for each connection, but the algorithm is greedy and does not plan ahead. Consider the diagram below, where the order in which connections are made is changed (note also that there can be multiple shortest paths for any given edge, see green and
purple connections).
My intuition says this is NP-Complete and that an exhaustive search is necessary, which will be extremely expensive as the size of the graph grows. However, I have no way of showing this, and it is not quite the same as other graph embedding problems which usually concern minimization of crossing.
You didn't really describe your problem and your image is gone, but your problem sounds like the minimum T-join problem.
The minimum T-join problem is defined on a graph G. You're given a set T of even size, and you're trying to find a subgraph of the graph where the vertices of T have odd degree and the other vertices have even degree. You've got weights on the edges and you're trying to minimise the sum of the weights of edges in the subgraph.
Surprisingly, the minimum T-join problem can be solved in polynomial time thanks to a very close connection with the nonbipartite matching problem. Namely, if you find all-pairs shortest paths between vertices of T, the minimum T-join is attained by the minimum-weight perfect matching of vertices in T, where there's an edge between two vertices whose length is the length of the shortest path in G.
The minimum T-join will be a collection of paths. If two distinct paths, say a->b and c->d, use the same edge uv, then they can be replaced by a->u->c and b->v->d and reduce the cost of the T-join. So it won't use the same edge twice.

Completely disconnecting a bipartite graph

I have a disconnected bipartite undirected graph. I want to completely disconnect the graph. Only operation that I can perform is to remove a node. Removing a node will automatically delete its edges. Task is to minimize the number of nodes to be removed. Each node in the graph has atmost 4 edges.
By completely disconnecting a graph, I mean that no two nodes should be connected through a link. Basically an empty edge set.
I think, you cannot prove your algorithm is optimal because, in fact, it is not optimal.
To completely disconnect your graph minimizing the number of nodes to be removed, you have to remove all the nodes belonging to the minimal vertex cover of your graph. Searching the minimal vertex cover is usually NP-complete, but for bipartite graphs there is a polynomial-time solution.
Find maximum matching in the graph (probably with Hopcroft–Karp algorithm). Then use König's theorem to get the minimal vertex cover:
Consider a bipartite graph where the vertices are partitioned into left (L) and right (R) sets. Suppose there is a maximum matching which partitions the edges into those used in the matching (E_m) and those not (E_0). Let T consist of all unmatched vertices from L, as well as all vertices reachable from those by going left-to-right along edges from E_0 and right-to-left along edges from E_m. This essentially means that for each unmatched vertex in L, we add into T all vertices that occur in a path alternating between edges from E_0 and E_m.
Then (L \ T) OR (R AND T) is a minimum vertex cover.
Here's a counter-example to your suggested algorithm.
The best solution is to remove both nodes A and B, even though they are different colors.
Since all the edges are from one set to another, find these two sets using say BFS and coloring using 2 colours. Then remove the nodes in smaller set.
Since there are no edges among themselves the rest of the nodes are disconnected as well.
[As a pre-processing step you can leave out nodes with 0 edges first.]
I have thought of an algorithm for it but am not able to prove if its optimal.
My algorithm: On each disconnected subgraph, I run a BFS and color it accordingly. Then I identify the number of nodes colored with each color and take the minimum of the two and store. I repeat the procedure for each subgraph and add up to get the required minimum. Help me prove the algorithm if it's correct.
EDIT: The above algorithm is not optimal. The accepted answer has been verified to be correct.

Resources