Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have this problem bellow and I'm not seeing an effective solution but taking the brute force approach. Would anyone mind lending me a hand?
The problem consists of a graph G= (V,E) directed, weighted and acyclic. Edges have weights w(u,v). The value of w(u, v) depends only on the vertex of origin ( w(u,x)= w(u,y) if (u,x) and (u,y) exist ). Originally, each vertex may have multiple incoming and/ or outgoing edges. The goal is to maintain one outgoing edge per vertex at most in a way the total remaining weight is maximum. Vertices that have outgoing edges cannot have incoming ones. For example, consider figure 1. The left-side graph is the original one. Keeping at most one outgoing edge, the right-side graph represents a solution for maximum total weight, 17.
However, there is another constraint to this problem. Each vertex is assigned 2 values, capacity and load. Capacity says how much load it can have attached to. Capacity must be also taken into account while finding the maximum total weight configuration. Figure 2 shows the same graph as figure 1 but now the capacity constraint plays a decisive role. See that the maximum total weight configuration is different in this situation (right-side graph, figure 2).
In summary, there are 3 restrictions in order to get the maximum total weight:
Obey capacity limitation;
Vertices with outgoing edge don't have incoming ones;
Vertices have one outgoing edge at most.
The only solution I've come up is testing all possible configurations, checking if it is a valid and keeping track the maximum. Does anyone have a better approach to tackle this problem?
Your problem looks like knapsack problem to me: pick up a set of edges to maximize profit.
What you can do for sure, is using constraint satisfaction approach. For example, check this code that solves knapsack problem. Adapting it to your needs should be a rather simple task.
You don't need any matching algorithm with this approach - a solving procedure will build the best possible solution directly. However, it might take a lot of time/memory for larger graphs (thousands edges/nodes).
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am not able to understand difference between positive and negative cycle in a weighted graph. The thing that is used bellman ford and SPFA.
What is it?
What are the properties?
If you can please include diagrams/examples of each.
The graph has weights on its edges.
The weight of a cycle is the sum of the weights of the edges that make the cycle.
A cycle is called positive if it has positive weight. A cycle is called negative if it has negative weight.
Think of the graph as a network of roads. Each edge represents one road. You are commuting through this network by car. A positive weight on an edge means you have to pay a toll to drive through that road. A negative weight on an edge means you are given a refund when you drive through that road.
Usually you expect driving through the network will cost you money, because of all the positive weights. When trying to drive from point A to point B, you will try to find a path of minimal cost.
If there is a negative cycle in the graph, then that means a car driving around this cycle will actually gain money instead of paying it.
If I ask you "please find a path with minimal cost from point A to point B" in a graph that has a negative cycle, you might tell me "Well, first go from point A to that cycle. Then, drive around the cycle forever and ever. When you're tired and wealthy, go to point B."
In other words, there are paths with arbitrarily high negative weight. So asking for a path of minimal weight in a graph that has a negative cycle is meaningless, because driving around the negative cycle again and again will always reduce your cost further.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to solve the problem of node/vertices disjoint paths in a directed graph and came to know about the idea of splitting nodes into in and out nodes respectively. I got the idea and how it works and all the related theorem's like menger theorem but still, I'm not sure how to code it in an efficient manner.
Which data structure should I use so that I can split the vertices and still manage to balance the time complexity? Is there any algorithm existing which tells how to approach the code.
Please help or suggest some appropriate link which may help me out.
Thanks
It's quite simple actually. Let's say you have graph as an edge list of pairs u v meaning theres an edge from u to v
If nodes are not integers already use a dictionary/hash/map to reduce them to integers in range 1..n where n is number of nodes.
Now we "split" all nodes, for each node i it will become 2 nodes i and i+n. where i is considered in-node and i+n out-node.
Now graph edges are modified, for every edge u --> v we instead store edge u+n --> v
Also we add edges from each nodes in-node to out-node, i.e. from node i to i+n
We can assign infinity capacities to all edges and capacities of 1 to edges that connect in-node to out-node
Now Node-disjoint paths from some node s to t can be found using any max-flow algorithm (Ford-Fulkerson, Edmonds-Karp, Dinic, etc)
pesudo-code for building residual network:
n = #nodes
for each node i in 1..n:
residual_graph.addEdge(i, i+n, capacity=1);
residual_graph.addEdge(i+n, i, capacity=0);
for each edge (u,v) in graph
residual_graph.addEdge(u+n, v, capacity=+Infinity);
residual_graph.addEdge(v, u+n, capacity=0);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to implement the A* algorithm and I have read about the heuristic function and how it works and I understand that an underestimate is needed to obtain an optimal path . But what heuristic function is the most suited for a random directed graph ? What I have tried so far is is taking the smallest edge weight from a node to the goal. As clearly the distance from a current node to the goal is not smaller than the smallest edge from a current node to the next.
The Manhattan distance only works when you have a well-defined distance metric that you can apply to pairs of nodes, such as with points in a 2D plane. For a graph, there's no inherent way to get the distance between two nodes.
With the little information available to you from the problem definition, I don't think you will do much better than using the heuristic that assumes all unseen edges have weight equal to the smallest weight in the graph.
You could get a bit more advanced if you sorted all the edges by weight. Then, as you see edges with particular weights during A*, you can remove them from the sorted list. This will let you know a running value of what the smallest remaining edge weight could be.
specific question here. Suppose you have a graph where each vertice specifies how many connections they must have to another vertices and the following rules/properties apply:
1- The graph can be incomplete (no need to every vertice to have a connection with every other)
2- There can be two connections between two vertices only if they are in opposite directions (e.g: A points do B, B points to A).
3- Suppose they are on a 2D plane, there can be no crossing of connections (not even tangents).
4- Theres no interest for the shortest path, just respecting the properties and knowing if the solution is unique or not.
5- There can be no possible solution
EDIT: Alright guys sorry for not being specific. I'll try to clarify my point here: what I want to do is given a number of vertices, know if a graph is connected (if all the points have at least a connection to the graph). The vertices given can be impossible to make a graph of it so I want to know if there's is a solution, if the solution is unique or not or (worst case scenario) if there is no possible solution. I think that clarifies point 4 and 5. The graph is undirected, the connections can Not curve, only straight lines.The Nodes (vertices) are fixed, we have their position from or W/E input. I wanted to know the best approach and I've been researching and it is a connectivity problem, though maybe some specific alg may be more efficient doing this task. That's all, sorry for late reply
EDIT2: Alright guys would the problem be different if we think that each vertice is on a row and column of a plane matrix and they can only connect with other Vertices on the same column or row? So it would be just 90/180/270/360 straight connections. This would hugely shorten the possibilities right?
I am going to assume that the question is: Given the degree of each vertex, work out a graph that passes all the constraints given.
I think you can reduce this to a very large integer programming problem - linear constraints, but with the variables required to be integers (in fact either 0 or 1), which makes the problem much more difficult than ordinary linear programming.
Let the unknowns be of the form Xij, where Xij is 1 if there is an edge from node i to node j, and 0 otherwise. The requirements on the number of connections then amount to requirements of the form SUM_{all i}Xij = K for some K dependent on the requirement. The requirement that the graph is planar reduces to the requirement that the graph not contain two known graphs as subgraphs - https://en.wikipedia.org/wiki/Graph_minor. Each possible subgraph then produces a constraint such as X01 + X02 + ... < 5 - there will be a huge number of these constraints - so large that for large number of nodes simply producing all the constraints may be too expensive to be practical, let alone solving them. The number of constraints goes up as at least the 6th power of the number of nodes. However this is polynomial, so theoretically practical to write down the MIP to be solved - so perhaps this is better than no algorithm at all.
Assuming that you are asking us to:
Find out if it is possible to generate one-or-more directed planar graphs such that each vertex has a given out-degree (not necessarily the same out-degree per vertex).
Let's also assume that you want the graph to be connected.
If there are n vertices and the vertices have degrees d_1 ... d_n then for vertex i there are C(n-1,d_i) = (n-1)!/((d_i)!*(n-1-d_i)!) possible combinations of out-edges from that vertex. Taking the product of all these combinations over all the vertices will give you the upper bound on the number of possible graphs.
The naive approach is:
Generate all possible graphs.
Filter the graphs to only have connected graphs.
Run a planarity test on the graph to determine if it is planar (you can consider the graph to be undirected in this step); discard if it isn't.
Profit!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In a directed graph, Is it possible to determine whether there exists at least one path between two predefined nodes in constant time? If I use adjacency matrix data structure will it be useful?
Please suggest me what I am missing, what I need to learn. If there is no standard algorithm can you explain some solution for me.
Well, without pre-processing it cannot be done in constant time, you are bounded by the shortest path between these nodes to find the shortest path, and if no such path exists - it might decay to the size of the graph.
If you allow pre-processing, you can construct Strongly Connected Components graph (let it be G'), lexicographically sort it, and add an indication of all pairs (v',u') if there is a path from v' to u' on G'.
On query time you can search for the v' that contains v, and u' that contains u. and check if there is a path from v' to u', the answer will be the same.
If you pre-process the graph with Kruskal's algorithm, then it's subsequently possible to determine whether two nodes are connected in constant time. The algorithm will generate one or more sets of connected nodes. Two nodes that are in the same set are connected by one or more paths. Nodes in disjoint sets are not connected.
If you aren't allowed to pre-process the graph, then the answer is "No".