Outline of Class 48: Introduction to Graphs

Held: Wednesday, April 29, 1998

• I hope to have exams graded by Friday (hah!). Given that many of you were only able to answer three questions, I'll grade the exam out of 80.
• No more homework assignments (yay!).
• Tomorrow at 4:30, three math majors will be discussing internship experiences. I strongly encourage you to go and learn about what kinds of cool things Grinnell students are able to do.
• If you signed up for the picnic and are planning to do "FOG Fast", you'll need to talk to dining services to see if there's an alternative (e.g., skipping dinner on Thursday).
• If you haven't done so already, you should begin reading the chapter in Bailey on graphs (which will be our primary topic for the rest of the term).

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 require.
• 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.
• 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.
• 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.

Introduction to 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.
• If the edges are bidirectional, the graph is called a undirected 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. Graphs with weights on edges are called weighted graphs. Typically, a weight represents the cost to get from one node to another.
• As we've noted 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 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 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 (the term in which you take A) is smaller than the number assigned to B.

Graph Algorithms

• One advantage of modeling problems with graphs is that we can then solvev 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 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. 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.
• There are also many variations. 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.

A `GraphNode` Class

• In order to develop algorithms, we need at least a minimal version of the `GraphNode` class that will be by the algorithms.
• (Unlike other structures which had a wrapper class, graphs seem most natural in their "pure node" state.)
• We'll work with nodes in a directed graph without weights
• This should be easy to extend to a weighted graph.
• What methods should a `GraphNode` provide?
• `addEdge(GraphNode neighbor)`: add an edge to a neighbor.
• `removeEdge(GraphNode neighbor)`: remove an edge to a neighbor.
• `getNeighbors()`: get the neighbors of a node.
• We might also want to associate some symbol or value with each node. Presumably, we can do the association when we construct the node.
• `getSymbol()`: looks up the symbol.
• `toString()`: uses the symbol when converting to a string.
• What else might we want? We might want to add a number of edges en masse, but we'll worry about that later.

On to Reachability and Shortest Path Algorithms
Back to Dictionaries and Hash Tables, Continued
Outlines: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
Current position in syllabus

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.