From f77b041306dea3af2a27936abf93e69d3f145521 Mon Sep 17 00:00:00 2001 From: Srishti-Gupta74 Date: Sat, 6 Jun 2026 19:51:03 +0530 Subject: [PATCH] feat: add comprehensive graphs module documentation --- app/sem2/dsc/[chapter]/page.tsx | 2 + app/sem2/dsc/components/sidebar.tsx | 1 + app/sem2/dsc/content/chapter0.tsx | 43 +- app/sem2/dsc/content/chapter6.tsx | 1941 +++++++++++++++++++++++++++ 4 files changed, 1970 insertions(+), 17 deletions(-) create mode 100644 app/sem2/dsc/content/chapter6.tsx diff --git a/app/sem2/dsc/[chapter]/page.tsx b/app/sem2/dsc/[chapter]/page.tsx index c0e7090..b089f9f 100644 --- a/app/sem2/dsc/[chapter]/page.tsx +++ b/app/sem2/dsc/[chapter]/page.tsx @@ -8,6 +8,7 @@ import { Ch2Content } from "../content/chapter2"; import { Ch3Content } from "../content/chapter3"; import { Ch4Content } from "../content/chapter4"; import { Ch5Content } from "../content/chapter5"; +import { Ch6Content } from "../content/chapter6"; import { ArrowBigLeft, ArrowBigRight } from "lucide-react"; import { moduleQuizzes } from "@/lib/quizData"; @@ -26,6 +27,7 @@ const chapters = [ { id: "ch3", title: "Stacks", component: Ch3Content }, { id: "ch4", title: "Queues", component: Ch4Content }, { id: "ch5", title: "Trees", component: Ch5Content }, + { id: "ch6", title: "Graphs", component: Ch6Content }, ]; type ChapterProps = { diff --git a/app/sem2/dsc/components/sidebar.tsx b/app/sem2/dsc/components/sidebar.tsx index 8f96e67..fc2e3e5 100644 --- a/app/sem2/dsc/components/sidebar.tsx +++ b/app/sem2/dsc/components/sidebar.tsx @@ -27,6 +27,7 @@ export default function Sidebar() { { id: "ch3", title: "Stacks" }, { id: "ch4", title: "Queues" }, { id: "ch5", title: "Trees" }, + { id: "ch6", title: "Graphs" }, ]; const quizSlugMap: Record = { diff --git a/app/sem2/dsc/content/chapter0.tsx b/app/sem2/dsc/content/chapter0.tsx index 9d852b4..1fe509f 100644 --- a/app/sem2/dsc/content/chapter0.tsx +++ b/app/sem2/dsc/content/chapter0.tsx @@ -65,29 +65,38 @@ export const Ch0Content = () => {

Module V: Trees

    -
  • Introduction to Trees and Terminology
  • -
  • Tree Characteristics and Components
  • -
  • Tree Representation
  • -
  • Types of Trees
  • -
  • Tree Traversal Techniques (DFS and BFS)
  • -
  • Preorder, Inorder, Postorder and Level Order Traversal
  • -
  • Tree Operations (Search, Insert, Delete)
  • -
  • Height, Node Count and Leaf Count
  • -
  • Binary Search Trees (BST)
  • -
  • Tree Implementation in C
  • -
  • BST Implementation in C
  • -
  • Complexity Analysis
  • -
  • Applications and Real-Life Examples
  • -
  • Advantages and Limitations
  • +
  • Introduction to Trees and Terminology
  • +
  • Tree Characteristics and Components
  • +
  • Tree Representation
  • +
  • Types of Trees
  • +
  • Tree Traversal Techniques (DFS and BFS)
  • +
  • Preorder, Inorder, Postorder and Level Order Traversal
  • +
  • Tree Operations (Search, Insert, Delete)
  • +
  • Height, Node Count and Leaf Count
  • +
  • Binary Search Trees (BST)
  • +
  • Tree Implementation in C
  • +
  • BST Implementation in C
  • +
  • Complexity Analysis
  • +
  • Applications and Real-Life Examples
  • +
  • Advantages and Limitations

Module VI: Graphs

    -
  • Graph representation
  • -
  • Breadth First Search
  • -
  • Depth First Search
  • +
  • Introduction to Graphs and Terminology
  • +
  • Characteristics and Components of Graphs
  • +
  • Need for Graphs
  • +
  • Types of Graphs
  • +
  • Graph Representations (Adjacency Matrix and Adjacency List)
  • +
  • Comparison of Graph Representations
  • +
  • Graph Traversal Techniques
  • +
  • Breadth First Search (BFS)
  • +
  • Depth First Search (DFS)
  • +
  • BFS vs DFS Comparison
  • +
  • Applications and Real-Life Examples
  • +
  • Advantages and Limitations
diff --git a/app/sem2/dsc/content/chapter6.tsx b/app/sem2/dsc/content/chapter6.tsx new file mode 100644 index 0000000..cb9a685 --- /dev/null +++ b/app/sem2/dsc/content/chapter6.tsx @@ -0,0 +1,1941 @@ +export const Ch6Content = () => { + return ( +
+ +

+ + Module VI: Graphs. + + Graphs are one of the most powerful non-linear data structures used in + Computer Science. While trees represent hierarchical relationships, + graphs represent complex relationships where any entity can be connected + to multiple other entities. Graphs are extensively used in computer + networks, social media platforms, maps, search engines, recommendation + systems, transportation systems, and routing algorithms. +

+ +
+ + {/* Introduction */} + +
+ +

+ Introduction to Graph +

+ +

+ A Graph is a non-linear data structure consisting of a collection of + vertices (also called nodes) and edges that connect pairs of vertices. + Unlike trees, graphs do not necessarily follow a hierarchical + structure and may contain cycles. +

+ +

+ Graphs are used whenever relationships between objects need to be + represented. Examples include social networks, airline routes, + computer networks, road maps, and web page connections. +

+ +
    +
  • Non-linear data structure
  • +
  • Consists of vertices and edges
  • +
  • Represents relationships between objects
  • +
  • May contain cycles
  • +
  • Supports traversal and path-finding operations
  • +
+ +
+ +
+ + {/* Characteristics */} + +
+ +

+ Characteristics of Graph +

+ +
    +
  • Collection of vertices and edges
  • +
  • Can be directed or undirected
  • +
  • May contain cycles
  • +
  • Can represent real-world networks
  • +
  • Supports multiple traversal techniques
  • +
  • Can be weighted or unweighted
  • +
  • Efficient for modeling relationships
  • +
+ +
+ +
+ + {/* Components */} + +
+ +

+ Components of Graph +

+ +

+ Vertex (Node) +

+ +

+ A Vertex is an individual entity present in a graph. It represents an + object or location in the graph structure. +

+ +

+ Edge +

+ +

+ An Edge is a connection between two vertices. Edges represent the + relationship between vertices. +

+ +

+ Path +

+ +

+ A Path is a sequence of vertices connected through edges. +

+ +

+ Cycle +

+ +

+ A Cycle is a closed path in which the starting and ending vertex are + the same, while no vertex is repeated except the starting vertex. +

+ +

+ Degree +

+ +

+ The Degree of a vertex is the number of edges incident on that + vertex. In directed graphs, degree is further classified into + Indegree and Outdegree. +

+ +

+ Adjacent Vertices +

+ +

+ Two vertices connected directly by an edge are called Adjacent + Vertices. +

+ +
+ +
+ + {/* Graph Representation */} + +
+ +

+ Graph Representation +

+ +
+ +
+                        {`      A ----- B
+      |       |
+      |       |
+      D ----- C`}
+                    
+ +
+ +

+ The graph contains four vertices: A, B, C, and D. Each edge + represents a connection between two vertices. +

+ +
+ +
+ + {/* Terminology */} + +
+ +

+ Important Graph Terminology +

+ +
    +
  • Vertex → Individual node in a graph
  • +
  • Edge → Connection between two vertices
  • +
  • Path → Sequence of connected vertices
  • +
  • Cycle → Closed path that begins and ends at the same vertex
  • +
  • Degree → Number of edges incident on a vertex
  • +
  • Adjacent Vertices → Vertices connected by an edge
  • +
  • Indegree → Number of incoming edges to a vertex
  • +
  • Outdegree → Number of outgoing edges from a vertex
  • +
  • Self Loop → Edge connecting a vertex to itself
  • +
  • Parallel Edges → Multiple edges connecting the same pair of vertices
  • +
  • Connected Component → A connected subgraph within a graph
  • +
  • Connected Graph → Graph where every vertex is reachable
  • +
  • Disconnected Graph → Graph containing isolated components
  • +
  • Weighted Graph → Graph whose edges contain weights
  • +
  • Unweighted Graph → Graph whose edges do not contain weights
  • +
+ +
+ +
+ + {/* Need for Graphs */} + +
+ +

+ Need for Graphs +

+ +

+ Graphs are used whenever relationships between entities need to be + represented efficiently. Many real-world problems involve objects that + are interconnected rather than organized hierarchically. Graphs provide + a flexible structure for modeling such systems. +

+ +
    +
  • Computer networks
  • +
  • Road and transportation systems
  • +
  • Social networking platforms
  • +
  • Web page linking structures
  • +
  • Recommendation systems
  • +
  • Routing and navigation algorithms
  • +
  • Dependency management systems
  • +
+ +
+ +
+ + {/* Types of Graphs */} + +
+ +

+ Types of Graphs +

+ +

+ Graphs can be classified based on edge direction, edge weights, + connectivity, and structural properties. +

+ +

+ 1. Undirected Graph +

+ +

+ In an Undirected Graph, edges do not have a direction. If vertex A is + connected to vertex B, then B is also connected to A. +

+ +
+ +
+                        {`A ----- B
+|       |
+|       |
+D ----- C`}
+                    
+ +
+ +
+ Applications +
+ +
    +
  • Friendship networks
  • +
  • Road maps with two-way roads
  • +
  • Peer-to-peer systems
  • +
+ +

+ 2. Directed Graph (Digraph) +

+ +

+ In a Directed Graph, every edge has a direction. An edge from A to B + does not imply the existence of an edge from B to A. +

+ +
+ +
+                        {`A -----> B
+^         |
+|         v
+D <----- C`}
+                    
+ +
+ +
+ Applications +
+ +
    +
  • Web page links
  • +
  • Task scheduling
  • +
  • Social media followers
  • +
+ +

+ 3. Weighted Graph +

+ +

+ A Weighted Graph stores numerical values called weights on edges. + These weights often represent cost, distance, time, or capacity. +

+ +
+ +
+                        {`A --5-- B
+|       |
+2       3
+|       |
+C-------D`}
+                    
+ +
+ +
+ Applications +
+ +
    +
  • GPS navigation
  • +
  • Network routing
  • +
  • Transportation planning
  • +
+ +

+ 4. Unweighted Graph +

+ +

+ An Unweighted Graph does not assign weights to edges. Every edge is + considered equal. +

+ +
    +
  • Basic connectivity problems
  • +
  • Social network analysis
  • +
  • Communication systems
  • +
+ +

+ 5. Cyclic Graph +

+ +

+ A Cyclic Graph contains at least one cycle where traversal can return + to the starting vertex. +

+ +
+ +
+                        {`A ----- B
+|       |
+|       |
+D ----- C`}
+                    
+ +
+ +

+ 6. Acyclic Graph +

+ +

+ An Acyclic Graph does not contain any cycle. +

+ +
+ +
+                        {`A
+|
+B
+|
+C
+|
+D`}
+                    
+ +
+ +

+ 7. Connected Graph +

+ +

+ A Connected Graph is a graph in which every vertex can be + reached from every other vertex through one or more paths. +

+ +
+ +
+                        {`A ----- B
+|       |
+|       |
+D ----- C`}
+                    
+ +
+ +

+ All vertices belong to a single connected component. +

+ +

+ 8. Disconnected Graph +

+ +

+ A Disconnected Graph contains two or more components that + are not connected to each other. +

+ +
+ +
+                        {`A ----- B
+
+C ----- D`}
+                    
+ +
+ +

+ No path exists between vertices belonging to different + components. +

+ +

+ 9. Complete Graph +

+ +

+ A Complete Graph is a graph in which every vertex is + directly connected to every other vertex. +

+ +
+ +
+                        {`      A
+     /|\\
+    / | \\
+   B--|--C
+    \\ | /
+     \\|/
+      D`}
+                    
+ +
+ +

+ For a complete graph containing n vertices, the number of + edges is: +

+ +
+ +
+                        {`Edges = n(n - 1) / 2`}
+                    
+ +
+ +

+ 10. Bipartite Graph +

+ +

+ A Bipartite Graph is a graph whose vertices can be divided + into two disjoint sets such that no two vertices within the + same set are adjacent. +

+ +
+ +
+                        {`Set 1      Set 2
+
+A -------- X
+A -------- Y
+
+B -------- X
+B -------- Y`}
+                    
+ +
+ +
+ Applications +
+ +
    +
  • Job assignment problems
  • +
  • Matching algorithms
  • +
  • Recommendation systems
  • +
+ +
+ +
+ + {/* Graph Storage Representations */} + +
+ +

+ Graph Storage Representations +

+ +

+ In computer memory, graphs can be represented using different + techniques. The choice of representation affects memory usage, + implementation complexity, and algorithm performance. +

+ +

+ The two most commonly used graph representations are: +

+ +
    +
  • Adjacency Matrix
  • +
  • Adjacency List
  • +
+ +
+ +
+ + {/* Adjacency Matrix */} + +
+ +

+ Adjacency Matrix Representation +

+ +

+ An Adjacency Matrix is a two-dimensional square matrix used to + represent a graph. The rows and columns represent vertices. + If an edge exists between two vertices, the corresponding cell + contains 1; otherwise, it contains 0. +

+ +

+ Example Graph +

+ +
+ +
+                        {`A ----- B
+|       |
+|       |
+C ----- D`}
+                    
+ +
+ +

+ Corresponding Adjacency Matrix +

+ +
+ +
+                        {`      A  B  C  D
+
+A     0  1  1  0
+B     1  0  0  1
+C     1  0  0  1
+D     0  1  1  0`}
+                    
+ +
+ +

+ Since the graph is undirected, the matrix is symmetric about the + main diagonal. +

+ +

+ Memory Requirement +

+ +

+ For a graph containing V vertices, an adjacency matrix requires + V × V memory locations. +

+ +
+ +
+                        {`Space Complexity = O(V²)`}
+                    
+ +
+ +
+ +
+ + {/* Advantages of Adjacency Matrix */} + +
+ +

+ Advantages of Adjacency Matrix +

+ +
    +
  • Simple and easy to implement
  • +
  • Efficient edge lookup
  • +
  • Suitable for dense graphs
  • +
  • Matrix operations can be applied directly
  • +
+ +

+ Edge Lookup Complexity +

+ +

+ Determining whether an edge exists between two vertices takes + constant time. +

+ +
+ +
+                        {`Time Complexity = O(1)`}
+                    
+ +
+ +
+ +
+ + {/* Disadvantages of Adjacency Matrix */} + +
+ +

+ Disadvantages of Adjacency Matrix +

+ +
    +
  • Consumes large amounts of memory for sparse graphs
  • +
  • Adding new vertices requires matrix resizing
  • +
  • Many matrix entries remain unused when few edges exist
  • +
+ +
+ +
+ + {/* Adjacency List Representation */} + +
+ +

+ Adjacency List Representation +

+ +

+ An Adjacency List represents a graph as an array or collection of + lists. Each vertex stores a list containing all vertices directly + connected to it. +

+ +

+ Instead of storing every possible connection as in an adjacency + matrix, only existing edges are stored. This makes adjacency lists + highly memory-efficient for sparse graphs. +

+ +

+ Example Graph +

+ +
+ +
+                        {`A ----- B
+|       |
+|       |
+C ----- D`}
+                    
+ +
+ +

+ Corresponding Adjacency List +

+ +
+ +
+                        {`A -> B -> C
+B -> A -> D
+C -> A -> D
+D -> B -> C`}
+                    
+ +
+ +

+ Each vertex stores only its adjacent vertices. Since only existing + edges are stored, adjacency lists are preferred for graphs with a + small number of edges. +

+ +

+ Memory Requirement +

+ +

+ The memory required depends on both the number of vertices and the + number of edges present in the graph. +

+ +
+ +
+                        {`Space Complexity = O(V + E)`}
+                    
+ +
+ +
+ +
+ + {/* Advantages of Adjacency List */} + +
+ +

+ Advantages of Adjacency List +

+ +
    +
  • Efficient memory utilization
  • +
  • Ideal for sparse graphs
  • +
  • Easy to traverse neighboring vertices
  • +
  • Adding vertices is comparatively easier
  • +
  • Widely used in graph algorithms
  • +
+ +
+ +
+ + {/* Disadvantages of Adjacency List */} + +
+ +

+ Disadvantages of Adjacency List +

+ +
    +
  • Edge lookup is slower than adjacency matrix
  • +
  • Implementation is slightly more complex
  • +
  • Less suitable for dense graphs
  • +
+ +

+ Edge Lookup Complexity +

+ +

+ To determine whether an edge exists, the adjacency list of a + vertex may need to be searched. The time required depends on + the number of adjacent vertices. In the worst case, this can + take O(V) time. +

+ +
+ +
+                        {`Worst Case Time Complexity = O(V)`}
+                    
+ +
+ +
+ +
+ + {/* Comparison Between Representations */} + +
+ +

+ Comparison Between Adjacency Matrix and Adjacency List +

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Feature + + Adjacency Matrix + + Adjacency List +
Space ComplexityO(V²)O(V + E)
Edge LookupO(1)O(V) (Worst Case)
Memory UsageHighLow
Best ForDense GraphsSparse Graphs
ImplementationSimpleModerate
+ +
+ +
+ +
+ + {/* Graph Traversal Techniques */} + +
+ +

+ Graph Traversal Techniques +

+ +

+ Graph traversal is the process of visiting all vertices of a graph + in a systematic manner. Traversal techniques are fundamental to + graph algorithms and are used in searching, path finding, network + analysis, dependency resolution, and many other applications. +

+ +

+ The two most common graph traversal techniques are: +

+ +
    +
  • Breadth First Search (BFS)
  • +
  • Depth First Search (DFS)
  • +
+ +
+ +
+ + {/* Breadth First Search */} + +
+ +

+ Breadth First Search (BFS) +

+ +

+ Breadth First Search (BFS) is a graph traversal algorithm that + explores all neighboring vertices before moving to the next level. + BFS visits vertices level by level and uses a Queue data structure + to keep track of vertices that need to be explored. +

+ +

+ Example Graph +

+ +
+ +
+                        {`        A
+      /   \\
+     B     C
+    / \\   /
+   D   E F`}
+                    
+ +
+ +

+ If BFS traversal starts from vertex A, the vertices are visited + level by level. +

+ +
+ +
+                        {`BFS Traversal:
+
+A → B → C → D → E → F`}
+                    
+ +
+ +

+ Working of BFS +

+ +
    +
  1. Start from the source vertex.
  2. +
  3. Mark the source as visited.
  4. +
  5. Insert the source into a queue.
  6. +
  7. Remove a vertex from the queue.
  8. +
  9. Visit all unvisited adjacent vertices.
  10. +
  11. Insert newly visited vertices into the queue.
  12. +
  13. Repeat until the queue becomes empty.
  14. +
+ +

+ BFS Algorithm +

+ +
+ +
+                        {`1. Mark starting vertex as visited
+2. Enqueue starting vertex
+
+3. While queue is not empty:
+      Dequeue a vertex
+      Visit it
+
+      For every adjacent vertex:
+            If not visited:
+                Mark as visited
+                Enqueue it`}
+                    
+ +
+ +
+ +
+ + {/* BFS Implementation */} + +
+ +

+ BFS Implementation in C +

+ +

+ The following program demonstrates Breadth First Search using an + adjacency matrix representation of a graph. A queue is used to + process vertices in level order. +

+ +
+ +
+                        {`#include 
+
+#define MAX 100
+
+int graph[MAX][MAX];
+int visited[MAX];
+int queue[MAX];
+
+int front = -1;
+int rear = -1;
+
+void enqueue(int value)
+{
+    if (rear == MAX - 1)
+        return;
+
+    if (front == -1)
+        front = 0;
+
+    queue[++rear] = value;
+}
+
+int dequeue()
+{
+    if (front == -1 || front > rear)
+        return -1;
+
+    return queue[front++];
+}
+
+void BFS(int start, int vertices)
+{
+    visited[start] = 1;
+    enqueue(start);
+
+    while (front <= rear)
+    {
+        int current = dequeue();
+
+        printf("%d ", current);
+
+        for (int i = 0; i < vertices; i++)
+        {
+            if (graph[current][i] == 1 && !visited[i])
+            {
+                visited[i] = 1;
+                enqueue(i);
+            }
+        }
+    }
+}
+
+int main()
+{
+    int vertices = 5;
+
+    graph[0][1] = 1;
+    graph[1][0] = 1;
+
+    graph[0][2] = 1;
+    graph[2][0] = 1;
+
+    graph[1][3] = 1;
+    graph[3][1] = 1;
+
+    graph[2][4] = 1;
+    graph[4][2] = 1;
+
+    printf("BFS Traversal: ");
+    BFS(0, vertices);
+
+    return 0;
+}`}
+                    
+ +
+ +
+ +
+ + {/* Complexity Analysis of BFS */} + +
+ +

+ Time and Space Complexity of BFS +

+ +

+ The theoretical time complexity of BFS is O(V + E) when an + adjacency list representation is used. In Breadth First Search, + every vertex is visited once and edges are examined during + traversal. +

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Operation + + Complexity +
+ Time Complexity + + O(V + E) +
+ Space Complexity + + O(V) +
+ +
+ +
+ +
+ + {/* Applications of BFS */} + +
+ +

+ Applications of BFS +

+ +
    +
  • Finding the shortest path in unweighted graphs
  • +
  • Web crawling by search engines
  • +
  • Social networking analysis
  • +
  • Network broadcasting systems
  • +
  • GPS and route discovery systems
  • +
  • Peer-to-peer networking
  • +
  • Connected component detection
  • +
+ +
+ +
+ + {/* Advantages and Limitations of BFS */} + +
+ +

+ Advantages and Limitations of BFS +

+ +

+ Advantages +

+ +
    +
  • Guarantees shortest path in unweighted graphs
  • +
  • Systematic level-by-level traversal
  • +
  • Useful for connectivity analysis
  • +
  • Simple to understand and implement
  • +
+ +

+ Limitations +

+ +
    +
  • Requires additional memory for the queue
  • +
  • Can consume significant memory for large graphs
  • +
  • Not suitable for very deep traversals when memory is limited
  • +
+ +
+ +
+ + {/* Depth First Search */} + +
+ +

+ Depth First Search (DFS) +

+ +

+ Depth First Search (DFS) is a graph traversal algorithm that explores + a path as deeply as possible before backtracking. Unlike BFS, which + visits vertices level by level, DFS moves deeper into the graph before + exploring remaining branches. +

+ +

+ DFS can be implemented using recursion or an explicit stack. The + recursive approach is the most commonly used because of its simplicity. +

+ +

+ Example Graph +

+ +
+ +
+                        {`        A
+      /   \\
+     B     C
+    / \\   /
+   D   E F`}
+                    
+ +
+ +

+ Starting from vertex A and visiting neighbors from left to right, + one possible DFS traversal is: +

+ +
+ +
+                        {`DFS Traversal:
+
+A → B → D → E → C → F`}
+                    
+ +
+ +

+ Working of DFS +

+ +
    +
  1. Start from the source vertex.
  2. +
  3. Mark the source as visited.
  4. +
  5. Visit an unvisited adjacent vertex.
  6. +
  7. Continue moving deeper into the graph.
  8. +
  9. If no unvisited neighbor exists, backtrack.
  10. +
  11. Repeat until all reachable vertices are visited.
  12. +
+ +

+ DFS Algorithm +

+ +
+ +
+                        {`1. Mark current vertex as visited
+2. Visit current vertex
+
+3. For every adjacent vertex:
+      If not visited:
+           Perform DFS on that vertex
+
+4. Backtrack when no unvisited neighbor exists`}
+                    
+ +
+ +
+ +
+ + {/* DFS Implementation */} + +
+ +

+ DFS Implementation in C +

+ +

+ The following program demonstrates Depth First Search using recursion + and an adjacency matrix representation. +

+ +
+ +
+                        {`#include 
+
+#define MAX 100
+
+int graph[MAX][MAX];
+int visited[MAX];
+
+void DFS(int vertex, int vertices)
+{
+    visited[vertex] = 1;
+
+    printf("%d ", vertex);
+
+    for (int i = 0; i < vertices; i++)
+    {
+        if (graph[vertex][i] == 1 && !visited[i])
+        {
+            DFS(i, vertices);
+        }
+    }
+}
+
+int main()
+{
+    int vertices = 5;
+
+    graph[0][1] = 1;
+    graph[1][0] = 1;
+
+    graph[0][2] = 1;
+    graph[2][0] = 1;
+
+    graph[1][3] = 1;
+    graph[3][1] = 1;
+
+    graph[2][4] = 1;
+    graph[4][2] = 1;
+
+    printf("DFS Traversal: ");
+    DFS(0, vertices);
+
+    return 0;
+}`}
+                    
+ +
+ +
+ +
+ + {/* Complexity Analysis of DFS */} + +
+ +

+ Time and Space Complexity of DFS +

+ +

+ The theoretical time complexity of DFS is O(V + E) when an + adjacency list representation is used. DFS visits each vertex + once and explores edges during traversal. +

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Operation + + Complexity +
+ Time Complexity + + O(V + E) +
+ Space Complexity + + O(V) +
+ +
+ +
+ +
+ + {/* Applications of DFS */} + +
+ +

+ Applications of DFS +

+ +
    +
  • Cycle detection in graphs
  • +
  • Topological sorting
  • +
  • Finding connected components
  • +
  • Maze solving algorithms
  • +
  • Path finding problems
  • +
  • Backtracking algorithms
  • +
  • Dependency resolution systems
  • +
+ +
+ +
+ + {/* Advantages and Limitations of DFS */} + +
+ +

+ Advantages and Limitations of DFS +

+ +

+ Advantages +

+ +
    +
  • Requires less memory than BFS in many cases
  • +
  • Suitable for deep traversal problems
  • +
  • Useful in recursive and backtracking algorithms
  • +
  • Simple recursive implementation
  • +
+ +

+ Limitations +

+ +
    +
  • Does not guarantee the shortest path
  • +
  • May explore long paths before finding a solution
  • +
  • Deep recursion may cause stack overflow in very large graphs
  • +
+ +
+ +
+ + {/* BFS vs DFS Comparison */} + +
+ +

+ Comparison Between BFS and DFS +

+ +

+ Breadth First Search and Depth First Search are the two fundamental + graph traversal techniques. Although both visit all reachable + vertices, they differ in their traversal strategy, memory usage, + and applications. +

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Feature + + BFS + + DFS +
Full FormBreadth First SearchDepth First Search
Traversal MethodLevel by LevelDepth Wise
Primary Data StructureQueueStack / Recursion
Time ComplexityO(V + E)O(V + E)
Space ComplexityO(V)O(V)
Shortest Path GuaranteeYes (Unweighted Graphs)No
Memory UsageUsually HigherUsually Lower
ApplicationsShortest Path, NetworkingBacktracking, Cycle Detection
+ +
+ +
+ +
+ + {/* Applications of Graphs */} + +
+ +

+ Applications of Graphs +

+ +

+ Graphs are among the most widely used data structures because they + efficiently represent relationships between entities. Many real-world + systems can be modeled as graphs where vertices represent objects and + edges represent connections between them. +

+ +
    +
  • Computer Networks and Internet Routing
  • +
  • Social Media Networks
  • +
  • Road Maps and Navigation Systems
  • +
  • Airline Route Planning
  • +
  • Search Engine Page Ranking
  • +
  • Recommendation Systems
  • +
  • Dependency Management in Software
  • +
  • Transportation Networks
  • +
  • Telecommunication Systems
  • +
  • Artificial Intelligence and Machine Learning
  • +
+ +
+ +
+ + {/* Real Life Examples of Graphs */} + +
+ +

+ Real Life Examples of Graphs +

+ +
    +
  • Facebook users connected through friendships
  • +
  • Instagram users connected through followers
  • +
  • Cities connected by roads and highways
  • +
  • Airports connected through flight routes
  • +
  • Web pages connected through hyperlinks
  • +
  • Computers connected in a network
  • +
  • Metro stations connected through railway lines
  • +
  • People connected in professional networks
  • +
  • Electric power distribution networks
  • +
  • Supply chain and logistics systems
  • +
+ +
+ +
+ + {/* Practical Example */} + +
+ +

+ Practical Example using Graphs +

+ +

+ One of the most common applications of graphs is route planning in + navigation systems such as Google Maps. +

+ +

+ Cities can be represented as vertices, while roads connecting cities + can be represented as edges. +

+ +
+ +
+                        {`Delhi -------- Agra
+  |               |
+  |               |
+Jaipur ------- Lucknow`}
+                    
+ +
+ +

+ Navigation systems use graph algorithms to determine the shortest + or fastest route between two locations. +

+ +
+ +
+ + {/* Advantages of Graphs */} + +
+ +

+ Advantages of Graphs +

+ +
    +
  • Efficiently represent complex relationships between entities
  • +
  • Can model real-world networks accurately
  • +
  • Support powerful traversal and search algorithms
  • +
  • Useful for path-finding and routing problems
  • +
  • Can represent both directed and undirected relationships
  • +
  • Flexible and scalable for large systems
  • +
  • Widely applicable across multiple domains
  • +
+ +

+ Because of their flexibility and ability to represent interconnected + data, graphs are extensively used in modern computing systems. +

+ +
+ +
+ + {/* Limitations of Graphs */} + +
+ +

+ Limitations of Graphs +

+ +
    +
  • Can become difficult to visualize as size increases
  • +
  • Graph algorithms may be computationally expensive
  • +
  • Storage requirements can be high for dense graphs
  • +
  • Implementation is generally more complex than linear data structures
  • +
  • Traversal may require additional memory for visited tracking
  • +
  • Large-scale graph processing can be challenging
  • +
+ +

+ Although graphs are highly versatile, efficient implementation and + algorithm selection are important when working with very large graphs. +

+ +
+ +
+ + {/* Summary */} + +
+ +

+ Summary +

+ +

+ A Graph is a non-linear data structure consisting of vertices and + edges that represent relationships between entities. Graphs can be + classified into various types such as directed, undirected, weighted, + unweighted, cyclic, acyclic, connected, disconnected, complete, and + bipartite graphs. +

+ +

+ Graphs can be stored using adjacency matrices or adjacency lists, + each having its own advantages and trade-offs. Traversal techniques + such as Breadth First Search (BFS) and Depth First Search (DFS) + enable systematic exploration of graph structures. +

+ +

+ Due to their ability to model complex relationships, graphs play a + crucial role in computer networks, navigation systems, social media, + recommendation engines, search engines, and many other real-world + applications. +

+ +
+ +
+ + {/* Interview Questions */} + +
+ +

+ Frequently Asked Interview Questions +

+ +
+ +
+

+ 1. What is a Graph? +

+ +

+ A Graph is a non-linear data structure consisting of vertices + (nodes) and edges that represent relationships between vertices. +

+
+ +
+

+ 2. What are vertices and edges? +

+ +

+ Vertices are the entities or nodes of a graph, while edges + represent connections between those vertices. +

+
+ +
+

+ 3. What is the difference between a directed and an undirected graph? +

+ +

+ In a directed graph, edges have direction. In an undirected + graph, edges do not have direction and can be traversed in + both directions. +

+
+ +
+

+ 4. What is a weighted graph? +

+ +

+ A weighted graph assigns numerical values (weights) to edges, + often representing distance, cost, or time. +

+
+ +
+

+ 5. What is an adjacency matrix? +

+ +

+ An adjacency matrix is a square matrix used to represent a + graph where matrix entries indicate the presence or absence + of edges. +

+
+ +
+

+ 6. What is an adjacency list? +

+ +

+ An adjacency list stores a list of neighboring vertices for + each vertex in the graph. +

+
+ +
+

+ 7. Which representation is better for sparse graphs? +

+ +

+ Adjacency lists are generally preferred for sparse graphs + because they require less memory. +

+
+ +
+

+ 8. What is Breadth First Search (BFS)? +

+ +

+ BFS is a graph traversal algorithm that explores vertices + level by level using a queue. +

+
+ +
+

+ 9. What is Depth First Search (DFS)? +

+ +

+ DFS is a graph traversal algorithm that explores a path as + deeply as possible before backtracking. +

+
+ +
+

+ 10. Which data structure is used in BFS? +

+ +

+ BFS uses a Queue data structure. +

+
+ +
+

+ 11. Which data structure is used in DFS? +

+ +

+ DFS uses a Stack or recursion. +

+
+ +
+

+ 12. What is the time complexity of BFS? +

+ +

+ The theoretical time complexity of BFS is O(V + E) when an + adjacency list representation is used, where V is the number + of vertices and E is the number of edges. +

+
+ +
+

+ 13. What is the time complexity of DFS? +

+ +

+ The theoretical time complexity of DFS is O(V + E) when an + adjacency list representation is used. +

+
+ +
+

+ 14. Which traversal guarantees the shortest path in an unweighted graph? +

+ +

+ Breadth First Search (BFS) guarantees the shortest path in + an unweighted graph. +

+
+ +
+

+ 15. Mention some real-world applications of graphs. +

+ +

+ Graphs are used in social networks, computer networks, + navigation systems, search engines, recommendation systems, + and transportation networks. +

+
+ +
+ +
+ +
+ ); +}; \ No newline at end of file