{"id":18470082,"url":"https://github.com/greyhatguy007/graphs","last_synced_at":"2025-07-01T04:08:40.996Z","repository":{"id":40502715,"uuid":"507581598","full_name":"greyhatguy007/graphs","owner":"greyhatguy007","description":"Everything you need to know about the data structure - GRAPHS","archived":false,"fork":false,"pushed_at":"2022-06-28T15:02:43.000Z","size":124,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-16T01:48:57.126Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/greyhatguy007.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}},"created_at":"2022-06-26T13:27:19.000Z","updated_at":"2025-03-25T11:31:14.000Z","dependencies_parsed_at":"2022-09-20T07:50:59.819Z","dependency_job_id":null,"html_url":"https://github.com/greyhatguy007/graphs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/greyhatguy007/graphs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greyhatguy007%2Fgraphs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greyhatguy007%2Fgraphs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greyhatguy007%2Fgraphs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greyhatguy007%2Fgraphs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/greyhatguy007","download_url":"https://codeload.github.com/greyhatguy007/graphs/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greyhatguy007%2Fgraphs/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262893665,"owners_count":23380712,"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-11-06T10:12:54.743Z","updated_at":"2025-07-01T04:08:40.971Z","avatar_url":"https://github.com/greyhatguy007.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# \u003cu\u003eGraphs\u003c/u\u003e\n\n## Table of Contents\n- [Introduction](#Graphs)\n- [Types of Graphs](#types-of-graphs)\n    - [Undirected Graph](#undirected-graph)\n    - [Directed Graph](#directed-graph-or-digraph)\n- [Restriction on a Graph](#restriction-on-a-graph)\n- [Graph Terminologies](#terminologies-in-graph)\n    - [Path](#path)\n    - [Cycle](#cycle)\n    - [Complete Graph](#complete-graph)\n    - [Subgraph](#subgraph)\n    - [Connected Graph](#connected-graph)\n        - [Unirected](#undirected)\n        - [Directed](#directed)\n    - [Degree of a Graph](#degree-of-a-graph)\n        - [Undirected Graph Degree](#undirected-graph-degree)\n        - [Directed Graph Degree](#directed-graph-degree)\n    - [Source Vertex](#source-vertex)\n    - [Sink Vertex](#sink-vertex)\n    - [Isolated Vertec](#isolated-vertex)\n    - [Weighted Graph](#weighted-graph)\n    - [Pendant Vertex](#pendant-vertex)\n- [Representation of a Graph](#representation-of-a-graph)\n    - [Incidence Matrix](#incidence-matrix)\n    - [Adjacency Matrix](#adjacency-matrix)\n    - [Adjacency List](#adjacency-list)\n\n## Introduction\n\nA graph is a non-linear data structure that consists of a set of non-empty **Vertices** with a set of **Edges**.Each edge joins two different Vertices.\n\n\u003cp align=\"center\" \u003e\n    \u003cimg src=\"resources/simple_graph.png\" width=\"35%\" height=\"35%\" title=\"Graph - Edge and Vertex\"\u003e\n\u003c/p\u003e\n\n### Types Of Graphs\n 1. Undirected Graph\n 2. Directed Graph\n\n## Undirected Graph\n\nIf an edge between any two nodes is *not directly oriented*, then it is undirected graph\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"resources/undirected_graph.png\" width=\"35%\" height=\"35%\" title=\"Directed Graph\"\u003e\n\u003c/p\u003e\n\n**(A,B) \u0026 (B,A) represents the same edge** \n\n## Directed Graph (or) *Digraph*\n\nIf an edge between any two nodes is *directly oriented, then it is directed graph\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"resources/digraph.png\" width=\"35%\" height=\"35%\" title=\"Undirected Graph\"\u003e\n\u003c/p\u003e\n\n**(A,B) Shows the edge between A and B**\n\nHere A is called the *Tail node* and B is called the *Head node*\n\n### Restriction On a Graph\n\n- A graph may not have an edge from a vertex V back to itself. If it has an eged to itself, then it is called **Self edges**.\n- A graph may not have multiple occurrence of the same edges *(multigraph)*\n\n\n## Terminologies in Graph\n\n### Path \n\nA path is a sequence of distinct Vertices each adjacent ot the next.\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg src=\"resources/path.png\" width=\"35%\" height=\"35%\" title=\"Path of a Graph\"\u003e\n\n*Path from A to C is (A,B),(B,C)*\n\u003c/div\u003e\n\n### Cycle\n\nA cycle is a simple path in which first and last vertices are same.\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg src=\"resources/path.png\" width=\"35%\" height=\"35%\" title=\"Cycle of a Graph\"\u003e\n\n*ABCD is cyclic as it starts and ends with the same vertex*\n\u003c/div\u003e\n\nIn a digraph. a cycle is referred as *Directed Cycle*.\n\n**Note : The maximum number of edges in a graph with *n* vertices is *n(n+1)/2***\n\n### Complete Graph\n\nAn n-vertex, undirected graph with *exactly n(n+1)/2 edges* is said to be a complete graph.\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg src=\"resources/complete_graph.png\" width=\"35%\" height=\"35%\" title=\"Complete Graph\"\u003e\n\n*n=4, no of edges = 4(4+1)/2 = 6*\n\u003c/div\u003e\n\n### Subgraph\n\nA subgraph of G is a graph G' such that *V(G') is a subset of V(G)\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg src=\"resources/subgraph.png\" width=\"80%\" height=\"80%\" title=\"Subgraph\"\u003e\n\n*Subgraphs of G*\n\u003c/div\u003e\n\n### Connected Graph \n#### Undirected\n\nIf there exists a path from any vertex to any other vertex, then that graph is called **connected graph**.\n#### Directed\n\nIf for every pair of distinct vertices there is a directed path from every vertex to every other vertices, then that graph is called **Strongly connected graph**.\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg src=\"resources/strongly_connected.png\" width=\"35%\" height=\"35%\" title=\"Connected Directed Graph\"\u003e\n\n*Strongly Connected Graph*\n\u003c/div\u003e\n\nIf any vertex dosen't have a directed path to any other vertices, then the graph is called **Weakly connected Graph**.\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg src=\"resources/weakly_connected.png\" width=\"35%\" height=\"35%\" title=\"Connected Undirected Graph\"\u003e\n\n*Weakly Connected graph*\n\u003c/div\u003e\n\n### Degree of a graph\n\n#### Undirected Graph Degree\n\nUndirected graph only have one degree, ie. the The number of edges connected directly to a node\n\u003cdiv align=\"center\"\u003e\n    \u003cimg src=\"resources/path.png\" width=\"35%\" height=\"35%\" title=\"Undirected Graph Degree\"\u003e\n\n*The degree of A is 2*\n\u003c/div\u003e\n\n#### Directed Graph Degree\n\nDirected Graphs have two types of degrees, namely\n\n- **Indegree** - The number of edges entering the node. \n- **Outdegree** - The number of edges leaving the node.\n\n\u003cbr/\u003e\n\u003cdiv align=\"center\"\u003e\n    \u003cimg src=\"resources/digraph.png\" width=\"35%\" height=\"35%\" title=\"Directed Graph Degree\"\u003e\n\u003c/div\u003e\n\n| Node | Indegree | Outdegree |\n|------|----------|-----------|\n| A    | 1        | 1         |\n| B    | 1        | 1         |\n\n### Source Vertex\nA vertex whose *indegree is zero* is referred as source vertex.\n\n### Sink Vertex\nA vertex whose *outdegree is zero* is referred as sink vertex.\n\n### Isolated Vertex\nIf a graph has onlu one vertex in it, it is a isolated graph vertex.\n\n### Weighted Graph\nIf every edge in the graph is assigned some weight (or) value, then the graph is called *Weighted Graph*\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg src=\"resources/weighted_graph.png\" width=\"35%\" height=\"35%\" title=\"Weighted Graph\"\u003e\n\n*Weighted Graph*\n\u003c/div\u003e\n\n### Pendant Vertex\nA vertex whose indegree is 1 and outdegree is 0 is referred to as *Pendant Vertex*.\n\n## Representation of a Graph\n\n### Incidence Matrix\nA graph containing m vertices and n edges can be represented by a matrix with m rows and n columns. The matrix is formed by storing 1 in the i\u003csup\u003eth\u003c/sup\u003e row and j\u003csup\u003eth\u003c/sup\u003e column corresponding to the matrix, if there exists a i\u003csup\u003eth\u003c/sup\u003e vertex connected to one end of the j\u003csup\u003eth\u003c/sup\u003e edge and a 0 if there is no i\u003csup\u003eth\u003c/sup\u003e vertex connected to any end of the j\u003csup\u003eth\u003c/sup\u003e edge of the graph, such a matrix is referred to as *incidence matrix*.\n\n\u003cbr/\u003e\n\n\u003cp align=\"center\"\u003e\u003ci\u003eConsider the given weighted graph\u003c/i\u003e\u003c/p\u003e\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg src=\"resources/incidence_matrix.png\" width=\"35%\" height=\"35%\" title=\"Weighted Graph\"\u003e\n\u003c/div\u003e\n\n\n    incidence_matrix[i][j] = 1, if there is an edge\n                             0, otherwise\n\nThe Incidence Matrix for this graph is given by:\n\n|               | E\u003csub\u003e1\u003c/sub\u003e | E\u003csub\u003e2\u003c/sub\u003e | E\u003csub\u003e3\u003c/sub\u003e |\n|---------------|---------------|---------------|---------------|\n| V\u003csub\u003e1\u003c/sub\u003e | 1             | 0             | 0             |\n| V\u003csub\u003e2\u003c/sub\u003e | 0             | 1             | 0             |\n| V\u003csub\u003e3\u003c/sub\u003e | 0             | 0             | 1             |\n| V\u003csub\u003e4\u003c/sub\u003e | 1             | 1             | 1             |\n\n### Adjacency Matrix\n\nA graph containing n vertices can be represented by a matrix with n rows and n columns if there exists an edge between i\u003csup\u003eth\u003c/sup\u003e and j\u003csup\u003eth\u003c/sup\u003e vertex of the graph, then 1 is stored in the i\u003csup\u003eth\u003c/sup\u003e row and j\u003csup\u003eth\u003c/sup\u003e column of the matrix, otherwise 0 is stored.\n\n\u003cdiv align=\"center\"\u003e\n\u003cimg src=\"resources/adjacency.png\"\u003e\n\u003c/div\u003e\n\nThe Adjacency matrix for the graph is given by:\n\n|   | A | B | C | D |\n|---|---|---|---|---|\n| A | 0 | 1 | 0 | 0 |\n| B | 0 | 0 | 1 | 0 |\n| C | 1 | 0 | 0 | 0 |\n| D | 1 | 1 | 1 | 0 |\n\n\n### Adjacency List\n\nA graph containing m vertices and n edges can be represented using a linked list is referred to as *adjacency list*.\n\n#### Adjacency List for Undirected Graph\n\n| V\u003csub\u003e1\u003c/sub\u003e | V\u003csub\u003e2\u003c/sub\u003e |\n|---------------|---------------|","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreyhatguy007%2Fgraphs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgreyhatguy007%2Fgraphs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreyhatguy007%2Fgraphs/lists"}