
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to count number of unique paths that includes given edges in Python
Suppose we have a list of edges in the form (u, v) and these are representing a tree. For each edge we have to find the total number of unique paths that includes said edge, in the same order as given in the input.
So, if the input is like edges = [[0, 1],[0, 2],[1, 3],[1, 4]]
then the output will be [6, 4, 4, 4].
To solve this, we will follow these steps −
adj := adjacency list from given edges
count := an empty map
Define a function dfs() . This will take x, parent
count[x] := 1
for each nb in adj[x], do
if nb is same as parent, then
come out from the loop
count[x] := count[x] + dfs(nb, x)
return count[x]
From the main method do the following −
dfs(0, -1)
ans := a new list
for each edge (a, b) in edges, do
x := minimum of count[a] and count[b]
insert (x *(count[0] - x)) at the end of ans
return ans
Example
Let us see the following implementation to get better understanding −
from collections import defaultdict class Solution: def solve(self, edges): adj = defaultdict(list) for a, b in edges: adj[a].append(b) adj[b].append(a) count = defaultdict(int) def dfs(x, parent): count[x] = 1 for nb in adj[x]: if nb == parent: continue count[x] += dfs(nb, x) return count[x] dfs(0, -1) ans = [] for a, b in edges: x = min(count[a], count[b]) ans.append(x * (count[0] - x)) return ans ob = Solution() edges = [ [0, 1], [0, 2], [1, 3], [1, 4] ] print(ob.solve(edges))
Input
[ [0, 1], [0, 2], [1, 3], [1, 4] ]
Output
[6, 4, 4, 4]
- Related Articles
- Program to count number of paths whose sum is k in python
- Unique Paths in Python
- Program to count number of paths with cost k from start to end point in Python
- Program to count number of sublists with exactly k unique elements in Python
- Program to count number of islands in a given matrix in Python
- Program to count number of unique palindromes we can make using string characters in Python
- Program to count number of unique subsequences of a string in C++
- Program to find total number of strings, that contains one unique characters in Python
- Program to count number of square submatrices in given binary matrix in Python
- C++ Program to find out the number of bridge edges in a given graph
- Python program to count number of vowels using set in a given string
- C++ program to perform unique factorization of a Given Number
- Write a program in Python to count the number of digits in a given number N
- Count number of edges in an undirected graph in C++
- Program to count number of points that lie on a line in Python
