I'm trying to use the concept of DFS traversal to go through a cycle, and attempt to get a balance of 0 in the end. Each student either owes or is owed some money, so I'm trying to go through all of said students and make sure each one can pay the other.
The input for your algorithm consists of two lists $owing$ and $friendship$. The length of the list $owing$ is the total number of students; $owing[i]$ is the amount that student $i$ is owing $(0 \leq i \leq len(owing)-1)$, and it is an integer than can be positive (owing money), negative (owed money) or zero. The list $friendship$ is a list of pairs that represent the remaining friendships, i.e., a pair $(i, j)$ means student $i$ and student $j$ are still friends and can still talk and give money to each other. Devise an efficient algorithm which, given the two input lists, returns whether or not it is possible for everyone to get even.
I've attempted to write psuecode for it, and it runs in O(|V + E|), the normal runtime of a DFS. I was wondering if it's correct, and whether there's a way to have it run more efficiently?
The general logic behind it is, visited all members in the owing list but paying the debts through friendship. Check that the total sum all frienships in this path can total up to 0. If it doesn't returns failure.