Minimum number of edges between two vertices of a graph using DFS
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.
Examples:
Input: For the following given graph, find the minimum number of edges between vertex pair (0, 4)
Output:1There 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:
C++
// C++ program to find minimum // number of edges between any two // vertices of the graph #include <bits/stdc++.h> using namespace std; // Class to represent a graph class Graph { // No. of vertices int V; // Pointer to an array containing // adjacency lists list<int>* adj; // A function used by minEdgeDFS void minEdgeDFSUtil(vector<bool>& visited, int src, int des, int& min_num_of_edges, int& edge_count); public: // Constructor Graph(int V); // Function to add an edge to graph void addEdge(int src, int des); // Prints the minimum number of edges void minEdgeDFS(int u, int v); }; Graph::Graph(int V) { this->V = V; adj = new list<int>[V]; } void Graph::addEdge(int src, int des) { adj[src].push_back(des); adj[des].push_back(src); } // Utility function for finding minimum number // of edges using DFS void Graph::minEdgeDFSUtil(vector<bool>& visited, int src, int des, int& min_num_of_edges, int& edge_count) { // For keeping track of visited // nodes in DFS visited[src] = true; // If we have found the destination vertex // then check whether count of total number of edges // is less than the minimum number of edges or not if (src == des) { if (min_num_of_edges > edge_count) min_num_of_edges = edge_count; } // If current vertex is not destination else { // Recur for all the vertices // adjacent to current vertex list<int>::iterator i; for (i = adj[src].begin(); i != adj[src].end(); i++) { int v = *i; if (!visited[v]) { edge_count++; minEdgeDFSUtil(visited, v, des, min_num_of_edges, edge_count); } } } // Decrement the count of number of edges // and mark current vertex as unvisited visited[src] = false; edge_count--; } // Function to print minimum number of edges // It uses recursive minEdgeDFSUtil void Graph::minEdgeDFS(int u, int v) { // To keep track of all the // visited vertices vector<bool> visited(V, false); // To store minimum number of edges int min_num_of_edges = INT_MAX; // To store total number of // edges in each path int edge_count = 0; minEdgeDFSUtil(visited, u, v, min_num_of_edges, edge_count); // Print the minimum number of edges cout << min_num_of_edges; } // Driver Code int main() { // Create a graph Graph g(5); g.addEdge(0, 1); g.addEdge(0, 4); g.addEdge(1, 2); g.addEdge(2, 4); g.addEdge(2, 3); g.addEdge(3, 4); int u = 0; int v = 3; g.minEdgeDFS(u, v); return 0; } |
Python3
# Python3 program to find minimum # number of edges between any two # vertices of the graph # Class to represent a graph class Graph: def __init__(self, V): self.V = V self.adj = [[] for i in range(V)] def addEdge(self, src, des): self.adj[src].append(des) self.adj[des].append(src) # Utility function for finding # minimum number of edges using DFS def minEdgeDFSUtil(self, visited, src, des, min_num_of_edges, edge_count): # For keeping track of visited nodes in DFS visited[src] = True # If we have found the destination vertex # then check whether count of total number of edges # is less than the minimum number of edges or not if src == des: if min_num_of_edges > edge_count: min_num_of_edges = edge_count # If current vertex is not destination else: # Recur for all the vertices # adjacent to current vertex for v in self.adj[src]: if not visited[v]: edge_count += 1 min_num_of_edges, edge_count = \ self.minEdgeDFSUtil(visited, v, des, min_num_of_edges, edge_count) # Decrement the count of number of edges # and mark current vertex as unvisited visited[src] = False edge_count -= 1 return min_num_of_edges, edge_count # Function to print minimum number of # edges. It uses recursive minEdgeDFSUtil def minEdgeDFS(self, u, v): # To keep track of all the # visited vertices visited = [False] * self.V # To store minimum number of edges min_num_of_edges = float('inf') # To store total number of # edges in each path edge_count = 0 min_num_of_edges, edge_count = \ self.minEdgeDFSUtil(visited, u, v, min_num_of_edges, edge_count) # Print the minimum number of edges print(min_num_of_edges) # Driver Code if __name__ == "__main__": # Create a graph g = Graph(5) g.addEdge(0, 1) g.addEdge(0, 4) g.addEdge(1, 2) g.addEdge(2, 4) g.addEdge(2, 3) g.addEdge(3, 4) u, v = 0, 3 g.minEdgeDFS(u, v) # This code is contributed by Rituraj Jain |
2
Recommended Posts:
- 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 contribute@geeksforgeeks.org. 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




