Articles on Trending Technologies

Technical articles with clear explanations and examples

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

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 2K+ Views

When working with undirected graphs, connected components are subsets of vertices where each vertex is reachable from every other vertex within the same component. We can find all connected components using Depth-First Search (DFS) traversal. Below is a demonstration of finding connected components using a graph class ? Graph Class Implementation First, let's create a graph class with methods for DFS traversal and finding connected components ? class Graph_struct: def __init__(self, V): self.V = V ...

Read More

Python Program to Calculate the Length of a String Without Using a Library Function

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 3K+ Views

When it is required to calculate the length of a string without using library methods, a counter is used to increment every time an element of the string is encountered. Below is the demonstration of the same ? Example my_string = "Hi Will" print("The string is :") print(my_string) my_counter = 0 for i in my_string: my_counter = my_counter + 1 print("The length of the string is") print(my_counter) Output The string is : Hi Will The length of the string is 7 Using While Loop ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 373 Views

When working with binary trees, finding the sum of all nodes is a common operation. We can implement this using a tree class that contains methods to build the tree and calculate the sum recursively. Each node stores data and maintains references to its children. Below is a demonstration of the same − Tree Class Implementation First, let's create a tree structure class with methods for adding nodes and calculating the sum ? class Tree_struct: def __init__(self, data=None): self.key = data ...

Read More

Python Program to solve Maximum Subarray Problem using Kadane's Algorithm

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 516 Views

The Maximum Subarray Problem finds the contiguous subarray within a one-dimensional array of numbers that has the largest sum. Kadane's Algorithm solves this problem efficiently in O(n) time complexity using dynamic programming principles. Understanding Kadane's Algorithm Kadane's algorithm maintains two variables: the maximum sum ending at the current position and the overall maximum sum seen so far. At each position, it decides whether to extend the existing subarray or start a new one. Basic Implementation Here's a simple version that returns only the maximum sum ? def kadane_simple(arr): max_ending_here = ...

Read More

Python Program to Find if Undirected Graph contains Cycle using BFS

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 589 Views

A cycle in an undirected graph occurs when you can start at a vertex and return to it by following edges without retracing the same edge. We can detect cycles using Breadth-First Search (BFS) by tracking parent nodes during traversal. How BFS Cycle Detection Works The algorithm uses a queue to traverse the graph level by level. For each visited vertex, we track its parent. If we encounter a visited vertex that is not the current vertex's parent, we've found a cycle. Implementation Helper Functions First, let's define a function to add edges to our ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 883 Views

Connected components in an undirected graph are maximal sets of vertices where every vertex is reachable from every other vertex in the same component. This article demonstrates finding all connected components using BFS (Breadth-First Search) traversal. Understanding Connected Components A connected component is a subgraph where: Every vertex can reach every other vertex in the component No vertex in the component can reach vertices outside it The component cannot be extended by adding more vertices BFS Implementation for Connected Components Here's how to find all connected components using BFS traversal ? ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 1K+ 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 ? Tree Structure Class First, let's create a simplified version that demonstrates the core BFS concept ? class Tree_struct: def __init__(self, data=None): ...

Read More

Python Program to Print only Nodes in Left SubTree

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 389 Views

When it is required to print the nodes in the left subtree, a binary tree class can be created with methods to perform operations like setting the root node, inserting elements, and traversing the tree. This program demonstrates how to build a binary tree and display only the nodes in the left subtree. A binary tree is a hierarchical data structure where each node has at most two children: a left child and a right child. The left subtree contains all nodes that are descendants of the left child of the root node. Binary Tree Implementation ...

Read More

Python Program to Find the Largest value in a Tree using Inorder Traversal

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 326 Views

Finding the largest value in a binary tree using inorder traversal is a common tree operation. This approach visits nodes in left-root-right order while tracking the maximum value encountered during traversal. Below is a complete implementation using a binary tree class with methods for tree construction and largest value finding ? Complete Implementation class BinaryTree: def __init__(self, key=None): self.key = key self.left = None self.right = None ...

Read More

Python Program to Remove the Characters of Odd Index Values in a String

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 828 Views

When working with strings in Python, you might need to remove characters at odd index positions (1, 3, 5, etc.). This can be accomplished using various approaches including loops, string slicing, and list comprehensions. Understanding String Indexing In Python, string indexing starts from 0. So for a string "Hello": Index 0: 'H' (even) Index 1: 'e' (odd) Index 2: 'l' (even) Index 3: 'l' (odd) Index 4: 'o' (even) Method 1: Using While Loop This approach iterates through the string and skips characters at odd indices ? def remove_odd_index_characters(my_str): ...

Read More
Showing 4921–4930 of 61,299 articles
« Prev 1 491 492 493 494 495 6130 Next »
Advertisements