Tensorum
YouTubeसीखना शुरू करें

Graph

BFS/DFS, topological sort, shortest paths, MST, union-find, bridges और SCCs — पढ़ाई के क्रम में।

DSAएडवांस्ड69 घंटे25 पैटर्न · 96 प्रॉब्लम
0%
आपकी प्रगति

प्रॉब्लम और केस स्टडी के नाम, कोड और तकनीकी कीवर्ड अंग्रेज़ी में ही रखे गए हैं।

मूल विचार

Unweighted shortest paths के लिए BFS, non-negative weights के लिए Dijkstra, dependencies के लिए topological sort, dynamic connectivity के लिए DSU, bridges और SCCs के लिए Tarjan।

कब इस्तेमाल करें

  • Grids, dependencies, networks, word ladders
  • Shortest path, connectivity, ordering, cycles
  • 'States' और 'moves' वाली कोई भी चीज़ (state-space BFS)

टेम्पलेट

import heapq
def dijkstra(adj, src):
    dist = {src: 0}
    pq = [(0, src)]
    while pq:
        d, u = heapq.heappop(pq)
        if d > dist.get(u, float('inf')):
            continue
        for v, w in adj[u]:
            nd = d + w
            if nd < dist.get(v, float(&#39;inf')):
                dist[v] = nd
                heapq.heappush(pq, (nd, v))
    return dist

Complexity

BFS/DFS O(V + E); Dijkstra O(E log V); DSU प्रति operation ~O(α(n))।

आम गलतियाँ

  • BFS में push की जगह pop पर visited चिह्नित करना (duplicates)
  • Negative edges के साथ Dijkstra
  • Directed और undirected cycle detection को अलग logic चाहिए

पहचान के संकेत

DFS/BFSDSUTopological SortShortest PathMSTSCCBridgesEulerianGraph DP