Project 1, COMP 1572, Winter 2006, due Friday, February 3

advertisement
Project 1, COMP 1572, Winter 2006, due Friday, February 3
Background:
In this project, you will program some basic operations for working with graphs. In this
context, a graph G is an abstract structure, consisting of two sets, V and E. V is a set of
items designated as vertices. E is a set whose elements are two-element subsets of V.
The subsets in E are called edges. To visualize a graph, you may think of the vertices as
points in a plane, and edges as connections between pairs of points. For example, suppose
the graph G consists of (V={0,1,2,3,4}, E={{0,2},{0,3},{2,3},{3,4}}). This could be
represented by the following diagram:
(For some graphs, visual representation requires that the edges be allowed to be curves,
or that they be allowed to cross. For example, consider a graph for an airline that has as
its vertices the U.S. airports used by the airline and as its edges the pairs of U.S. airports
connected by direct flights. Drawing this graph by locating the airports on the map and
connecting them with actual flight paths results in curved, intersecting edges. ) Graphs
capture important features of many systems: circuits in electrical engineering,
transportation networks in civil engineering, proximity data in computational geometry,
and state diagrams in the theory of computation, for example. When working with a
graph, one should check that the graph is correctly specified. In particular, the elements
of E must, in fact, be two-element subsets of the vertices. Further, the graph may
decompose into separate pieces that should be analyzed separately.
A basic decomposition that is often useful is the decomposition into connected
components. To define this decomposition carefully, first define a subgraph of a graph G:
H= (V1, E1) is a subgraph of G= (V, E) if V1 is a subset of V and E1 is that subset of E
consisting of all edges in E that are subsets of V1. Two subgraphs are disjoint if their
vertex sets have no elements in common. The decomposition of G into connected
components is a particular collection of pairwise disjoint subgraphs of G.
To define the subgraphs making up the decomposition into connected components, first
define the concept of a walk in a graph. A walk in the graph G=(V,E) is an alternating
sequence of vertices and edges, of G, beginning and ending with a vertex, such that each
edge equals the set consisting of the vertices to either side. For example, in the graph
sketched above, 2{2,3}3{3,4}4{3,4}3{3,0}0 is a walk. So is this very short sequence: 4.
Intuitively, a walk specifies a route from vertex to vertex along edges of the graph.
The set of vertices in the connected component of a graph associated with a vertex v is
the set of all vertices w for which there exists a walk in G beginning at v and ending at w.
The edges are those edges required by the vertices to make a subgraph. Note that if w is
in the connected component associated with v, then the connected component associated
with w is the same as the connected component associated with v: if there is a walk from
v to w and a walk from v to u, then there is a walk from w to u. Just walk 'backwards'
from w to v, then walk from v to u. Consequently, the connected components associated
with two vertices are either equal or disjoint. The collection of all connected components
of G is the decomposition in question.
In our example graph, there are two connected components. The vertices 0, 2, 3, and 4 lie
in one connected component of the graph, while 0 is the only vertex in the other.
Program:
The data for the program is a set V of vertices of a graph and a set of proposed edges for
the graph. The program will select as edges of the graph those proposed edges that are, in
fact, two element subsets of the set of vertices. The program will display V and E for the
resulting graph. The program will also display the vertex set and the edge set for each of
the connected components of the graph.
In particular, the vertices will be non-negative integers. The program will read them as
space-separated int's from a text file, vertices.dat. The proposed edges will be in a text
file called edges.dat, also consisting of space-separated int's. In edges.dat, the zeroth and
first entries are the vertices in the zeroth proposed edge. The second and third int's are the
vertices in the first proposed edge, etc.
Grading:
The project grade will be based on the creation of a program that works correctly, up to
some details (50%), the clear and efficient organization of the program (20%), the
appropriate use of functions, structures, and containers (10%), the production of clear
output, with readable formatting and without unnecessary repetition (10%), and the
composition of informative comments (10%). Programs must compile. If necessary,
comment out problem code, describe the actual performance of the program, and describe
the intended performance.
Restrictions:
You may work individually or in groups of two. Use only material from class or from the
text, chapters 0-5, inclusive. All code must be the work of the individual or group. Do not
share or discuss specific strategies.
Download