I came across a problem in leetcode named "Binary Tree Camera".
I was wondering how to approach this similar problem:-
You have to place cameras at nodes of a graph such that whole graph is covered. A camera on a node monitors all its immediate neighbour nodes and itself. Find the minimum number of cameras required to cover all nodes.
This is a set cover problem, which there are many well-known algorithms for. To model it as an instance of the set cover problem, map each node to the set of nodes which a camera at that node would cover. The original problem of choosing the fewest number of nodes is equivalent to choosing the fewest number of those sets.
In general, this is an "NP Hard" problem, meaning there is no known algorithm which always gives the minimum covering and also scales well to large instances of the problem. Since the problem asks for the minimum, a heuristic algorithm is not suitable, so you will need to do something like a backtracking search.
This problem is called the Minimum Dominating Set and is NP-hard for the general graph case. Algorithms exist that approach this problem by either approximation, parameterization or restricting the class of graphs. See the Wikipedia link for details.
Related
With this undirected graph
In this graph I have different nodes with following types [A,B,C,D,E].
It means that is possible to exists different nodes with same type
Now imagine you have a set of node types [A,B,E]. You don´t know which node are those given nodes in the graph, only thing you know is the type of each node.
What you have to do is to find a best fit for that given set of nodes.
Node has to be connected to each other
I´ve been testing an algorithm which consists in steps below:
Convert the graph to a linked list
Generate all possible combinations between all nodes considering those given types and how many times a node type appears. The given example is [A,B,E] but it could be other set such as [A,B,C,A].
Some of possible (not all) combinations for [A,B,E] are:
Check if nodes in those combinations are connected to each other
the best fit is the first combination where all nodes are connected.
The problem is number of nodes in the given graph. For small sets of nodes and small graphs the algorthim is ok. But when the number of nodes are increased I have a thousands of possible combinations and those combinations consume a lot of memory.
I`ve been searching for some algorithm which could be able to solve this problem efficiently with low cost memory.
I have spent days reading and testing all kind of algorithm and until now I couldn`t find a better solution.
Suggestions are very appreciated
This is called the Graph Motif problem and unfortunately it's NP-hard, even when the graph is a tree with maximum degree 3: see Theorem 1 in https://people.mpi-inf.mpg.de/~hermelin/Conference%20Publications/Connected%20Motifs.pdf
This means it's very unlikely that any polynomial-time algorithm exists that can solve this 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).
i'm trying to make multiple agents move at the same time to a specified point on a 2d map and have an upper limit for the maximum distance one agent can move.
If possible, all agents should move the maximum distance, else less.
The paths of different agents shouldn't cross if possible, but if not, they can still cross.
My idea was some sort of adjusted A* algorithm.
Would this be a good approach or is there a better algorithm for this kind of problem?
(to be honest,i currently have A* and dijkstra on my radar as possiblities for solving this, so if there is anything better,a push in the right direction would be great)
Thanks for your help already
PS: i don't have any kind of underlying graph yet, so i'm still open to any idea, but can of course create a graph that works for dijkstra/A*
Your problem is close to vertex/edge disjoint path problem, which is NP-Complete in general, also your restricted version seems to be NP-Complete because shortest disjoint path in grid graph is NP-Hard, which is related to your restricted version. But there are lots of algorithms for disjoint paths in grid (even if you have different layers), so best option that I can suggest is use one of the exact algorithms, to find the vertex disjoint path, after that increase the size of paths (if is needed), by traversing some adjacent vertices.
Also for grid you don't need Dijkstra for finding path between two nodes (even shortest path or path with specific length), you can do it simply by running a BFS and is O(n) (start BFS from vertex v, and set the number of its adjacent to 1, and then for each adjacent of 1's set the new value to 2, ... see this answer and numbering algorithm part).
May be this question also helps if you looking for some heuristics in dynamic situation.
I'm going through an algorithms textbook to improve my algorithm skills but I'm completely stuck on this question and it's bugging me. I think the underlying data structure is a graph but I don't even know where to begin with this problem. Can anyone give some insight? Thanks
You are given a topographical map that provides the maximum altitude
along the direct road between any two neighboring cities, and two
cities a and b. Come up with an a linear time algorithm that finds a
route from s to t that minimizes the maximum altitude. Roads can be
traversed in both directions.
This is a tricky question. I would assume that there are some hints in the chapter that are supposed to guide you towards the solution.
The problem you are describing is an instance of the minimax path problem, or widest path problem.
http://en.wikipedia.org/wiki/Widest_path_problem
According to wikipedia, there is a linear time algorithm, but it is pretty complicated, so I doubt the book expects you to figure that out. The simpler way to solve this problem is to find a minimum spanning tree. Due to the "min cut" property of a minimum spanning tree, the path connecting a and b along a minimum spanning tree will have the minimax property, meaning that the maximum altitude along this path will be the minimum of any path connecting a to b.
However, there is no linear time minimum spanning tree algorithm. On the other hand, if we can assume that the graph is planar -- which we probably can since it is a road map -- then it is possible to find a minimum spanning tree in linear time. So I think this is what they might be after. Does the chapter containing this problem talk about minimum spanning trees and/or planar graphs?
I've searched both Google and Stack Overflow for an answer to my problem but I can't find one.
I need to find the optimal distribution for the power network of a city. The city is represented by a connected graph. I want to distribute power plants among some of those nodes in order to cover all of them in the electrical grid. The problem being that every power plant has a certain "range" (it can only cover for example in a "radius" of two nodes). My program needs to find the minimum number of power plants and their locations to cover the entire city.
I know from my searches that it should be related to MST's (minimum spanning trees) but the problem is in the limited range of the power plants.
I've thought about going through every node in the city and calculate the sub-graph containing all nodes within the range of a power plant in that node until I find the one that covers the most uncovered nodes and then keep doing that until the entire city is covered (basically brute forcing the problem) but that seems very unpractical and I was wondering if there is any other more effective way to solve this problem.
Thanks.
Unfortunately, this problem is known to be NP-hard by a reduction from the dominating set problem.
Given a graph G, a dominating set in G is a set of nodes D such that every node in the graph is either in D or is one hop away from D. The problem of finding the smallest dominating set in a graph is known to be NP-hard, and this problem easily reduces to the one you're trying to solve: given a graph G, produce a city (represented as a graph) that has the same structure as G, then give every power plant a radius of 1 (meaning that it can cover a node and all its neighbors). Finding the smallest set of power plants to cover the entire city then ends up producing a dominating set for the graph. Therefore, your problem is NP-hard.
As mentioned in this section of the Wikipedia page, it turns out that this problem is surprisingly hard to approximate. The Wikipedia page lists a few algorithms and approaches for approximating it, but it appears to be one of those NP-hard problems that resists polynomial-time approximation schemes.
Hope this helps!