Hello! I've been using an adj array to describe a graph. As in, each node has many children and many parents. This is synonymous to a DAG (https://en.wikipedia.org/wiki/Directed_acyclic_graph)
Traversing and modifying this graph has been tough without reference counting or interior mutability. After lots of hacky tricks (multiple clones, rerunning functions on the clones since the clone is passed back to the adjarray later, anti-programming practices, etc.) I've decided to finally go ahead and try to implement Rc<Refcell<T>>. The existing AdjArray is really a hashmap named ReqHolder described as (alongside Req):
#[derive(Debug, Clone)]
struct ReqHolder {
reqs: HashMap<i32, Req>
}
pub struct Req {
pub id: i32,
pub name: String,
pub children: Vec<i32>,
pub parents: Vec<i32>,
pub in_analysis: bool,
}
where Req has children and parents that hold indices into this hashmap. Because of Rc<Refcell<T>>, should children instead hold references instead of indices?
If children holds references instead of indices, I expect due my crappy programming skills, to run into lots of runtime errors. However, I'll no longer need this struct as long as I have a reference to the root Req. So, should I keep ReqHolder and have the value be reqs: HashMap<i32, Rc<RefCell<T>>>, or should I modify Req and have children + parents be Vec<Rc<RefCell<T>>>? Much appreciated for any suggestions!
In mathematics, particularly graph theory, and computer science, a directed acyclic graph (DAG) is a directed graph with no directed cycles. That is, it consists of vertices and edges (also called arcs), with each edge directed from one vertex to another, such that following those directions will never form a closed loop. A directed graph is a D...
so much performance lost