-
-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented Disjoint Set, Stack and Queue Data Structures #54
Open
Ramy-Badr-Ahmed
wants to merge
18
commits into
TheAlgorithms:master
Choose a base branch
from
Ramy-Badr-Ahmed:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ramy-Badr-Ahmed
changed the title
Implemented Disjoint Set (Union-Find) with Union by Rank and Path Compressio
Implemented Disjoint Set and Stack Data Structure
Aug 25, 2024
Hi @RationalAsh Github Actions have passed in my forked version. Looking forward to your review 🙂 |
Ramy-Badr-Ahmed
changed the title
Implemented Disjoint Set and Stack Data Structure
Implemented Disjoint Set, Stack and Queue Data Structures
Sep 11, 2024
#55 is included |
…ments to my files in TheAlgorithms#54.
Ramy-Badr-Ahmed
commented
Sep 28, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR to introduces:
It includes support for both union by rank and path compression optimisations.
Implementation Details:
makeSet :: Int -> ST s (DisjointSet s)
: Initializes a new disjoint set with each node as its own parent and rank zero.findSet :: DisjointSet s -> Node -> ST s Node
: Finds the root of the set containing the given node with path compression.unionSet :: DisjointSet s -> Node -> Node -> ST s ()
: Unites the sets containing the two nodes using union by rank.example :: Int -> [(Node, Node)] -> [Node] -> [Node]
: Example function demonstrating how to use the disjoint set.test :: IO ()
: Basic test function to validate the implementation with sample data.Testing:
The implementation has been tested with the following unions and find operations:
[(0, 1), (1, 2), (3, 4), (4, 5), (6, 7), (8, 9), (0, 5), (6, 9)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 0, 0, 6, 6, 6, 6, 6, 6, 6]
The stack supports standard stack operations such as push, pop, and checking for emptiness.
Implementation Details:
newStack :: Int -> ST s (Stack s a)
: Initializes a new stack with a given capacity.push :: Stack s a -> a -> ST s ()
: Pushes an element onto the stack.pop :: Stack s a -> ST s (Maybe a)
: Pops an element from the stack. Returns Nothing if the stack is empty.isEmpty :: Stack s a -> ST s Bool
: Checks if the stack is empty.Testing:
A testing function that pushes a list of elements onto the stack, then pops them off, and checks if the stack was empty before and after operations.
testStack :: [a] -> ([Maybe a], Bool, Bool)
main :: IO ()
Input:
let input = [1, 2, 3, 4, 5]
Expected Output
[Just 5, Just 4, Just 3, Just 2, Just 1]
False
True
The queue supports typical queue operations such as enqueue, dequeue, and checking for emptiness.
Implementation Details:
newQueue :: Int -> ST s (Queue s a)
: Initializes a new queue with a given capacity.enqueue :: Queue s a -> a -> ST s ()
: Adds an element to the end of the queue.dequeue :: Queue s a -> ST s (Maybe a)
: Removes an element from the front of the queue. Returns Nothing if the queue is empty.isEmptyQueue :: Queue s a -> ST s Bool
: Checks if the queue is empty.Testing:
A testing function that enqueues a list of elements, then dequeues them, and checks if the queue was empty before and after operations.
testQueue :: [a] -> ([Maybe a], Bool, Bool)
main :: IO ()
: tests the queue implementationlet input = [1, 2, 3, 4, 5]
[Just 1, Just 2, Just 3, Just 4, Just 5]
False
True
Reference
Data Structures and Algorithms in C++, 2nd Edition