Converting cyclic graph to acyclic in a weighted graph - algorithm

I am given a connected, weighted graph, with non-negative weights. I want to convert it to a connected, acyclic graph such that sum of weights of the removed edges is minimised. The output would be the removed edges.
My thoughts: Since a connected, acyclic graph is a tree, I can simply take the maximum n-1 edges, and remove all others. But, this may not always be correct. It may lead to a disconnected graph.
Then, I thought of using dfs. I know how we can detect if a graph has cycle using dfs, but I don't know how to detect all the edges that were involved and how we can convert it to a acyclic graph. Any help (code/pseudocode/algo in words) would be appreciated. Thanks...

You need maximum spanning tree.
use Kruskal's algorithm for minimal spanning tree with the negating weights.

Related

Transforming Weighted to Unweighted Graph for BFS/DFS

Are there any general or simple algorithms that convert a weighted graph into a non-weighted graph (where each edge has the same weight)?
I know an algorithm called Djikstra's algorithm that works very similar to BFS on a weighted graph when finding the shortest distance between any two vertices in a graph.
But I would like to change the question a little that takes in any weighted graph where each edge is in the set of weights {1,2,3,4,...,n} where each number in the set is a weight of an edge. That way, if every edge is the same weight, I can apply BFS or DFS to it directly.
I want to find a way to modify the graph so that each edge in the modified graph has the same weight (meaning preferably each edge in the modified graph has weight 1). I know that I may have to delete or add edges and add intermediate vertices or delete any vertices to/from the original graph to satisfy this.
Is there a general idea on how to accomplish this? I've never seen a post on StackOverflow that goes over this.
Note: There is nothing about a weighted graph that prevents you from using BFS/DFS.
With that being said you certainly can convert a weighted graph to an unweighted graph by just adding a bunch of intermediate notes. However, I would not recommend that you do this because there are already algorithms like dijkstra's, bellman ford, floyd warshall ... that can compute shortest paths on weighted graphs. Additionally, I would assume that it would be very difficult to represent a weighted graph with negative edges using an unweighted graph.

Relation between Dijkstra and MST

This question came to my mind when I see this question. For simplicity, we can limit our discussion to undirected, weighted, connected graphs. It is clear that Dijkstra cannot guarantee to produce a MST if we choose an arbitrary node from a graph as the source. However, is it guaranteed that there must exist one node in an undirected, weighted, connected graph, which will produce a MST for the graph if we choose it as the source and apply Dijkstra's algorithm? Maybe you can give a proof or a counterexample. Thanks!
However, is it guaranteed that there must exist one node in an
undirected, weighted, connected graph, which will produce a MST for
the graph if we choose it as the source and apply Dijkstra's
algorithm?
Nope, Dijkstra's algorithm minimizes the path weight from a single node to all other nodes. A minimum spanning tree minimizes the sum of the weights needed to connect all nodes together. There's no reason to expect that those disparate requirements will result in identical solutions.
Consider a complete graph where the sum of the weight of any two edges exceeds the weight of any single edge. That forces Dijkstra to always select the direct connection as the shortest path between two nodes. Then, if the lowest weight edges in the graph don't all originate from a single node, the minimum spanning tree won't be the same as any of the trees that Dijkstra will produce.
Here's an example:
The minimum spanning tree consists of the three edges with weight 3 (total weight 9). The trees returned by Dijkstra's algorithm will be whichever three edges connect directly to the source node (total weight 10 or 11).

A* algorithm for undirected graph

I know A* algorithm can be used in directed graph, can we use it in undirected graph as well?
The A* algorithm is generic for all the graphs. So, yes, you can use it with an undirected graph.
In an undirected graph, all edges are by definition bidirectional. So it's like a directional graph where for every edge, you'd have an edge in the opposite direction. In consequence, if you have an implementation of the algorithm working for directed graphs, you should be able to extend it to undirected graphs according to this principle.
The only difficulty here is to have the appropriate data structure. If implementing the edges with a matrix, you just have to make sure that the matrix is symmetric. If you use adjacency lists, be sure that everytime you add an edge from a to b, the edge of b to a is added, with the same cost factor.

Non-directed graph algorithm to find lowest cost path

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.

Graph algorithms: Prim

I was wondering if any minimum spanning tree of a graph G can be provided by an execution of the algorithm Prim on this graph?
Does the Prim algorithm give us all the possible MST?
I was wondering if any minimum spanning tree of a graph G can be
provided by an execution of the algorithm Prim on this graph?
Yes.
Prim's algorithm is known to be a good algorithm to find a minimum
spanning tree.
Since Prim's algorithm constructs a MST from a weighted, connected, undirected graph, yes, you can use it to get a minimum spanning tree from such a graph. If your graph is not connected it won't work (but neither will any other algorithm because there is no spanning tree, then). If your graph is not weighted it will just create a spanning tree.
If you use Prim's once to get a MST then delete an edge. Then use prim's again to see if you can still get a MST of the same length. If you do, repeat, otherwise put the edge back and remove another edge. It will be slowish ... Perhaps only remove heavy edges?

Resources