Algorithm to select points from matrix - algorithm

This seems to be a simple problem but I am stuck at this problem. The problem is that I have a matrix of size X x Y. There may be some points at (i,j). It is, however, not necessary that all locations (i,j) should have a point (i.e. there are < XY points). How should I can select the biggest subset of these points such that no points in the selected subset has same i (row number) or j (column number)

This is the maximum matching problem, which is poly-time solvable. Given an instance of your problem, construct a bipartite graph with nodes a1, …, aX, b1, …, bY, and edges aibj for all (i,j) where there's a point. Take the points corresponding to the edges in a maximum matching.

Construct graph. Points are vertices of graph. Every vertice is connected with all vertices in the same row and same column - edges of graph. Your problem is to select maximum independent set of vertices (without common edges).
Independent_set
This problem is equivalent to to maximum clique problem in complement graph, so Bron-Kerbosch algorithm could be used.
http://en.wikipedia.org/wiki/Clique_problem#Finding_maximum_cliques_in_arbitrary_graphs
http://en.wikipedia.org/wiki/Complement_graph
Bron-Kerbosch algorithm

Related

Finding the max sum of cells in a square grid where cells cannot be adjacent

I'm trying to find an algorithm to find the max sum of non adjacent (adjacent being horizontal/vertical elements) elements in an n sized square grid.
I've tried converting the grid into a flow graph and calculating the max flow, but not having much luck. Is there an algorithm to solve this? If not, how would we go about making one?
There is an algorithm for solving this efficiently, using the max-flow min-cut theorem. This involves a series of transformations of your problem-- you may want to verify for yourself why each transformation works.
First, let's get the positive/negative number technicalities out of the way. If your max sum is allowed to be empty, then the smallest it can ever be is 0. If there is at least one positive number in the grid, we never have to take negative numbers into our sum, so we can completely ignore those cells.
However, if your maximum sum can't be empty, and all numbers are negative, then take the single maximum number in the grid as the best sum.
You have a 'grid graph' in two dimensions: each cell corresponds to a vertex, and vertices are adjacent if and only if their cells are horizontally or vertically adjacent.
This is a bipartite graph (just like a chessboard): if you number the rows and columns, then cell (x, y) is adjacent to (x+1, y), (x-1, y), (x, y+1), and (x, y-1). Looking at the parity of x+y, a cell is only adjacent to cells whose coordinates sum to the opposite parity.
Now, you're looking for the maximum weighted independent set in this bipartite graph. The algorithm for finding that has been completely laid out in this SO post, and also in this CS StackExchange post with references, so I won't repeat all of the same details again here for a third time.
Instead, here's an outline of the problem transformations, in order:
Max sum of nonadjacent cells in square grid S
Maximum weighted independent set of vertices in bipartite graph G
Minimum weighted vertex cover in G
Minimum weight cut in flow graph F, where F has same vertices as G plus a source and sink vertex
Maximum flow in F from source to sink
After removing all negative cells and finding this maximum flow, the answer to your maximum sum question is: (Sum of all cells) - (Max flow in F).

Find Path of a Specific Weight in a Weighted DAG

Given a DAG where are Edges have a Positive Edge Weight. Given a Value N.
Algorithm to calculate a simple (no cycles or node repetitions) Path with the Total weight N?
I am aware of the Algorithm where we have to find a Path of Given Path Length (number of Edges) but somewhat confused about for the Given Path Weight?
Can Dijkstra be modified for this case? Or anything else?
This is NP-complete, so don't expect any reasonably fast (polynomial-time) algorithm. Here's a reduction from the NP-complete Subset Sum problem, where we are given a multiset of n integers X = {x_1, x_2, ..., x_n} and a number k, and asked if there is any submultiset of the n numbers that sum to exactly k:
Create a graph G with n+1 vertices v_1, v_2, ..., v_{n+1}. For each vertex v_i, add edges to every higher-numbered vertex v_j, and give all these edges weight x_i. This graph has O(n^2) edges and can be constructed in O(n^2) time. Clearly it contains no cycles.
Suppose the answer to the Subset Sum problem is YES: That is, there exists a submultiset Y of X such that the numbers in Y total to exactly k. Actually, let Y = {y_1, y_2, ..., y_m} consist of the m <= n indices 1 <= i <= n of the selected elements of X. Then there is a corresponding path in the graph G with exactly the same weight -- namely the path that starts at v_{y_1}, takes the edge to v_{y_2} (which is of weight x_{y_1}), then takes the edge to v_{y_3}, and so on, finally arriving at v_{y_m} and taking a final edge (which is of weight x_{y_m}) to the terminal vertex v_{n+1}.
In the other direction, suppose that there is a simple path in G of total weight exactly k. Since the path is simple, each vertex appears at most once. Thus each edge in the path leaves a unique vertex. For each vertex v_i in the path except the last, add x_i to a set of chosen numbers: these numbers correspond to the edge weights in the path, so clearly they sum to exactly k, implying that the solution to the Subset Sum problem is also YES. (Notice that the position of the final vertex in the path doesn't matter, since we only care about the vertex that it leaves, and all edges leaving a vertex have the same weight.)
A YES answer to either problem implies a YES answer to the other problem, so a NO answer to either problem implies a NO answer to the other problem. Thus the answer to any Subset Sum problem instance can be found by first constructing the specified instance of your problem in polynomial time, and then using any algorithm for your problem to solve that instance -- so if an algorithm exists that can solve any instance of your problem in polynomial time, the NP-hard Subset Sum problem can also be solved in polynomial time.

Algorithm to Maximize Degree Centrality of Subgraph

Say I have some graph with nodes and undirected edges (the edges may have a weight associated to them).
I want to find all (or at least one) connected subgraphs that maximize the sum of the degree centrality of all nodes in the subgraph (the degree centrality is based on the original graph) under the constraint that the sum of the weighted edges is < X.
Is there an algorithm that will do this?
A quick search took me to this description of degree centrality. It turns out that the "degree centrality" of a vertex is simply its degree (neighbour count).
Unfortunately your problem is NP-hard, so it's very unlikely that any algorithm exists that can solve every instance quickly. First notice that, assuming edge weights are positive, the edges in any optimal solution necessarily form a tree, since in any non-tree you can delete at least 1 edge without destroying connectivity, and doing so will decrease the total edge weight of the subgraph. (So, as a positive spinoff: If you compute the minimum spanning tree of your input graph and find that it happens to have total weight < X, then you can simply include every vertex in the graph in your solution.)
Let's formulate a decision version of your problem. Given a graph G = (V, E) with positive (I'll assume) weights on the edges, a number X and a number Y, we want to know: Does there exist a connected subgraph G' = (V', E') of G such that the sum of the edge weights in E' is at most X, and the sum of the degrees of V' (w.r.t. G) is at least Y? (Clearly this is no harder than your original problem: If you had an algorithm to solve your problem, then you could just run it, add up the degrees of the vertices in the subgraph it found and compare this to Y to answer "my" problem.)
Here's a reduction from the NP-hard Steiner Tree in Graphs problem, where we are given a graph G = (V, E) with positive weights on the edges, a subset S of its vertices, and a number k, and the task is to determine whether it's possible to connect the vertices in S using a subset of edges with total weight at most k. (As I showed above, the solution will necessarily be a tree.) If the sum of all degrees in G is d, then all we need to do to transform G into an input for your problem is the following: For each vertex s_i in S we add enough new "ballast" vertices that are each connected only to s_i, via edges with weight X+1, to bring the degree of s_i up to d+1. We set X to k, and set Y to |S|(d+1).
Now suppose that the solution to the Steiner Tree problem is YES -- that is, there exists a subset of edges having total weight <= k that does connect all the vertices in S. In that case, it's clear that the same subgraph in the instance of your problem constructed above connects (possibly among others) all the vertices in S, and since each vertex in S has degree d+1, the total degree is at least |S|(d+1), so the answer to your decision problem is also YES.
In the other direction, suppose that the answer to your decision problem is YES -- that is, there exists a subset of edges having total weight <= X ( = k) that connects a set of vertices having total degree at least |S|(d+1). We need to show that this implies a YES answer to the original Steiner Tree problem. Clearly it suffices to show that the vertex set V' of any subgraph satisfying the conditions above (i.e. edges have total weight <= k and vertices have total degree >= |S|(d+1)) contains S (possibly among other vertices). So let V' be the vertex set of such a solution, and suppose to the contrary that there is some vertex u in S that is not in V'. But then the largest sum of degrees that we could possibly make would be to include all other non-ballast vertices in the graph in V', which would give a degree total of at most (|S|-1)(d+1) + d (the first term is the degree sum for the other vertices in S; the second is an upper bound on the degree sum of all non-S vertices in G; note that none of the ballast vertices we added in could be in the subgraph, because the only way to include any of them is to use an edge of weight X+1, which we obviously can't do). But clearly (|S|-1)(d+1) + d = |S|(d+1) - 1, which is strictly less than |S|(d+1), contradicting our assumption that V' has a degree total at least |S|(d+1). So it follows that S is a subset of V', and thus that it is possible to use the same subset of edges to connect the vertices in S for a total weight of at most k, i.e. that the answer to the Steiner Tree problem is also YES.
So a YES answer to either problem implies a YES answer to the other one, in turn implying that a NO answer to either implies a NO answer to the other. Thus if it were possible to solve the decision version of your problem in polynomial time, it would imply a polynomial-time solution to the NP-hard Steiner Tree in Graphs problem. This means the decision version of your problem is itself NP-hard, and so is the optimisation version (which as I said above is at least as hard). (The decision form is also NP-complete, since a YES answer can be easily verified in polynomial time.)
Sidenote: At first I thought I had a very straightforward reduction from the NP-hard Knapsack problem: Given a list of n weights w_1, ..., w_n and a list of n profits p_1, ..., p_n, make a single central vertex c, and n other vertices v_1, ..., v_n. For each v_i, attach it to c with an edge of weight w_i, and add p_i other leaf vertices, each attached only to v_i with an edge of weight X+1. However this reduction doesn't actually work, because the profits can be exponential in the input size n, meaning that the constructed instance of your problem might need to have an exponential number of vertices, which isn't allowed for a polynomial-time reduction.

Find the maximum number of edges in the graph

There are 'n' vertices and 0 edges of an undirected graph. What can be the maximum number of edges that we can draw such that the graph remains disconnected.
I have made the solution that we can exclude one vertex and can find the maximum number of edges between n-1 vertices of undirected graph, so that the graph still remains disconnected.
which is n(n-1)/2 for n vertices and will be (n-1)(n-2)/2 for n-1 vertices.
Can there be a better solution?
You can resolve this using analysis. Take your idea and generalize it. You divide the n vertices in two groups , of size x and n-x.
Now the number of edges is a function of x, expressed by
f(x)= x(x-1)/2 + (n-x)(n-x-1)/2
f(x) = 1/2(2x^2 - 2nx +n^2 - n)
The value which maximize this function is the partition size you want. If you make calculation you find that it decrease from x=0 to x=n/2, then increase to x=n. As x = 0 or x = n means the graph is collected, you take the next greatest value which is x=1. So your intuition is optimal.
Your solution should be the best solution.
Because any new edge added must have the nth vertex at one end.
If graph can have multi edges, answer is infinity for n>=3.
If it can also contain self-loops, answer is infinity for n>=2,
If none of those holds your solution is correct.

unique maximum matching

I am trying to use bipartite graph to code my program with following properties:
in each side, there is N vertices
the graph is connected
Now, I wanna add a condition in my code which check if the number of edges is bigger than M, do not allow user to more activities(in a simple sentence print something in that condition) where M is maximum number edges such that it still has a unique maximum matching.
The question is how can I find M?
Any idea will be appreciated
Thanks
if you mean to find maximum m such that there is at least one graph with n vertices and m edges with a unique maximum matching, the answer is (n + 1) * n / 2.
to show that there is at least one graph with this number of edge, consider a graph with vertices x1, x2, .., xn in one part and vertices y1, .. yn in another part. draw an edge between vertex xi and yj iff (i <= j).
to show there can be no more edges, use induction on the number of vertices. first of all we can show if every vertex in the graph is connected to at least two vertex, the graph has at least two different maximum matching. (consider one maximum matching, follow a path from a vertex whose edges alternates between matching edges and non-matching edges, make a circle and reverse all the edges.)
so we know there is one vertex with degree equal to one. remove this vertex and it's neighbor and use induction on the remaining graph.
sorry for bad English.

Resources