Suppose that I have a graph and a set of requests between node pairs. For each request, I need to find a path with maximum capacity (the maximum of the minimum capacity of the links in the path). Besides, each node has a capacity and the nodes in the path consume the node capacity. I want to serve as many requests as possible (maximizing the request of it).
In summary, it's a widest path problem with node capacity constraints. How can I solve it?
Split each node into two, connected by a link with capacity equal to original node capacity. One node is the destination for all the in links, the other is the source for all the out links. Apply usual algorithm.
Related
We are given a network flow, as well as a max flow in the network. An edge would be called increasing edge if increasing its capacity in an arbitrary positive number, would also increase the max flow.
Present an algorithm the finds an increaing edge (if one exists) and runs at $O(n^2)$.
I thought about the following idea -
Find the minimum cut in the graph, as its given to us with the ford-fulkerson algorithm.
Increase the capacity of all the edges in the left hand side of the cut by 1.
Run BFS in the residual network to find if an improved path exists. If one exists, we have an increasing edge. To find it, we have to compare the original network with the new network. We have to do that n times since we have to check for an improved path every time we increase the capacity by 1.
Is it correct, an am I in line with the required running time?
Thank you!
I think you just need to find a path from the source to the sink that would be an augmenting path if at most one node were increased in capacity.
First find all the best paths to vertices you can reach with residual capacity. If you found the sink, then you weren't given a max flow to begin with.
Then find all the other vertices that are adjacent to those ones though edges that are at capacity.
Then try to find an augmenting path from those vertices to the sink.
Total complexity is O(N), so whoever asked you this question probably had something else in mind.
I have an algorithm problem that needs binary tree structure similar to a binary tree. but the difference is that it may have nodes apart from the original tree independently.
And each node has three types. The first type is to point out starting node and only one exists. The second type is to point out connecting node and of course, and the last type is to point out a leaf node. Each edge has a cost to traverse to its bottom node.
Which data structure is good for me to cost to reach each node?
UPDATE
OK, I questioned this with data-structure tag so that I want to avoid to explain what the problem is. But inevitably, I explain about the problem because of lack of my explaination and my poor English.
I have nodes lists and edges with costs. There is a starting node(root node), nodes where will be located in the middle of a tree and leaf nodes are the destination for my program to traverse starting from a root node. But some of the leaf nodes may be ignored depending on the value in it. It is not important anyway. I have to calculate all leaf nodes' cost to reach its node from the root node and get the maximum value for them. Now, The problem is to adjust the cost value in edges for all other leaf nodes to have the same total cost with the maximum cost. But the sum of the adjust values has to be the minumum.
I am working on a graph library that requires to determine whether two nodes are connected or not and if connected what is the degree of separation between them
i.e number of nodes needed to travel to reach the target node from the source node.
Since its an non-weighted graph, a bfs gives the shortest path. But how to keep the track of number of nodes discovered before reaching the target node.
A simple counter which increments on discovering a new node will give a wrong answer as it may include nodes which are not even in the path.
Another way would be to treat this as a weighted graph of uniform weighted edges and using Djkastra's shortest path algorithm.
But I want to manage it with bfs only.
How to do it ?
During the BFS, have each node store a pointer to its predecessor node (the node in the graph along whose edge the node was first discovered). Then, once you've run BFS, you can repeatedly follow this pointer from the destination node to the source node. If you count up how many steps this takes, you will have the distance from the destination to the source node.
Alternatively, if you need to repeatedly determine the distances between nodes, you might want to use the Floyd-Warshall all-pairs shortest paths algorithm, which if precomputed would let you immediately read off the distances between any pair of nodes.
Hope this helps!
I don't see why a simple counter wouldn't work. In this case, breadth-first search would definitely give you the shortest path. So what you want to do is attach a property to every node called 'count'. Now when you encounter a node that you have not visited yet, you populate the 'count' property with whatever the current count is and move on.
If later on, you come back to the node, you should know by the populated count property that it has already been visited.
EDIT: To expand a bit on my answer here, you'll have to maintain a variable that'll track the degree of separation from your starting node as you navigate the graph. For every new set of children that you load into the queue, make sure that you increment the value in that variable.
If all you want to know is the distance (possibly to cut off the search if the distance is too large), and all edges have the same weight (i.e. 1):
Pseudocode:
Let Token := a new node object which is different from every node in the graph.
Let Distance := 0
Let Queue := an empty queue of nodes
Push Start node and Token onto Queue
(Breadth-first-search):
While Queue is not empty:
If the head of Queue is Target node:
return Distance
If the head of Queue is Token:
Increment Distance
Push Token onto back of the Queue
If the head of Queue has not yet been seen:
Mark the head of the Queue as seen
Push all neighbours of the head of the Queue onto the back of Queue
Pop the head of Queue
(Did not find target)
My professor wants us to implement it for a single source node to all other nodes in the network. He said to keep track of the shortest path by using parent nodes, but I have no idea what this means in the context of the algorithm.
I can implement my code more or less properly, in the sense that my output distances are all correct for any network I run it on.
But most online resources talk about visiting nodes and marking them as visited once you explore all of the neighboring nodes. So for instance, if nodes A and B neighbor node C, and the new distance to A is smaller than that of B, do I mark node C visited? And then what happens if I get to node A and realize that the path it leads me down would actually cause an already recorded distance to actually be larger?
In order to get a path (as opposed to just a cost) from Dystra's algo, instead of saving a best-cost for each node, save the pair (best_cost, from_where). The from-where is a handle to the adjacent node that produced the best_cost.
You can then follow the from_where pointers all the way back to the origin to get the best path. I suspect "parent" is his name for the from_where element in the 2-tuple/pair.
My professor wants us to implement it for a single source node to all other nodes in the network. He said to keep track of the shortest path by using parent nodes, but I have no idea what this means in the context of the algorithm.
Well, that just mean that for each node, you store which node is the node it came from in the shortest path to it. This way, you can walk the shortest path in reverse order once you're done with your algorithm to not only find the distance of the shortest path, but also the shortest path itself.
But most online resources talk about visiting nodes and marking them as visited once you explore all of the neighboring nodes.
You mark a node visited after it was the unvisited node with the lowest distance. Unless there are negative distances, you won't be able to find a path that has a lower distance (and even then, it's only a problem if your graph has a cycle with distance below zero).
Suppose there are 3 target nodes in a graph.
A vertex-disjoint path means there is not any same node except the end nodes during the path.
For any one single node, say node i, how to find all vertex-disjoint paths from node i to the three target nodes?
You can solve this problem by reducing it to a max-flow problem in an appropriately-constructed graph. The idea is as follows:
Split each node v in the graph into to nodes: vin and vout.
For each node v, add an edge of capacity one from vin to vout.
Replace each other edge (u, v) in the graph with an edge from uout to vin of capacity 1.
Add in a new dedicated destination node t.
For each of the target nodes v, add an edge from vin to t with capacity 1.
Find a max-flow from sout to t. The value of the flow is the number of node-disjoint paths.
The idea behind this construction is as follows. Any flow path from the start node s to the destination node t must have capacity one, since all edges have capacity one. Since all capacities are integral, there exists an integral max-flow. No two flow paths can pass through the same intermediary node, because in passing through a node in the graph the flow path must cross the edge from vin to vout, and the capacity here has been restricted to one. Additionally, this flow path must arrive at t by ending at one of the three special nodes you've identified, then following the edge from that node to t. Thus each flow path represents a node-disjoint path from the source node s to one of the three destination nodes. Accordingly, computing a max-flow here corresponds to finding the maximum number of node-disjoint paths you can take from s to any of the three destinations.
Hope this helps!