Fading Coder

One Final Commit for the Last Sprint

Validating a Course Schedule with Topological Ordering: BFS and DFS Solutions

You are given numCourses labeled from 0 to numCourses - 1 and a list of prerequisite pairs prerequisites where prerequisites[i] = [a, b] means that course a depends on course b (i.e., b must be taken before a). Determine if its possible to finish all courses, that is, whether the dependency graph co...

Lilypad Pond [USACO07FEB] – Shortest Path Solution

A great problem that requires building a graph in a non-obvious way. Problem link At first glance, it's not obvious to build a graph, but with some thought it's not too hard. The idea is to set edge weights to 0 for moving onto lily pads and 1 for moving on to water cells. Then just run Dijkstra. Th...

Breadth-First Search Algorithms: Trees, Graphs, and Matrices

Core Concepts and Queue Usage Breadth-First Search (BFS) is fundamental for traversing graphs and trees, particularly useful for level order traversal, identifying connected components, topological sorting, and finding the shortest path in unweighted graphs (where all edges possess a length of 1). W...

Counting Node Pairs Requiring Two Hubs in a Connected Graph

Given a connected undirected graph with $n$ nodes and $m$ edges, and two distinct nodes $a$ and $b$, count unordered pairs $(u, v)$ where $u < v$ such that: $u \neq a$, $u \neq b$, $v \neq a$, $v \neq b$ Every path from $u$ to $v$ passes through both $a$ and $b$ Approach The solution involves ide...

Determining the Champion Team in a Tournament

Problem: Find Champion I In a tournament with n teams, numbered from 0 to n - 1, you are given a square boolean matrix grid of size n x n. For all pairs (i, j) where 0 <= i, j <= n - 1 and i != j, if grid[i][j] == 1, then team i is stronger than team j. Otherwise, team j is stronger than team...