EECS 560

advertisement
EECS 560
Project 3
Due: Tuesday December 7, 2010
Description: This project deals with graphs. You will generate random graphs with specified
edge densities, and you will also find minimal spanning trees for graphs.
Specifications: You will find a minimum spanning tree for a graph in two ways—first method:
Kruskal’s algorithm; second method: find a cycle, then remove the largest edge in that cycle
and continue this process until no cycles remain. The graph that remains will be a spanning
tree. Your program should be able to either generate random graphs or read a graph from a
file as described below.
Random graph generation. Given the number of vertices (n), and edge density (d) you are to
generate a random connected graph with vertices numbered 1 to n, that has d% of the
possible edges, and has integer edge weights in the range from 1 to 5n. Generate random
numbers to determine, for each possible edge in a graph, whether the edge is present and if
so assign it a weight. For example, for vertices u and v, let r be a random number in the range
0  r  1. If d = 50% and r  0.5 then (u, v) is an edge is in the graph, so you generate a
random integer between 1 and 5n as the weight of (u, v). (Similarly, if d = 75% then use r 
.75 to determine if a particular edge should be in the graph.) Be careful you don't try to
generate the same edge twice. For example, when you determine whether or not (2, 3) is in
the graph then do not consider edge (3, 2). You must also ensure the graph is connected. To
do this, perform a depth first search on it, starting from vertex 1. If the DFS stops before all
vertices have been visited, insert an edge between the last vertex added to the depth first
search tree and the lowest numbered vertex not already in the DFS tree. Randomly generate
the weight for that edge. Continue in this way until all vertices have been visited.
Testing and timing: Randomly generate connected graphs as described above. Use n values
of 20, 50, and 100 vertices and edge densities 40%, 70% and 100%. (For the complete
graphs, just generate edge weights.) Edge weights are integers between 1 and 5n, inclusive.
Run the spanning tree algorithms and compare the times taken by both. Try to time only the
algorithm that finds the spanning tree, not the cost of building the initial graph. You will have
to find a strategy that will be able to get the relatively small times required to find the spanning
trees. Run at least 10 tests for each combination of n and d and report your findings in one or
more tables. Report the number of edges in the graph you generated (including any that were
added during the depth first search), the actual edge density, the cost of a spanning tree, and
the time required by each of the two algorithms.
Verifying spanning tree correctness: To ensure that your spanning tree algorithms are working
correctly you should have a version of your program that will read graph data from a file and
output the edges in a minimum spanning tree and the total cost of that tree. Print out the
edges in lexicographic order, one edge per line. That is for edge (u, v), u < v. If (u, v) comes
before (x, y) then either u < x or, if u = x then v < y. For example, (2, 3) comes before (2, 4)
and both of them precede (3, 4). The file format will be as follows:
Line 1 contains n, the number of vertices in the graph.
Line 2 has n - 1 integers, the weights for edges (1, 2), (1, 3), …, (1, n)
Line 3 has n - 2 integers, the weights for edges (2, 3), (2, 4), …, (2, n)
.
.
.
Line n (the last line) has a single integer--the weight of edge (n-1, n)
Integers will be separated by spaces, not commas.
To turn in:
Your written report should include all of the following:
1. A clear description of the overall structure of your program. That is, you are to specify and
briefly describe each of the functions used in the program and how the overall program is
structured in much the same way as a flowchart would provide this information. For
example, Kruskal’s algorithm might be described in the following way:
function Kruskal
Input: adjacency list for a graph
sort the edges in order of increasing weight
select edges one at a time, in order, and
add edge to spanning tree if no cycle is created
otherwise, discard the edge
stop when n – 1 edges have been added to spanning tree
calls insertion (or other) sort, union
2. A hard copy of your code.
3. The results of the random graph generation and spanning tree tests. Be sure to report any
problems encountered and if the edge density differs by more than a couple of percentage
points from the specified d value, see if you can explain why.
4. A description of how you tested the correctness of your algorithms. Include all data sets
used and the results produced on them by your program(s).
NOTE: NO LATE PROJECTS WILL BE ACCEPTED, EVEN IF THERE IS A HARDWARE
FAILURE. ALL PROJECTS ARE DUE ON DECEMBER 7.
Download