This lesson introduces a new data structure: graphs. Graphs are extremely useful, both for modeling certain types of data, and for enabling certain algorithms. In fact, trees, which we’ve been studying previously, are themselves a type of graph. Graphs are also great for practice with recursion. So let’s get started!
Wikipedia defines a graph as:
A graph data structure consists of a finite (and possibly mutable) set of vertices (also called nodes or points), together with a set of unordered pairs of these vertices for an undirected graph or a set of ordered pairs for a directed graph. These pairs are known as edges (also called links or lines), and for a directed graph are also known as edges but also sometimes arrows or arcs. The vertices may be part of the graph structure, or may be external entities represented by integer indices or references.
Let’s look at some diagrams that will help visualize this new data structure, and introduce some important terminology.
Graphs can be used to model a variety of real-world entities, which is part of what makes them so useful to computer scientists. Some examples include:
Let’s look at these example a bit more:
Depending on the properties of their edges, graphs can be directed or undirected, weighted or unweighted. We’ll focus our attention on undirected unweighted graphs. But let’s discuss the differences briefly.
We’ve discussed trees previously, and it turns out that trees are one specific type of graph. Now that we have been introduced to graphs, we can define a tree more precisely.
Previous we introduced recursion, a problem solving technique that works by breaking down large problems into smaller pieces, solving them, and then combining the results. Graphs are another data structure that we frequently will examine recursively. Let’s see why:
In addition, like trees, compared to lists and arrays and String
s, graphs can be hard to work with using iterative solutions!
Recursion is a much better approach…
To help you prepare for our next homework problem, let’s discuss the recursive approach to counting the number of nodes in a graph. Specifically, we’ll review the idea of graph traversal, and how not to get stuck along the way.
Create a public class named GraphSize
that provides a single static method size
.
size
receives an unweighted graph containing Integer
values using cs1.graphs.UnweightedGraph
, so an
UnweightedGraph<Integer>
.
To complete this problem you'll need to implement graph traversal.
From a given node, you want to visit all of its neighbors except any nodes that you've already visited.
If you use a Set
to track the nodes that you've visited, then you can simply return the size of that Set
when
you are finished.
We've provided some starter code to get you off on the right track.
For reference, cs1.graphs.UnweightedGraph
has the following public properties:
And cs1.graphs.GraphNode
has the following public properties:
Create a public class Connections
with a single public constructor that accepts a String
.
The String
contains, in CSV format, a list of cities and other cities that they are connected to.
So, for example, the input:
Champaign,Chicago,St. Louis
Chicago,Detroit,Milwaukee
St. Louis,Champaign,Cincinnati
Means that Champaign is connected to Chicago and St. Louis, and that Chicago is connected to Detroit and
Milwaukee, and so on.
Essentially the CSV serializes a directed graph, where the first item on each line is a node and the other items
represent other nodes that it is connected to.
This is one way of serializing a directed, unweighted graph.
If the String
passed to the constructor is null
, throw an IllegalArgumentException
.
Make sure to trim
all the String
s that you extract from the CSV.
Your class should parse this String
and provide a single instance method isConnected
.
isConnected
accepts two String
s and returns true
if the first city is connected to the second based on the
graph passed to the constructor.
So, given the input above, isConnected("Champaign", "Chicago")
would return true
, but
isConnected("Chicago", "Champaign")
would return false
.
(Note that the graph is not necessarily symmetric.)
If either String
passed to isConnected
is null
, or if you don't have connection information for the source,
you should throw an IllegalArgumentException
.
Note that only the cities that appear first on each line in the CSV should be treated as cities you have
connection information for.
So, for example, even though "Detroit" appears as a destination from "Chicago" in the data set above, we do not
have a line starting with "Detroit", and therefore a call to isConnected
with "Detroit" as the first parameter
should throw an IllegalArgumentException
.
Our suggestion is to use a private field to store a data structure that you populate in your constructor.
This will keep your isConnected
method simpler.
Good luck, and have fun!
Need more practice? Head over to the practice page.