LEARNING JOURNAL

I created this learning journal to practice writting and to help me learn by writting everything down. Source code here

2/15/2024

Rotting Oranges - 3rd attempt

THE PROBLEM


Essentially, this problem is finding the shortest path to all reachable vertices from a number of given vertices in a directed graph. The vertices are oranges, the given vertices are the rotted one at the beginning, the edge is formed between all the neighboring oranges, the direction is from either rotted to fresh or fresh to fresh. There are two catches. First, you need to count the steps of the shortest path and, second, you need to know whether or not you can reach all the vertices given the vertices at the beginning. To find the shortest path in a directed graph, we can use a queue to perform breadth first search algorithm. Basically, in each round, you take vertices from the queue, one by one, find its neighbor vetices, push those that is a fresh orange (because the direction only goes from rotted to fresh, not rotted to rotted). After each round, you increase the timer by one. You keep doing that until you run out of vertices to push to the queue. Then you return the timer. However, how do you know that you reaches all of the fresh oranges to return the timer? (Otherwise, you need to return -1 indicating that you can not reach all of the fresh oranges). The simple solution is that you can iterate the grid at the beginning to get how many fresh oranges there are (you need to do that anyway to add all of the rotten oranges originally to the queue). Then, after each time you see a fresh orange on your breadth first search way, you just decrease the number of fresh oranges by one. At the end, you can check whether or not that number goes to 0.

Revised by ChatGPT

This problem essentially involves finding the shortest path to all reachable vertices in a directed graph, where vertices represent oranges, and edges exist between neighboring oranges. The initial vertices are the rotten oranges and the direction of traversal is from rotten to fresh or from fresh to fresh.

There are two main challenges: counting the steps in the shortest path and determining whether all vertices (oranges) can be reached from the initial set of vertices (rotten oranges). To solve this, we can use a queue to implement a breath-first search (BFS) algorithm. In each round of BFS, we dequeue vertices one by one, find their neighboring vertices, and enqueue those that are fresh oranges. After each round, we increment a timer to count the steps. This process continues until the queue is empty.

To determine whether all fresh oranges have been reached, we initially count the number of fresh oranges in the grid. Each time we encounter a fresh orange during the BFS, we decrement this count. If, at the end of the BFS, the count of fresh oranges is zero, it means all fresh oranges have been reached, and we return the timer value. Otherwise, we return -1 to indicate that not all fresh oranges could be reached.

This approach ensures that we find the shortest path to all reachable fresh oranges while also verifying that all fresh oranges have been infected.