Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 22 of 2547
Python Program to solve Maximum Subarray Problem using Kadane's Algorithm
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 MorePython Program to Find if Undirected Graph contains Cycle using BFS
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 MorePython Program to Find All Connected Components using BFS in an Undirected Graph
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 MorePython Program to Display the Nodes of a Tree using BFS Traversal
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 MorePython Program to Print only Nodes in Left SubTree
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 MorePython Program to Find the Largest value in a Tree using Inorder Traversal
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 MorePython Program to Remove the Characters of Odd Index Values in a String
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 MorePython Program for Depth First Binary Tree Search using Recursion
Depth First Search (DFS) is a tree traversal algorithm that explores nodes by going as deep as possible before backtracking. In binary trees, DFS can be implemented recursively by visiting the current node, then traversing the left subtree, and finally the right subtree. Binary Tree Class Structure First, let's define a binary tree class with methods for insertion and DFS traversal ? class BinaryTree_struct: def __init__(self, key=None): self.key = key self.left = None ...
Read MorePython Program to Sort using a Binary Search Tree
A Binary Search Tree (BST) is a tree data structure where the left subtree contains values less than the root, and the right subtree contains values greater than or equal to the root. The inorder traversal of a BST gives elements in sorted order, making it useful for sorting. How BST Sorting Works BST sorting involves two main steps: Insertion: Add elements to the BST maintaining the BST property Inorder Traversal: Visit nodes in left-root-right order to get sorted sequence Implementation We'll create two classes: BinSearchTreeNode for individual nodes and BinSearchTree for the ...
Read MoreVertical Concatenation in Matrix in Python
Vertical concatenation in Python matrix operations involves combining elements from corresponding positions across different rows. This creates new strings by joining elements column-wise rather than row-wise. Using zip_longest() for Vertical Concatenation The zip_longest() function from the itertools module handles matrices with unequal row lengths by filling missing values ? from itertools import zip_longest matrix = [["Hi", "Rob"], ["how", "are"], ["you"]] print("The matrix is:") print(matrix) result = ["".join(elem) for elem in zip_longest(*matrix, fillvalue="")] print("The matrix after vertical concatenation:") print(result) The matrix is: [['Hi', 'Rob'], ['how', 'are'], ['you']] The matrix after ...
Read More