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

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.

4

u/chanamasala4life Jul 02 '20

Just to reinforce this point: Even problems that do not sound like graph-problems at all can be solved with graph algorithms. A few examples: String matching, sorting, resource allocation in OSes, and a lot of other stuff. Then there are the more obvious applications like mapping and traversing traffic routes or also modelling network flow.

1

u/[deleted] Jul 02 '20

Pretty sure anything can be considered as a graph even numerical functions.

18

u/taricorp Jul 01 '20

Many useful domains are conveniently represented as graphs. Two examples offhand, geographic maps can be represented as graphs of roads (and you can use graph algorithms to find directions); control flow graphs are often used in compilers to support code analysis for optimization and similar.

12

u/tocs1975 Jul 02 '20

Graph theory is an extremely rich area of mathematics that can be used to solve a very large number of problems in the physical universe (optimizing wire layouts in microprocessors, road/rail/subway layouts) and digital universe (see GrabCut).

I took something like 7 algorithm classes (one of them called Graph Theory) in college and lots of algorithms make use of graph theory, though you may not realize it.

8

u/[deleted] Jul 02 '20

Ok you have a power station. Find each shortest path to each home.

Second scenario is to find the minimum spanning tree, or the set of edges that connect all nodes with the minimum weight.

Graphs are also used in social media.

8

u/emasculine Jul 02 '20

your brain is a big ol' graph.

2

u/JP4G Jul 02 '20

Graph = linked list on crack