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);
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 have a school question that I'm not sure what to code with. Lets say you have an undirected and unweighted graph G, which is a city road network. The nodes, n are intersections and m edges as the roads. Among the n nodes, there are h amount of hospitals. The question wants us to find for each node n, the distance from each node to the nearest hospital. Would it be possible to do using BFS or would djikstra be a better choice?
In addition, we would also need to propose a new algorithim that would find K amount of nearest hospitals nearest to each node with K being user input. In this case, is bfs still possible or is djikstra the only solution? Thank you.
The difference between Dijkstra and BFS is that with Dijkstra the queue is sorted so that closer nodes appear first.
In your case every edge has equal length and so this order comes automatically.
Thus, the algorithms are equal in this case.
Breadth-first search can be viewed as a special-case of Dijkstra's
algorithm on unweighted graphs, where the priority queue degenerates
into a FIFO queue.
https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
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).
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
N-numbers , d1,d2,d3..dn are given.
How do we check if it is possible to construct a undirected graph with vertices v1,v2,v3,...vn with degress d1,d2,...dn respectively.
Graph should not contain multiple edges between the same pair of nodes, or "loop" edges
(where both end vertices are the same node).
Also, what is the running time of the algorithm ?
This is what Wikipedia calls the graph realization problem, solvable by the Havel--Hakimi algorithm. Start with a graph having n vertices, v1..vn, and 0 edges. Define the deficit of a vertex vk to be the difference between dk and the current degree of vk. Repeatedly choose the vertex vk with the largest deficit D and connect it to the D other vertices having the D largest deficits. If a vertex would have negative deficit, then the instance is unsolvable. Otherwise, we terminate with a solution. I'll leave the running time as an exercise.
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".
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I have a simple question on DFS and I'm trying to understand how to use it and not how to solve the whole problem. I'm really looking for an explanation and not a solution to my homework.
I'll write down the question first.
"Suppose you have an undirected graph G=(V,E) and let three of its
vertices to be called v1, v2 and v3. Find an algorithm which
determines if these three vertices are part of a clique
(complete graph) (k>=3)"
Now I suppose to use DFS in order to solve it. As far as I understand DFS will let me know if v1, v2 and v3 are in the same strongly connected component. If I'm correct I should also determine if G is also a clique(complete graph).
I read in the internet and I found out that asserting if a graph is clique or not is NP and cannot be solved easily. Am I correct? Am I missing anything? Is there any propery I can use to determine immediately if a graph is comeplete ?
To clarify the confusion about the NP-completeness: checking whether a graph is a clique is not NP-complete; just count the edges and see whether there are n(n-1)/2. What is NP-complete is to find a maximum clique (meaning the subgraph that has the biggest number of vertices and is a clique) or a clique of k vertices in a graph of n vertices (if k is part of the input instead of a fixed number); the latter case is called the clique decision problem.
EDIT: I just realized you asked something regarding strongly connected components as well; that term only applies to directed graphs (i.e. the edges have a direction, which means for two vertices v and w, the edge v->w is not the same as the edge w->v). Cliques are commonly defined on undirected graphs, for which there are only connected components.
If I understood it properly, all you have to check whether these three vertices are connected, i.e., the edges v1-v2, v2-v3 and v3-v1 exists. If they exist, they form a clique of K=3. If at least one of them does not, these three vertices together can not be in a clique of size k>=3.