{"id":10919,"date":"2025-11-05T18:41:28","date_gmt":"2025-11-05T13:11:28","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=10919"},"modified":"2025-11-24T17:11:23","modified_gmt":"2025-11-24T11:41:23","slug":"introduction-to-graphs","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/introduction-to-graphs\/","title":{"rendered":"Introduction to Graphs"},"content":{"rendered":"\n<!-- PrismJS for Syntax Highlighting -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.min.css\" rel=\"stylesheet\">\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.min.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n.wp_blog_theme {\n  --primary: #E58C32;\n  --secondary: #030302;\n  --light-bg: #fef9f4;\n  --text-dark: #2d2d2d;\n  --tab-radius: 12px;\n  --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);\n  --code-bg: #001f3f;\n  --code-text: #d4f1ff;\n}\n\n.wp_blog_container {\n  font-family: 'Segoe UI', sans-serif;\n  background: var(--light-bg);\n  margin: 0;\n  padding: 0;\n  color: var(--text-dark);\n}\n\n\/* Heading *\/\n.wp_blog_main-heading {\n  text-align: center;\n  font-size: 2.4rem;\n  color: var(--primary);\n  margin-top: 2.5rem;\n  font-weight: bold;\n}\n\n\/* Explanation Card *\/\n.wp_blog_explanation,\n.wp_blog_code-tabs-container {\n  max-width: 940px;\n  margin: 2rem auto;\n  padding: 2rem;\n  background: white;\n  border-radius: var(--tab-radius);\n  box-shadow: var(--shadow);\n}\n\n\/* Text and Visuals *\/\n.wp_blog_explanation h2 {\n  font-size: 1.4rem;\n  color: var(--primary);\n  margin-bottom: 0.5rem;\n}\n\n.wp_blog_explanation p,\n.wp_blog_explanation li {\n  font-size: 1.05rem;\n  line-height: 1.7;\n  margin: 0.5rem 0;\n}\n\n.wp_blog_explanation code {\n  background: #fef9f4;   \/* light bg instead of dark blue *\/\n  color: #E58C32;        \/* brand orange *\/\n  padding: 3px 6px;\n  border-radius: 4px;\n  font-family: 'Courier New', monospace;\n  font-weight: 600;      \/* optional, makes it pop *\/\n}\n\n.wp_blog_explanation img {\n  max-width: 100%;\n  border-radius: var(--tab-radius);\n  margin-top: 1rem;\n  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);\n}\n\n\/* Tab Buttons *\/\n.wp_blog_code-tabs-header {\n  display: flex;\n  flex-wrap: wrap;\n  gap: 0.5rem;\n  margin-bottom: 1rem;\n}\n\n.wp_blog_code-tab-button {\n  padding: 0.6rem 1.2rem;\n  border: 1px solid var(--primary);\n  background: white;\n  color: var(--primary);\n  border-radius: 50px;\n  font-weight: 600;\n  cursor: pointer;\n  transition: all 0.3s ease;\n}\n\n.wp_blog_code-tab-button:hover {\n  background: var(--secondary);\n}\n\n.wp_blog_code-tab-button.active {\n  background: var(--primary);\n  color: white;\n}\n\n\/* Code Content *\/\n.wp_blog_code-tab-content {\n  display: none;\n  background: var(--code-bg);\n  border-radius: var(--tab-radius);\n}\n\n.wp_blog_code-tab-content.active {\n  display: block;\n}\n\n.wp_blog_code-tab-content pre {\n  margin: 0;\n  padding: 1.5rem;\n  font-size: 1rem;\n  overflow-x: auto;\n  background: var(--code-bg);\n  border-radius: var(--tab-radius);\n  color: var(--code-text);\n}\n\n\/* Dark mode variables *\/\n.wp_blog_theme.dark-mode {\n  --light-bg: #121212;\n  --text-dark: #f5f5f5;\n  --shadow: 0 4px 12px rgba(255, 255, 255, 0.08);\n  --code-bg: #1e1e1e;\n  --code-text: #c5f0ff;\n}\n\n.wp_blog_theme.dark-mode .wp_blog_explanation {\n  background: #1e1e1e;\n}\n\n\/* Dark mode code highlight *\/\n.wp_blog_theme.dark-mode .wp_blog_explanation code {\n  background: #333;\n  color: #ffd27f;\n}\n\n.wp_blog_theme {\n  position: relative; \/* makes it the reference for absolute children *\/\n}\n\n.wp_blog_toggle-btn {\n  position: absolute;\n  top: 1rem;\n  right: 1rem;\n  z-index: 9999;\n  padding: 0.5rem 0.8rem;\n  border-radius: 10%;\n  background: var(--primary);\n  color: white;\n  font-weight: bold;\n  cursor: pointer;\n  border: none;\n  box-shadow: var(--shadow);\n  transition: background 0.3s, transform 0.2s;\n}\n\n.wp_blog_toggle-btn:hover {\n  background: #cc772e;\n}\n\n.wp_blog_theme.dark-mode .wp_blog_code-tabs-container {\n  background: #1e1e1e;\n}\n<\/style>\n\n<div class=\"wp_blog_container wp_blog_theme\">\n  <button id=\"blogNotesThemeToggle\" class=\"wp_blog_toggle-btn\">\ud83c\udf19<\/button>\n  <h1 class=\"wp_blog_main-heading\"><\/h1>\n\n  <div class=\"wp_blog_explanation\">\n    <h2>Graphs<\/h2>\n    <p><strong>A graph is a data structure that helps us represent relationships or connection between objects.<\/strong><\/p>\n    <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/11\/Screenshot-2025-10-30-at-6.39.39\u202fPM.png\" alt=\"\">\n    \n    <h3>It consists of: <\/h3>\n    <ul>\n        <li><strong>Vertices(Node):<\/strong> <code>Entities or objects.<\/code><\/li>\n        <li><strong>Edges:<\/strong> <code>Links or connection between vertices.<\/code><\/li>\n    <\/ul>\n    <p><strong>Mathematically, A graph is written as<\/strong> <code>G = (V, E)<\/code><\/p>\n    <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/11\/Screenshot-2025-11-03-at-12.37.38\u202fPM.png\" alt=\"\">\n<h2>Degree<\/h2>\n  <p><strong><code>How many connections does the node have. As how many edges going from the particular nodes.<\/code><\/strong><\/p>\n<ul>\n  <li><strong>Examples:<\/strong> How many degree&#8217;s they have?<\/li>\n  <ul>\n    <li>3 \u2192 4 degree&#8217;s<\/p><\/li>\n    <li>7 \u2192 2 degree&#8217;s<\/p><\/li>\n    <li>2 \u2192 1 degree&#8217;s<\/p><\/li>\n  <\/ul>\n  <p><strong>There is a special type of graph called a <code>directed graph<\/code>, which has in-degree and out-degree.<\/strong>(Further, we learn about these topics in detail)<\/p>\n<\/ul>\n  \n<h2>Path<\/h2>\n  <p><strong><code>It is a way to go from one node to another node.<\/code><\/strong><\/p>\n  <p><strong>Different Paths in above Graph<\/strong><\/p>\n  <ul>\n    <li>2 \u2192 7 \u2192 3 \u2192 5<\/li>\n    <li>2 \u2192 7 \u2192 3 \u2192 6 \u2192 9<\/li>\n    <li>2 \u2192 7 \u2192 3 \u2192 1 \u2192 6 \u2192 9<\/li>\n    <li><strong><code>We can go from one place to another using different paths; there can be many paths or sometimes no path at all.<\/code><\/strong><\/li>\n  <\/ul>\n\n  <h2>Cycle<\/h2>\n  <p><strong><code>If some components are connected to each other in a loop, they form a cycle.<\/code><\/strong><\/p>\n  <p>Example: <strong>3 \u2192 1 \u2192 6<\/strong><\/p>\n  <p><strong>Basically, if you start from any node and reach the same node again after traversing some other nodes, it is called a cycle.<\/strong><\/p>\n\n  <h2>Real-World Analogy<\/h2>\n  <h2><strong>Example 1:<\/strong><\/h2>\n  <p>Let\u2019s take Facebook friends as an example.<\/p>\n  <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/11\/Screenshot-2025-11-03-at-12.12.46\u202fPM.png\" alt=\"friends\">\n  <ul>\n    <li>B has 3 friends (A, C, and, D) means, B Node has 3 degree<\/li>\n    <li>C has 2 friends (B and G)<\/li>\n    <li>D has 2 friends (B and E)<\/li> \n  <\/ul>\n  <p><strong><code>In conclusion, if we need to store the data of friends, we can use a graph data structure.<\/code><\/strong><\/p>\n\n    <h2><strong>Example 2:<\/strong><\/h2>\n    <p><strong>Cities<\/strong><\/p>\n    <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/11\/3.png\" alt=\"\">\n    <p>If someone has to travel from Jaipur to Mussoorie, they can take many different routes.    <\/p>\n  <ul>\n    <li>Jaipur \u2192 Delhi \u2192 Dehradun \u2192 Mussoorie<\/li>\n    <li>Jaipur \u2192 Delhi \u2192 Chandigarh \u2192 Dehradun \u2192 Mussoorie<\/li>\n  <\/ul>\n  \n  <h2>Two Types of Graphs<\/h2>\n  <h2>1. Undirected Graphs<\/h2>\n  <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/11\/Screenshot-2025-11-03-at-12.27.19\u202fPM.png\" alt=\"\">\n  <p>We saw this graph in the previous examples.<\/p>\n  <ul>\n    <li><code>Facebook Friends<\/code><\/li>\n    <li><code>Cities<\/code><\/li>\n  <\/ul>\n  <h2>2. Directed Graph (Digraphs)<\/h2>\n  <p><strong><code>Directed Graph shows the realtionship between the two entities or two objects in one direction.<\/code><\/strong><\/p>\n  <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/11\/Screenshot-2025-11-03-at-12.30.00\u202fPM.png\" alt=\"\">\n  <p><strong>Examples: <\/strong><\/p>  \n  <ul>\n    <li><code>Instagram Followers<\/code><\/li>\n    <li><code>Web Pages<\/code><\/li>\n    <li><code>Dependencies<\/code><\/li>\n  <\/ul>\n\n  <h2>3. Weighted Graphs<\/h2>\n  <p><strong>If we assign weights to the edges, it becomes a weighted graph.<\/strong><\/p>\n  <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/11\/Screenshot-2025-11-24-at-5.05.32-PM.png\" alt=\"\">\n  <p><strong>\u2219 From the above example, we can give some weighted to the edges.<\/strong><\/p>\n  <p><strong>\u2219 Also, weights are also referred to by terms such as <code>cost, distance, time, price, or weight<\/code>.<\/code><\/strong><\/p>\n\n  <h2>4. Unweighted Graphs<\/h2>\n  <p><strong>\u2219 All edges are considered equal.<\/strong><\/p>\n  <p><strong>\u2219 You only care about connections, not their cost\/weight.<\/strong><\/p>\n\n  <h2>5. Cyclic Graphs<\/h2>\n  <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/11\/Screenshot-2025-11-24-at-5.06.14-PM.png\" alt=\"\">\n  <p><strong>\u2219 Atleast one cycle.<\/strong><\/p>\n\n  <h2>6. Acyclic Graphs<\/h2>\n  <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/11\/Screenshot-2025-11-24-at-5.10.44-PM.png\" alt=\"\">\n  <p><strong>\u2219 No Cycles<\/strong><\/p>\n  <p><strong>\u2219 You only care about connections, not their cost\/weight.<\/strong><\/p>\n\n  <h2>7. Directed Acyclic Graphs<\/h2>\n  <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/11\/Screenshot-2025-11-24-at-5.06.50-PM.png\" alt=\"\">\n  <p><strong>Examples: <\/strong><code>NPM Dependencies, Version Control (git), Trees (It is a special type of graph)<\/code><\/p>\n\n  <h2>Directed Graphs<\/h2>\n  <h3>Indegree and Outdegree<\/h3>\n  <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/11\/Screenshot-2025-11-03-at-9.30.48\u202fPM.png\" alt=\"\">\n  <p><strong>\u2219 Indegree: <\/strong><code>It is the number of edges pointing into a vertex.<\/code><\/p>\n  <p><strong>Example: <\/strong>Indegree &#8216;C&#8217;: 1<\/p>\n  <p><strong>\u2219 Outdegree: <\/strong><code>It is the number of edges pointing out of a vertex<\/code><\/p>\n    <p><strong>Example: <\/strong>Outdegree &#8216;C&#8217;: 2<\/p>\n\n  <h2>8. Connected Graphs<\/h2>\n  <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/11\/Screenshot-2025-11-24-at-5.06.50-PM.png\" alt=\"\">\n  <p><strong><code>\u2219 Atleast one path between each pair of node.<\/code><\/strong><\/p>\n  <p><strong><code>\u2219 Everything is reachable.<\/code><\/strong><\/p>\n  <p><strong>Example: <\/strong>Path: C \u2192 D, D \u2192 E, B \u2192 C and more.<\/p>\n\n  <h2>9. Disconnected Graphs<\/h2>\n  <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/11\/Screenshot-2025-11-04-at-12.14.15\u202fPM.png\" alt=\"\">\n  <h3>Disconnected Graphs uses: <\/h3>\n  <p><strong><code>\u2219 Grouped Friend Circle<\/code><\/strong><\/p>\n  <p><strong><code>\u2219 Unconnected Clusters<\/code><\/strong><\/p>\n\n  <h2>Summary<\/h2>\n  <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/11\/Screenshot-2025-11-05-at-6.26.50\u202fPM.png\" alt=\"\">\n<\/div>  \n\n<\/div>\n\n<script>\ndocument.addEventListener(\"DOMContentLoaded\", () => {\n  const buttons = document.querySelectorAll(\".wp_blog_code-tab-button\");\n  const contents = document.querySelectorAll(\".wp_blog_code-tab-content\");\n\n  buttons.forEach((button) => {\n    button.addEventListener(\"click\", () => {\n      const lang = button.getAttribute(\"data-lang\");\n\n      buttons.forEach((btn) => btn.classList.remove(\"active\"));\n      contents.forEach((content) => content.classList.remove(\"active\"));\n\n      button.classList.add(\"active\");\n      document\n        .querySelector(`.wp_blog_code-tab-content[data-lang=\"${lang}\"]`)\n        .classList.add(\"active\");\n    });\n  });\n\n  const themeToggle = document.getElementById(\"blogNotesThemeToggle\");\n  const themeContainer = document.querySelector(\".wp_blog_theme\");\n\n  themeToggle.addEventListener(\"click\", () => {\n    themeContainer.classList.toggle(\"dark-mode\");\n    themeToggle.textContent =\n      themeContainer.classList.contains(\"dark-mode\") ? \"\u2600\ufe0f\" : \"\ud83c\udf19\";\n  });\n});\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udf19 Graphs A graph is a data structure that helps us represent relationships or connection between objects. It consists of: Vertices(Node): Entities or objects. Edges: Links or connection between vertices. Mathematically, A graph is written as G = (V, E) Degree How many connections does the node have. As how many edges going from the<\/p>\n","protected":false},"author":108,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[210,260,176,175,211,811,810,174,172,173],"tags":[],"class_list":{"0":"post-10919","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-algorithms","7":"category-c-c-plus-plus","8":"category-csharp","9":"category-cplusplus","10":"category-data-structures","11":"category-data-structures-and-algorithms","12":"category-dsa","13":"category-java","14":"category-javascript","15":"category-python"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/108"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=10919"}],"version-history":[{"count":2,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10919\/revisions"}],"predecessor-version":[{"id":11236,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10919\/revisions\/11236"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=10919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=10919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=10919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}