shortest path between all vertex using bfs - algorithm

I'm learning about graph theory and I need help.
I need algorithm for shortest path between all vertex in graph using bfs. I know how bfs works but I don't know "remake" that algorithm to find shortest path between all vertex in graph.

The most simple way is the repeated BFS for each node, as Yerken said. Its time complexity is O( (v+e)*v ), where v and e is the number of vertices and edges, respectively. O(1) < e < O(v^2), so if your graph is very dense, it is an O(v^3) algorithm. However if the graph is sparse and the complexity is O(v^2), it can be done faster with repeated Dijkstra's, because its complexity is O(v * e * log(v)) = O(v*log(v)).
Alternatively you can try the Floyd-Warshall algorithm with O(v^3), but it gives you only the distances, not the paths.

Related

Weighted Directed Graph best method for shortest path

For a question I was doing I'm confused about why the answer would be a BFS and not Dijkstra's algorithm.
The question was : There is a weighted digraph G=(V,E) with n nodes and m edges. Each node has a weight of 1 or 2. The question was to figure out which algorithm to use to find the shortest path in G from a given vetex u to a given vertex v. The options were:
a) O(n+m) time using a modified BFS
b) O(n+m) time using a modified DFS
c) O(mlogn) time using Dijkstra's Algorithm
d) O(n^3) time using modified Floyd-Warshall algorithm
The answer is a) O(n+m) time using a modified BFS,
I know that when comparing BFS to DFS, BFS is better for shorter paths. I also know Dijkstra's algorithm is similar to a BFS and if I'm not mistaken Dijkstra's algorithm is better for weighted graphs like in this case. I'm assuming BFS is better because it says modified BFS but what would modified exactly mean or is there another reason BFS would be better.
Since all paths are limited to either a distance of 1 or 2, for every edge of length 2 from nodes a to b you can just create a new node c with an edge from a to c of length 1 and an edge from c to b of length 1, and then this becomes a graph with only edges of weight 1 which can be BFS'd normally to find shortest path from u to v. Since you only add O(m) new nodes and O(m) new edges, this keeps the BFS's time complexity of O(n+m).
Another possibility is to, at each layer of BFS, store another list of nodes that are attained by edges with a weight of 2 from the current layer, and consider them at the same time as nodes attained two layers later. This approach is a bit more finicky though.

Find cheapest cycle in an undirected weighted graph

I'm stuck on searching an algorithm to find the cheapest cycle in a weighted undirected graph in O(n^2). The cycle does not have to visit every vertex in the graph (i.e. I'm not looking for a Hamiltonian cycle).
Can someone help me finding a strategy?
an example of weighted undirected graph:
I'm not sure where the O(n^2) time bound came from. The obvious O(n (m + n log n))-time algorithm is, for each vertex, to compute a shortest-path tree from that vertex and then consider the fundamental cycle made by a non-tree edge and some tree edges.

Multi-start and Multi-end shortest path set

I am having problem with shortest path in directed weighted graph. I know Dijkstra, BFS, DFS. However, I have a set of vertices S for starting points and a set of vertices E to end. S and E doesn't overlap. So how can I find the set of edges with minimal sum of edge weight? The edge set doesn't have to include all vertices in S, but have to reach all vertices in E. Should I start with Dijkstra on all permutation of {Si, Ei} and optimize or I miss any important algorithm I should know? Or even I am over-thinking....
If I understand you correctly, you want to find the tree of minimal weight in the graph that contains all the vertices of E and at least one vertex from S.
The problem is called general Steiner tree, and it is NP-hard. So the best you can probably hope for is an exponential-time algorithm or some kind of approximation (the minimum spanning tree of the whole graph comes to mind, maybe after removing some unneeded subtrees).
There is a simple DP solution that works in O(2^n * (n + m)): Let f(S) be the cost of the minimum tree in the graph that spans all the nodes in S. It can be shown that there is such a tree T such that the weight of T \ {x} is f(S \ {x}) for some x, so the transition can be done in O(n + m).

Breadth-first search algorithm (graph represented by the adjacency list) has a quadratic time complexity?

A friend told me that breadth-first search algorithm (graph represented by the adjacency list) has a quadratic time complexity. But in all the sources says that the complexity of BFS algortim exactly O (|V| + |E|) or O (n + m), from which we obtain a quadratic complexity ?
All the sources are right :-) With BFS you visit each vertex and each edge exactly once, resulting in linear complexity. Now, if it's a completely connected graph, i.e. each pair of vertices is connected by an edge, then the number of edges grows quadratic with the number of vertices:
|E| = |V| * (|V|-1) / 2
Then one might say the complexity of BFS is quadratic in the number of vertices: O(|V|+|E|) = O(|V|^2)
BFS is O(E+V) hence in terms of input given it is linear time algorithm but if vertices of graph are considered then no of edges can be O(|V|^2) in dense graphs hence if we consider time complexity in terms of vertices in graph then BFS is O(|V|^2) hence can be considered quadratic in terms of vertices
O(n + m) is linear in complexity and not quadratic. O(n*m) is quadratic.
0. Initially all the vertices are labelled as unvisited. We start from a given vertex as the current vertex.
1. A BFS will cover (visit) all the adjacent unvisited vertices to the current vertex queuing up these children.
2. It would then label the current vertex as 'visited' so that it might not be visited (queued again).
3 BFS would then take out the first vertex from the queue and would repeat the steps from 1 till no more unvisited vertices remain.
The runtime for the above algorithm is linear in the total no. of vertices and edges together because the algorithm would visit each vertex once and check each of its edges once and thus it would take no. of vertices + no. of edges steps to completely search the graph

Even length path algorithm

I was asked for my homework to write an efficient algorithm that finds all the vertices in a directed graph which have even length of path to them from the given vertex.
This is what I thought of:
(It's very similar to "Visit" algorithm of DFS)
Visit(vertex u)
color[u]<-gray
for each v E adj[u]
for each w E adj[v]
if color[w] = white then
print w
Visit(w)
I think it works but I'm having hard time calculating it's efficiency, especially when the graph is with cycles. Could you help me?
If I may suggest an alternative - I would have reduced the problem and use DFS instead of modifying DFS.
Given a graph G = (V,E), cretae a graph G' = (V,E') where E'={(u,v) | there is w in V such that (u,w) and (w,v) are in E)
In other words - we are creating a graph G', which has edge (u,v) if and only if there is a path of length 2 from u to v.
Given that graph, we can derive the following algorithm [high level pseudo-code]:
Create G' from G
run DFS on G' from the source s, and mark the same nodes DFS marked.
Correctness and time complexity analysis of the solution:
Complexity:
The complexity is obviously O(min{|V|^2,|E|^2} + |V|), because of part 1 - since there are at most min{|E|^2,|V|^2} edges in G', so DFS on step 2 runs in O(|E'| + |V|) = O(min{|V|^2,|E|^2} + |V|)
Correctness:
If the algorithm found that there is a path from v0 to vk, then from the correctness of DFS - there is a path v0->v1->...->vk on G', so there is a path v0->v0'->v1->v1'->...->vk of even length on G.
If there is a path of even length on G from v0 to vk, let it be v0->v1->...->vk. then v0->v2->...->vk is a path on G', and will be found by DFS - from the correctness of DFS.
As a side note:
Reducing problems instead of modifying algorithms is usually less vulnurable to bugs, and easier to analyze and prove correctness on, so you should usually prefer these over modifying algorithms when possible.
EDIT: regarding your solution: Well, analysing it shows they are both pretty much identical - with the exception of I was generating E' as pre-processing, and you are generating it on the fly, in each iteration.
Since your solution is generating the edges on the fly - it might to doing some work more then once. However, it is bounded to the job at most |V| times more, since each vertex is being visited at most once.
Assuming |E| = O(|V|^2) for simplicity, giving us total an upper bound for the run time of O(|V|^3) for your solution.
It is also a lower bound, look at the example of a clique, During each visit() of any node, the algorithm will do O(|V|^2) to generate all possibilities, and visit() one of the possibilities, since we visit exactly |V| nodes, we get total run time of Omega(|V|^3)
Since we found the solution is both O(|V|^3) and Omega(|V|^3), it is total of Theta(O(|V|^3))
For each undirected graph G(V,E) we should rebuilt the mentioned graph to be bipartite graph G'(V',E') when:
V' = V1 ∪ V2
E'={(u1,v2):(u,v)∈E, u1∈V1, v2∈V2}
V1={v1: v∈V}
V2={v2: v∈V}
For example the graph
becomes
On this graph (bipartite graph) we should run BFS algorithm - BFS(G',S1').
After running BFS(G',S1') we should return array d that contains length of shortest even path δ(s1,u1)

Resources