# Class 51: Simple Graph Algorithms

Back to Introduction to Graphs. On to Shortest Path.

Held Thursday, December 2, 1999

Overview

Today, we continue our consideration of the graph data structure by visiting some common graph problems.

Notes

• For Friday, try to think of a faster way to do shortest path.
• Are there any new questions on exam 3?
• Some of you have asked what you have to turn in for the project.
• Your portion of the project, which has been tested with any other components you use or are used by.
• A short (3 page) "reader's guide" for your code. This should be sufficient for someone else to modify or maintain your code. It should not be the Javadoc for your code.
• Each team member should turn in a one-page report on the experience. What were good/bad/helpful/etc parts of doing this scale of project?

Contents

Summary

• Graphs and modeling
• Common graph problems
• The traveling salescritter problem
• Reachability

## Modeling with Graphs

• We can phrase a number of modeling problems in terms of graphs.
• In the telephone system, find the least congested route between two phones, given connections between switching stations.
• In a nonnegatively weighted (by congestion) undirected graph, find a path from node A to node B with lowest maximum edge.
• On the Web, determine if there is a way to get to one page from another, just by following normal links.
• In a directed graph, determine whether node B is reachable from node A.
• While driving, find the shortest path from one city to another.
• In a weighted directed graph (or simply a directed graph) (or even an undirected graph), find the shortest path from node A to node B.
• Determine someone's ``Kevin Bacon Degree''
• In an undirected graph, find the shortest path from from node A to node B.
• Actors are nodes.
• Movies are edges. A movie connects all pairs of actors that appeared in the movie.
• As a traveling salesperson who needs to visit a number of cities, find the shortest path that includes all the cities.
• In an undirected graph, find the shortest path that includes all nodes (often called a tour).
• On the Internet, determine how many connections between computers you can eliminate and still make sure that every computer can reach every other computer.
• In a (possibly weighted) graph, find a minimum spanning tree. A spanning three is a set of edges such that every node is reachable from every other node, and the removal of any edge from the tree eliminates the reachability property. A minimum spanning tree is the smallest such tree.
• Determine an ordering of classes so that you always take prerequisite courses first.
• Number the nodes in a directed acyclic graph in such a way that if there is a path from A to B, then the number assigned to A (the term in which you take A) is smaller than the number assigned to B.

### Graph Problems

• One advantage of modeling problems with graphs is that we can then solve more general problems on the graphs an then use those solutions to solve a wide variety of ``real-world'' questions.
• That is, we will develop a number of graph algorithms to solve typical problems.
• What are some of the core graph problems? In no particular order,
• Reachability. Can you get to B from A?
• Shortest path (min-cost path). Find the path from B to A with the minimum cost (determined as some simple function of the edges traversed in the path).
• Minimum spanning tree. Find the ``smallest'' subset of the edges in which all the nodes are connected.
• Traveling salesman. Find the smallest cost path through all the nodes.
• Visit all nodes. Traversal.
• Transitive closure. Determine all pairs of nodes that can reach each other.
• Topological sort. Number the nodes in such a way that any node has a smaller number than all of its successors.
• We'll consider most of these algorithms in the next few days.
• There are also many variations of each of these. For example, some versions of traveling salesperson require a cycle (returning to the start), rather than a path.

## Traveling Salescritter Problem

• Many of you seemed to have heard vague "rumors" about the traveling salescritter problem (TSP), so we'll begin with that problem.
• The problem is to find the shortest path through a graph that visits every node in the graph.
• Just as a salescritter must visit every location in its teritory, so must this algorithm visit every node.
• As you might guess, this problem is typically considered for weighted graphs (sometimes directed, sometimes undirected).
• Is there a solution? Certainly.
```List every path that visits all the cities
Find the shortest such path
```
• Is this a good algorithm? No. It is O(n!). How bad is that?
• 10! = 3,628,800
• 20! = 2,432,902,008,176,640,000
• If we could check and compare the length of one trillion paths each second, checking 20! paths would take us 2,432,902 seconds
• That's 40548 minutes
• That's 675 hours
• That's about one month
• Surpisingly, no significantly better algorithm is known. That is, all known algorithms are O(n!). Some just have better constants.
• However, it turns out that if you're willing to accept approximate answers (e.g., a path no worse than two times as long as the best path), then there are much faster solutions.

## Reachability Analysis

• Reachability is perhaps the simplest graph problem . Starting at one node in a graph (directed or undirected), determine if you can reach another node in that graph (or how many steps it takes).
• Note that this is, in effect, a variant of the theory behind the legendary ``six degrees of Kevin Bacon'' game.
• That is, Kevin Bacon can be reached from any actor (in six steps or less).
• We might begin by a ``mathematical'' (perhaps just formal) definition of reachability. B is reachable from A if
• B is the same node as A.
• There is an edge from A to B (between A and B).
• There exists a node, C, such that C is reachable from A and B is reachable from C.
• We can simplify this slightly, since it is redundant. B is reachable from A if
• B is the same node as A.
• There is an edge from A to C and B is reachable from C.
• (How might you prove these equivalent?)
• Note that we can turn this definition into an algorithm.
```boolean pathBetween(GraphNode source, GraphNode destination) {
// Base case
if (source.equals(destination)) return true;
// Recursive case: see if there's a path from
// a neighbor to the destination.
for each neighbor of source {
if pathBetween(neighbor,destination) return true;
}
// If we reach this point, we've effectively tried every
// possible way to reach the destination.  It must not
// be possible.
return false;
} // reachable
```
• A problem with this algorithm is that there may be an edge back to A, so we may end up revisiting A without ever finding a solution.
• Consider
```   A <-> C -> D -> B
```
• How might our algorithm progress?
```Is there a path from A to B?
Is B equal to A?  No.
Pick some neighbor of A.  C is the only one.
Is there a path from C to B?
Is B equal to C?  No.
Pick some neighbor of C.  Let's pick A.
Is there a path from A to B?
Is B equal to A?  No.
Pick some neighbor of A.  C is the only one.
Is there a path form C to B?
...
```
• How do we fix this problem? By keeping track of which nodes we've visited.
```boolean pathBetween(GraphNode source, GraphNode destination) {
// Note that we've seen the source.
source.mark();
// Base case: we're already there.
if (source.equals(destination)) return true;
// Recursive case: try all the neighbors.
for each neighbor of source
if (!neighbor.isMarked() && pathBetween(neighbor,destination)) return true;
}
// Tried everything.  Give up.
return false;
} // reachable
```
• This gives a ``depth-first'' search of the graph.
• How might you do breadth-first search?
• We may try this algorithm on a few larger examples.

## History

Tuesday, 10 August 1999

• Created as a blank outline.

Wednesday, 1 December 1999

• Copied list of problems from previous outline.
• Added notes on reachability from outline 51 of CS152 99S
• Reformatted.
• Added notes on TSP.

Thursday, 2 December 1999

• Removed section on shortest path (moved to next outline).

Back to Introduction to Graphs. On to Shortest Path.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99F/Outlines/outline.51.html

Source text last modified Thu Dec 2 20:35:16 1999.

This page generated on Thu Dec 2 21:07:26 1999 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu