{"id":15605720,"url":"https://github.com/gangelo/graph-algorithms","last_synced_at":"2025-03-30T10:40:16.159Z","repository":{"id":151197139,"uuid":"587745730","full_name":"gangelo/graph-algorithms","owner":"gangelo","description":"In Ruby","archived":false,"fork":false,"pushed_at":"2023-01-14T00:38:51.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-05T12:25:32.519Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","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/gangelo.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-11T13:43:31.000Z","updated_at":"2023-01-12T15:06:04.000Z","dependencies_parsed_at":"2023-04-23T21:54:50.863Z","dependency_job_id":null,"html_url":"https://github.com/gangelo/graph-algorithms","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gangelo%2Fgraph-algorithms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gangelo%2Fgraph-algorithms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gangelo%2Fgraph-algorithms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gangelo%2Fgraph-algorithms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gangelo","download_url":"https://codeload.github.com/gangelo/graph-algorithms/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246307932,"owners_count":20756478,"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":[],"created_at":"2024-10-03T04:12:09.884Z","updated_at":"2025-03-30T10:40:16.122Z","avatar_url":"https://github.com/gangelo.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Graph Algorithms\n\n## Keys/notes\n\nIn graph text representations in this document:\n`(x)` = node\n`-*` = edges, where `*` represents direction or \"arrowhead\".\n\n\n## References\n\n[Graph Algorithms for Technical Interviews - Full Course](https://youtu.be/tWVWeAqZ0WU)\n\n## Basics (high level)\n\n### Graph\n\nQ. What is a **graph**?\nA. A _graph_ is a collection of nodes + edges (i.e. graph = nodes + edges).\n\nA **graph** describes the relationship between \"things\". The **nodes** represent the \"things\" and the **edges** describe the \"relationship\" between the \"things\".\n\nTo put it another way, the **nodes** represent _cities_ and the **edges** represent the _roads_ _connecting_ the cities.\n\n### Nodes\nA **node** represents _data_ in a _graph_. Some use the term **vertex** in lieu of **node**; _vertext_ and _node_ are synonymous terms.\n\n#### Neighbor node\nA **neighbor node** is any node that is directly _accessible_ from any other node _through an edge_ (obeying any direction indicated).\n\nFor example, nodes `(b)` and `(c)` are _neighbor nodes_ to node `(a)` in the below _directed graph_ example; however, `(c)`'s only neighbor is `(e)` because of the given _direction_:\n```\n(a) -* (c)\n |      |\n *      *\n(b) *- (e)\n |\n *\n(d) *- (f)\n```\n\n### Edges\nAn **edge** is any connection between a pair of _nodes_.\n\nIn the following example, there is an \"edge\" between (a) and (b) and (b) and (c):\n\n`(a) -* (b) -* (c)`\n`(a) - (b) - (c)`\n\n### Graph types (2 types)\n#### Directed graph\nThink of the direction as a _1-way street_. From `(a)`, I can traverse to `(b)` and `(c)`; however, I cannot traverse from `(b)` back to `(a)`, and I cannot traverse from `(c)` back to `(a)`.\n##### Example\n```\n(a) -* (c)\n |      |\n *      *\n(b) *- (e)\n |\n *\n(d) *- (f)\n```\n#### Undirected graph\nThink of the direction as a _2-way street_. From `(c)`, I can traverse to `(a)` and `(e)`; from `(a)`, I can traverse to `(b)` and `(c)`.\n##### Example\n```\n(a) - (c)\n |     |\n(b) - (e)\n |\n(d) - (f)\n```\n\n## Traversing a graph\n\n### Two algorithums\n* Depth first\n* Breadth first\n\n#### Depth first algorithm\nUses a **_stack_**\n```\n(a) -* (c)\n |      |\n *      *\n(b) *- (e)\n |\n *\n(d) *- (f)\n```\n\nTraversal would proceed from a starting node (for example `(a)`, and traverse the nodes in the following order:\n`(a)` -* `(b)` -* `(d)`\n`(c)` -* `(e)` -* `(b)` -* `(d)`\n\nNotice how the traversal starting with `(c)` winds up traversing `(b)` -* `(d)` yet again; this is normal due to the nature of the algorithm.\nNotice also that node `(f)` is never reached; this also is normal due to the nature of the algorithm, and due to the lack of direction leading _to_ node `(f)`\n\n#### Breadth first algorithm\nMust use a **_queue_**\n```\n(a) -* (c)\n |      |\n *      *\n(b) *- (e)\n |\n *\n(d) *- (f)\n```\n\nTraversal would proceed from a starting node (for example `(a)`, and traverse the nodes in the following order:\n`(a)` -* `(b)` -* `(c)`\n`(b)` -* `(d)`\n`(c)` -* `(e)`\n`(d)` -* `(f)`\n`(e)` -* `(b)`\n`(b)` -* `(d)`\n\nNOTE: The order in which neighbor nodes are traversed (for example `(b)` and `(c)`) is insignificant.\n\n### What's the difference?\n\nThey are the same in that both algorithms traverse the _whole_ graph; however, the _order_ in which the graph is traversed differs.\n\n## Miscellaneous terms\n### Cycle\nA _cycle_ is any path through a set of nodes _**where I can end up where I once started.**_\n```\n# Notes (a), (b) and (c) are cyclic.\n(a) -* (c)\n |    *\n *  /\n(b)\n```\n### Cyclic\nWhen a graph is _cyclic_, we need to be concerned about _infinite loops_ when traversing the graph _**because there are one or more cycles present in the graph**_.\n\n### Acyclic\nWhen a graph is _acyclic_, we DO NOT need to be concerned about _infinite loops_ when traversing the graph _**because there are NO cycles present in the graph**_.\n\n## Calculating complexity - Big O\n### Using _multiple_ variables\nn = number of **nodes**.\ne = number of **edges**.\n\nTime: O(e)\nSpace: O(n)\n\n### Using a _single_ variable\n\nn = number of **nodes**.\nn2 = number of **edges**.\nWhere the \"squared\" number = the number of edges between nodes.\n\n#### Example\n```\n# Where *-* = 2 edges in each direction (i.e. \u003c-, -\u003e)\n(a) *-* (c)\n *      *\n |    /\n *  *\n(b)\n```\nTime: O(n2)\nSpace: O(n)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgangelo%2Fgraph-algorithms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgangelo%2Fgraph-algorithms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgangelo%2Fgraph-algorithms/lists"}