Comparison Mode
Side-by-side: use case, requirements, complexity, strengths, weaknesses, example problems, and a clear “choose this when…”.
BFS vs DFSDijkstra vs Bellman-FordKruskal vs PrimMerge Sort vs Quick SortHeap vs Priority QueueHash Map vs Tree MapBFS vs DijkstraSliding Window vs Two PointersPrefix Sum vs Segment TreeGreedy vs Dynamic ProgrammingMemoization vs TabulationTarjan vs KosarajuSegment Tree vs Fenwick TreeArray vs Linked ListStack vs QueueQuick Sort vs Heap SortKMP vs Rabin-KarpUnion-Find vs DFSTrie vs Hash MapAVL Tree vs Red-Black Tree
Union-FindSpecialized | DFSGraph Algos | |
|---|---|---|
| Use case | Connectivity that changes as edges are added; "same group?" queries; Kruskal. | Components of a fixed graph, plus anything needing the traversal itself. |
| Requirements | Parent and rank arrays; path compression. | Adjacency list and a visited array. |
| Time complexity | Near O(alpha(n)) amortized per union or find. | O(V + E) total for all components. |
| Space complexity | O(V). | O(V) stack plus adjacency. |
| Strengths | Online: handles interleaved unions and queries; no adjacency list needed; detects the first cycle-creating edge. | Also yields the nodes of each component, paths, cycle structure and works on directed graphs. |
| Weaknesses | Cannot delete edges; gives no traversal order or path; only undirected connectivity. | Must be rerun from scratch after each new edge; recursion depth on large components. |
| Example problems | Number of connected components, redundant connection, accounts merge, min cost to connect all points. | Number of islands, clone graph, surrounded regions, flood fill. |
| Choose this when | Choose Union-Find when edges arrive over time or you answer many "are these connected" queries, and when building an MST. | Choose DFS when the graph is fixed and you also need the members, paths, or directed-graph structure of each component. |