Category: Data structures and algorithms
-
Understanding Stack Abstract Data Type (ADT)
Introduction to Stack ADT In computer science, a Stack is an abstract data type (ADT) that serves as a collection of elements, with two principal operations: A Stack follows the LIFO (Last In, First Out) principle. This means the last item inserted will be the first item removed. It is similar to a deck of…
-
Reversing the linked list in place (without using extra space)
The goal is to reverse the linked list such that: Approach 1: Reversing a Singly Linked List (SLL) Logic Given a singly linked list like this: We need to reverse it to: Steps to Reverse Initialize three pointers: Traverse the list: Once the loop completes, prev will point to the new head. Java Code to…
-
Finding the Middle Node of a Linked List in Java
In this tutorial, we will explore different approaches to find the middle node of a linked list in Java. This is a common coding interview question, and understanding its logic is essential for working with linked lists. Approach 1: Using Slow and Fast Pointers (Tortoise and Hare Algorithm) Logic This is the most common and…
-
Detecting Cycles in a Directed Graph
Introduction Cycle detection in directed graphs is an essential problem in computer science, with applications in various domains, such as financial trading, multithreading, and operating systems. Understanding how to detect cycles allows us to optimize systems, prevent deadlocks, and even identify arbitrage opportunities in financial markets. Applications of Cycle Detection Detecting Cycles in a Directed…
-
Finding shortest path with topological ordering
1. Introduction In this tutorial, we’ll discuss about finding the shortest path with topological ordering. Usually we use Dijkstra’s algorithm if we want to find the shortest path in an arbitrary graph G(V,E) graph. But if the graph is a directed acyclic graph, we can achieve linear running time to calculate the shortest path in…
-
Topological ordering
1. Introduction Topological ordering is a fundamental concept in graph theory and is particularly applied to Directed Acyclic Graphs (DAGs). The algorithm for obtaining a topological order was introduced by Robert Tarjan in 1976. Topological ordering(topological sort) of a G(V,E) directed graph is a linear ordering of its vertices such that for every directed (u,v)…
-
Maze problem
1. Problem description The problem is that we have to construct an algorithm that can find its way out of an NxN maze. We can represent this maze with an NxN matrix. The maze problem is a classic application of Depth First Search (DFS), where the objective is to find a path from a starting…
-
Depth First Search Algorithm
1. Introduction In this tutorial, we will explore another graph traversal technique: Depth First Search (DFS). This algorithm is designed to visit each vertex exactly once. Consider a graph with vertices A, B, C, D, E, and F, connected by multiple directed edges. DFS navigates through the graph by exploring as far as possible along…
-
Doubly linked list
Introduction In this tutorial, we are going to discuss Doubly Linked Lists (DLL), how they are structured, their advantages, disadvantages, and why they are often preferred over singly linked lists in certain situations. We will also look into Java’s built-in LinkedList class that uses a doubly linked list internally. What is a Doubly Linked List? A Doubly…
-
Linked list data structure
1. Introduction A Linked List is a linear data structure where elements (nodes) are stored in a sequence, and each node holds two components: the data and a reference to the next node. Unlike arrays, linked lists don’t need to be stored in contiguous memory locations, making insertions and deletions easier. In this tutorial, we…