
- 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
Find if there is a path of more than k length from a source in Python
Suppose we have a graph, we also have a source vertex and a number k. The k is the path length of graph between source to destination, we have to check whether we can find a simple path (without cycle) starting from source and ending at any other vertex (as destination). The graph is shown in following −
So, if the input is like Source = 0, k = 64, then the output will be True as there exists a simple path 0 to 7 to 1 to 2 to 8 to 6 to 5 to 3 to 4, this path length has total distance of 68 which is more than 64.
To solve this, we will follow these steps −
Define graph using adjacency matrix adj of order nodes x nodes and fill with edge cost
Define a function solve() . This will take source, k, path
if k <= 0, then
return True
i := 0
while i is not same as size of adj[source], do
v := adj[source, i, 0]
w := adj[source, i, 1]
i := i + 1
if path[v] is True, then
go for next iteration
if w >= k, then
return True
path[v] := True
if solve(v, k-w, path) is true, then
return True
path[v] := False
return False
From the main method, do the following −
path := a list of size same as nodes, then fill with false values
path[source] := 1
return solve(source, k, path)
Example
Let us see the following implementation to get better understanding −
class Graph: def __init__(self, nodes): self.nodes = nodes self.adj = [[] for i in range(nodes)] def insert_edge(self,u, v, w): self.adj[u].append([v, w]) self.adj[v].append([u, w]) def solve(self,source, k, path): if (k <= 0): return True i = 0 while i != len(self.adj[source]): v = self.adj[source][i][0] w = self.adj[source][i][1] i += 1 if (path[v] == True): continue if (w >= k): return True path[v] = True if (self.solve(v, k-w, path)): return True path[v] = False return False def is_there_any_path(self,source, k): path = [False]*self.nodes path[source] = 1 return self.solve(source, k, path) nodes = 9 g = Graph(nodes) g.insert_edge(0, 1, 5) g.insert_edge(0, 7, 9) g.insert_edge(1, 2, 9) g.insert_edge(1, 7, 12) g.insert_edge(2, 3, 8) g.insert_edge(2, 8, 3) g.insert_edge(2, 5, 5) g.insert_edge(3, 4, 10) g.insert_edge(3, 5, 15) g.insert_edge(4, 5, 11) g.insert_edge(5, 6, 3) g.insert_edge(6, 7, 2) g.insert_edge(6, 8, 7) g.insert_edge(7, 8, 8) source = 0 k = 64 print(g.is_there_any_path(source, k))
Input
nodes = 9 g = Graph(nodes) g.insert_edge(0, 1, 5) g.insert_edge(0, 7, 9) g.insert_edge(1, 2, 9) g.insert_edge(1, 7, 12) g.insert_edge(2, 3, 8) g.insert_edge(2, 8, 3) g.insert_edge(2, 5, 5) g.insert_edge(3, 4, 10) g.insert_edge(3, 5, 15) g.insert_edge(4, 5, 11) g.insert_edge(5, 6, 3) g.insert_edge(6, 7, 2) g.insert_edge(6, 8, 7) g.insert_edge(7, 8, 8) source = 0 k = 64
Output
True
- Related Articles
- Find if there is a path of more than k length from a source in C++
- Find maximum path length in a binary matrix in Python
- Check if the frequency of any character is more than half the length of the string in Python
- Program to count k length substring that occurs more than once in the given string in Python
- Program to find length of longest matrix path length in Python
- Display substring in MySQL if the string is less than a specific length or display a custom message if it is more?
- Program to find length of longest alternating path of a binary tree in python
- Program to find length of longest consecutive path of a binary tree in python
- Convert the undirected graph into directed graph such that there is no path of length greater than 1 in C++
- Find length of the longest consecutive path from a given starting characters in C++
- Program to find array of length k from given array whose unfairness is minimum in python
- If the point $P (2, 2)$ is equidistant from the points $A (-2, k)$ and $B (-2k, -3)$, find $k$. Also, find the length of AP.
- Program to find maximum length of k ribbons of same length in Python
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in Python
- Program to find length of the longest path in a DAG without repeated nodes in Python
