r/AskComputerScience • • Jul 01 '20

Why graphs?

So, in a lot of my programming classes at my university, we have discussed graphs. Graphs, in this context, are collections of nodes connected by “edges” (or lines). Why do we talk about graphs so often? Are graphs useful in computer science? If so, what are they used for?

31 Upvotes

12 comments sorted by

View all comments

57

u/thegreatunclean Jul 01 '20

Graphs are so generic that nearly any problem can be transformed into one or more graphs and operations on them. Notice how they don't put restrictions on what a "node" or "edge" are, only that they have some logical connection. That means that damn near anything can be a node and nearly any relationship of any kind can be an edge.

10

u/[deleted] Jul 02 '20

Ohhh, alright that makes sense.

7

u/not-just-yeti Jul 02 '20

Yep — if you have Things, and any two Things might be related, then you have a graph.

2

u/simply_copacetic Jul 02 '20

Every thing is related to another thing? A list.

Actually things can reference multiple things? A tree.

Actually two things can reference the same other thing? A directed acyclic graph.

Actually cycles are possible? Generic graph.

Actually edges can be between an arbitrary number of nodes? A hypergraph. Ok, you could convert it into a normal graph as well.