Transforming Weighted to Unweighted Graph for BFS/DFS - data-structures

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.

Related

Converting cyclic graph to acyclic in a weighted 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.

Given undirected weighted connected graph, s,t. Find path from s to t that its most weighted edge is low as possible

Given: undirected weighted connected graph. s,t are vertices.
Question: Find an algorithm as efficient as possible that returns a path from s to t. In that path, the edge that has the highest weight, will has the least weight as possible. So if we have 5 paths from s,t and for every path we have the heaviest edge, so the minimum edge of these 5.
What I've tried:
Use some algorithm to find the shortest path between s and t.
Delete all the edges that are not part of the shortest paths we've found
Use BFS with some modification, We run BFS depending on the number of paths from s to t. Every time we find a maximum edge and store it in an array, then we find the minimum of the array.
I'm struggling to find an algorithm that can be ran in (1), Bellman ford won't work - because it has to be directed graph. Dijkstra won't work because we don't know if it has negative circles or negative edges. And Prim is for finding MST which I'm not aware of how it can help us in finding the shortest path. Any ideas?
And other from that, If you have an algorithm in mind that can solve this question, would be much appreciated.
You can solve this with Kruskal's algorithm. Add edges as usual and stop as soon as s and t are in the same cluster.
The idea is that at each stage of the algorithm we have effectively added all edges below a certain weight threshold. Therefore, if s and t are in the same cluster then there is a route between them consisting entirely of edges with weight less than the threshold.
You can solve it by converting into a MST problem, basically the path from s to t in the MST would be the path which has the least possible maximum weight
find the most negative edge in the graph
add that (weight+1) to every edge.
Now all edge are positive so you can apply Dijkstra's algorithm
you can get the shortest_path between source and destination
Now count the number of edges between source and destination (say x)
Real shortest path will be: shortest_path - x * (weight+1)

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.

Lightest paths tree

I need to write algorithms finding the lightest path tree in directed and weighted graph.
(should be efficient as possible)
I'm getting a vertex S and need to build a paths tree from S to all vertex that can be approach from S so the paths in the tree are the lightest ( path weight is the path without the ends)
I thought about first calculating all the distances from S And then for each path there will be a new weight:
The weight minus The ends
And then on the graph with the new weights to run dijkstra...
Will it work? Is it efficient enough? How to calculate the distances?
Your comments suggest you are actually looking for the shortest path from a single source - to all vertices.
Have a look at how Dijkstra's algorithm works. The algorithm starts with a tree of size 1 (the source alone), and iteratively adds vertices to the tree. In the end, the tree created by dijkstra's algorithm, represent the shortest path from the source to each of the nodes in the graph.
Note that dijkstra's algorithm, needs a weight function on edges, while your weight function is on vertices. It can be solved easily by defining a new weight function w':E->R: w'(u,v) = w(u) (it works since you don't want to count the end).
It sounds like you're asking for a minimum spanning tree. The wikipedia page discusses algorithms.

Resources