- Related Questions & Answers
- Possible walks from a source to a destination with exactly k edges
- Shortest path with exactly k Edges
- Print all paths from a given source to a destination in C++
- Print all paths from a given source to a destination using BFS in C++
- C++ Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
- 8085 program to move blocks of bits from source location to a destination location
- All Paths From Source to Target in C++
- Count all distinct pairs with difference equal to k in C++
- Count all the numbers in a range with smallest factor as K in C++
- C++ Program to Find All Forward Edges in a Graph
- Count all possible paths from top left to bottom right of a mXn matrix in C++
- Maximum possible middle element of the array after deleting exactly k elements in C++
- Maximum of smallest possible area that can get with exactly k cut of given rectangular in C++
- Find if there is a path of more than k length from a source in C++
- Count all possible paths between two vertices in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this tutorial, we will be discussing a program to find the number of walks from a source to a destination with exactly k edges.

For this we will be provided with a graph and the values of source and destination. Our task is to find all the possible paths starting from the source to the destination having exactly k edges.

#include <iostream> using namespace std; #define V 4 //counting walks using recursion int countwalks(int graph[][V], int u, int v, int k){ if (k == 0 && u == v) return 1; if (k == 1 && graph[u][v]) return 1; if (k <= 0) return 0; int count = 0; //moving to the adjacent nodes for (int i = 0; i < V; i++) if (graph[u][i] == 1) count += countwalks(graph, i, v, k-1); return count; } int main(){ int graph[V][V] = { {0, 1, 1, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 0} }; int u = 0, v = 3, k = 2; cout << countwalks(graph, u, v, k); return 0; }

2

Advertisements