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 1 year ago.
Improve this question
I am working on a specific exercise and I am stuck.
To solve:
Solve the circulation demand problem. There are some factories that produce goods and some villages where the goods have to be delivered. They are connected by a networks of roads with each road having a capacity for the maximum amount of goods that can flow through it. The problem is to find if there is a circulation that satisfies the demand. This problem can be transformed into a maximum-flow problem.
Assume that every factory node fi has a production rate pi. In addition, that di is the demand rate of village vi. Your input will be the graph given using an adjacency list for each node of it. Initially give a number describing the number of nodes of the graph and then one line for the adjacency list of each node (together with the capacities) e.g. “d a 10 c 5” means that node d is connected to a (with capacity 10) and to c (with capacity 5). Finally give the production rates for each node (where there are factories) and after that the demand rates for the villages again on each node.
As I have understood I need an input file like this:
10
a b 10 c 20
b c 5 d 10
d e 7 f 8
a 10
e -5
//nodes = 10
//directed graph -> a to b with capacity 10, a to c with capacity 20
//a production = 10, e consumption = -5
I have concluded that I should use the Ford-Fulkerson Algorithm to find the maximum flow (since that's what is requested as the output)
Looking around at different implementations of the algorithm (I am considering using C or Java to code it), I have stumbled upon the following issue:
Ford-Fulkerson works only with 1 source and 1 sink. In this problem we have test-cases where there are 3 factories for example and 2 villages. Can someone enlighten me because I am really stuck.
This is a typical extension of the 1-source 1-sink Ford-Fulkerson algorithm. In essence, you consider another "imaginary" node U to be the 1 source, and connect that node U to all the factories. (i.e. which are theK sources in your problem)
Similarly, you connect all the M sinks, which are the villages, to another imaginary sink node V that you add to the given graph. Then, when you compute the maximum flow from U to V, you will have computed the max flow from all the factories to all the villages.
Obviously, the weights of the edges connecting U to factories and the ones connecting villages to V should be thought through. In your case, each incoming edge to a factory from U should have a weight that is equal to the capacity of that factory. In case of the edges connecting villages to V, there does not need to be a bound, so it can be as high as the highest weight in the entire graph, or a practical value which could represent infinity.
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 10 days ago.
Improve this question
We know that the K-median problem is proved to be NP-Hard. In fault-tolerant K-median problem on an undirected graph G=(V, E):
We are given a set of facilities F⊆ V and a set of demands (or clients) D ⊆ V in a metric space. We can open at most k facilities and then assign each client j to the r_j≥1 facility. Assigning demand j to facility i incurs an assignment cost of d(i, j), where d(i, j) is the shortest path between i and j. Our goal is to minimize the sum of the assignment costs for all clients.
If r_j is uniform, e.g. r_j=3 for all clients, each client should be connected to three facilities. Why the below algorithm does not solve the problem in polynomial time; let's assume r_j =3.
Go through all the vertices in F
For each v ∈ F Calculate the sum of the distance to all clients D
Store this sum value in a variable
Select the first vertext v with minimum sum variable as the first facility to be opened
Select the second vertex v' with minimum sum variable as the second facility to be opened
Select the third vertex v'' with minimum sum variable as the third facility to be opened
Connect all clients to facilities calculated in steps 2,3,4.
Note: This problem was considered in this article , and the authors provided an approximation algorithm for it. Hence, it should be a hard problem. They did not specifically mention the problem to be solved on the graph but mentioned that it should be on metric space. Moreover, they mention the existence of an approximation algorithm even in the uniform case of r_j.
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 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).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
suppose we have a weighted grid graph and our problem is to find maximum independent set. There is a greedy algorithm that each time choose the heaviest node and remove it and its neighbors until all nodes of G have been chosen or removed. we want to prove that W(s) >= 1/4 W(T) where S is our greedy result and T is the OPT solution.
let S be the result of our greedy algorithm and T be an arbitrary independent set which can be the OPT. We know that for any T for any node v which belongs to T-S there exist a node v' in S which is neighbor of v and w(v) <= w(v').
Is there any idea to how prove that?
Just use your last statement, and consider T as the maximum independent set and you have this two results :
for every node in T-S like v there exist a node like u in S were W(v) <= W(u).
each node like u in S is at most neighbor of 4 nodes in T.
now use them :)
The desired result can be obtained with the following proof.
Let S be the set generated by the greedy algorithm, let T be an independent set of maximal weight. We will stepwise transform T into S and bound the loss for each transformation step.
Choose v in T\S with maximal weight. By the statement included in the question above, there exists v' in S such that w(v') >= w(v); choose such a v'. Let N be the neighbourhood of v' in T; N contains v and at most 4 vertices (as we have a grid graph). As v was chosen with maximal weight and w(v')>=w(v), we obtain w(v')>=w(N)/4. We set T':=(T\N) and add v' to it. By construction, T' remains an independent set and we have w(T') >= w(T) - (3/4)w(N).
In total, for each exchange step, vertices from T\S get eliminated, but a node from S is added such that the added total weight is at least one quarter of the lost total weight.
Furthermore, the constructed sets N in each step are disjoint, which means that in each step, at least one quarter of w(N) is preserved. In total, as we have constructed S, andS has weight at least (1/4)w(T).
Note that the input graph is not required to be a grid graph but maximum degree 4 is sufficient; furthermore, the proof can be generalized by permitting an abitrary graph, replacing 4 by the maximum degree Δ yielding an approximation ratio of 1/Δ.
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 8 years ago.
Improve this question
I am practicing Algorithms problems from my book and came across this one:
You are given a directed network G with n nodes and m edges, a source s, a sink t and a maximum flow f from s to t. Assuming that the capacity of every edge is a positive integer, describe an O(m + n) time algorithm for updating the flow f in each of the following two cases.
(i) The capacity of edge e increases by 1.
(ii) The capacity of edge e decreases by 1.
It seems like it would be as simple as walking through the network flow edges and adjusting flows, but I do not think it is really that simple. Wikipedia only give algorithms that are O(n^2 m) or O(n m^2). Any help or thoughts would be appreciated.
There is a solution here.
Suppose e is an edge between u and v.
Increased capacity
The idea for increasing the capacity is to simply do a DFS in the residual flow graph for a path from s to t.
Decreased capacity
If the edge is unsed in the maximum flow then you are done.
Otherwise, the idea is to see if there is an alternative path from u to v in the residual flow graph. This takes O(n+m). If found, then you can increase the maximum flow by 1.
Otherwise, you need to decrease the flow. You do this by finding a path from u to s along which the flow can increase by 1, and a path from t to v along which the flow can increase by 1.
(The increases are in a reverse direction so this reduces the flow from s to t).