{"id":18244182,"url":"https://github.com/nouraalgohary/algorithms","last_synced_at":"2026-04-16T00:31:26.244Z","repository":{"id":119996120,"uuid":"591212228","full_name":"NouraAlgohary/Algorithms","owner":"NouraAlgohary","description":"An easy-to-understand description of algorithms, simple illustrations, pseudo code, and examples.","archived":false,"fork":false,"pushed_at":"2023-11-11T09:53:43.000Z","size":114,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-06T19:01:46.307Z","etag":null,"topics":["algorithms","cpp","leetcode","python"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NouraAlgohary.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-01-20T07:32:39.000Z","updated_at":"2025-03-11T10:11:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"ff4d21f3-eaef-4983-8f3d-e16ce22cddba","html_url":"https://github.com/NouraAlgohary/Algorithms","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/NouraAlgohary/Algorithms","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NouraAlgohary%2FAlgorithms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NouraAlgohary%2FAlgorithms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NouraAlgohary%2FAlgorithms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NouraAlgohary%2FAlgorithms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NouraAlgohary","download_url":"https://codeload.github.com/NouraAlgohary/Algorithms/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NouraAlgohary%2FAlgorithms/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31866228,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-15T15:24:51.572Z","status":"ssl_error","status_checked_at":"2026-04-15T15:24:39.138Z","response_time":63,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithms","cpp","leetcode","python"],"created_at":"2024-11-05T09:15:42.253Z","updated_at":"2026-04-16T00:31:25.723Z","avatar_url":"https://github.com/NouraAlgohary.png","language":"C++","readme":"# Algorithms\n| No.  | Algorithm | \n| --| ------------- |\n| 1 | [Brute Force](#brute-force)   |\n| 2 | [Backtracking](#backtracking) |\n| 3 | Kadane's Algorithm |\n| 4 | [Depth First Search (DFS)](#depth-first-search) |\n| 5 | [Breadth First Search (BFS)](#breadth-first-search) |\n| 6 | [Sliding Window](#sliding-window) |\n| 7 | [Dijkstra Algorithm](#Dijkstra-Algorithm) |\n| 8 | Floyd Warshall Algoithm |\n| 9 | Bellman Ford Algorithm |\n| 10 | [Binary Search Algorithm](#binary-search-algorithm) |\n| 11 | [Floyd's Cycle Detection Algorithm (Hare-Tortoise Algorithm)](#hare-tortoise-algorithm) |\n| 12 | [Union-Find](#union-find-algorithm)  |\n\n## Brute Force \nIt goes through all possible choices until the solution is found \n\n#### Template\n```\nFor each element in the array\n  if element equal target value then\n    print success message\n    return its index\n  if element is not found \n    print Value not found message\n    return -1\n```\nReturn all choices\n```\nFor each element in the searchList\n  if element equal target value then\n    Add its index to a list of occurrences\nif the list of occurrences is empty\n  raise ValueError\notherwise\n  return the list occurrences\n```\n#### Examples\n\n#### Read about Brute Force \n- [Codeacademy](https://www.codecademy.com/learn/learn-data-structures-and-algorithms-with-python/modules/brute-force-algorithms/cheatsheet)\n- [GeeksforGeeks](https://www.geeksforgeeks.org/brute-force-approach-and-its-pros-and-cons/)\n\n## Backtracking\nBacktracking is used when searching for every possible combinations. It's **check the subsequences/combinations/permutations of some group of letters/numbers**.\nBased on Recursion.\n\n\u003e Conceptually, one can imagine the procedure of backtracking as the tree traversal. Starting from the root node, one sets out to search for solutions that are located at the leaf nodes. Each intermediate node represents a partial candidate solution that could potentially lead us to a final valid solution. At each node, we would fan out to move one step further to the final solution, i.e. we iterate the child nodes of the current node. Once we can determine if a certain node cannot possibly lead to a valid solution, we abandon the current node and backtrack to its parent node to explore other possibilities.\n\n#### Template\n```\ndef backtrack(candidate):\n    if find_solution(candidate):\n        output(candidate)\n        return\n    \n    # iterate all possible candidates.\n    for next_candidate in list_of_candidates:\n        if is_valid(next_candidate):\n            # try this partial candidate solution\n            place(next_candidate)\n            # given the candidate, explore further.\n            backtrack(next_candidate)\n            # backtrack\n            remove(next_candidate)\n```\n\n#### Examples\n- [NQueens (Number of solutions).cpp](https://github.com/NouraAlgohary/Algorithms/tree/main/Backtracking)\n- [Palindrome Partitioning (Number of palindrome).cpp](https://github.com/NouraAlgohary/Algorithms/blob/main/Backtracking/Palindrome%20Partitioning.cpp)\n- [Palindrome Partitioning (Number of palindrome).py](https://github.com/NouraAlgohary/Algorithms/blob/main/Backtracking/Palindrome%20Partitioning.py)\n\n#### Read about Backtracking\n- [LeetCode](https://leetcode.com/explore/featured/card/recursion-ii/472/backtracking/2654/)\n- [GeeksforGeeks](https://www.geeksforgeeks.org/backtracking-algorithms/)\n\n\n## Kadane's Algorithm\n\n## Depth First Search\nThe DFS algorithm works as follows:\n1. Start by putting any one of the graph's vertices on top of a stack.\n2. Take the top item of the stack and add it to the visited list.\n3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to the top of the stack.\n4. Keep repeating steps 2 and 3 until the stack is empty.\u003cbr/\u003e\nNote: A graph can have more than one DFS traversal.\n![dfs](https://user-images.githubusercontent.com/103903785/218292569-336bba74-eff9-4572-a02d-8414481287c6.png)\n\n#### Template\n```\nInitialize an empty stack for storage of nodes, S.\nFor each vertex u, define u.visited to be false.\nPush the root (first node to be visited) onto S.\nWhile S is not empty:\n    Pop the first element in S, u.\n    If u.visited = false, then:\n        U.visited = true\n        for each unvisited neighbor w of u:\n            Push w into S.\nEnd process when all nodes have been visited.\n```\n#### Examples\n- [Minimum Fuel Cost to Report to the Capital](https://github.com/NouraAlgohary/Algorithms/tree/main/Depth%20First%20Search%20(DFS)/Minimum%20Fuel%20Cost%20to%20Report%20to%20the%20Capital)\n\n#### Problems\n\n#### Read about DFS\n- [LeetCode - A Beginners guid to BFS and DFS](https://leetcode.com/discuss/study-guide/1072548/A-Beginners-guid-to-BFS-and-DFS)\n- [GeeksforGeeks](https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/)\n- [Briliant](https://brilliant.org/wiki/depth-first-search-dfs/)\n\n## Breadth First Search\nDFS is used for **traverse or searsh** in a tree or graph. Starting from the root node (level 0) then visiting all the nodes at the next level (level 1) before moving to the next level (level 2)\n\n![BFS](https://user-images.githubusercontent.com/103903785/214479205-a14c2dbd-e3b7-445e-86bf-ea101fb9a654.png)\n\n#### Template\n```\nBFS(graph, source_node):                 \n      Define a Queue q\n      q.enqueue(source_node)    # inserting s in queue until all its neighbour vertices are marked.\n\n      mark source_node as visited.\n      while (q is not empty)\n           # removing that vertex from queue, whose neighbour will be visited now\n           node  =  q.dequeue( )\n\n          # processing all the neighbours of the node\n          for all neighbours neighbour of node in graph\n               if neighbour is not visited \n                        q.enqueue(neighbour)             # Stores w in Q to further visit its neighbour\n                        mark neighbour as visited.\n```\n\n#### Examples\n- [Same Tree.cpp](https://github.com/NouraAlgohary/Algorithms/blob/main/Breadth%20First%20Search%20(BFS)/Same%20Tree.cpp_)\n- [Same Tree.py](https://github.com/NouraAlgohary/Algorithms/blob/main/Breadth%20First%20Search%20(BFS)/Same%20Tree.py)\n\n#### Problems\n- [LeetCode](https://leetcode.com/tag/breadth-first-search/)\n\n#### Read about Backtracking\n- [LeetCode - A Beginners guid to BFS and DFS](https://leetcode.com/discuss/study-guide/1072548/A-Beginners-guid-to-BFS-and-DFS) \n- [GeeksforGeeks](https://www.geeksforgeeks.org/breadth-first-search-or-bfs-for-a-graph/)\n\n## Sliding Window \nIt's used for optimizing loops. \n\n#### Template\n```\n\n```\n#### Examples\n- [Maximum Number of Vowels in a Substring of Given Length(cpp)](https://github.com/NouraAlgohary/Algorithms/blob/main/Sliding%20Window/Maximum%20Number%20of%20Vowels%20in%20a%20Substring%20of%20Given%20Length(cpp).cpp)\n- [Maximum Number of Vowels in a Substring of Given Length(py)](https://github.com/NouraAlgohary/Algorithms/blob/main/Sliding%20Window/Maximum%20Number%20of%20Vowels%20in%20a%20Substring%20of%20Given%20Length(py).py)\n\n#### Read about Brute Force \n- [Medium](https://itnext.io/sliding-window-algorithm-technique-6001d5fbe8b3)\n- [GeeksforGeeks](https://www.geeksforgeeks.org/window-sliding-technique/)\n\n## Dijkstra Algorithm\nAn algorithm that is used to find the shortest path between two edges/nodes having non-negative edge values.\n\n#### Template\n```\nfunction dijkstra(G, S)\n    for each vertex V in G\n        distance[V] \u003c- infinite\n        previous[V] \u003c- NULL\n        If V != S, add V to Priority Queue Q\n    distance[S] \u003c- 0\n\t\n    while Q IS NOT EMPTY\n        U \u003c- Extract MIN from Q\n        for each unvisited neighbour V of U\n            tempDistance \u003c- distance[U] + edge_weight(U, V)\n            if tempDistance \u003c distance[V]\n                distance[V] \u003c- tempDistance\n                previous[V] \u003c- U\n    return distance[], previous[]\n```\n\n#### Examples\n\n#### Problems\n\n#### Read about Dijkstra Algorithm\n- [GeeksforGeeks](https://www.geeksforgeeks.org/introduction-to-dijkstras-shortest-path-algorithm/)\n\n## Floyd Warshall Algoithm\n\n## Bellman Ford Algorithm\n\n## Binary Search Algorithm \nIt's used to search for an element in a sorted array\n\nEx1 \n![Binary Search 1](https://user-images.githubusercontent.com/103903785/228853053-d6eb3551-dd7b-422a-8877-1e95fa55f1b3.png)\nEx2\n![Binary Search 2](https://user-images.githubusercontent.com/103903785/228853033-786ec4d7-ffca-44ac-b23c-ed6391eb48d4.png)\n\n\n#### Template\n1. Iteration Method\n```\n    binarySearch(arr, x, low, high)\n        repeat till low = high\n               mid = (low + high)/2\n                   if (x == arr[mid])\n                   return mid\n   \n                   else if (x \u003e arr[mid]) // x is on the right side\n                       low = mid + 1\n   \n                   else                  // x is on the left side\n                       high = mid - 1\n```\n2. Recursive Method (Divide and Conquer approach)\n\n```\n    binarySearch(arr, x, low, high)\n           if low \u003e high\n               return False \n   \n           else\n               mid = (low + high) / 2 \n                   if x == arr[mid]\n                   return mid\n       \n               else if x \u003e arr[mid]        // x is on the right side\n                   return binarySearch(arr, x, mid + 1, high)\n               \n               else                        // x is on the left side\n                   return binarySearch(arr, x, low, mid - 1) \n```\n\n#### Examples\n- [Binary Search.cpp](https://github.com/NouraAlgohary/Algorithms/blob/main/Binary%20Search/Binary%20Search.cpp_)\n- [Binary Search.py](https://github.com/NouraAlgohary/Algorithms/blob/main/Binary%20Search/Binary%20Search.py)\n\n#### Read about Binary Search\n[Geeks for Geeks](https://www.geeksforgeeks.org/binary-search/)\n\n## Hare-Tortoise Algorithm\nUsing two pointers(Hare \u0026 Tortoise) to traverse the sequence(Linked List) at differernt speeds\n- Hare will reach the tail of the linked list(null), which means that there is no cycle in it.\n- Hare will meet tortoise, which means that there is a cycle\n\n![Hare-Tortoise Algorithm](https://user-images.githubusercontent.com/103903785/228295688-0ca0cdaa-c11f-42ca-893f-ebc95143f8ff.png)\n\n#### Pseudocode\n1) Initialize two-pointers and start traversing the linked list.\n2) Move the slow pointer by one position.\n3) Move the fast pointer by two positions.\n4) If both pointers meet at some point then a loop exists and if the fast pointer meets the end position then no loop exists.\n\n#### Examples\n- [Linked List Cycle](https://github.com/NouraAlgohary/Algorithms/tree/main/Floyd%E2%80%99s%20Cycle%20Detection%20Algorithm)\n\n#### Read about Floyd’s Cycle-Finding\n- [Geeks for Geeks](https://www.geeksforgeeks.org/floyds-cycle-finding-algorithm/)\n- [Code Studio](https://www.codingninjas.com/codestudio/library/floyds-cycle-finding-algorithm)\n\n\n## Union-Find Algorithm\nIt's used to Find subsets of elements and Union similar subsets together.\n\n#### Template\n```\nInitially create a parent[] array to keep track of the subsets.\nTraverse through all the edges:\nCheck to which subset each of the nodes belong to by finding the parent[] array till the node and the parent are the same.\nIf the two nodes belong to the same subset then they belong to a cycle.\nOtherwise, perform union operation on those two subsets.\nIf no cycle is found, return false.\n```\n\n#### Pseudocode\n```\nfunc find( var element )\n  while ( element is not the root ) element = element's parent\n  return element\nend func\n\nfunc union( var setA, var setB )\n  var rootA = find( setA ), rootB = find( setB )\n  if ( rootA is equal to rootB ) return\n  else\n     set rootB as rootA's parent\nend func\n```\n\n#### Examples\n\n#### Resources\n- [Geeks for Geeks](https://www.geeksforgeeks.org/introduction-to-disjoint-set-data-structure-or-union-find-algorithm/)\n- [Algorithmist](https://algorithmist.com/wiki/Union_find)\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnouraalgohary%2Falgorithms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnouraalgohary%2Falgorithms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnouraalgohary%2Falgorithms/lists"}