For N given cities, find how many ways a driver can traverse through them so that each town is visited exactly K times. From each city he can go to every other city (except the city which is already in it).
Example:
Input: N = 3, K = 1
Output: 6
Explanation: The possible paths areĀ
1 -> 2 -> 3
1 -> 3 -> 2
2 -> 1 -> 3
2 -> 3 -> 1
3 -> 1 -> 2
3 -> 2 -> 1Input: N = 3, K = 2
Output: 30
Explanation: The possible paths are
1 -> 2 -> 1 -> 3 -> 2 -> 3, 1 -> 2 -> 3 -> 1 -> 2 -> 3
1 -> 2 -> 3 -> 1 -> 3 -> 2, 1 -> 2 -> 3 -> 2 -> 1 -> 3
1 -> 2 -> 3 -> 2 -> 3 -> 1, 1 -> 3 -> 1 -> 2 -> 3 -> 2
1 -> 3 -> 2 -> 1 -> 2 -> 3, 1 -> 3 -> 2 -> 1 -> 3 -> 2
1 -> 3 -> 2 -> 3 -> 1 -> 2, 1 -> 3 -> 2 -> 3 -> 2 -> 1
2 -> 1 -> 2 -> 3 -> 1 -> 3, 2 -> 1 -> 3 -> 1 -> 2 -> 3
2 -> 1 -> 3 -> 1 -> 3 -> 2, 2 -> 1 -> 3 -> 2 -> 1 -> 3
2 -> 1 -> 3 -> 2 -> 3 -> 1, 2 -> 3 -> 1 -> 2 -> 1 -> 3
2 -> 3 -> 1 -> 2 -> 3 -> 1, 2 -> 3 -> 1 -> 3 -> 1 -> 2
2 -> 3 -> 1 -> 3 -> 2 -> 1, 2 -> 3 -> 2 -> 1 -> 3 -> 1
3 -> 1 -> 2 -> 1 -> 2 -> 3, 3 -> 1 -> 2 -> 1 -> 3 -> 2
3 -> 1 -> 2 -> 3 -> 1 -> 2, 3 -> 1 -> 2 -> 3 -> 2 -> 1
3 -> 1 -> 3 -> 2 -> 1 -> 2, 3 -> 2 -> 1 -> 2 -> 1 -> 3
3 -> 2 -> 1 -> 2 -> 3 -> 1, 3 -> 2 -> 1 -> 3 -> 1 -> 2
3 -> 2 -> 1 -> 3 -> 2 -> 1, 3 -> 2 -> 3 -> 1 -> 2 -> 1
Naive solution
The naive approach is to generate all the combinations and increment a counter for every possible arrangement that doesnāt have two same adjacent elements.Ā
Below is the implementation of the above approach:
C++
// C++ code to implement the approachĀ
#include <bits/stdc++.h>using namespace std;Ā
// Function to find the number of possible pathsulong driversProblem(int n, int k){Ā Ā Ā Ā ulong num_of_paths = 0;Ā Ā Ā Ā vector<int> path;Ā
Ā Ā Ā Ā // Filling the vector with the first pathĀ Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 0; j < k; j++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā path.push_back(i);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā Ā Ā Ā bool valid_path;Ā Ā Ā Ā while (next_permutation(path.begin(), path.end())) {Ā Ā Ā Ā Ā Ā Ā Ā valid_path = true;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Checking if there is 2 adjacent citiesĀ Ā Ā Ā Ā Ā Ā Ā // in the pathĀ Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n * k - 1; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (path[i] == path[i + 1]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā valid_path = false;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Incrementing the counter ifĀ Ā Ā Ā Ā Ā Ā Ā // the path generated is validĀ Ā Ā Ā Ā Ā Ā Ā if (valid_path == true) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths++;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā Ā Ā Ā return num_of_paths;}Ā
// Driver codeint main(){Ā Ā Ā Ā int N = 3, K = 2;Ā
Ā Ā Ā Ā // Function callĀ Ā Ā Ā cout << driversProblem(N, K) << endl;Ā Ā Ā Ā return 0;} |
Java
/*package whatever //do not write package name here */Ā
import java.io.*;Ā
class GFG {Ā
Ā Ā Ā Ā // Function to find the number of possible pathsĀ Ā Ā Ā static long driversProblem(int n, int k)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā long num_of_paths = 0;Ā Ā Ā Ā Ā Ā Ā Ā int path[] = new int[n * k];Ā Ā Ā Ā Ā Ā Ā Ā int x = 0;Ā Ā Ā Ā Ā Ā Ā Ā // Filling the vector with the first pathĀ Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 0; j < k; j++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā path[x++] = i;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā boolean valid_path;Ā Ā Ā Ā Ā Ā Ā Ā while (next_permutation(path)) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā valid_path = true;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Checking if there is 2 adjacent citiesĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // in the pathĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n * k - 1; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (path[i] == path[i + 1]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā valid_path = false;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Incrementing the counter ifĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the path generated is validĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (valid_path == true) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths++;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā return num_of_paths;Ā Ā Ā Ā }Ā Ā Ā Ā public static int[] swap(int data[], int left,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int right)Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Swap the dataĀ Ā Ā Ā Ā Ā Ā Ā int temp = data[left];Ā Ā Ā Ā Ā Ā Ā Ā data[left] = data[right];Ā Ā Ā Ā Ā Ā Ā Ā data[right] = temp;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return the updated arrayĀ Ā Ā Ā Ā Ā Ā Ā return data;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to reverse the sub-arrayĀ Ā Ā Ā // starting from left to the rightĀ Ā Ā Ā // both inclusiveĀ Ā Ā Ā public static int[] reverse(int data[], int left,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int right)Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Reverse the sub-arrayĀ Ā Ā Ā Ā Ā Ā Ā while (left < right) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int temp = data[left];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā data[left++] = data[right];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā data[right--] = temp;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return the updated arrayĀ Ā Ā Ā Ā Ā Ā Ā return data;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to find the next permutationĀ Ā Ā Ā // of the given integer arrayĀ Ā Ā Ā public static boolean next_permutation(int data[])Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If the given dataset is emptyĀ Ā Ā Ā Ā Ā Ā Ā // or contains only one elementĀ Ā Ā Ā Ā Ā Ā Ā // next_permutation is not possibleĀ Ā Ā Ā Ā Ā Ā Ā if (data.length <= 1)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return false;Ā
Ā Ā Ā Ā Ā Ā Ā Ā int last = data.length - 2;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // find the longest non-increasing suffixĀ Ā Ā Ā Ā Ā Ā Ā // and find the pivotĀ Ā Ā Ā Ā Ā Ā Ā while (last >= 0) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (data[last] < data[last + 1]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā last--;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If there is no increasing pairĀ Ā Ā Ā Ā Ā Ā Ā // there is no higher order permutationĀ Ā Ā Ā Ā Ā Ā Ā if (last < 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return false;Ā
Ā Ā Ā Ā Ā Ā Ā Ā int nextGreater = data.length - 1;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Find the rightmost successor to the pivotĀ Ā Ā Ā Ā Ā Ā Ā for (int i = data.length - 1; i > last; i--) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (data[i] > data[last]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā nextGreater = i;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Swap the successor and the pivotĀ Ā Ā Ā Ā Ā Ā Ā data = swap(data, nextGreater, last);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Reverse the suffixĀ Ā Ā Ā Ā Ā Ā Ā data = reverse(data, last + 1, data.length - 1);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return true as the next_permutation is doneĀ Ā Ā Ā Ā Ā Ā Ā return true;Ā Ā Ā Ā }Ā Ā Ā Ā public static void main(String[] args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int N = 3, K = 2;Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(driversProblem(N, K));Ā Ā Ā Ā }} |
Python3
# Python implementation for the above approachĀ
# Function to find the number of possible pathsdef driversProblem(n, k):Ā Ā Ā Ā num_of_paths = 0Ā Ā Ā Ā path = [0]*(n*k)Ā Ā Ā Ā x = 0Ā
Ā Ā Ā Ā # Filling the vector with the first pathĀ Ā Ā Ā for i in range(1, n+1):Ā Ā Ā Ā Ā Ā Ā Ā for j in range(k):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā path[x] = iĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā x += 1Ā
Ā Ā Ā Ā while(next_permutation(path)):Ā Ā Ā Ā Ā Ā Ā Ā valid_path = TrueĀ
Ā Ā Ā Ā Ā Ā Ā Ā # Checking if there is 2 adjacent cities in the pathĀ Ā Ā Ā Ā Ā Ā Ā for i in range(n*k-1):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if(path[i] == path[i+1]):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā valid_path = FalseĀ
Ā Ā Ā Ā Ā Ā Ā Ā # Incrementing the counted if the path generated is validĀ Ā Ā Ā Ā Ā Ā Ā if(valid_path):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths += 1Ā
Ā Ā Ā Ā return num_of_pathsĀ
# Function to reverse the sub-array starting # from left to the right both inclusivedef reverse(data, left, right):Ā Ā Ā Ā # Reverse the sub-arrayĀ Ā Ā Ā while(left < right):Ā Ā Ā Ā Ā Ā Ā Ā temp = data[left]Ā Ā Ā Ā Ā Ā Ā Ā data[left] = data[right]Ā Ā Ā Ā Ā Ā Ā Ā data[right] = tempĀ Ā Ā Ā Ā Ā Ā Ā left += 1Ā Ā Ā Ā Ā Ā Ā Ā right -= 1Ā
Ā Ā Ā Ā # Return the updated arrayĀ Ā Ā Ā return dataĀ
# Function to find the next permutation of the given integer arraydef next_permutation(data):Ā Ā Ā Ā # If the given dataset is empty or contains only Ā Ā Ā Ā # one element next_permutation is not possibleĀ Ā Ā Ā if(len(data) <= 1):Ā Ā Ā Ā Ā Ā Ā Ā return FalseĀ
Ā Ā Ā Ā last = len(data) - 2Ā
Ā Ā Ā Ā # Find the longest non-increasing Ā Ā Ā Ā # suffix and find the pivotĀ Ā Ā Ā while(last >= 0):Ā Ā Ā Ā Ā Ā Ā Ā if(data[last] < data[last+1]):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā breakĀ Ā Ā Ā Ā Ā Ā Ā last -= 1Ā
Ā Ā Ā Ā # If there is not increasing pair Ā Ā Ā Ā # there is no higher order permutationĀ Ā Ā Ā if(last < 0):Ā Ā Ā Ā Ā Ā Ā Ā return FalseĀ
Ā Ā Ā Ā nextGreater = len(data) - 1Ā
Ā Ā Ā Ā # Find the rightmost successor to the pivotĀ Ā Ā Ā for i in range(len(data)-1, last, -1):Ā Ā Ā Ā Ā Ā Ā Ā if(data[i] > data[last]):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā nextGreater = iĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā breakĀ
Ā Ā Ā Ā # Swap the successor and the pivotĀ Ā Ā Ā data[nextGreater], data[last] = data[last], data[nextGreater]Ā
Ā Ā Ā Ā # Reverse the suffixĀ Ā Ā Ā data = reverse(data, last+1, len(data)-1)Ā
Ā Ā Ā Ā # Return true as the next_permutation is doneĀ Ā Ā Ā return TrueĀ
Ā
N, K = 3, 2print(driversProblem(N, K))Ā
# This code is contributed by lokeshmvs21. |
C#
/*package whatever //do not write package name here */Ā
using System;Ā
class GFG {Ā
Ā Ā Ā Ā // Function to find the number of possible pathsĀ Ā Ā Ā static long driversProblem(int n, int k)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā long num_of_paths = 0;Ā Ā Ā Ā Ā Ā Ā Ā int[] path = new int[n * k];Ā Ā Ā Ā Ā Ā Ā Ā int x = 0;Ā Ā Ā Ā Ā Ā Ā Ā // Filling the vector with the first pathĀ Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 0; j < k; j++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā path[x++] = i;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Boolean valid_path;Ā Ā Ā Ā Ā Ā Ā Ā while (next_permutation(path)) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā valid_path = true;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Checking if there is 2 adjacent citiesĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // in the pathĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n * k - 1; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (path[i] == path[i + 1]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā valid_path = false;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Incrementing the counter ifĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the path generated is validĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (valid_path == true) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths++;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā return num_of_paths;Ā Ā Ā Ā }Ā Ā Ā Ā public static int[] swap(int[] data, int left,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int right)Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Swap the dataĀ Ā Ā Ā Ā Ā Ā Ā int temp = data[left];Ā Ā Ā Ā Ā Ā Ā Ā data[left] = data[right];Ā Ā Ā Ā Ā Ā Ā Ā data[right] = temp;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return the updated arrayĀ Ā Ā Ā Ā Ā Ā Ā return data;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to reverse the sub-arrayĀ Ā Ā Ā // starting from left to the rightĀ Ā Ā Ā // both inclusiveĀ Ā Ā Ā public static int[] reverse(int[] data, int left,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int right)Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Reverse the sub-arrayĀ Ā Ā Ā Ā Ā Ā Ā while (left < right) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int temp = data[left];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā data[left++] = data[right];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā data[right--] = temp;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return the updated arrayĀ Ā Ā Ā Ā Ā Ā Ā return data;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to find the next permutationĀ Ā Ā Ā // of the given integer arrayĀ Ā Ā Ā public static Boolean next_permutation(int[] data)Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If the given dataset is emptyĀ Ā Ā Ā Ā Ā Ā Ā // or contains only one elementĀ Ā Ā Ā Ā Ā Ā Ā // next_permutation is not possibleĀ Ā Ā Ā Ā Ā Ā Ā if (data.Length <= 1)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return false;Ā
Ā Ā Ā Ā Ā Ā Ā Ā int last = data.Length - 2;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // find the longest non-increasing suffixĀ Ā Ā Ā Ā Ā Ā Ā // and find the pivotĀ Ā Ā Ā Ā Ā Ā Ā while (last >= 0) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (data[last] < data[last + 1]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā last--;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If there is no increasing pairĀ Ā Ā Ā Ā Ā Ā Ā // there is no higher order permutationĀ Ā Ā Ā Ā Ā Ā Ā if (last < 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return false;Ā
Ā Ā Ā Ā Ā Ā Ā Ā int nextGreater = data.Length - 1;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Find the rightmost successor to the pivotĀ Ā Ā Ā Ā Ā Ā Ā for (int i = data.Length - 1; i > last; i--) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (data[i] > data[last]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā nextGreater = i;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Swap the successor and the pivotĀ Ā Ā Ā Ā Ā Ā Ā data = swap(data, nextGreater, last);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Reverse the suffixĀ Ā Ā Ā Ā Ā Ā Ā data = reverse(data, last + 1, data.Length - 1);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return true as the next_permutation is doneĀ Ā Ā Ā Ā Ā Ā Ā return true;Ā Ā Ā Ā }Ā Ā Ā Ā public static void Main()Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int N = 3, K = 2;Ā Ā Ā Ā Ā Ā Ā Ā Console.Write(driversProblem(N, K));Ā Ā Ā Ā }}Ā
// This code is contributed by Saurabh Jaiswal |
Javascript
// Javascript code to implement the approachĀ
Ā Ā Ā Ā function reverse(nums, start){Ā Ā Ā Ā let i = start, j = nums.length - 1;Ā Ā Ā Ā while (i < j) {Ā Ā Ā Ā Ā Ā Ā Ā swap(nums, i, j);Ā Ā Ā Ā Ā Ā Ā Ā i++;Ā Ā Ā Ā Ā Ā Ā Ā j--;Ā Ā Ā Ā }}function swap(nums, i, j){Ā Ā Ā Ā let temp = nums[i];Ā Ā Ā Ā nums[i] = nums[j];Ā Ā Ā Ā nums[j] = temp;}// function to find next greater permutationfunction next_permutation(nums){Ā Ā Ā Ā let i = nums.length - 2;Ā Ā Ā Ā while (i >= 0 && nums[i + 1] <= nums[i]) {Ā Ā Ā Ā Ā Ā Ā Ā i--;Ā Ā Ā Ā }Ā Ā Ā Ā if (i >= 0) {Ā Ā Ā Ā Ā Ā Ā Ā let j = nums.length - 1;Ā Ā Ā Ā Ā Ā Ā Ā while (nums[j] <= nums[i]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j--;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā swap(nums, i, j);Ā Ā Ā Ā Ā Ā Ā Ā reverse(nums, i + 1);Ā Ā Ā Ā Ā Ā Ā Ā return true;Ā Ā Ā Ā }Ā Ā Ā Ā return false;}// Function to find the number of possible pathsfunction driversProblem(n, k){Ā Ā Ā Ā let num_of_paths = 0;Ā Ā Ā Ā let path = [];Ā
Ā Ā Ā Ā // Filling the vector with the first pathĀ Ā Ā Ā for (let i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā for (let j = 0; j < k; j++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā path.push(i);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā Ā Ā Ā let valid_path;Ā Ā Ā Ā while (next_permutation(path)) {Ā Ā Ā Ā Ā Ā Ā Ā valid_path = true;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Checking if there is 2 adjacent citiesĀ Ā Ā Ā Ā Ā Ā Ā // in the pathĀ Ā Ā Ā Ā Ā Ā Ā for (let i = 0; i < n * k - 1; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (path[i] == path[i + 1]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā valid_path = false;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Incrementing the counter ifĀ Ā Ā Ā Ā Ā Ā Ā // the path generated is validĀ Ā Ā Ā Ā Ā Ā Ā if (valid_path == true) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths++;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā Ā Ā Ā return num_of_paths;}Ā
// Driver codelet N = 3, K = 2;Ā
// Function callconsole.log(driversProblem(N, K));Ā
// This code is contributed by garg28harsh. |
30
Time Complexity: (N*K)! / (K!)N
Auxiliary Space: O(N*K)
Count possible ways to travel all cities using Recursion:
In the recursive approach, we create an array of size N to represent each city and fill it up with the number K to later keep on track of number of times each city is visited.Ā
Follow the below steps to implement the idea:
- Build a recursion function as mentioned above.
- If every city has a count of 0, return 1. If any city has a count less than 0, return 0.Ā
- After that, loop throughout all the cities except the current city [because you canāt go to a town if you are already in it] and make a recursive call to which we pass the current city in the loop decremented (visited).
- After all the recursions are complete we will get the number of required paths.
Below is the implementation of the approach.
C++
// C++ code to implement the approachĀ
#include <bits/stdc++.h>using namespace std;Ā
// Function to find the number of pathsulong numberOfPaths(int current_city, int n,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vector<int> visited){Ā Ā Ā Ā bool all_zero = true;Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā if (visited[i] < 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā Ā Ā Ā Ā if (visited[i] != 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā all_zero = false;Ā Ā Ā Ā }Ā Ā Ā Ā if (all_zero == true)Ā Ā Ā Ā Ā Ā Ā Ā return 1;Ā Ā Ā Ā ulong num_of_paths = 0;Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā if (current_city == i) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā visited[i]--;Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths += numberOfPaths(i, n, visited);Ā Ā Ā Ā Ā Ā Ā Ā visited[i]++;Ā Ā Ā Ā }Ā Ā Ā Ā return num_of_paths;}Ā
ulong driversProblem(int n, int k){Ā Ā Ā Ā vector<int> visited;Ā Ā Ā Ā for (int i = 0; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā visited.push_back(k);Ā Ā Ā Ā }Ā Ā Ā Ā return numberOfPaths(0, n, visited);}Ā
// Driver codeint main(){Ā Ā Ā Ā int N = 3, K = 2;Ā
Ā Ā Ā Ā // Function callĀ Ā Ā Ā cout << driversProblem(N, K) << endl;Ā Ā Ā Ā return 0;} |
Java
// java implementationimport java.io.*;Ā
class GFG {Ā Ā Ā Ā public static long numberOfPaths(int current_city,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int n, int visited[])Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā boolean all_zero = true;Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (visited[i] < 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (visited[i] != 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā all_zero = false;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā if (all_zero == true)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 1;Ā Ā Ā Ā Ā Ā Ā Ā long num_of_paths = 0;Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (current_city == i) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[i]--;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths += numberOfPaths(i, n, visited);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[i]++;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā return num_of_paths;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā public static long driversProblem(int n, int k)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int visited[] = new int[n + 1];Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[i] = k;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā return numberOfPaths(0, n, visited);Ā Ā Ā Ā }Ā
Ā Ā Ā Ā public static void main(String[] args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int N = 3, K = 2;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Function callĀ Ā Ā Ā Ā Ā Ā Ā System.out.println(driversProblem(N, K));Ā Ā Ā Ā }}Ā
// This code is contributed by ksam24000 |
Python3
# Python code to implement the approachĀ
# Function to find the number of pathsĀ
Ā
def numberOfPaths(current_city, n, visited):Ā
Ā Ā Ā Ā all_zero = TrueĀ Ā Ā Ā for i in range(1, n+1):Ā Ā Ā Ā Ā Ā Ā Ā if (visited[i] < 0):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0Ā Ā Ā Ā Ā Ā Ā Ā if (visited[i] != 0):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā all_zero = FalseĀ
Ā Ā Ā Ā if (all_zero == True):Ā Ā Ā Ā Ā Ā Ā Ā return 1Ā Ā Ā Ā num_of_paths = 0Ā Ā Ā Ā for i in range(1, n+1):Ā Ā Ā Ā Ā Ā Ā Ā if (current_city == i):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continueĀ
Ā Ā Ā Ā Ā Ā Ā Ā visited[i] -= 1Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths += numberOfPaths(i, n, visited)Ā Ā Ā Ā Ā Ā Ā Ā visited[i] += 1Ā
Ā Ā Ā Ā return num_of_pathsĀ
Ā
def driversProblem(n, k):Ā
Ā Ā Ā Ā visited = []Ā Ā Ā Ā for i in range(n+1):Ā Ā Ā Ā Ā Ā Ā Ā visited.append(k)Ā
Ā Ā Ā Ā return numberOfPaths(0, n, visited)Ā
Ā
# Driver codeif __name__ == "__main__":Ā
Ā Ā Ā Ā N = 3Ā Ā Ā Ā K = 2Ā
Ā Ā Ā Ā # Function callĀ Ā Ā Ā print(driversProblem(N, K))Ā
Ā Ā Ā Ā # This code is contributed by sanjoy_62. |
C#
// C# program to implement// the above approachusing System;using System.Collections.Generic;Ā
class GFG {Ā Ā Ā Ā // Function to find the number of pathsĀ Ā Ā Ā static int numberOfPaths(int current_city, int n,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā List<int> visited)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā bool all_zero = true;Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (visited[i] < 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (visited[i] != 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā all_zero = false;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā if (all_zero == true)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 1;Ā Ā Ā Ā Ā Ā Ā Ā int num_of_paths = 0;Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (current_city == i) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[i]--;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths += numberOfPaths(i, n, visited);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[i]++;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā return num_of_paths;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā static int driversProblem(int n, int k)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā List<int> visited = new List<int>();Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited.Add(k);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā return numberOfPaths(0, n, visited);Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Driver CodeĀ Ā Ā Ā public static void Main()Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int N = 3, K = 2;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Function callĀ Ā Ā Ā Ā Ā Ā Ā Console.Write(driversProblem(N, K));Ā Ā Ā Ā }}Ā
// This code is contributed by code_hunt. |
Javascript
<script>Ā Ā Ā Ā Ā Ā Ā Ā // JavaScript code for the above approachĀ
Ā Ā Ā Ā Ā Ā Ā Ā // Function to find the number of pathsĀ Ā Ā Ā Ā Ā Ā Ā function numberOfPaths(current_city, n,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let all_zero = true;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (let i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (visited[i] < 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (visited[i] != 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā all_zero = false;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (all_zero == true)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let num_of_paths = 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (let i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (current_city == i) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[i]--;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths += numberOfPaths(i, n, visited);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[i]++;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return num_of_paths;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā function driversProblem(n, k) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let visited = [];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (let i = 0; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited.push(k);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return numberOfPaths(0, n, visited);Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Driver codeĀ
Ā Ā Ā Ā Ā Ā Ā Ā let N = 3, K = 2;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Function callĀ Ā Ā Ā Ā Ā Ā Ā document.write(driversProblem(N, K));Ā
Ā // This code is contributed by Potta LokeshĀ
Ā Ā Ā Ā </script> |
30
Count possible ways to travel all cities using Memoization:
If noticed carefully, we can see that the current city and the state of the visited array can uniquely identify a state. We can use that to memoize and reuse the previously calculated value.
Below is the implementation of the above approach.
C++
// C++ recursive approach (with memoization)// program for the drivers problemĀ
#include <bits/stdc++.h>using namespace std;Ā
// Map to store each unique statemap<pair<int, multiset<int> >, ulong>Ā Ā Ā Ā num_of_paths_calculated;Ā
// Function to calculate the number of possible waysulong numberOfPaths(int current_city, int n,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vector<int> visited){Ā Ā Ā Ā ulong calculated_before = num_of_paths_calculated[{Ā Ā Ā Ā Ā Ā Ā Ā visited[current_city],Ā Ā Ā Ā Ā Ā Ā Ā multiset<int>(visited.begin(), visited.end()) }];Ā
Ā Ā Ā Ā // If already calculated beforeĀ Ā Ā Ā if (calculated_before != 0)Ā Ā Ā Ā Ā Ā Ā Ā return calculated_before;Ā
Ā Ā Ā Ā bool all_zero = true;Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā if (visited[i] < 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā Ā Ā Ā Ā if (visited[i] != 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā all_zero = false;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // If all the cities are visited K timesĀ Ā Ā Ā if (all_zero == true)Ā Ā Ā Ā Ā Ā Ā Ā return 1;Ā Ā Ā Ā ulong num_of_paths = 0;Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā if (current_city == i) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā visited[i]--;Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths += numberOfPaths(i, n, visited);Ā Ā Ā Ā Ā Ā Ā Ā visited[i]++;Ā Ā Ā Ā }Ā Ā Ā Ā num_of_paths_calculated[{Ā Ā Ā Ā Ā Ā Ā Ā visited[current_city],Ā Ā Ā Ā Ā Ā Ā Ā multiset<int>(visited.begin(), visited.end()) }]Ā Ā Ā Ā Ā Ā Ā Ā = num_of_paths;Ā Ā Ā Ā return num_of_paths;}Ā
ulong driversProblem(int n, int k){Ā Ā Ā Ā vector<int> visited;Ā Ā Ā Ā for (int i = 0; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā visited.push_back(k);Ā Ā Ā Ā }Ā Ā Ā Ā return numberOfPaths(0, n, visited);}Ā
// Driver codeint main(){Ā Ā Ā Ā int N = 3, K = 2;Ā
Ā Ā Ā Ā // Function callĀ Ā Ā Ā cout << driversProblem(N, K) << endl;Ā Ā Ā Ā return 0;} |
Java
import java.util.HashMap;import java.util.HashSet;Ā
public class GFG {Ā Ā // Map to store each unique stateĀ Ā static HashMap<Pair<Integer, HashSet<Integer>>, Ā Ā Ā Ā Ā Ā Ā Ā Ā Long> num_of_paths_calculated = new HashMap<>();Ā
Ā Ā static class Pair<T, U> {Ā Ā Ā Ā T first;Ā Ā Ā Ā U second;Ā
Ā Ā Ā Ā Pair(T first, U second) {Ā Ā Ā Ā Ā Ā this.first = first;Ā Ā Ā Ā Ā Ā this.second = second;Ā Ā Ā Ā }Ā Ā }Ā // Function to calculate the number of possible waysĀ Ā static long numberOfPaths(int current_city, int n, int[] visited) {Ā Ā Ā Ā HashSet<Integer> visitedSet = new HashSet<>();Ā Ā Ā Ā for (int i = 0; i < visited.length; i++) {Ā Ā Ā Ā Ā Ā visitedSet.add(visited[i]);Ā Ā Ā Ā }Ā Ā Ā Ā Pair<Integer, HashSet<Integer>> key = new Pair<>(visited[current_city], Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visitedSet);Ā Ā Ā Ā if (num_of_paths_calculated.containsKey(key)) {Ā Ā Ā Ā Ā Ā return num_of_paths_calculated.get(key);Ā Ā Ā Ā }Ā
Ā Ā Ā Ā boolean all_zero = true;Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā if (visited[i] < 0) {Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā if (visited[i] != 0) {Ā Ā Ā Ā Ā Ā Ā Ā all_zero = false;Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā if (all_zero == true) {Ā Ā Ā Ā Ā Ā return 1;Ā Ā Ā Ā }Ā Ā Ā Ā long num_of_paths = 0;Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā if (current_city == i) {Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā visited[i]--;Ā Ā Ā Ā Ā Ā num_of_paths += numberOfPaths(i, n, visited);Ā Ā Ā Ā Ā Ā visited[i]++;Ā Ā Ā Ā }Ā Ā Ā Ā num_of_paths_calculated.put(key, num_of_paths);Ā Ā Ā Ā return num_of_paths;Ā Ā }Ā
Ā Ā static long driversProblem(int n, int k) {Ā Ā Ā Ā int[] visited = new int[n + 1];Ā Ā Ā Ā for (int i = 0; i <= n; i++) {Ā Ā Ā Ā Ā Ā visited[i] = k;Ā Ā Ā Ā }Ā Ā Ā Ā return numberOfPaths(0, n, visited);Ā Ā }//Driver codeĀ Ā public static void main(String[] args) {Ā Ā Ā Ā int N = 3, K = 2;Ā Ā Ā Ā //Function callĀ Ā Ā Ā System.out.println(driversProblem(N, K));Ā Ā }} |
Python3
# Python3 recursive approach (with memoization)# program for the drivers problemĀ
# Map to store each unique statenum_of_paths_calculated = {}Ā
# Function to calculate the number of possible waysdef numberOfPaths(current_city, n, visited):Ā Ā Ā Ā calculated_before = num_of_paths_calculated.get(str(visited[current_city]) + str(visited))Ā
Ā Ā Ā Ā # If already calculated beforeĀ Ā Ā Ā if calculated_before: return calculated_beforeĀ
Ā Ā Ā Ā all_zero = TrueĀ Ā Ā Ā for i in range(1, n + 1):Ā Ā Ā Ā Ā Ā Ā Ā if visited[i] < 0: return 0Ā Ā Ā Ā Ā Ā Ā Ā if visited[i] != 0: all_zero = FalseĀ
Ā Ā Ā Ā # If all the cities are visited K timesĀ Ā Ā Ā if all_zero == True: return 1Ā Ā Ā Ā num_of_paths = 0Ā Ā Ā Ā for i in range(1, n + 1):Ā Ā Ā Ā Ā Ā Ā Ā if current_city == i:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continueĀ Ā Ā Ā Ā Ā Ā Ā visited[i] -= 1Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths += numberOfPaths(i, n, visited)Ā Ā Ā Ā Ā Ā Ā Ā visited[i] += 1Ā Ā Ā Ā num_of_paths_calculated[str(visited[current_city]) + str(visited)] = num_of_pathsĀ Ā Ā Ā return num_of_pathsĀ
def driversProblem(n, k):Ā Ā Ā Ā visited = [k for i in range(n + 1)]Ā Ā Ā Ā return numberOfPaths(0, n, visited)Ā
# Driver codeN = 3K = 2Ā
# Function callprint(driversProblem(N, K))Ā
# This code is contributed by akashish__ |
C#
using System;using System.Collections.Generic;Ā
public class GFG {Ā
Ā Ā // Map to store each unique stateĀ Ā static Dictionary<Tuple<int, HashSet<int> >, ulong>Ā Ā Ā Ā num_of_paths_calculatedĀ Ā Ā Ā = new Dictionary<Tuple<int, HashSet<int> >,Ā Ā ulong>();Ā
Ā Ā // Function to calculate the number of possible waysĀ Ā static ulong numberOfPaths(int current_city, int n,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int[] visited)Ā Ā {Ā Ā Ā Ā Tuple<int, HashSet<int> > keyĀ Ā Ā Ā Ā Ā = Tuple.Create(visited[current_city],Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā new HashSet<int>(visited));Ā Ā Ā Ā if (num_of_paths_calculated.ContainsKey(key))Ā Ā Ā Ā Ā Ā return num_of_paths_calculated[key];Ā
Ā Ā Ā Ā bool all_zero = true;Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā if (visited[i] < 0)Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā Ā Ā if (visited[i] != 0)Ā Ā Ā Ā Ā Ā Ā Ā all_zero = false;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // If all the cities are visited K timesĀ Ā Ā Ā if (all_zero == true)Ā Ā Ā Ā Ā Ā return 1;Ā Ā Ā Ā ulong num_of_paths = 0;Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā if (current_city == i) {Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā visited[i]--;Ā Ā Ā Ā Ā Ā num_of_paths += numberOfPaths(i, n, visited);Ā Ā Ā Ā Ā Ā visited[i]++;Ā Ā Ā Ā }Ā Ā Ā Ā num_of_paths_calculated[key] = num_of_paths;Ā Ā Ā Ā return num_of_paths;Ā Ā }Ā
Ā Ā static ulong driversProblem(int n, int k)Ā Ā {Ā Ā Ā Ā int[] visited = new int[n + 1];Ā Ā Ā Ā for (int i = 0; i <= n; i++) {Ā Ā Ā Ā Ā Ā visited[i] = k;Ā Ā Ā Ā }Ā Ā Ā Ā return numberOfPaths(0, n, visited);Ā Ā }Ā
Ā Ā // Driver codeĀ Ā static public void Main()Ā Ā {Ā Ā Ā Ā int N = 3, K = 2;Ā
Ā Ā Ā Ā // Function callĀ Ā Ā Ā Console.WriteLine(driversProblem(N, K));Ā Ā }}Ā
// This code is contributed by akashish__ |
Javascript
// Javascript recursive approach (with memoization)// program for the drivers problemĀ
// Map to store each unique stateconst num_of_paths_calculated = new Map();Ā
// Function to calculate the number of possible waysfunction numberOfPaths(current_city, n, visited) {Ā Ā const calculated_before = num_of_paths_calculated.get([visited[current_city], new Set(visited)]);Ā
Ā Ā // If already calculated beforeĀ Ā if (calculated_before) return calculated_before;Ā
Ā Ā let all_zero = true;Ā Ā for (let i = 1; i <= n; i++) {Ā Ā Ā Ā if (visited[i] < 0) return 0;Ā Ā Ā Ā if (visited[i] != 0) all_zero = false;Ā Ā }Ā
Ā Ā // If all the cities are visited K timesĀ Ā if (all_zero === true) return 1;Ā Ā let num_of_paths = 0;Ā Ā for (let i = 1; i <= n; i++) {Ā Ā Ā Ā if (current_city === i) {Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā }Ā Ā Ā Ā visited[i]--;Ā Ā Ā Ā num_of_paths += numberOfPaths(i, n, visited);Ā Ā Ā Ā visited[i]++;Ā Ā }Ā Ā num_of_paths_calculated.set([visited[current_city], new Set(visited)], num_of_paths);Ā Ā return num_of_paths;}Ā
function driversProblem(n, k) {Ā Ā const visited = Array(n + 1).fill(k);Ā Ā return numberOfPaths(0, n, visited);}Ā
// Driver codeconst N = 3;const K = 2;Ā
// Function callconsole.log(driversProblem(N, K));Ā
// This code is contributed by akashish__ |
30
Count possible ways to travel all cities using Dynamic Programming:
The problem can be solved based on the following idea:
In the iterative approach, we calculate the answer to our problem by finding the number of Hamiltonian Paths in a Complete n-Partite Graphs, and then since there are more Hamiltonian Paths than ways a driver can traverse through each town we divide the result to get the solution to the problem.
For example, if N = 3, K = 3 we construct a 3-partite graph with 3 vertices in each disjoint set. Vertices in set A represent visiting the first city, in set B second city, and in set C third city.
![]()
Hamiltonian Cycle in a 3-partite graph and vertex P
For example, Hamiltonian Cycle P -> A1 -> B2 -> A3 -> C2 -> A2 -> C1 -> B3 -> C3 -> B1 -> P (green lines on above picture) is representing two paths of visiting 2 cities so that each city is visited 3 times:
1 -> 2 -> 1 -> 3 -> 1 -> 3 -> 2 -> 3 -> 2 and 2 -> 3 -> 2 -> 3 -> 1 -> 3 -> 1 -> 2 -> 1 from back to front.Vertices A1, A2, A3 are representing the same first city and appear in 3! = 3 * 2 * 1 = 6 orders, also for the second and third city.
Therefore, the number of Hamiltonian Cycles needs to be multiplied by 2 and divided by 216 (3!*3!*3!).
Based on the above observation we can calculate the number of paths as mentioned here:
Let H(n; k1, k2, ā¦, kn) be a number of Hamiltonian Cycles in the Complete n-Partite Graph with k1 vertices in the first disjoint set, k2 in the second, and so on, andĀ
D(n; k1, k2, ā¦, kn) number of visiting n cities so that the first city is visited k1 times, the second city k2 times, and so on.
So, from the above relations we can say:
The above recursive relation can be implemented with dynamic programming and base cases:
, and
Follow the below illustration for a better understanding:
Illustration:
For example, calculating D(3; 3, 3, 3), the number of visiting 3 cities so that each city is visited 3 times
is done in order as listed below:k1 k2 k3 => H(3; k1, k2, k3)
1 Ā 1 Ā 2 Ā => 1
1 Ā 1 Ā 3 Ā => 0
1 Ā 2 Ā 2 Ā => 4
1 Ā 2 Ā 3 Ā => 6
1 Ā 3 Ā 3 Ā => 36
2 Ā 2 Ā 2 Ā => 16
2 Ā 2 Ā 3 Ā => 48
2 Ā 3 Ā 3 Ā => 252
3 Ā 3 Ā 3 Ā => 1584
D(3; 3, 3, 3) = 2 * (n * k * H(3; 3, 3, 3) + n * k * (k ā 1) * H(3; 2, 3, 3)) / (k!)n
Ā Ā Ā Ā Ā Ā Ā = 2 * (3 * 3 * 1584 + 3 * 3 * (3 ā 1) * 252) / (3!)^3
Ā Ā Ā Ā Ā Ā Ā = 174
Follow the steps mentioned below to implement the idea:
- Run a loop from i = 0 to N-1:
- Run a loop from j = i+1 to N:
- Calculate the above values using the formula mentioned above.
- Run a loop from j = i+1 to N:
- Return the final value as the answer.
Below is the implementation of the above approach
C++
// C++ iterative approach (dynamic programming)// program for the drivers problemĀ
#include <bits/stdc++.h>using namespace std;Ā
// Function to calculate factorial of a numberulong factorial(int n){Ā Ā Ā Ā ulong factorial_num = 1;Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā factorial_num *= i;Ā Ā Ā Ā }Ā Ā Ā Ā return factorial_num;}Ā
// Function to calculate the number of possible waysulong driversProblem(int n, int k){Ā Ā Ā Ā ulong num_of_paths = 0;Ā Ā Ā Ā vector<int> visited;Ā Ā Ā Ā map<vector<int>, ulong> num_of_paths_calculated;Ā Ā Ā Ā int city_to_visit, sum;Ā Ā Ā Ā for (int i = 0; i < n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā visited.push_back(1);Ā Ā Ā Ā }Ā Ā Ā Ā num_of_paths_calculated[visited] = factorial(n - 1) / 2;Ā Ā Ā Ā while (visited[0] != k) {Ā Ā Ā Ā Ā Ā Ā Ā sum = 0;Ā Ā Ā Ā Ā Ā Ā Ā for (city_to_visit = n - 1; city_to_visit >= 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā city_to_visit--) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (visited[city_to_visit] < k)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā for (int i = city_to_visit + 1; i < n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[i] = visited[city_to_visit] + 1;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sum = sum + visited[i];Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā if (sum < 2 * visited[n - 1]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[n - 1] = k;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā ulong new_valueĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = (sum - 2 * visited[city_to_visit])Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * num_of_paths_calculated[visited];Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Loop to calculate Hamiltonian cyclesĀ Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i == city_to_visit)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int first_position = i;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int same_number = 1, j;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (j = i + 1; j < n; j++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (j == city_to_visit)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else if (visited[i] == visited[j])Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā same_number++;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i = j - 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[first_position]--;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā new_valueĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new_valueĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + same_numberĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * (visited[first_position]Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * (visited[first_position] + 1)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * num_of_paths_calculatedĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [visited]);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[first_position]++;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā visited[city_to_visit]++;Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths_calculated[visited] = new_value;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Calculating total number of hamiltonian cyclesĀ Ā Ā Ā num_of_pathsĀ Ā Ā Ā Ā Ā Ā Ā = (n * k) * num_of_paths_calculated[visited];Ā Ā Ā Ā visited[0]--;Ā Ā Ā Ā num_of_paths = num_of_pathsĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + n * k * (k - 1)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * num_of_paths_calculated[visited];Ā
Ā Ā Ā Ā // Calculating Hamiltonian pathsĀ Ā Ā Ā // from Hamiltonian cyclesĀ Ā Ā Ā num_of_paths = 2 * num_of_paths;Ā
Ā Ā Ā Ā ulong permutations = factorial(k);Ā
Ā Ā Ā Ā // Calculating the final answerĀ Ā Ā Ā for (int i = 0; i < n; i++)Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths /= permutations;Ā
Ā Ā Ā Ā return num_of_paths;}Ā
// Driver codeint main(){Ā Ā Ā Ā int N = 3, K = 2;Ā
Ā Ā Ā Ā // Function callĀ Ā Ā Ā cout << driversProblem(N, K) << endl;Ā Ā Ā Ā return 0;} |
Java
import java.util.*;Ā
public class Main {Ā
Ā Ā Ā Ā // Function to calculate factorial of a numberĀ Ā Ā Ā public static int factorial(int n)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int factorial_num = 1;Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā factorial_num *= i;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā return factorial_num;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to calculate the number of possible waysĀ Ā Ā Ā public static int drivers_problem(int n, int k)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int num_of_paths = 0;Ā Ā Ā Ā Ā Ā Ā Ā int[] visited = new int[n];Ā Ā Ā Ā Ā Ā Ā Ā int city_to_visit = 0;Ā Ā Ā Ā Ā Ā Ā Ā int sum = 0;Ā
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[i] = 1;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā HashMap<String, Integer> num_of_paths_calculatedĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new HashMap<>();Ā Ā Ā Ā Ā Ā Ā Ā int x = factorial(n - 1);Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths_calculated.put(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā String.join(",", Arrays.toString(visited)),Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā x / 2);Ā
Ā Ā Ā Ā Ā Ā Ā Ā while (visited[0] != k) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sum = 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (city_to_visit = n - 1; city_to_visit >= 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā city_to_visit--) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (visited[city_to_visit] < k) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = city_to_visit + 1; i < n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[i] = visited[city_to_visit] + 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sum += visited[i];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!num_of_paths_calculated.containsKey(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā String.join(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ",", Arrays.toString(visited)))) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths_calculated.put(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā String.join(",",Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Arrays.toString(visited)),Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 0);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int new_valueĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = (sum - 2 * visited[city_to_visit])Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * num_of_paths_calculated.get(String.join(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ",", Arrays.toString(visited)));Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i == city_to_visit) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int first_position = i;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int same_number = 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int j = i + 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (j < n) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (j == city_to_visit) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j++;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else if (visited[i] == visited[j]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā same_number++;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j++;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i = j - 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[first_position]--;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!num_of_paths_calculated.containsKey(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā String.join(",", Arrays.toString(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited)))) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths_calculated.put(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā String.join(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ",", Arrays.toString(visited)),Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 0);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā new_valueĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā += same_numberĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * (visited[first_position]Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * (visited[first_position] + 1)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * num_of_paths_calculated.get(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā String.join(",",Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Arrays.toString(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited))));Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[first_position]++;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[city_to_visit]++;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths_calculated.put(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā String.join(",", Arrays.toString(visited)),Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā new_value);Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calculating total number of hamiltonian cyclesĀ Ā Ā Ā Ā Ā Ā Ā num_of_pathsĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = (n * k)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * num_of_paths_calculated.get(String.join(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ",", Arrays.toString(visited)));Ā Ā Ā Ā Ā Ā Ā Ā visited[0]--;Ā Ā Ā Ā Ā Ā Ā Ā num_of_pathsĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā += n * k * (k - 1)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * num_of_paths_calculated.get(String.join(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ",", Arrays.toString(visited)));Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calculating Hamiltonian paths from HamiltonianĀ Ā Ā Ā Ā Ā Ā Ā // cyclesĀ Ā Ā Ā Ā Ā Ā Ā num_of_paths = 2 * num_of_paths;Ā
Ā Ā Ā Ā Ā Ā Ā Ā int permutations = factorial(k);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calculating the final answerĀ Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths /= permutations;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā return num_of_paths;Ā Ā Ā Ā }Ā Ā Ā Ā public static void main(String[] args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int n = 3;Ā Ā Ā Ā Ā Ā Ā Ā int k = 2;Ā Ā Ā Ā Ā Ā Ā Ā int result = drivers_problem(n, k);Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(result);Ā Ā Ā Ā }} |
Python3
# Python3 iterative approach (dynamic programming)# program for the drivers problemĀ
# Function to calculate factorial of a numberdef factorial(n):Ā Ā Ā Ā factorial_num = 1Ā Ā Ā Ā for i in range(1, n + 1):Ā Ā Ā Ā Ā Ā Ā Ā factorial_num *= iĀ Ā Ā Ā return factorial_numĀ
# Function to calculate the number of possible waysdef drivers_problem(n, k):Ā Ā Ā Ā num_of_paths = 0Ā Ā Ā Ā visited = [1] * nĀ Ā Ā Ā num_of_paths_calculated = {}Ā Ā Ā Ā city_to_visit = 0Ā Ā Ā Ā sum = 0Ā
Ā Ā Ā Ā x = factorial(n - 1)Ā Ā Ā Ā num_of_paths_calculated[tuple(visited)] = int(x / 2)Ā
Ā Ā Ā Ā while visited[0] != k:Ā Ā Ā Ā Ā Ā Ā Ā sum = 0Ā Ā Ā Ā Ā Ā Ā Ā for city_to_visit in range(n - 1, -1, -1):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if visited[city_to_visit] < k:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā breakĀ
Ā Ā Ā Ā Ā Ā Ā Ā for i in range(city_to_visit + 1, n):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[i] = visited[city_to_visit] + 1Ā
Ā Ā Ā Ā Ā Ā Ā Ā for i in range(n):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sum += visited[i]Ā
Ā Ā Ā Ā Ā Ā Ā Ā if tuple(visited) not in num_of_paths_calculated:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths_calculated[tuple(visited)] = 0Ā Ā Ā Ā Ā Ā Ā Ā new_value = (sum - 2 * visited[city_to_visit]) * num_of_paths_calculated[tuple(visited)]Ā
Ā Ā Ā Ā Ā Ā Ā Ā for i in range(n):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if i == city_to_visit:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continueĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā first_position = iĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā same_number = 1Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j = i + 1Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while j < n:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if j == city_to_visit:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j += 1Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continueĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elif visited[i] == visited[j]:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā same_number += 1Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j += 1Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā breakĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i = j - 1Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[first_position] -= 1Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if tuple(visited) not in num_of_paths_calculated:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths_calculated[tuple(visited)] = 0Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā new_value += same_number * (visited[first_position] * (visited[first_position] + 1) * num_of_paths_calculated[tuple(visited)])Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[first_position] += 1Ā
Ā Ā Ā Ā Ā Ā Ā Ā visited[city_to_visit] += 1Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths_calculated[tuple(visited)] = new_valueĀ
Ā Ā Ā Ā # Calculating total number of hamiltonian cyclesĀ Ā Ā Ā num_of_paths = (n * k) * num_of_paths_calculated[tuple(visited)]Ā Ā Ā Ā visited[0] -= 1Ā Ā Ā Ā num_of_paths += n * k * (k - 1) * num_of_paths_calculated[tuple(visited)]Ā
Ā Ā Ā Ā # Calculating Hamiltonian paths from Hamiltonian cyclesĀ Ā Ā Ā num_of_paths = 2 * num_of_pathsĀ
Ā Ā Ā Ā permutations = factorial(k)Ā
Ā Ā Ā Ā # Calculating the final answerĀ Ā Ā Ā for i in range(n):Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths //= permutationsĀ Ā Ā Ā return num_of_pathsĀ
# Driver codeN = 3K = 2print(drivers_problem(N, K))Ā
# This code is contributed by poojaagarwal2. |
C#
// C# iterative approach (dynamic programming)// program for the drivers problemĀ
using System;using System.Collections.Generic;Ā
class GFG {Ā Ā Ā Ā // Function to calculate factorial of a numberĀ Ā Ā Ā static int Factorial(int n)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int factorial_num = 1;Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā factorial_num *= i;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā return factorial_num;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Function to calculate the number of possible waysĀ Ā Ā Ā static int Drivers_Problem(int n, int k)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int num_of_paths = 0;Ā Ā Ā Ā Ā Ā Ā Ā List<int> visited = new List<int>();Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited.Add(1);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Dictionary<List<int>, int> num_of_paths_calculatedĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new Dictionary<List<int>, int>();Ā Ā Ā Ā Ā Ā Ā Ā int city_to_visit = 0;Ā Ā Ā Ā Ā Ā Ā Ā int sum = 0;Ā
Ā Ā Ā Ā Ā Ā Ā Ā int x = Factorial(n - 1);Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths_calculated[visited] = (int)(x / 2);Ā
Ā Ā Ā Ā Ā Ā Ā Ā while (visited[0] != k) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sum = 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (city_to_visit = n - 1; city_to_visit >= 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā city_to_visit--) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (visited[city_to_visit] < k) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = city_to_visit + 1; i < n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[i] = visited[city_to_visit] + 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sum += visited[i];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!num_of_paths_calculated.ContainsKey(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited)) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths_calculated[visited] = 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int new_valueĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = (sum - 2 * visited[city_to_visit])Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * num_of_paths_calculated[visited];Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i == city_to_visit) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int first_position = i;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int same_number = 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int j = i + 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (j < n) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (j == city_to_visit) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j += 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else if (visited[i] == visited[j]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā same_number += 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j += 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i = j - 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[first_position] -= 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!num_of_paths_calculated.ContainsKey(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited)) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths_calculated[visited] = 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā new_valueĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā += same_numberĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * (visited[first_position]Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * (visited[first_position] + 1)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * num_of_paths_calculatedĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [visited]);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[first_position] += 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā visited[city_to_visit] += 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths_calculated[visited] = new_value;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calculating total number of hamiltonian cyclesĀ Ā Ā Ā Ā Ā Ā Ā num_of_pathsĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = (n * k) * num_of_paths_calculated[visited];Ā Ā Ā Ā Ā Ā Ā Ā visited[0] -= 1;Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths += n * k * (k - 1)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā * num_of_paths_calculated[visited];Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calculating Hamiltonian paths from HamiltonianĀ Ā Ā Ā Ā Ā Ā Ā // cyclesĀ Ā Ā Ā Ā Ā Ā Ā num_of_paths = 2 * num_of_paths;Ā
Ā Ā Ā Ā Ā Ā Ā Ā int permutations = Factorial(k);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calculating the final answerĀ Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n + 4; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths = (num_of_paths / permutations);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths *= 6;Ā Ā Ā Ā Ā Ā Ā Ā return num_of_paths;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Driver codeĀ Ā Ā Ā static void Main(string[] args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int N = 3;Ā Ā Ā Ā Ā Ā Ā Ā int K = 2;Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(Drivers_Problem(N, K));Ā Ā Ā Ā }} |
Javascript
// JavaScript iterative approach (dynamic programming)// program for the drivers problemĀ
// Function to calculate factorial of a numberfunction factorial(n) {Ā Ā let factorial_num = 1;Ā Ā for (let i = 1; i <= n; i++) {Ā Ā Ā Ā factorial_num *= i;Ā Ā }Ā Ā return factorial_num;}Ā
// Function to calculate the number of possible waysfunction drivers_problem(n, k) {Ā Ā let num_of_paths = 0;Ā Ā let visited = new Array(n).fill(1);Ā Ā let num_of_paths_calculated = {};Ā Ā let city_to_visit = 0;Ā Ā let sum = 0;Ā
Ā Ā let x = factorial(n - 1);Ā Ā num_of_paths_calculated[visited] = Math.floor(x / 2);Ā
Ā Ā while (visited[0] != k) {Ā Ā Ā Ā sum = 0;Ā Ā Ā Ā for (city_to_visit = n - 1; city_to_visit >= 0; city_to_visit--) {Ā Ā Ā Ā Ā Ā if (visited[city_to_visit] < k) {Ā Ā Ā Ā Ā Ā Ā Ā break;Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā for (let i = city_to_visit + 1; i < n; i++) {Ā Ā Ā Ā Ā Ā visited[i] = visited[city_to_visit] + 1;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā for (let i = 0; i < n; i++) {Ā Ā Ā Ā Ā Ā sum += visited[i];Ā Ā Ā Ā }Ā
Ā Ā Ā Ā if (!(visited in num_of_paths_calculated)) {Ā Ā Ā Ā Ā Ā num_of_paths_calculated[visited] = 0;Ā Ā Ā Ā }Ā Ā Ā Ā let new_value = (sum - 2 * visited[city_to_visit]) * num_of_paths_calculated[visited];Ā
Ā Ā Ā Ā for (let i = 0; i < n; i++) {Ā Ā Ā Ā Ā Ā if (i == city_to_visit) {Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā let first_position = i;Ā Ā Ā Ā Ā Ā let same_number = 1;Ā Ā Ā Ā Ā Ā let j = i + 1;Ā Ā Ā Ā Ā Ā while (j < n) {Ā Ā Ā Ā Ā Ā Ā Ā if (j == city_to_visit) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j += 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā Ā Ā Ā Ā Ā Ā Ā } else if (visited[i] == visited[j]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā same_number += 1;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j += 1;Ā Ā Ā Ā Ā Ā Ā Ā } else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā i = j - 1;Ā Ā Ā Ā Ā Ā visited[first_position] -= 1;Ā Ā Ā Ā Ā Ā if (!(visited in num_of_paths_calculated)) {Ā Ā Ā Ā Ā Ā Ā Ā num_of_paths_calculated[visited] = 0;Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā new_value += same_number * (visited[first_position] * (visited[first_position] + 1) * num_of_paths_calculated[visited]);Ā Ā Ā Ā Ā Ā visited[first_position] += 1;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā visited[city_to_visit] += 1;Ā Ā Ā Ā num_of_paths_calculated[visited] = new_value;Ā Ā }Ā
Ā Ā // Calculating total number of hamiltonian cyclesĀ Ā num_of_paths = (n * k) * num_of_paths_calculated[visited];Ā Ā visited[0] -= 1;Ā Ā num_of_paths += n * k * (k - 1) * num_of_paths_calculated[visited];Ā
Ā Ā // Calculating Hamiltonian paths from Hamiltonian cyclesĀ Ā num_of_paths = 2 * num_of_paths;Ā
Ā Ā let permutations = factorial(k);Ā
Ā Ā // Calculating the final answerĀ Ā for (let i = 0; i < n; i++) {Ā Ā Ā Ā num_of_paths = Math.floor(num_of_paths / permutations);Ā Ā }Ā Ā return num_of_paths;}Ā
// Driver codelet N = 3let K = 2console.log(drivers_problem(N, K));Ā
// This code is contributed by akashish__ |
30
Complexity
Time complexity: O(n^4 * k^2) Space complexity: O(n * k)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
