{"id":18637599,"url":"https://github.com/justin-marian/cost-graphs","last_synced_at":"2025-11-04T14:30:29.467Z","repository":{"id":226603335,"uuid":"769019257","full_name":"justin-marian/cost-graphs","owner":"justin-marian","description":"Shortest path algorithms for directed graphs with/without cycles, optimized for minimal costs and bounded edge values.","archived":false,"fork":false,"pushed_at":"2024-03-10T23:59:18.000Z","size":7179,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-27T09:12:25.435Z","etag":null,"topics":["cpp","cycle-detection","graphs-algorithms","shortest-path-algorithm"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/justin-marian.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-08T07:11:59.000Z","updated_at":"2024-03-08T14:56:05.000Z","dependencies_parsed_at":"2024-03-17T12:49:18.844Z","dependency_job_id":null,"html_url":"https://github.com/justin-marian/cost-graphs","commit_stats":null,"previous_names":["justin-marian/cost-graphs"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justin-marian%2Fcost-graphs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justin-marian%2Fcost-graphs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justin-marian%2Fcost-graphs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justin-marian%2Fcost-graphs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/justin-marian","download_url":"https://codeload.github.com/justin-marian/cost-graphs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239433910,"owners_count":19637806,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["cpp","cycle-detection","graphs-algorithms","shortest-path-algorithm"],"created_at":"2024-11-07T05:36:57.395Z","updated_at":"2025-11-04T14:30:29.418Z","avatar_url":"https://github.com/justin-marian.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [Cost graphs](#summary)\n\nAlgorithms for **cycle detectio**n and **finding single-source shortest paths** using different approaches such as **topological sorting** and the **Shortest Path Faster Algorithm (SPFA)**. Each algorithm is explained briefly, along with its time complexity and usage.\n\n## [Cycle Detection](https://en.wikipedia.org/wiki/Cycle_(graph_theory))\n\nThe `Cycle Detection` algorithm used in the provided code is based on **Depth-First Search (DFS)**. It explores the graph, tracking visited nodes and maintaining a stack of currently active nodes. If during the traversal, DFS encounters an already visited node in the stack, it indicates the presence of a cycle.\n\n- `DFS Traversal`: Initiates DFS from each unvisited vertex, exploring recursively by prioritizing depth over breadth.\n- `Visited Set`: Tracks visited vertices to prevent redundant exploration, ensuring each vertex is processed once.\n- `Stack`: Maintains a stack of vertices being explored, helping detect cycles by identifying back edges.\n\n## Single-Source Shortest Path\n\n### [Topological Sort](https://en.wikipedia.org/wiki/Topological_sorting)\n\nThe `Topological Sort` algorithm leverages topological sorting to relax edges in a specific order, ensuring that each vertex's shortest path is computed only after all its predecessors' shortest paths have been determined.\n\n- `Topological Sort`: The algorithm performs a topological sort of the graph, ensuring that vertices are ordered such that no edge points backward in the order.\n- `Edge Relaxation`: After obtaining the topological order, the algorithm iterates through each vertex in this order and relaxes its outgoing edges, updating shortest path distances accordingly.\n\n### [Shortest Path Faster Algorithm](https://en.wikipedia.org/wiki/Shortest_path_faster_algorithm)\n\nThe `Shortest Path Faster Algorithm (SPFA)` is a variation of the Bellman-Ford algorithm, designed to handle graphs with negative edge weights efficiently. SPFA maintains a queue of vertices to optimize relaxation steps, avoiding unnecessary relaxation iterations unless there's a change in the shortest path estimate.\n\n- `Queue`: SPFA uses a queue to keep track of vertices whose distances may need to be updated. This queue is populated during the relaxation process.\n- `Edge Relaxation with Optimization`: Unlike **Bellman-Ford**, SPFA **relaxes edges dynamically**. If the shortest path to a vertex is updated, it is added to the queue for further relaxation. This optimization **prevents redundant relaxation steps**.\n\n## Summary\n\n| Algorithm                                          | Time Complexity        | Space Complexity        | Description                                                                                                         |\n|:--------------------------------------------------:|:----------------------:|:-----------------------:|--------------------------------------------------------------------------------------------------------------------|\n| **Cycle Detection**                               | ***O(V + E)***             | ***O(V + E)***              | Detects cycles in the graph efficiently using Depth-First Search (DFS).                                             |\n| *Single-Source Shortest Path* **Topological Sort** | ***O(V + E)***          | ***O(V + E)***              | Computes shortest paths from a single source vertex in a directed acyclic graph (DAG) using topological sorting.   |\n| *Single-Source Shortest Path* **SPFA**             | ***O(E) (average)***   | ***O(V)***                  | Finds shortest paths from a single source vertex in a graph with negative edge weights efficiently using SPFA.     |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustin-marian%2Fcost-graphs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjustin-marian%2Fcost-graphs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustin-marian%2Fcost-graphs/lists"}