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 ప్రతి op కి ~O(α(n)).

సాధారణ పొరపాట్లు

  • BFS లో push బదులు pop వద్ద visited గుర్తించడం (duplicates)
  • Negative edges తో Dijkstra
  • Directed, undirected cycle detection కి వేర్వేరు logic కావాలి

గుర్తించే సంకేతాలు

DFS/BFSDSUTopological SortShortest PathMSTSCCBridgesEulerianGraph DP