Held Wednesday, December 1, 1999
Overview
Today, we move on to another data structure: the graph. Graphs
are useful for modeling many different kinds of problems.
Notes
- Are there any more questions on
exam 3?
- Problem A:
- Most people assume that given two values, a and b, one of
the following four options hold: (1) a < b; (2) a = b;
(3) a > b; or (4) a and b cannot be compared.
- The annual Mathematical Contest in Modeling is
set for February 4-7, 2000. For those of you who are unfamiliar
with this contest, teams of three students work for a
weekend on one of two real-world problems. At the end
of the weekend each team submits a report. Teams from around the
world particpate though most of the teams are from the
United States. Grinnell College has done very well in the past;
the Math/CS showcase displays several certificates and two plaques.
In 1999 Grinnell hosted four teams.
For examples of some problems from the last few years, see
http://www.math.grin.edu/mcm-1999.html
.
If you have interest or questions, please speak to Dr.
Chamberland ASAP. Experience has shown that the optimal
team has reasonable talent in mathematics, computing and
writing (not necessarily in that order!) so talents of
various sorts could be useful.
Contents
Summary
- Modeling and pictures
- Some sample modeling problems
- Introduction to graphs
- Terminology
- Methods
- Common implementations
- As you have (hopefully) noted, computer science is not (always) done
for its own sake. Often, we write computer programs to help us
with ``real world'' problems.
- Sometimes, we answer real-world questions by modeling them
with one or more data structures.
- For example, we might model the consumer's model of a store with
a series of queues (plus some methods for choosing how long
between service and entry in each queue).
- There are a number of problems that require nonlinear data structures,
and which need fewer restrictions than trees require.
- Suppose we are building a new campus network. Determine the
least-cost way to connect building so that the network stays
active, even if we cut any one connection. (Alternately, given
the existing system, determine if any one connection will disconnect
parts of the network.)
- In the telephone system, find the least congested route between
two phones, given connections between switching stations.
- On the Web, determine if there is a way to get to one page from
another, just by following normal links.
- ``Six degrees of Kevin Bacon''.
- While driving, find the shortest path from one city to another.
- As a traveling salescritter who needs to visit a number of cities,
find the shortest path that includes all the cities.
- On the Internet, determine how many connections between
computers you can eliminate and still make sure that
every computer can reach every other computer.
(The Internet was originally funded by the military; they wanted
it robust even if we were attacked.)
- Determine an ordering of courses so that you always take
prerequisite courses first.
- We often draw pictures to help us think about these problems. I
may ask you to draw pictures.
- These, and many other problems, can be modeled by a data structure
known as the graph.
- Previously, we've used data structures to improve particular tasks.
Here, we use the data structure to motivate the algorithms.
- Graphs are data structures that contain labeled
nodes which are connected by edges.
- Sound familiar? We often think of lists and trees similarly (or
at least implementations of lists and trees).
- How do graphs differ from lists and trees?
- The nodes are labeled, so that we can refer to them by name.
- Each node may have multiple edges connected to it.
- In graphs, we don't always distinguish between successors and predecessors.
- Graphs don't have a unique start node.
- Graphs don't necessarily have a unique end node.
- In a graph, a node may be its own ``descendant''.
- The graph may have independent subparts.
- Just as each list is a tree (although not a very balanced tree), each
tree is a graph.
- As with lists and trees, we can make the edges unidirectional or
bidirectional.
- If the edges are unidirectional, the graph is called a
directed graph.
- If the edges are bidirectional, the graph is called an
undirected graph.
- In some uses of graphs, we may associate a numeric weight to each
edge. Graphs with weights on edges are called weighted graphs.
Typically, a weight represents the cost to get from one node to another.
- Note that in some graphs it is possible to follow a sequence of edges and
return to the place you started. That path (sequence of edges) is called
a cycle. Graphs with cycles are called cyclic
graphs.
- If there are no cycles in a graph, it is an acyclic graph.
- If you don't say whether or not a graph is cyclic, you are implying
that you will deal with either type.
- Because graphs can be disconnected, we often refer to connected
components of graphs. In a connected component, it is possible
to reach every node from every other node.
- What are the basic methods we might use for graphs? They may depend on the
algorithms we write.
- However, it is still useful to think of some basic ones.
- Accessors
- Given a node, determine all the neighbors of that node.
- Given a pair of nodes, determine if there is an edge between
them (or the weight of the edge between th em).
- Modifiers
- Create an edge between new nodes.
- Delete an edge between new nodes.
- Add a new node (could also be implicit in the "create an
edge" method).
- When writing recursive graph algorithms that are not restricted to
acyclic graphs, you may need to mark the nodes in the graph
to ensure that you don't repeatedly use the same node.
- Clear all marks.
- Mark a node.
- Remove a mark.
- As we study graph algorithms over the next few days, think about what
core operations we're using.
- How do we implement graphs? There are many techniques, which may
depend on our intended use of the graph.
- The simplest one is to use some sort of
Node
structure,
and to treat the graph as a collection of nodes.
- Each node will need a list of nodes for its neighbors.
- We will probably store the nodes in a hash table for each access
(indexed by label).
- If we can number the nodes, we can use an adjacency matrix:
if there is an edge from A to B, then M[A,B] is true.
- In a weighted graph, we give M[A,B] the weight of the edge. We use
some special value (perhaps the largest integer or a negative integer)
fo ``not there'').
- We can make a list of all the edges in the graph.
- Which implementation should we use? It depends a lot on the algorithm(s)
we intend to use.
- If we need all the neighbors of a node, we want the simple
``collection of nodes'' implementation.
- If we need all the edges of the graph, we use the list of edges.
- If we need to quickly determine whether there is an edge between
two nodes, we use the adjacency matrix.
Tuesday, 10 August 1999
- Created as a blank outline.
Monday, 29 November 1999
- Added the note about the modeling contest.
Tuesday, 30 November 1999
- Filled in some details about exam 3.
Wednesday, 1 December 1999
- Filled in the details, primarily from
outline 50 of
CS152 99S.
- Reformatted and rearranged.
- Added section on graph methods.
- After class: removed uncovered section on common graph algorithms
Back to Traversing Trees.
On to Simple Graph Algorithms.