I am trying to perform BFS on a weighted undirected graph. can anyone give me an algorithm for converting weighted graph to unweighted so that I can use it as input for BFS algorithm. thanks.
To perform transformation of weighted undirected graph in one unweighted he algorithm is very simple. Is summarized to change of nonzero elements in adjacency matrix to true and zero elements into false. However, you can apply BFS into weighted undirected graph as well.
Related
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.
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.
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.
Question: Adapt Dijkstra’s algorithm to solve the SSSP problem on a weighted undirected
graph.
surely there's no need to modify the algorithm? if the graph is undirected then its just a directed graph with edges both ways, right?
Yes, Dijkstra's algorithm works for both types of graphs, and in the undirected case you just allow to use an edge from both end points.
If your implementation works with graphs given by an adjacency list, then this information is already implicitly given by this data structure: in the undirected case you list, for an edge (u,v), u in the adjacency of v and v in the adjacency of u, which gives you both directions. So you can use the same implementation for both types of graphs.
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.