I am trying to solve the following problem using Greedy Algorithm,
We have n friends and we want to give a present to each one of them. But we don't want to give the same present to two person who know each other. (if x knows y, then y knows x). People who do not know each other may take the same gift, it is okay. We want to minimize the number of distinct gifts given.
Here is what I thought, We try to make pairs of people who do not know each other, and give them all the same gift. But I am not sure whether this is a greedy algorithm. Also, we may want to find maximum group of people in which no one knows any other, so we can give hem the same gift. But can we do this? Can we find the maximum group of people who do not know each other?
Can anyone propose a greedy algorithm for the problem?
The problem you have mentioned is a restatement of Graph Coloring problem. You have to label the graph’s vertices with colors such that no two vertices sharing the same edge have the same color. The link given below is to the Greedy Coloring Algorithm.
http://en.wikipedia.org/wiki/Greedy_coloring
This is graph coloring problem, and greedy algorithm for it is straightforward:
a greedy coloring is a coloring of the vertices of a graph formed by
a greedy algorithm that considers the vertices of the graph in sequence
and assigns each vertex its first available color
Related
I am trying to solve this problem : Jobs.
So far i have thought that the problem is same as the Assignment Problem with the distributors and districts represented as a bipartite graph and the edges representing the probability. But here we would need to maximize the product rather than the sum of weights of matched edges.
One idea that came to my mind was to change each edge weight to log ( weight ). Then the problem essentially changes to finding the maximum sum, which is can then be solved using the algorithms for Assignment Problem. But this poses a problem, since applying log will make the edge weights non-integer, something which i think the Hungarian Algorithm does not work for.
Please suggest some other alternative approach.
In theory, the Hungarian algorithm works fine with real weights.
In practice, it's possible that, since most integer logarithms cannot be represented exactly as floating-point numbers, it could come to pass that rounding would change the optimal solution. There are ways to deal with that even so, but it's unlikely that you'll need them for this programming contest problem.
I have n points and I need to connect all of them minimizing the final distance. The image above represents an algorithm that in each node it connects to the nearest one but the final output might be really of.
I've been searching a lot, I know some pathfinding algos but unaware of one that solves exactly this case. I found a question on Math Stackexchange but the answer is not providing any algorithm - https://math.stackexchange.com/a/581844/156584.
Is there any algorithm that solves exactly this problem? Otherwise I can bruteforce it.
Edit: Some clarification regarding the result I'm expecting: each node can be connected to 2 other nodes, creating a continuous path (like taking a pen and without ever lifting it, connect the nodes minimizing the final distance). I don't want to create a cycle (that being the travelling salesman problem).
PS: this question can also be translated to "complete graph with n vertices, and wanting to choose the set of edges such that the graph is connected, but the sum of the edge weights is minimized"
This problem is known as the shortest Hamiltonian path problem and it is NP-hard. So if the number of points is small, you can use backtracking or dynamic programming to find an optimal solution. If the number of points is large, you can use heuristics and/or approximations to obtain a relatively good answer(it is not always possible to find the best one in this case, though).
For some project in computer vision I have N points in high-dimensional space. I want to select k of them that will be "the most distinguishable" from each other. For example, it can translate to sum of distances between chosen points is maximum. Or it can be that volume of polyhedron is maximum. But generally anything that has some intuition behind can go.
As expected I want to find these representative points.
There are two questions:
What definition for "the most distinguishable" points is more commonly used? Do they change the algorithm used to find those points?
What is the algorithm to find the points? It highly reminds me maximal weighted clique problem. Is it NP-hard problem? In this case can we make some good approximation against optimal solution?
The way you define "the most distinguishable" will definitely affect the algorithm you'll want to use. for example, you can define "the most distinguishable" as the set with the maximal sum of distances between any two points in the set, but you could also define it as the set with the maximal minimum distance between any two points. these are two completely different problems.
As for algorithms, as I've said, that depends on your definition. If you're looking to find the K farthest points, you should look into this question. This problem is NP-Complete, but you may get some ideas about how to approach the problem.
is there an algorithm for finding all the independent sets of an directed graph ?
From what i've read an independent set represents a set formed by the nodes that are not adjacent.
So for this example I would have {1} {2} {1,3}
So how is possible to find all of them, I am thinking about something recursive but I don't really know the algorithm, if someone could point me in the right direction it would be much appreciated !
Thank you!
Typical way to find independent sets is to consider the complement of a graph. A complement of a graph is defined as a graph with the same set of vertices and an edge between a pair if and only if there is no edge between them in the original graph. An independent set in the graph corresponds to a clique in the complements. Finding all the cliques is exponential in complexity so you can not improve brute force much. Still I believe considering the complement of the graph may make the problem easier to deal with.
Other than complement and finding cliques, I can also think about "Graph Coloring", you color the vertices somehow that no two adjacent vertices have the same color (you can do it with a very simple heuristic algorithm like SL = Smallest Last), and then choose vertices in every color as a subset (as a maximal independent subset).
The only problem is that there are probably too many ways of coloring a graph. You have to keep all the found (maximal) independent sets and move on until you get enough sets!
The Bron–Kerbosch algorithm is commonly used for this problem, see the Wikipedia article for a description and pseudocode that can be turned into a useable program without too much problem. The size of output is, in the worst case, exponential in the number of vertices, but brute force will always be exponential while BK will be polynomial if the output is polynomial. In other words if you know that the output will be reasonable then BK will produce it in a reasonable time. This is an active area of research and there are a number of other algorithms that do the same thing with varying efficiency depending of the type and size of graph. There are applications in several areas, in particular genetics.
I have a problem which I like and I love to think about solutions, but I'm stuck unfortunately. I hope you like it too. The problem states:
I have two lists of 2D points(say A and B) and need to pair up points from A with points from B, under the condition that the sum of the distances in all pairs is minimal. A pair contains one point from A and one from B, a point can be used only once, and as many as possible pairs should be created(i.e. min(length(A), length(B))).
I've made a simple example, where color denotes which list the point is from, and the black connections are the solution.
Although this is a nice problem and I suspect is NP-hard, it gets nicer. I can build on existing solutions. Suppose I have two lists and the corresponding solution(i.e. the set of pairs), then the problem I need to solve is to reoptimalize that solution when a point is added to or removed from either list.
I've unfortunately not been able to come up with any non-brute force algorithm yielding the optimal solution. I hope you can. Any algorithm is appreciated in any (pseudo) language, preferably C#.
This problem is solvable in polynomial time via the Hungarian algorithm. To get a square matrix, add dummy entries to the shorter list at "distance 0" from everything.
Your problem is an instance of the weighted minimum maximal matching problem (as described in this Wikipedia article). There is no polynomial-time algorithm even for the unweighted problem (all distances equal). There are efficient algorithms to approximately solve it in polynomial time (within a factor of 2).
This is the minimum weight Euclidean bipartite matching problem. There is a O(n^(2+epsilon)) algorithm.