Given an undirected graph G(V, E) with N vertices and M edges. We need to find the minimum number of edges between a given pair of vertices (u, v).
We have already discussed this problem using the BFS approach, here we will use the DFS approach.
Input: For the following given graph, find the minimum number of edges between vertex pair (0, 4)
There are three paths from 0 to 4:
0 -> 1 -> 2 -> 4
0 -> 1 -> 2 -> 3 -> 4
0 -> 4
Only the third path results in minimum number of edges.
Approach: In this approach we will traverse the graph in a DFS manner, starting from the given vertex and explore all the paths from that vertex to our destination vertex.
We will use two variables, edge_count and min_num_of_edges. While exploring all the paths, between these vertices, edge_count will store count of total number of edges among them, if number of edges is less than the minimum number of edges we will update min_num_of_edges.
Below is the implementation of the above approach:
- Minimum number of edges between two vertices of a Graph
- Number of Simple Graph with N Vertices and M Edges
- Largest subset of Graph vertices with edges of 2 or more colors
- Maximum and minimum isolated vertices in a graph
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Maximum number of edges in Bipartite graph
- Count number of edges in an undirected graph
- Program to find total number of edges in a Complete Graph
- Maximum number of edges among all connected components of an undirected graph
- Maximum number of edges to be added to a tree so that it stays a Bipartite graph
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method
- Finding in and out degrees of all vertices in a graph
- Construct a graph from given degrees of all vertices
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rituraj_jain