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

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

# Outline of Class 51: Graphs, Concluded

## Miscellaneous

• I had hoped to get a lot of grading done this weekend. Unfortunately, William and a disabling headache prevented that. I apologize for the repeated delays.
• I won't be grading assignment eight as I think you've learned enough from it by this point, and some of you still haven't finished it.
• Let's go over assignment ten before we do anything else.

## Notes on Prior Algorithms

### Shortest Path

• In the previous class, we were experimenting with the shortest path algorithm. At first glance, it appeared that we always got the same "tree" when computing shortest paths.
• However, this won't always be the case.
• Consider
```     A
5 / \ 4
/   \
B-----C
3
```
• The shortest path from B to A is BA. The shortest path from B to C is BC. However, the shortest path from C to A is CA, which doesn't go over either of those edges.

### Traveling Salesperson Problem

• We also talked about the traveling salesperson problem (TSP) and questioned why a greedy algorithm wouldn't work. Consider the following directed graph (represented by edges and weights):
• A to B costs 1
• B to A costs 2
• A to C costs 3
• C to A costs 100
• B to C costs 100
• C to B costs 2
• The shortest cycle in the graph is ACBA with cost of 7.
• The greedy algorithm suggests that we take AB, but every path that include AB has a cost greater than 7.

## Minimum Spanning Tree

• An interesting variant of the two previous problems is that of finding the minimum spanning tree (MST).
• The MST of an undirected graph is a set of edges that span the graph (permit one to get from each node to every other node).
• It is minimum in the sense of having the smallest sum of edge weights.
• Note that the MST is a tree since there is no use to having a cycle (the extra edges can only add cost).
• It is possible to solve the MST problem by a greedy algorithm. Actually, I've been told that there are a variety of greedy algorithms that help solve the MST problem.
• Here's one version:
• Separate the nodes into two groups: those in the MST and those not in the MST. Initially one node is in the MST (it doesn't matter which one).
• Repeatedly pick the smallest edge between nodes in the MST and nodes not in the MST and add it to the MST.
• If there are any nodes left in the set of nodes not in the MST, then there is no MST.
• Here's another version.
• Here, we'll only pay attention to edges.
• Repeatedly pick the smallest edge that doesn't form a cycle with edges already in the MST and add it to the MST.

### Efficiency

• How efficient are these two algorithms?
• The first requires us to repeatedly find the smallest edge between nodes in the MST and nodes not in the MST. That should be implementable as an O(m) operation. We do that O(n) times (there are n-1 edges in the MST), so the overally running time is O(n*m).
• The second requires us to repeatedly look at the smallest remaining edge. If we put the edges in a heap, then we can do each smallest operation in O(log_2(m)) time, giving an overally running time of O(m*log_2(m)).

### Correctness

• Are both algorithms correct?
• Yes.
• How do we prove it?
• Suppose there is a smaller minimum spanning tree, SST.
• Pick an edge in the MST computed in the second algorithm and add it to SST. + That edge forms a cycle. If it was in the MST, then it must have been picked before another edge in the cycle.
• This means that there is another edge in that cycle with weight greater than that of the edge we picked.
• We can improve SST by replacing that edge with the one from MST.
• So SST wasn't a minimum spanning tree.
• This proof assumes that no two edges have the same weight. With some effort, we can show that it also holds if multiple edges can have the same weight (basically, we have to find a cycle in which the edge we add has smaller weight).
• We can also ensure that no two edges have the same weight by modifying the weight of each two identical edges slightly (by a weight that is smaller than 1/(# of edges) or some such).
• Can we do the proof for the other version? Yes, but it's more difficult. Basically, we consider the point at which we add a edge to the MST that's not in the SST. There must be some other path in SST from "MST so far" to the node that edge connects. If we add the edge to the SST, we form a cycle. As before, we can then make the SST smaller by using that edge.

### An Alternative

• Let's start to consider one other possible means of computing the MST. Instead of adding edges, we'll remove them.
• Repeatedly remove the largest edge whose removal does not separate the graph into unconnected components.
• Does this work? Try it on a few examples. See if you can come up with an example for which it fails, or a proof that it works.

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.