Fundamentals of Computer Science II (CSC-152 97F)

[News] [Basics] [Syllabus] [Outlines] [Assignments] [Examples] [Readings] [Bailey Docs]

# Outline of Class 48: Graphs

## Miscellaneous

• Final reminder: tonight at 7:30pm in the forum coffeeshop the math and CS faculty will present I was a 70's math junkie.
• I should have the last homework assignment done in a day or so (perhaps even by class time today, if I'm lucky).
• There's a talk at 4:15pm on Thursday on Mathematical Modeling.
• I will be focusing on the algorithms pertaining to graphs, rather than on the underlying implementation (although we may return to the implementation later). Nonetheless, I would like you to carefully read chapter 15 of Bailey and ask questions about it.

## Modeling

• 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 provide.
• In the telephone system, find the least congested route between to 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.
• While driving, find the shortest path from one city to another.
• As a traveling salesperson 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.
• Determine an ordering of classes so that you always take prerequisite courses first.
• These, and many other problems, can be modeled by a data structure known as the graph.

## Graphs

• Graphs are data structures that contain labeled nodes which are connected by edges.
• Sound familiar? We came up with similar definitions for 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.
• Graphs don't have a unique start node.
• Graphs don't necessarily have a unique end node.
• 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.
• Just as each list is a tree (although not a very balanced tree), each tree is a graph.
• In some uses of graphs, we may associate a numeric weight to each edge.
• As we've suggested in the past, if you designate a root node in a directed graph, the directed graph can serve as a more compact representation of a tree.
• 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.
• 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.

## Modeling with Graphs

• We can rephrase each of the earlier problems in terms of graphs
• In the telephone system, find the least congested route between to 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 weighed directed graph (or simply a directed graph) (or even an undirected graph), find the shortest path from node A to node B.
• 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 a path that includes all nodes.
• 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.
• In a directed acyclic graph, find a numbering such that if there is a path from A to B, then the number assigned to A is smaller than the number assigned to B. smaller number than

## Graph Algorithms

• As the previous examples suggest, if we develop solutions to a number of core graph algorithms, we can use them to answer a host of real-world questions.
• What are some of the core graph algorithms/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. See above.
• Traveling salesman. See above.
• Visit all nodes. Traversal.
• Transitive closure. Determine all pairs of nodes that can reach each other.
• There are also variations of each of these. For example, Bailey has a `reachableFrom` algorithm that returns all the nodes reachable from a partiuclar node.
• We'll consider most of these algorithms in the next few days.

### Reachability

• Reachability is perhaps the simplest graph algorithms. Starting at one node in a graph (directed or undirected), determine if you can reach another node in that graph.
• We might begin by a "mathematical" 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 A.
• We can simplify this slightly. 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.
```static boolean pathBetween(Object A, Object B) {
// Base case
if (A.equals(B)) return true;
// Recursive case
for(Iterator Cs = nextNodes(A); Cs.hasMoreElements(); ) {
C = Cs.nextElement();
if pathBetween(C,B) return true;
}
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
```   +----+
|    |
v    |
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.
```static boolean pathBetween(Object A, Object B) {
// Note that we've seen the current node.
mark(A);
// Base case
if (A.equals(B)) return true;
// Recursive case
for(Iterator Cs = nextNodes(A); Cs.hasMoreElements(); ) {
C = Cs.nextElement();
if (!marked(C) && pathBetween(C,B)) return true;
}
return false;
} // reachable
```
• We may want to try this algorithm on a few larger examples.

Outlines: prev next

[News] [Basics] [Syllabus] [Outlines] [Assignments] [Examples] [Readings] [Bailey Docs]

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.

Source text last modified Tue Dec 2 10:50:23 1997.

This page generated on Tue Dec 2 10:52:06 1997 by SiteWeaver.

Contact our webmaster at rebelsky@math.grin.edu