- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Possible walks from a source to a destination with exactly k edgesn
A directed graph is given. Another two vertices u and v are also given, u is the starting vertex, and v is the ending vertex. Our task is to find a number of walks from vertex u to v with exactly k edges. The value of k is also provided in the algorithm.
By using dynamic programming, we need to create a 3D table, Where the row will point the values of u, columns will point the values v and depth will be used to track the number of edges from start to end.
Input and Output
Input: The adjacency matrix of the graph: The destination vertex is 3. K = 2 0 1 1 1 0 0 0 1 0 0 0 1 0 0 0 0 Output: There are 2 Possible Walks, from 0 to 3 with 2 edges.
Algorithm
numberOdWalks(u, v, k)
Input: The start vertex u, the end vertex v, the number of edges k.
Output: Number of possible walks with k edges.
Begin define 3D array count of order (n x n x k+1) //n is number of vertices for edge in range 0 to k, do for i in range 0 to n-1, do for j in range 0 to n-1, do count[i, j, edge] := 0 if edge = 0 and i = j, then count[i, j, edge] := 1 if edge = 1 and (i, j) is connected, then count[i, j, edge] := 1 if edge > 1, then for a in range 0 to n, and adjacent with i do count[i, j, edge] := count[i, j, edge] + count[a, j, edge - 1] done done done done return count[u, v, k] End
Example
#include <iostream> #define NODE 7 using namespace std; int graph[NODE][NODE] = { {0, 1, 1, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 0} }; int numberOfWalks(int u, int v, int k) { int count[NODE][NODE][k+1]; for (int edge = 0; edge <= k; edge++) { //for k edges (0..k) for (int i = 0; i < NODE; i++) { for (int j = 0; j < NODE; j++) { count[i][j][edge] = 0; //initially all values are 0 if (edge == 0 && i == j) //when e is 0 and ith and jth node are same, only one path count[i][j][edge] = 1; if (edge == 1 && graph[i][j]) //when e is 0 and direct path from (i to j), one path count[i][j][edge] = 1; if (edge >1) { //for more than one edges for (int a = 0; a < NODE; a++) // adjacent of source i if (graph[i][a]) count[i][j][edge] += count[a][j][edge-1]; } } } } return count[u][v][k]; } int main() { int u = 0, v = 3, k = 2; cout << "There are "<< numberOfWalks(u, v, k)<<" Possible Walks, from "; cout <<u<<" to "<<v<<" with "<<k<<" edges."; }
Output
There are 2 Possible Walks, from 0 to 3 with 2 edges.
- Related Articles
- Count all possible walks from a source to a destination with exactly k edges in C++
- Shortest path with exactly k Edges
- Print all paths from a given source to a destination in C++
- How OSPF routes the packets from source to destination?
- Print all paths from a given source to a destination using BFS in C++
- 8085 program to move blocks of bits from source location to a destination location
- Concatenating n characters from source string to destination string in C
- C++ Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
- Maximum of smallest possible area that can get with exactly k cut of given rectangular in C++
- Maximum possible middle element of the array after deleting exactly k elements in C++
- C++ program to find out the minimum amount of time needed to reach from source to destination station by train
- Find if there is a path of more than k length from a source in Python
- Find if there is a path of more than k length from a source 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

Advertisements