A* algorithm for undirected graph - algorithm

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.

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.

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.

modifying Dijkstra’s algorithm to undirected graph

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.

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.

Visiting edges, vertices in an undirected graph

Problem: You have an Undirected graph G = (V, E) (V = vertices, E = edges) and you must visit each vertex and pass each edge in both directions.
The only algorithms I know for graphs are DFS, BFS, and a few MST's (Kruskal, etc.) My friend and I were discussing this problem, if it were directed I would simply DFS, and then DFS the transpose but the graph is unfortunately undirected. My friend proposed we perform an MST and DFS the MST and then find the remaining edges by iterating through those that arent in MST. I sort of see what he means but I am not sure this is a good approach? opinions? Also, how would I be able to pass by an edge in both directions if it is undirected?
It doesn't matter if the graph is directed or undirected. You could just replace every undirected edge with two directed edges and perform whatever algorithm you have for a directed graph. Both DFS and BFS will traverse the whole vertices and edges.
I think what you are looking for is called Graph Traversal. BFS and DFS are two graph traversal algorithms and they do not require the graph to be directed. MST on the other hand, is not a graph traversal algorithm.

Resources