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 number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Maximum and minimum isolated vertices in a graph
- Path with minimum XOR sum of edges in a directed 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
- Count number of edges in an undirected graph
- Maximum number of edges in Bipartite 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
- Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Minimum number of edges that need to be added to form a triangle
- Maximize the number of uncolored vertices appearing along the path from root vertex and the colored vertices
- Construct a graph from given degrees of all vertices
- Articulation Points (or Cut Vertices) in a Graph
- Finding in and out degrees of all vertices in a graph
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.