The maximum number of edges in a graph of bounded dimension, with applications to ring theory