Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'd amend the definition of a graph to be something like, a graph is a set of nodes and a set of their relationships called edges. A tree is a special case of a graph where every node has exactly one relationship with its parent. Essentially, now that we know about N-aray trees where any node can have N many children, what about a datastructure where any node can have N many parents?

This avoids confusion with some non-optimal representations, like a list of trees and a list of edges between those trees. While that's useful for some algorithms (for example, what's called a least-single-common-ancestor (LSCA) tree and auxiliary list of edges, which can be used for efficiently finding the least common ancestor of two or more nodes), it's really bad for most.

The logical build up from linked lists to trees to graphs is useful for practical applications.

It's also useful to know that this is an abstraction and you can store everything in faster data structures like hashmaps and arrays to efficiently lookup tree or graph relationships.



> A tree is a special case of a graph where every node has exactly one relationship with its parent.

Is a triangle a tree, a graph where every node has one parent and one child then?


No, since it's undirected every node has two parents


perhaps you should amend your amendment to make directed graph clear ?

Does that solve your problem?


I intentionally omitted direction from the description to avoid pedantry, because the problem I was highlighting was that "interconnected trees" is very wrong.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: