COMP 200: Elements of Computer Science
Spring 2013

Representing Graphs

Python Representation of Graphs

We have seen that graphs/networks have many uses. So, we want to write programs that use graphs. However, Python, like most programming languages, does not have a notion of graphs built-in. Instead, we need to choose some combination of our other data structures to represent graphs. For the time being, we will simplify the matter by not allowing node labels and edge labels. Thus, we only need to represent graphs such as the following.

A directed graph and undirected graph, respectively. A directed graph has one-way edges, while an undirected graph has bidirectional edges.

We can model both directed and undirected graphs with the following representation. A node is represented by its name. We can use numbers or strings for names. A graph is then represented by a dictionary mapping nodes to its successors, a set of nodes.

For example, to represent the directed graph above, we have

For example, whenever there is a directed edge from node A to a node B, then the dictionary maps A to a list that includes B. Furthermore, we can allow unconnected nodes. For example, there are no edges from node 2, so the dictionary maps 2 to an empty list of nodes.

Similarly, to represent the undirected graph above, we have

Our representation of an undirected graph mentions each edge twice. If there is an edge from node A to node B, then the dictionary maps A to a set that includes B, and it also maps B to a set that includes A.

So, for the following graphs, give the corresponding Python dictionary representation.

Example directed graph and undirected graph, respectively, for you to encode.

Later, we will need to augment this representation to also include labels.

Examples of Building a Graph

Defining graphs “by hand” like this is tedious, especially as the graphs get larger. So, we typically define functions that create graphs of a certain type.

Directed ring graphs

Our first example will be to define a function that creates directed ring graphs. A directed ring graph looks like a big circle, with each node pointing to the next in the circle.

A directed ring graph of 3 nodes has how many edges? Can you draw or list them?

More generally, a directed ring graph of n nodes has how many edges?

Define a function that takes n, the number of desired graph nodes, and creates a directed ring graph. The graph nodes will be named with the numbers 0…n-1. Thus, node 0 has an edge to node 1. Node 1 has an edge to node 2, etc. Node n-1 has an edge to node 0.

Complete graphs

Our next example will be to define a function that creates complete graphs. A complete graph is simply one that has all possible edges. That is, each node has an edge to each of the other nodes. We can have both complete directed graphs and complete undirected graphs.

A complete graph of 3 nodes has how many edges? Can you draw or list them?

More generally, a complete graph of n nodes has how many edges?

Define a function that takes n, the number of desired graph nodes, and creates a complete graph. Assume that we want the graph nodes to be named with the numbers 0…n-1.

Random graph

Often, we want some sort of randomly-generated graph. Typically, we want such graphs to be random, but still maintain some particular properties. The following is one such example.

Write a function directed_random_neighbors(num_nodes, num_edges). It returns a directed graph with the given number of nodes. Each node has the given number of distinct edges chosen randomly. E.g., for node 2, we cannot choose edges (2, 5), (2, 1), and (2, 5), because two of those are not distinct.

Need a hint on how to guarantee distinctness?

Adding Labels to the Graph Representation

Many applications of graphs, including the ones we will focus on, use node or edge labels. We could augment our dictionary-plus-set-based representation to incorporate labels. However, the representation would get more complicated to the point of being somewhat unwieldy. Instead, we will soon use this as motivation to introduce classes and objects in Python, and then use an object-based representation of graphs.