CS 261 Lab K - Graph Layouts

Due Wed Apr 24 at 9:00am

Overview

As we discussed in class, Graphs are a general data structure that can be used for lots of different purposes. One of the interesting features of graphs is that they do not suggest a single spatial layout: the nodes and edges of a graph can be drawn wherever we want without losing any information in the visualization (compare to binary trees, where it mattered whether we drew a branch on the left or right side of its parent!).

Nevertheless, there are certain ways of drawing the graph that are more sensible or aesthetically pleasing: maybe we want to minimize line crossings, or keep similar nodes next to one another. But getting a computer to automatically produce aesthetically pleasing graph visualizations can be quite challenging and algorithmically intensive!

In this lab, you will be implementing a simple force-directed layout algorithm to organize and visualize a random graph.

This lab will be completed in pairs. Review the pair programming guidelines!

Objectives

Necessary Files

You will need to download the copy of the GraphLab.zip file. This file contains two classes you will need to import into Eclipse. Take a moment to read through these classes and see how they work!

Details

The first step in this lab is to import the provided classes and run them. Play around with the given UI; can you place the nodes in a random graph so that the graph is "aesthetically pleasing"? How did you decide where to place the nodes? Can you translate that into a computer algorithm?

Have you actually played with the program? I'll wait...

.

..

...

In this lab you will just be implementing a single method: the layout() method of the VisualGraph class. In particular, your method should implement the Fruchterman-Reingold algorithm (rehosted original paper) for performing a force-directed layout.

Force-Directed Layouts

One of the most common types of algoritms for doing graph layouts are force-directed. These algorithms treat the graph as being under a set of physical "forces" that move the nodes around the graph until they've settled into an equilibrium state. For example, we may treat the nodes as circular "magnets" that push each other away, but treat the edges as kinds of "springs" that draw connected nodes back to one another---force-directed layouts are also known as spring-and-damper algorithms.

We let each node push each other a little ways, let each edge pull its nodes a little ways, and keep looping until the graph looks good. Because we're using physical forces as our model (e.g., magnitism, gravity, spring-tension), we can often borrow from simple algorithms like Columb's Law (the "inverse square law") and Hooke's Law to determine how far and in what direction each node should move.

These algorithms are fairly simple to implement but produce interesting behaviors (and nice layouts). However, they have the bad habit of having a high run-time. Because each node influences every other node, they can often be O(n2) or O(n3).

Fruchterman-Reingold

The Fruchterman-Reingold algorithm is one of the simplest and earliest force-directed layout algorithms. It has the benefits of working well on small to medium-sized graphs and converging relatively quickly (within 100 iterations usually). In this model, each node applies a repelling force upon every other node inversely proportional to the square of the distance between them. Simpler, edges act as springs that apply an attracting force between nodes that is proportional to the square of the distance between them.

The pseudocode for this algorithm can be found on page 4 of the original paper and has also been copied below (as an image, because formatting this kind of math in HTML is a pain. Typos are not mine!). Note that you will need to make a couple of modifications to this algorithm!

You will need to make a few modifications to this algorithm.

Implementing this algorithm within the provided UI is the full extent of this lab! There are a couple of other extensions below however, in case you get done early. :)

A Note about Vectors

The above algorithm treats forces and positions as mathematical vectors. You should have encountered this concept previously in a math course, but if you need a refresher there is a pretty good one here. A few notes:

If you have any questions on this math, be sure to ask!

Submitting

Submit all of your classes to the LabK submission folder on hedwig. Make sure you upload your work to the correct folder! The lab is due at the start of class on the day after the lab.

Remember to fill out your lab partner evaluation!

Extensions

As always, there are lots of good extensions you might consider if you have time:

Grading

This assignment will be graded based on approximately the following criteria: