Given a weighted undirected graph, Length of walks N and Cost X. The task is to count the number of different walks W of length N such that Cost(W) = X.
We define the cost of a walk W, as the maximum over the weights of the edges along the walk.
The nodes are numbered from 1 to n. The graph does not contain any multiple edges or self-loops.
N = 4, X = 2
A walk W on the graph is a sequence of vertices (with repetitions of vertices and edges allowed) such that every adjacent pair of vertices in the sequence is an edge of the graph.
For X = 2, all possible 10 walks are listed below :
- 1 -> 2 -> 1 -> 2 -> 3
- 1 -> 2 -> 3 -> 2 -> 1
- 1 -> 2 -> 3 -> 2 -> 3
- 2 -> 1 -> 2 -> 3 -> 2
- 2 -> 3 -> 2 -> 1 -> 2
- 2 -> 3 -> 2 -> 3 -> 2
- 3 -> 2 -> 1 -> 2 -> 1
- 3 -> 2 -> 1 -> 2 -> 3
- 3 -> 2 -> 3 -> 2 -> 1
- 3 -> 2 -> 3 -> 2 -> 3
N = 4, X = 2
The idea is to precalculate the no. of walks of length N for each vertex of all possible cost and store them in a 2-D matrix.Let us call this matrix as B.These values can be calculated by running DFS on the given undirected graph.
The given snapshot of matrix B shows the values stored in it. here B(i, j) means no. of walks of length N from vertex i having cost of walk j.
We maintain a 1-D array Maxedge in which we keep the cost of walk of length N. We call the same function when the length of walk is less than N and there is some cost X associated with edge(u, v).
We put a base condition for length == N for which we update the array B and return the call.
After calculating the matrix B we simply count the total no of walks by adding the no of walks of all the vextex having cost = x.
Ans += B[i][x];
Here i ranges from 1 to n where n is the no of vertices.
Below is the implementation of the above approach
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number of Walks from source to destination
- Number of non-decreasing sub-arrays of length greater than or equal to K
- Check whether count of odd and even factors of a number are equal
- Count all possible walks from a source to a destination with exactly k edges
- Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X
- Number of elements less than or equal to a number in a subarray : MO's Algorithm
- Count of Numbers in Range where first digit is equal to last digit of the number
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Count number of ways to divide a number in 4 parts
- Number of subarrays with GCD equal to 1
- Length of the smallest number which is divisible by K and formed by using 1's only
- Smallest number containing all possible N length permutations using digits 0 to D
- Find all powers of 2 less than or equal to a given number
- Find the number of paths of length K in a directed graph
- Check if it is possible to reach a number by making jumps of two given length
- Check if the length of all connected components is a Fibonacci number
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Length of Longest Perfect number Subsequence in an Array
- Smallest number greater than Y with sum of digits equal to X
- Minimum number of moves to make all elements equal
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.