← Back to DSA Course | Chapter 13: Graphs | Lesson 2 of 10

Graph का Representation

Graph representation friendships लिखने के दो तरीकों जैसा है: हर नाम के नीचे उनके friends की एक list, या कौन किसके friends हैं यह मार्क करती एक बड़ी grid।
Syntax
markup
# Adjacency list
graph = {vertex: [neighbor1, neighbor2]}

# Adjacency matrix
matrix = [[0] * n for _ in range(n)]
matrix[i][j] = 1

Adjacency List

एक adjacency list हर vertex के लिए, उन vertices की एक list store करती है जिनसे यह सीधे connected है — यह sparse graphs के लिए compact और efficient है (जहां ज़्यादातर vertices सिर्फ कुछ दूसरों से connect होते हैं), जो practice में सबसे आम case है।

उदाहरण: Adjacency List

#include <iostream>
#include <vector>
using namespace std;
int main() {
	vector<vector<int>> adj(4);
	adj[0].push_back(1); adj[0].push_back(2);
	adj[1].push_back(0);
	for (int v : adj[0]) cout << v << " ";
	return 0;
}
import java.util.*;
public class Main {
	public static void main(String[] args) {
		List<List<Integer>> adj = new ArrayList<>();
		for (int i = 0; i < 4; i++) adj.add(new ArrayList<>());
		adj.get(0).add(1); adj.get(0).add(2);
		System.out.println(adj.get(0));
	}
}
adj = [[] for _ in range(4)]
adj[0].extend([1, 2])
adj[1].append(0)
print(adj[0])
#include <stdio.h>
int main() {
	int adj[4][4] = {0}, adjCount[4] = {0};
	adj[0][adjCount[0]++] = 1;
	adj[0][adjCount[0]++] = 2;
	for (int i = 0; i < adjCount[0]; i++) printf("%d ", adj[0][i]);
	return 0;
}

Adjacency Matrix

एक adjacency matrix एक 2D grid उपयोग करता है जहां rows और columns vertices represent करते हैं, और (i, j) पर cell indicate करता है कि vertex i और vertex j के बीच एक edge मौजूद है या नहीं — यह किसी specific edge के मौजूद होने की जांच को एक instant O(1) lookup बनाता है।

उदाहरण: Adjacency Matrix

#include <iostream>
using namespace std;
int main() {
	int matrix[4][4] = {0};
	matrix[0][1] = 1; matrix[1][0] = 1;
	cout << "Edge (0,1) exists: " << (matrix[0][1] ? "yes" : "no");
	return 0;
}
public class Main {
	public static void main(String[] args) {
		int[][] matrix = new int[4][4];
		matrix[0][1] = 1; matrix[1][0] = 1;
		System.out.println("Edge (0,1) exists: " + (matrix[0][1] == 1 ? "yes" : "no"));
	}
}
matrix = [[0]*4 for _ in range(4)]
matrix[0][1] = matrix[1][0] = 1
print("Edge (0,1) exists:", "yes" if matrix[0][1] else "no")
#include <stdio.h>
int main() {
	int matrix[4][4] = {0};
	matrix[0][1] = 1; matrix[1][0] = 1;
	printf("Edge (0,1) exists: %s", matrix[0][1] ? "yes" : "no");
	return 0;
}

Edge List

एक edge list बस हर edge को एक pair (या एक triple, अगर यह एक weight रखता है) के रूप में store करती है बिना उन्हें vertex से organize किए — यह बनाने के लिए सबसे simple representation है और अक्सर एक list या matrix में convert करने से पहले एक intermediate format के रूप में उपयोग होती है।

उदाहरण: Edge List

#include <iostream>
#include <vector>
using namespace std;
int main() {
	vector<vector<int>> edges = {{0,1,4}, {1,2,7}, {0,2,3}};
	for (auto& e : edges) cout << e[0] << "-" << e[1] << "(w=" << e[2] << ") ";
	return 0;
}
public class Main {
	public static void main(String[] args) {
		int[][] edges = {{0,1,4}, {1,2,7}, {0,2,3}};
		for (int[] e : edges) System.out.print(e[0] + "-" + e[1] + "(w=" + e[2] + ") ");
	}
}
edges = [(0,1,4), (1,2,7), (0,2,3)]
for u, v, w in edges:
    print(f"{u}-{v}(w={w})", end=" ")
#include <stdio.h>
int main() {
	int edges[3][3] = {{0,1,4}, {1,2,7}, {0,2,3}};
	for (int i = 0; i < 3; i++) printf("%d-%d(w=%d) ", edges[i][0], edges[i][1], edges[i][2]);
	return 0;
}

Choosing a Representation

सही representation graph की density और algorithm को सबसे ज़्यादा किन operations की ज़रूरत है इस पर निर्भर करता है: adjacency lists sparse graphs और neighbor-iteration-heavy algorithms को सूट करती हैं, जबकि adjacency matrices dense graphs या specific edges बार-बार जांचने वाले algorithms को सूट करती हैं।

उदाहरण: Choosing a Representation

#include <iostream>
using namespace std;
int main() {
	cout << "Sparse graph + neighbor iteration -> adjacency list; dense graph + edge lookup -> matrix";
	return 0;
}
public class Main {
	public static void main(String[] args) {
		System.out.println("Sparse graph + neighbor iteration -> adjacency list; dense graph + edge lookup -> matrix");
	}
}
print("Sparse graph + neighbor iteration -> adjacency list; dense graph + edge lookup -> matrix")
#include <stdio.h>
int main() {
	printf("Sparse graph + neighbor iteration -> adjacency list; dense graph + edge lookup -> matrix");
	return 0;
}

Complexity Idea

Memory usage representations के बीच sharply अलग है: एक adjacency matrix हमेशा O(V²) space उपयोग करता है चाहे असल में कितने edges मौजूद हों, जबकि एक adjacency list O(V + E) space उपयोग करती है, जो relatively कम edges वाले sparse graphs के लिए कहीं छोटी है।

उदाहरण: Complexity Idea

#include <iostream>
using namespace std;
int main() {
	int V = 1000, E = 3000;
	cout << "Matrix: O(V^2)=" << V*V << " cells; List: O(V+E)=" << V+E << " entries";
	return 0;
}
public class Main {
	public static void main(String[] args) {
		int V = 1000, E = 3000;
		System.out.println("Matrix: O(V^2)=" + (V*V) + " cells; List: O(V+E)=" + (V+E) + " entries");
	}
}
V, E = 1000, 3000
print(f"Matrix: O(V^2)={V*V} cells; List: O(V+E)={V+E} entries")
#include <stdio.h>
int main() {
	int V = 1000, E = 3000;
	printf("Matrix: O(V^2)=%d cells; List: O(V+E)=%d entries", V*V, V+E);
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक बड़े sparse graph के लिए एक adjacency matrix उपयोग करना, जिसे O(V^2) memory चाहिए।
  2. एक undirected edge सिर्फ एक direction में जोड़ना, इसलिए graph असल में directed है।
  3. Adjacency list को V से size देना जब vertices 1..V से numbered हों, इसलिए आखिरी index bounds से बाहर है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.