Found 33676 Articles for Programming

Python Program to Find if Undirected Graph contains Cycle using BFS

AmitDiwan
Updated on 17-Apr-2021 11:50:55

445 Views

When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Demofrom collections import deque def add_edge(adj: list, u, v):    adj[u].append(v)    adj[v].append(u) def detect_cycle(adj: list, s, V, visited: list):    parent = [-1] * ... Read More

Python Program to Find All Connected Components using BFS in an Undirected Graph

AmitDiwan
Updated on 17-Apr-2021 11:50:37

723 Views

When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Democlass Graph_structure:    def __init__(self, V):       self.V = V       self.adj = [[] for i in range(V)]    def DFS_Utility(self, temp, ... Read More

Python Program to Find All Nodes Reachable from a Node using BFS in a Graph

AmitDiwan
Updated on 17-Apr-2021 11:50:18

787 Views

When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Demofrom collections import deque def add_edge(v, w):    global visited_node, adj    adj[v].append(w)    adj[w].append(v) def BFS_operation(component_num, src):    global visited_node, adj    queue ... Read More

Python Program to Find the Sum of All Nodes in a Binary Tree

AmitDiwan
Updated on 17-Apr-2021 11:49:29

243 Views

When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Democlass Tree_struct:    def __init__(self, data=None):       self.key = data       self.children = []    def set_root(self, data):       self.key = ... Read More

Python Program to Display the Nodes of a Tree using BFS Traversal

AmitDiwan
Updated on 17-Apr-2021 11:49:49

900 Views

When it is required to display the nodes of a tree using the breadth first search traversal, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, perform ‘bfs’ (breadth first search) and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Democlass Tree_struct:    def __init__(self, data=None):       self.key = data       self.children = []    def set_root(self, data):       self.key = data   ... Read More

Python Program to Print All Permutations of a String in Lexicographic Order using Recursion

AmitDiwan
Updated on 16-Apr-2021 12:43:57

338 Views

When it is required to print all the permutations of a string in lexicographic order using recursion, a method is defined, that uses the ‘for’ loop to iterate over the sequence of elements, and use the ‘join’ method to join the elements.Below is the demonstration of the same −Example Live Demofrom math import factorial def lexicographic_permutation_order(s):    my_sequence = list(s)    for _ in range(factorial(len(my_sequence))):       print(''.join(my_sequence))       next = next_in_permutation(my_sequence)       if next is None:          my_sequence.reverse()       else:          my_sequence = next def ... Read More

Python Program to Print only Nodes in Left SubTree

AmitDiwan
Updated on 16-Apr-2021 12:41:26

277 Views

When it is required to print the nodes in the left subtree, a class can be created that consists of methods can be defined to set the root node, perform in order traversal, insert elements to the right of the root node, to the left of the root node, and so on. An instance of the class is created, and the methods can be used to perform the required operations.Below is a demonstration of the same −Example Live Democlass BinaryTree_struct:    def __init__(self, data=None):       self.key = data       self.left = None       self.right = ... Read More

Python Program to Print All Permutations of a String in Lexicographic Order without Recursion

AmitDiwan
Updated on 16-Apr-2021 12:38:58

459 Views

When it is required to print all the permutations of a string in the lexicographic order without using recursion, a method is defined, that takes the string as the parameter. It uses a simple ‘for’ loop to iterate over the string elements and uses ‘while’ condition to check for certain constraints.Below is the demonstration of the same −Example Live Demofrom math import factorial def lex_permutation(my_string): for i in range(factorial(len(my_string))):    print(''.join(my_string))    i = len(my_string) - 1    while i > 0 and my_string[i-1] > my_string[i]:       i -= 1    my_string[i:] = reversed(my_string[i:])    if i > 0: ... Read More

Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given String

AmitDiwan
Updated on 16-Apr-2021 12:38:18

722 Views

When it is required to form a new string that is made from the first two and last two characters of a given string, a counter can be defined, and indexing can be used to access specific range of elements.Below is the demonstration of the same −Example Live Demomy_string = "Hi there how are you" my_counter = 0 for i in my_string:    my_counter = my_counter + 1 new_string = my_string[0:2] + my_string [my_counter - 2: my_counter ] print("The string is ") print(my_string) print("The new string is ") print(new_string)OutputThe string is Hi there how are you The new ... Read More

Python Program to Create a Mirror Copy of a Tree and Display using BFS Traversal

AmitDiwan
Updated on 16-Apr-2021 12:34:24

665 Views

When it is required to create a mirror copy of a tree, and display it using breadth first search, a binary tree class is created with methods set the root element, insert element to left, insert element to right, search for a specific element, and perform post order traversal and so on. An instance of the class is created, and it can be used to access the methods.Below is the demonstration of the same −Example Live Democlass BinaryTree_struct:    def __init__(self, key=None):       self.key = key       self.left = None       self.right = None ... Read More

Advertisements