For fun I'm learning about graph theory and I came across this problem. Given a set of vertices V, a set of edges E, and a weight for each edge in E, how can I efficiently construct a graph G such that:
G is connected (all vertices are connected via some path)
the sum of the weights of the edges is minimized
The edges in E are directed, when all edges in E are present there can be cycles.
See Minimum Spanning Tree algorithms.
ok... can i know what MrDatabase is after? SSSP algorithms (dijkstra, Bellman-Ford) are variation of MST, which ars just mentioned. Dijkstra does not solve negative weight cycle issue while Bellman-Ford does.
To add to ars's answer, if your graph contains edges with negative weight, then the problem becomes more difficult (and there may be no solution if you have a negative-weight cycle).
Read Bellman-Ford Algorithm. It supports negative weight cycles. Dijkstra's algorithm is more efficient but it doesn't support negative weight cycles.
Related
I came across this problem - In an undirected graph every node and edge has a weight. All the weights are non-negative. Given a value S, Find the connected subgraph with minimum sum of edge weights such that its sum of node weights is at least S.
The most obvious solution is a brute force approach considering all possible subgraphs. But the time complexity is exponential. Is there any better algorithm for this? My intuition is that we can convert node weights to edge weights and then apply spanning tree algorithm. But I couldn't solve it clearly. How to solve this problem?
EDIT : Looks like I was not clear enough about the description of subgraph. The selected subgraph must be a single, connected component. I hope it's clear now.
I think this problem is NP-hard via a reduction from the Steiner tree problem. Given a graph G and a set of nodes S that need to be spanned, set the weight of all of the nodes in S to one and all the other nodes to 0. A subgraph with node weight at least |S| with minimum total edge cost must be a tree (if there are any cycles, deleting an edge from the cycle only decreases the cost) and must connect all of the nodes that need to be spanned. It's therefore a Steiner tree. Overall, this reduction can be computed in polynomial time, so your problem is NP-hard.
I know a few algorithms that are able to find the lowest cost path for directed graph (just as Dijkstra and Floyd).
Is there any algorithm that works for non-directed graphs?
My problem is: I need to find the lowest cost path from a to b passing through all vertexes (undirected graph).
My problem is: I need to find the lowest cost path from a to b passing
through all vertexes (non-oriented graph)
This is the Traveling Salesman Problem, which is NP-Hard, so there is no known efficient solution to it.
However, if the graph is fairly small, there are some techniques to solve it optimally (in exponential time), like Dynamic Programming.
In general, changing an undirected graph to a directed one is fairly easy and is done by changing an undirected edge {u,v} to two directed edges (u,v) and (v,u)
Provided you have nonnegative edge values, you could consider every edge in an undirected graph as two edges in a directed graph, one pointed to and from connected vertices. Then you could use one of many algorithms including the ones you listed.
I know that Bellman-Ford Algorithm works for directed graphs. Will it will work for an undirected graph? It seems that with an undirected graph, it will not be able to detect cycles because parallel edges will be considered cycles. Is this true or not? Can the algorithm be applied?
As a matter of fact any undirected graph is also a directed graph.
You just have to specify any edges {u, v} twice (u, v) and (v, u).
But don't forget, that this also means any edge with a negative weight will count as a loop.
As the Bellman-Ford algorithm ONLY works on graphs that don't contain any cycles with negative weights this actually means your un-directed graph mustn't contain any edges with negative weight.
If it doesn't its pretty fine to use Bellmann-Ford.
Bellman Ford Algorithm Does not work on undirected graph with negative weight, because an undirected edge {u, v} with negative weight can be defined as 2 directed edge, (u, v) and (v, u) with negative weigh, which would be picked up as a negative cycle by Bellman Ford Algorithm.
Therefore, Bellman Ford would only work on positively weighted undirected graph. However in that case, it is preferred to use Dijkstra's algorithm instead as it is asymptotically faster.
Bellman-Ford is not applicable to find shortest paths on graphs which contain negative cycles, but it finds shortest paths on graphs and can detect if the graph contains a negative cycle, although it won't find a shortest path, as no such path exists.
I had a question regarding designing an algorithm to make a bipartite graph acyclic. I hope someone could help me out here. The problem statement is described below:
Consider an undirected bipartite graph G = (U,V,E), where U = {u_1, u_2, ...u_M} is a set of M nodes, V = {v_1, v_2, ..., v_N} is a set of N nodes, and E is the set of edges connecting nodes in U to nodes in V. For simplicity, assume that the graph is connected and cyclic, i.e., has cycles.
The aim is to design an algorithm that eliminate cycles and reduces the graph to a tree or forest as follows. The algorithm proceeds in rounds. A round is described as choosing each node u_i, i = 1, 2, ..., M, in U and removing an edge connected to it. In case a node u_i is isolated (i.e., it has no edges connected to it) we ignore it and proceed. This way at most M edges are removed in each round. The algorithm stops when the graph reduces to a tree or forest at the end of some round.
I wish to know if it is possible to have a polynomial-time algorithm (in M, N) for designing the rounds such that the number of rounds is minimized (for reducing the graph to a tree/forest).
Refer the Cycle article on Wikipedia.
To detect a cycle in any undirected graph, perform a DFS, maintaining a list of visited nodes. If you detect a back edge, it is part of a cycle and you can remove that edge from the graph. A DFS with no back edges has no cycles. Complexity is O(M+N).
If we modify the shortest path problem such that the cost of a path between two vertices is the maximum of the costs of the edges on it, then for any pair of vertices u and v,
the path between them that follows a minimum-cost spanning tree is a min-cost path.
How can I prove this approach is true? It makes sense but I am not sure. Does anyone know if this algorithm exists in the literature? Is there a name for it?
The approach you mentioned is discussed in detail in literatures that discuss the relationship between Prim's algorithm and Dijkstra's algorithm, as usual wikipedia is a good place to start your research:
The process that underlies Dijkstra's algorithm is similar to the greedy process used in Prim's algorithm. Prim's purpose is to find a minimum spanning tree that connects all nodes in the graph; Dijkstra is concerned with only the lowest cost path beteen two nodes.
You can use some basic facts of MST (that are usually discussed in the correctness proof for Prim's & Kruskal's algorithms). The one that matters now is that
Lema 1:
Given a graph cut (a partitioning of the vertices into two
disjoint sets) the edge in the MST connecting the two parts will be
the cheapest of the edges connecting the two parts.
(The proof is straighfoward, if there were a cheaper edge we would be able to easily contruct a cheaper spanning tree)
We can now prove that the paths in a MST are all min-cost paths if you consider the maximum-cost:
Take any two vertices s and t in G and the path p that connects them in a MST of G. Now let uv be the most expensive edge in this path. We can describe a graph cut over this edge, with one partition with the vertices on the u side of the MST and the other partition with the vertices on the v side. We know that any path connecting s and t must pass this cut, therefore we can determine that the cost of any path from s to t must be at least the cost of the cheapest edge on this cut. But Lemma 1 tells us that uv is the cheapest edge on this cut so p must be a min-cost path.