COMP 200: Elements of Computer Science
Spring 2013

Representing Graphs

Practice with Graphs

These exercises are meant to give you more practice with graphs and sets, both conceptually and in code. As such, they should help you prepare for the current assignment.

As a running example, we will use one of the directed graphs from the previous class exercises.

A directed graph

Of course, the previous class provided additional examples, including programs that would generate additional examples. You should also use those when testing today's programs.

Exercises

First, let's consider some graph representation issues.

Before the next exercises, let's introduce some definitions. A directed graph node's out-degree is the number of edges pointing out of that node. A directed graph node's in-degree is the number of edges pointing into that node. For example, the out-degree of our example directed graph's node 3 is 3, and its in-degree is 2. For undirected graphs, we instead similarly define the node degree, instead of differentiating between the out- and in-degrees.

Next, we want to test if two sets are equal, i.e., have the same elements. Of course, this is really easy with == or !=. But, let's pretend that our local CodeSkulptor implementor has accidentally broken the built-in set-equality operations. Alas, you have witnessed that such mistakes happen. Of course, that's just an excuse for you to think about how you can use the remaining set operations.