r/AskComputerScience • u/[deleted] • 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?
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
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
2
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.