- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count all possible walks from a source to a destination with exactly k edges in C++
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.
Example
#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; }
Output
2
- Related Articles
- Possible walks from a source to a destination 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++
- Shortest path with exactly k Edges
- C++ Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
- Concatenating n characters from source string to destination string in C
- Count number of substrings with exactly k distinct characters in C++
- How OSPF routes the packets from source to destination?
- 8085 program to move blocks of bits from source location to a destination location
- Count all possible paths from top left to bottom right of a mXn matrix in C++
- Count all distinct pairs with difference equal to k in C++
- Print all words occurring in a sentence exactly K times
- Program to count number of sublists with exactly k unique elements in Python
- Count all the numbers in a range with smallest factor as K in C++
- Count of Numbers in a Range where digit d occurs exactly K times in C++

Advertisements