BCA / B.Tech 12 min read

Graph in Hindi

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: DS "Data Structure" Notes in Hindi | डीएस नोट्स हिंदी में (BCA / B.Tech Syllabus)
  • • मुख्य विषय: Graph in Hindi - General Topics
  • • अवलोकन: Quick Answer: Data structure में Graph एक non-linear data structure है, जो vertices (nodes) और उनको आपस में जोड़ने वाले edges का collection होता है। इस...
  • • मुख्य अवधारणाएं: Graph DS, Directed undirected vertices edges Hindi, Representations graph,
Quick Answer: Data structure में Graph एक non-linear data structure है, जो vertices (nodes) और उनको आपस में जोड़ने वाले edges का collection होता है। इसे गणितीय रूप में G = (V, E) लिखते हैं, जहाँ V vertices का set है और E edges का set है। Graph का इस्तेमाल Google Maps, social media connections और computer networks जैसे complex networks को model करने के लिए किया जाता है।

Graph in Data Structure in Hindi: Graph क्या है?

कंप्यूटर साइंस में data store करने के लिए array, linked list, stack और queue जैसे linear data structures होते हैं, जिनमें data एक sequence में रहता है। Hierarchical data के लिए Tree data structure का इस्तेमाल होता है।

लेकिन real world में बहुत सा data ऐसा होता है जो न तो linear होता है और न ही parent-child hierarchy में बंधा होता है। जैसे शहरों के बीच सड़कें, routers के बीच cables, या social network पर दोस्तों के connection। इस तरह के network relationships को दर्शाने के लिए Graph का इस्तेमाल करते हैं। Graph एक non-linear data structure है।

Graph दो मुख्य components से बनता है:

  • Vertices (Nodes): यह data elements को दर्शाते हैं (जैसे शहर, computer, या user)। इसे V से लिखते हैं।
  • Edges (Links): यह दो vertices के बीच के connection या relation को दर्शाती हैं। इसे E से लिखते हैं।

Formula रूप में graph को ऐसे लिखते हैं:

G = (V, E)
V = {A, B, C, D}
E = {(A,B), (B,C), (C,D), (D,A), (A,C)}
Graph in Hindi

Graph data structure का basic structure: vertices (nodes) और edges.

Advertisement

Graph की Basic Terminology

Term मतलब Example
Vertex (Node) Graph का basic data point Node A, Node B
Edge दो vertices को जोड़ने वाली line Edge (A, B)
Degree किसी node से जुड़ी कुल edges की संख्या अगर A से 3 edges जुड़ी हैं, तो degree(A) = 3
In-Degree Directed graph में node की तरफ आने वाली edges Arrow pointing inside
Out-Degree Directed graph में node से बाहर जाने वाली edges Arrow pointing outside
Path Vertices का sequence जिससे एक node से दूसरे तक पहुँचते हैं A → B → C
Cycle ऐसा path जहाँ starting और ending node एक ही हो A → B → C → A
Adjacent Nodes दो nodes जो सीधे एक edge से जुड़े हों (पड़ोसी nodes) A और B के बीच edge है तो दोनों adjacent हैं

Characteristics of Graph in Data Structure (Graph के Features)

Graph बाकी data structures की तुलना में सबसे flexible माना जाता है। इसके मुख्य features ये हैं:

  • Non-linear Data Structure: इसमें elements सीधी line में नहीं होते। कोई भी node किसी भी node से जुड़ सकता है।
  • Multiple Paths: दो nodes के बीच एक से ज्यादा paths हो सकते हैं। यह routing और navigation के लिए जरूरी है।
  • No Fixed Root: Tree की तरह इसमें कोई root node नहीं होता। Traversal किसी भी node से शुरू हो सकता है।
  • Cycles की संभावना: Graph में loop या cycle बन सकते हैं।
  • Dynamic Relationships: यह one-to-one, one-to-many, और many-to-many relationships को store कर सकता है।
  • Direction और Weight: Edges पर direction (दिशा) और weight (distance, cost) store किया जा सकता है।

Types of Graph in Data Structure (Graph के Types)

Edges की दिशा, वजन और connection के आधार पर graph के मुख्य types नीचे दिए गए हैं:

Graph in Hindi

Data structure में graph के मुख्य types: directed, undirected, weighted और cyclic graph.

1. Undirected Graph

इस graph में edges की कोई दिशा नहीं होती। अगर A और B के बीच edge है, तो आप A से B और B से A दोनों तरफ जा सकते हैं।

(A)-------(B)
 |         |
(C)-------(D)

Example: Facebook पर दोस्ती। दोनों users एक-दूसरे के friend होते हैं।

2. Directed Graph (Digraph)

Directed graph में हर edge की एक दिशा (arrow) होती है। अगर edge A से B की तरफ है (A → B), तो आप सिर्फ A से B जा सकते हैं, B से A नहीं।

(A) ------> (B)
 ^           |
 |           v
(C) <------ (D)

Example: Instagram या Twitter पर follow करना। आप किसी user को follow करते हैं, लेकिन ज़रूरी नहीं कि वो भी आपको follow करे।

3. Weighted Graph

जब graph की हर edge के साथ कोई value (weight या cost) जुड़ी हो, तो उसे Weighted Graph कहते हैं। यह value दूरी (distance), समय (time), या network cost हो सकती है।

       5 km
(Delhi)-----(Noida)
   |           |
10 km       8 km
   |           |
(Gurugram)--(Faridabad)
       12 km

Example: Google Maps, जहाँ दो स्थानों के बीच दूरी और समय दिया होता है।

4. Unweighted Graph

इसमें edges पर कोई weight नहीं होता। सभी edges की cost बराबर मानी जाती है। सिर्फ यह देखा जाता है कि connection है या नहीं।

5. Cyclic Graph

ऐसा graph जिसमें कम से कम एक cycle मौजूद हो, यानी किसी node से चलकर edges के रास्ते वापस उसी node पर पहुँचा जा सके।

6. Acyclic Graph in Data Structure

जिस graph में कोई cycle न हो, उसे Acyclic Graph कहते हैं। इसमें किसी भी node से चलकर वापस उसी node पर नहीं आ सकते।

Data structure में सबसे महत्वपूर्ण प्रकार DAG (Directed Acyclic Graph) है। DAG का इस्तेमाल task scheduling, compiler dependency tree, और build systems में होता है। Tree भी एक connected acyclic graph ही होता है।

7. Complete Graph

वह graph जिसमें हर vertex बाकी सभी vertices से सीधे जुड़ा होता है। n vertices वाले undirected complete graph में कुल edges की संख्या n * (n - 1) / 2 होती है।

8. Connected और Disconnected Graph

  • Connected Graph: ऐसा graph जिसमें हर pair of vertices के बीच कम से कम एक path मौजूद हो।
  • Disconnected Graph: जिसमें graph दो या अधिक अलग-अलग components में बंटा हो और उनके बीच कोई path न हो।

Graph Representation: Memory में Graph कैसे Store करते हैं?

Memory में graph को store करने के 2 मुख्य तरीके हैं:

  1. Adjacency Matrix (Sequential Representation)
  2. Adjacency List (Linked Representation)

1. Adjacency Matrix

Adjacency Matrix एक V x V size का 2D array होता है, जहाँ V vertices की संख्या है।

  • अगर vertex i और vertex j के बीच edge है, तो Matrix[i][j] = 1 (weighted graph में edge का weight)।
  • अगर edge नहीं है, तो Matrix[i][j] = 0।
  • Undirected graph के लिए matrix हमेशा symmetric होती है।

2. Adjacency List

इसमें array of linked list का उपयोग होता है। Array का size V होता है। Array के हर index i पर एक linked list होती है, जिसमें vertex i से जुड़े सभी adjacent nodes store रहते हैं।

Adjacency Matrix vs Adjacency List

Comparison Point Adjacency Matrix Adjacency List
Memory Space O(V2) - ज्यादा memory लगती है O(V + E) - memory efficient
Data Structure 2D Array Array of Linked Lists
Edge Lookup (u, v) O(1) - बहुत तेज O(degree(u)) - list scan करनी पड़ती है
New Vertex Add करना O(V2) - पूरा matrix resize करना पड़ता है O(1) - आसान
कब इस्तेमाल करें? Dense Graph (जहाँ edges बहुत ज्यादा हों) Sparse Graph (जहाँ edges कम हों)

Solved Example: Graph Representation Trace

मान लीजिए 4 vertices वाला एक undirected graph है:

  • Vertices: V = {0, 1, 2, 3}
  • Edges: E = {(0, 1), (0, 2), (1, 2), (2, 3)}
   (0)-------(1)
     \       /
      \     /
       \   /
        (2)-------(3)

Step 1: Adjacency Matrix Trace

4 vertices के लिए 4 x 4 table बनाते हैं:

Vertex 0 1 2 3
0 0 1 1 0
1 1 0 1 0
2 1 1 0 1
3 0 0 1 0

Row 0 में col 1 और 2 पर 1 है, क्योंकि 0 से 1 और 2 जुड़े हैं। इसी तरह बाकी rows भरी गई हैं।

Step 2: Adjacency List Trace

हर node के पड़ोसियों की linked list बनाते हैं:

[0] → 1 → 2 → NULL
[1] → 0 → 2 → NULL
[2] → 0 → 1 → 3 → NULL
[3] → 2 → NULL

Adjacency List में केवल actual edges के लिए memory लगती है, फालतू 0 store नहीं होते।

Advertisement

Graph vs Tree: मुख्य अंतर

Feature Graph Tree
Definition Vertices और Edges का general collection है Hierarchical structure है जिसमें cycle नहीं होती
Root Node कोई root node नहीं होता ठीक एक root node होता है
Cycles Cycles और loops हो सकते हैं कोई cycle नहीं हो सकती (Strictly Acyclic)
Edges की संख्या कितनी भी edges हो सकती हैं N nodes के लिए हमेशा ठीक N - 1 edges होती हैं
Structure Network model Hierarchical (Parent-Child) model
Traversal BFS और DFS Inorder, Preorder, Postorder
Visited Flag Cycle से बचने के लिए visited array जरूरी है Visited array की जरूरत नहीं होती

Graph Traversal: BFS और DFS

Graph के सभी vertices को एक-एक बार visit करने के process को Graph Traversal कहते हैं। Cycle की वजह से infinite loop से बचने के लिए visited[] array का इस्तेमाल करते हैं। Detailed algorithms के लिए देखें: Graph Traversal Algorithms.

1. BFS (Breadth First Search)

  • यह level by level (चौड़ाई में) traversal करता है।
  • यह Queue data structure का इस्तेमाल करता है।
  • Unweighted graph में shortest path ढूँढने के लिए सबसे अच्छा है। Time complexity: O(V + E)।

2. DFS (Depth First Search)

  • यह गहराई में एक branch के अंत तक जाता है और फिर backtrack करता है।
  • यह Stack data structure या recursion का इस्तेमाल करता है।
  • Cycle detection और topological sort में इसका उपयोग होता है। Time complexity: O(V + E)।

Advantages और Disadvantages of Graph

Advantages Disadvantages
Real-world network relationships को आसानी से model करता है Adjacency matrix में memory O(V2) लगती है
Shortest path problems solve करने के लिए best है Graph algorithms की time complexity ज्यादा हो सकती है
Complex networks को manage करना सरल बनाता है Cycle होने पर infinite loop का खतरा रहता है

Real-World Applications of Graph

  • Google Maps और GPS: चौराहों को vertices और सड़कों को edges मानकर shortest route निकाला जाता है।
  • Social Networks: Facebook और LinkedIn पर friend recommendations graph algorithms से निकलते हैं।
  • Web Search Engines: Google PageRank पूरे web को एक graph मानकर pages को rank करता है।
  • Network Routing: Internet routers packets को भेजने के लिए shortest path graph algorithms चलाते हैं।
  • Operating Systems: Deadlock detection में Resource Allocation Graph (RAG) का इस्तेमाल होता है।

Exam में कैसे लिखें (5 marks)

Exam में full marks पाने के लिए answer इस structure में लिखें:

  1. Definition: Graph एक non-linear data structure है, जो vertices और edges का collection है: G = (V, E)।
  2. Diagram: 4 nodes का एक साफ undirected graph बनाकर V और E के sets लिखें।
  3. Basic Terms: Vertex, Edge, और Degree की short definition दें।
  4. Types: Directed, Undirected, Weighted, Cyclic, और Acyclic graph के नाम और छोटे diagrams बनाएँ।
  5. Representation: Adjacency Matrix और Adjacency List का एक छोटा example बनाकर दिखाएँ।
  6. Applications: Google Maps और Social Network routing का उदाहरण लिखें।

Common Mistakes Students Make (आम गलतियाँ)

  • Graph में Root Node ढूँढना: Graph में Tree की तरह कोई root node नहीं होता। Traversal किसी भी node से शुरू हो सकता है।
  • Visited Array भूलना: Traversal code में visited array न लगाने से program cyclic graph में loop में फँस जाता है।
  • Matrix Size गलत लिखना: Adjacency matrix का size V x V होता है, V x E नहीं।
  • Tree को Cyclic लिखना: Tree हमेशा acyclic graph होता है, उसमें cycle नहीं हो सकती।

Exam के लिए Important Questions

Short Questions (2–3 marks)

  1. Graph क्या है? इसका mathematical formula लिखिए। (G = (V, E), जहाँ V vertices और E edges का set है)
  2. Directed और Undirected graph में मुख्य अंतर क्या है? (Directed में direction arrow होता है, undirected में two-way connection होता है)
  3. Complete graph किसे कहते हैं? 4 nodes वाले complete graph में कितनी edges होंगी? (जहाँ हर node बाकी सभी nodes से जुड़ा हो; edges = 4*3/2 = 6)
  4. Acyclic graph क्या होता है? (ऐसा graph जिसमें कोई cycle न हो; जैसे Tree या DAG)
  5. In-degree और Out-degree क्या हैं? (In-degree = अंदर आने वाली edges; Out-degree = बाहर जाने वाली edges)

Long Questions (5–10 marks)

  1. Data structure में Graph क्या है? इसके types और applications detail में समझाइए।
  2. Graph को memory में store करने के Adjacency Matrix और Adjacency List तरीकों को example के साथ समझाइए।
  3. Graph Traversal क्या है? BFS और DFS algorithms के काम करने के तरीके को example के साथ समझाइए।
  4. Graph और Tree के बीच मुख्य अंतर स्पष्ट कीजिए।

FAQs

Graph in data structure in Hindi क्या है?

Data structure में graph एक non-linear data structure है जो vertices (objects) और edges (connections) से मिलकर बनता है। इसका उपयोग computer networks, maps और social media data को model करने में होता है।

Acyclic graph in data structure क्या होता है?

Acyclic graph वह graph है जिसमें कोई closed loop या cycle नहीं बनता। यानी किसी vertex से शुरू करके वापस उसी vertex पर नहीं पहुँचा जा सकता। Data structure का Tree भी एक acyclic graph होता है।

Tree और Graph में सबसे बड़ा अंतर क्या है?

Tree में ठीक एक root node होता है और cycle कभी नहीं होती। Graph में कोई root node नहीं होता और cycles हो सकते हैं। N nodes वाले tree में हमेशा N - 1 edges होती हैं।

Adjacency Matrix और Adjacency List में कौन बेहतर है?

Dense graph (जहाँ बहुत ज्यादा edges हों) के लिए Adjacency Matrix बेहतर है क्योंकि इसमें O(1) में edge check हो जाती है। Sparse graph (जहाँ edges कम हों) के लिए Adjacency List बेहतर है क्योंकि यह memory बचाती है।

Graph की degree क्या होती है?

किसी vertex से जितनी edges जुड़ी होती हैं, वह उस vertex की degree कहलाती है। Directed graph में अंदर आने वाली edges को In-degree और बाहर जाने वाली edges को Out-degree कहते हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: Data structure में Graph एक non-linear data structure है, जो vertices (nodes) और उनको आपस में जोड़ने वाले edges का collection होता है।
DS "Data Structure" Notes in Hindi | डीएस नोट्स हिंदी में के पाठ्यक्रम में Graph in Hindi एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको Graph in Hindi के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement