Number of unique paths in tree such that every path has a value greater than K
Given a tree as a set of edges such that every node has a unique value. We are also given a value k, the task is to count the unique paths in the tree such that every path has a value greater than K. A path value is said to be > K if every edge contributing to the path is connecting two nodes both of which have values > K.
Examples:
Input:
Output: 9
Approach: The idea is to not form the tree with all the given edges. We only add an edge if it satisfies the condition of > k. In this case, a number of trees will be formed. While forming the different trees, we will only add the edge into the tree if both the node value are greater than K. After this, various numbers of trees will be created. Run a DFS for every node which in the end traverses the complete tree with which the node is attached and count the number of nodes in every tree. The number of unique paths for every tree which has X number of nodes is X * (X – 1) / 2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int dfs( int node, int parent, list< int >* adj, bool vis[])
{
int ans = 1;
vis[node] = true ;
for ( auto it : adj[node]) {
if (it != parent)
ans += dfs(it, node, adj, vis);
}
return ans;
}
int countPaths(list< int >* adj, int k, int maxn)
{
bool vis[maxn + 1];
int ans = 0;
memset (vis, false , sizeof vis);
for ( int i = 1; i <= maxn; i++) {
if (!vis[i]) {
int numNodes = dfs(i, 0, adj, vis);
ans += numNodes * (numNodes - 1) / 2;
}
}
return ans;
}
void addEdge(list< int >* adj, int u, int v, int k)
{
if (u > k && v > k) {
adj[u].push_back(v);
adj[v].push_back(u);
}
}
int main()
{
int maxn = 12;
list< int >* adj = new list< int >[maxn + 1];
int k = 3;
addEdge(adj, 2, 11, k);
addEdge(adj, 2, 6, k);
addEdge(adj, 5, 11, k);
addEdge(adj, 5, 10, k);
addEdge(adj, 5, 12, k);
addEdge(adj, 6, 7, k);
addEdge(adj, 6, 8, k);
cout << countPaths(adj, k, maxn);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int dfs( int node, int parent,
Vector<Integer>[] adj, boolean [] vis)
{
int ans = 1 ;
vis[node] = true ;
for (Integer it : adj[node])
{
if (it != parent)
ans += dfs(it, node, adj, vis);
}
return ans;
}
static int countPaths(Vector<Integer>[] adj,
int k, int maxn)
{
boolean [] vis = new boolean [maxn + 1 ];
int ans = 0 ;
Arrays.fill(vis, false );
for ( int i = 1 ; i <= maxn; i++)
{
if (!vis[i])
{
int numNodes = dfs(i, 0 , adj, vis);
ans += numNodes * (numNodes - 1 ) / 2 ;
}
}
return ans;
}
static void addEdge(Vector<Integer>[] adj,
int u, int v, int k)
{
if (u > k && v > k) {
adj[u].add(v);
adj[v].add(u);
}
}
public static void main(String[] args)
{
int maxn = 12 ;
@SuppressWarnings ( "unchecked" )
Vector<Integer>[] adj = new Vector[maxn + 1 ];
for ( int i = 0 ; i < maxn + 1 ; i++)
{
adj[i] = new Vector<>();
}
int k = 3 ;
addEdge(adj, 2 , 11 , k);
addEdge(adj, 2 , 6 , k);
addEdge(adj, 5 , 11 , k);
addEdge(adj, 5 , 10 , k);
addEdge(adj, 5 , 12 , k);
addEdge(adj, 6 , 7 , k);
addEdge(adj, 6 , 8 , k);
System.out.println(countPaths(adj, k, maxn));
}
}
|
Python3
def dfs(node, parent, adj, vis):
ans = 1
vis[node] = True
for it in adj[node]:
if it ! = parent:
ans + = dfs(it, node, adj, vis)
return ans
def countPaths(adj, k, maxn):
vis = [ False ] * (maxn + 1 )
ans = 0
for i in range ( 1 , maxn + 1 ):
if not vis[i]:
numNodes = dfs(i, 0 , adj, vis)
ans + = numNodes * (numNodes - 1 ) / / 2
return ans
def addEdge(adj, u, v, k):
if u > k and v > k:
adj[u].append(v)
adj[v].append(u)
if __name__ = = "__main__" :
maxn = 12
adj = [[] for i in range (maxn + 1 )]
k = 3
addEdge(adj, 2 , 11 , k)
addEdge(adj, 2 , 6 , k)
addEdge(adj, 5 , 11 , k)
addEdge(adj, 5 , 10 , k)
addEdge(adj, 5 , 12 , k)
addEdge(adj, 6 , 7 , k)
addEdge(adj, 6 , 8 , k)
print (countPaths(adj, k, maxn))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int dfs( int node, int parent,
List< int >[] adj, bool [] vis)
{
int ans = 1;
vis[node] = true ;
foreach ( int it in adj[node])
{
if (it != parent)
ans += dfs(it, node, adj, vis);
}
return ans;
}
static int countPaths(List< int >[] adj,
int k, int maxn)
{
bool [] vis = new bool [maxn + 1];
int ans = 0;
for ( int i = 1; i <= maxn; i++)
{
if (!vis[i])
{
int numNodes = dfs(i, 0, adj, vis);
ans += numNodes * (numNodes - 1) / 2;
}
}
return ans;
}
static void addEdge(List< int >[] adj,
int u, int v, int k)
{
if (u > k && v > k)
{
adj[u].Add(v);
adj[v].Add(u);
}
}
public static void Main(String[] args)
{
int maxn = 12;
List< int >[] adj = new List< int >[maxn + 1];
for ( int i = 0; i < maxn + 1; i++)
{
adj[i] = new List< int >();
}
int k = 3;
addEdge(adj, 2, 11, k);
addEdge(adj, 2, 6, k);
addEdge(adj, 5, 11, k);
addEdge(adj, 5, 10, k);
addEdge(adj, 5, 12, k);
addEdge(adj, 6, 7, k);
addEdge(adj, 6, 8, k);
Console.WriteLine(countPaths(adj, k, maxn));
}
}
|
Javascript
<script>
function dfs(node, parent, adj, vis)
{
let ans = 1;
vis[node] = true ;
for (let it = 0; it < adj[node].length; it++)
{
if (adj[node][it] != parent)
ans += dfs(adj[node][it], node, adj, vis);
}
return ans;
}
function countPaths(adj, k, maxn)
{
let vis = new Array(maxn + 1);
let ans = 0;
vis.fill( false );
for (let i = 1; i <= maxn; i++)
{
if (!vis[i])
{
let numNodes = dfs(i, 0, adj, vis);
ans += numNodes * (numNodes - 1) / 2;
}
}
return ans;
}
function addEdge(adj, u, v, k)
{
if (u > k && v > k) {
adj[u].push(v);
adj[v].push(u);
}
}
let maxn = 12;
let adj = new Array(maxn + 1);
for (let i = 0; i < maxn + 1; i++)
{
adj[i] = [];
}
let k = 3;
addEdge(adj, 2, 11, k);
addEdge(adj, 2, 6, k);
addEdge(adj, 5, 11, k);
addEdge(adj, 5, 10, k);
addEdge(adj, 5, 12, k);
addEdge(adj, 6, 7, k);
addEdge(adj, 6, 8, k);
document.write(countPaths(adj, k, maxn));
</script>
|
Time Complexity: O(N), as we are using recursion to traverse N times. Where N is the total number of nodes.
Auxiliary Space: O(N), as we are using extra space for the visited array. Where N is the total number of nodes.
Last Updated :
16 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...