Graph marking with lives - algorithm

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.

Related

Negative weight edges

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).

Prove that a graph is bipartite

Given a graph G in which every edge connects an even degree node with an odd degree node. How can i prove that the graph is bipartite?
Thanks in advance
This is the Welsh-Powell graph colouring algorithm:
All vertices are sorted according to the decreasing value of their degree in a list V
Colours are ordered in a list C
The first non-coloured vertex v in V is coloured with the first available colour in C. "Available" means a colour that has not previously used by this algorithm
The remaining part of the ordered list V is traversed and the same colour is allocated to every vertex for which no adjacent vertex has the same colour
Steps 3 and 4 are applied iteratively until all the vertices have been coloured
A graph is bipartite if it is 2-colourable. This intuitive fact is proven in Cambridge Tracts in Mathematics 131.
This is, of course, the cannon with which to shoot a mosquito. A graph is bipartite iff its vertices can be divided into two sets, such that every edge connects a vertex from set 1 to one in set 2. You already have such a division: each edge connects a vertex from the set of odd-degree vertices, to a vertex in the set of even-degree vertices.
Because by definition you already have two disjoint sets of vertices such that the only edges go between a vertex in one set and a vertex in the other set.
The even degree nodes are one set, and the odd degree nodes are the other set.
Pick any node, put it in set A. Take all the nodes that link to it, put them in set B. Now for every node added, add all it's neighbors to the opposite set, and check for the ones that already belong to one of the sets that they are in the right set. If you get a contradiction then the graph is not bipartite.
If you run out of neighbors but there are still nodes left, pick again any node and continue the algorithm until you either have no nodes or you found a contradiction.
If by "prove", you mean "find out", the complete solution is here
Bipartite.java
It works by keeping two boolean arrays. A marked array to check if a node has been visited, and another colored array. It then does a depth first search, marking neighbors with alternate colors. If a neighbor is marked with same color, graph is not bipartite.

Partially coloring a graph with 1 color

I just started reading graph theory and was reading about graph coloring. This problem popped in my mind:
We have to color our undirected graph(not completely) with only 1 color so that number of colored nodes are maximized. We need to find this maximum number. I was able to formulate an approach for non cyclic graphs :
My approach : First we divide graph into isolated components and do this for each component. We make a dfs tree and make 2 dp arrays while traversing it so that root comes last :
dp[0][u]=sum(dp[1][visited children])
dp[1][u]=sum(dp[0][visited children])
ans=max(dp[1][root],dp[0][root])
dp[0][i] , dp[1][i] are initialized to 0,1 respectively.
Here 0 signifies uncolored and 1 signifies colored.
But this does not work for cyclic graphs as I have assumed that no visited children are connected.
Can someone guide me in the right direction on how to solve this problem for cyclic graphs(not by brute force)? Is it possible to modify my approach or do we need to come up with a different approach? Would a greedy approach like coloring a nodes with least edges work?
This problem is NP-Hard as well, and is known as maximum independent set problem.
A set S<=V is said to be Independent Set in a graph if for each two vertices u,v in S, there is no edge (u,v).
The maximum size of S (which is the number you are seeking) is called the independence number of the graph, and unfortunately finding it is NP-Hard.
So, unless P=NP, your algorithm fails for general purposes graphs.
Proving it is fairly simple, given a graph G=(V,E), create the complementary graph G'=(V,E') where (u,v) is in E' if and only if (u,v) is NOT in E.
Now, given a graph G, there is a clique of size k if and only if there is an independent set of size k in G', using the same vertices (since if (u,v) are two vertices the independent set, there is no edge (u,v) in E', and by definition there is an edge in E. Repeat for all vertices in the independent set, and you got a clique in G).
Since clique problem is NP-Hard, this makes this one such as well.

How to find shortest path in a directed graph that has edge weights either 0 or 1 in linear time?

I am looking for a way to augment the BFS method used to find the single source shortest paths in an unweighted directed graph and solve the above problem in O(N+M) time.
where N is the number of vertices, M is the number of edges
I have thought the following:
Contract the vertices of the graph that have an edge weight 0 between them. But this would definitely be wrong as then I would be changing the graph's properties and adding new edges to vertices that originally had none.
Changing the edge weights to 1 and 2. And then creating dummy vertices in the paths that are of length 2 to convert those edges to edges of weight 1. But this would give the wrong answer.
In more generality, how can I find single source shortest paths in a directed graph when the edge weights are between 0 and MAX in linear time. (MAX is the maximum edge weight)
You can use bfs with some modifications: maintain a deque instead of a queue and add a vertex to the front of the deque if 0 edge is used and to the back of the deque otherwise.(I mean 0-1 case now)
I think you can work this out with vertex contraction. Just a hint here:
First form connected components of vertices that are connected by 0-weight edges and select one representative member in every component. This will give you a contracted graph.
Then solve the unweighted problem.
The true path will be formed of "inter-edges" (weight 1) joining the representative members, and "intra-edges", joining vertices within the component, from the incoming inter-edge to the outgoing inter-edge. In other words, you need to be able to find the path from any representative to any other representative.

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