主题:求助:无向图的实现,并用Kruskal算法计算最小生成树
我在国内学过的C++课程完全是C,出国后教授根据我以前的课给了我这门基于C++的数据结构和算法的课程,结果发现完全不懂。
第二次作业周五就交,再交不出来就over了……特来求援……
麻烦大家帮忙……多谢了
CSCI 303 Homework #2
Due Friday, October 6, 2006
Implement the Graph ADT using a class template. Use the following code in your class declaration:
template < class T >
class Graph
{
private:
T * vertices;
unsigned int * edges;
public:
Graph(unsigned int numVertices);
void addVertex(unsigned int vertexNumber, const T & vertexData);
void addEdge(unsigned int vertex1, unsigned int vertex2);
void removeEdge(unsigned int vertex1, unsigned int vertex2);
bool hasEdge(unsigned int vertex1, unsigned int vertex2) const;
T getVertexData(unsigned int vertexNumber) const;
bool isConnected() const;
bool hasCycle() const;
bool isTree() const;
T operator[](unsigned int vertexNumber) const;
};
Make sure that each of your class methods is functional! Note that the operator[] method should have the same functionality as getVertexData.
Then implement a derived class called WeightedGraph in which each edge may be assigned a weight of type double. Supply a method that returns a minimum weight spanning tree using Kruskal's Algorithm, and find the asymptotic time-complexity of this method. Show your work. You may add members to the Graph class, but do not add variables which are for local use only. Make sure to add a copy constructor, operator=, and destructor. WeightedGraph also needs these methods. Make sure that your class is foolproof against all possible uses; in particular, perform bounds checking before attempting to access an array element, and throw exceptions when appropriate. Make sure that your hasCycle method checks all components of the graph, in case the graph is not connected.
第二次作业周五就交,再交不出来就over了……特来求援……
麻烦大家帮忙……多谢了
CSCI 303 Homework #2
Due Friday, October 6, 2006
Implement the Graph ADT using a class template. Use the following code in your class declaration:
template < class T >
class Graph
{
private:
T * vertices;
unsigned int * edges;
public:
Graph(unsigned int numVertices);
void addVertex(unsigned int vertexNumber, const T & vertexData);
void addEdge(unsigned int vertex1, unsigned int vertex2);
void removeEdge(unsigned int vertex1, unsigned int vertex2);
bool hasEdge(unsigned int vertex1, unsigned int vertex2) const;
T getVertexData(unsigned int vertexNumber) const;
bool isConnected() const;
bool hasCycle() const;
bool isTree() const;
T operator[](unsigned int vertexNumber) const;
};
Make sure that each of your class methods is functional! Note that the operator[] method should have the same functionality as getVertexData.
Then implement a derived class called WeightedGraph in which each edge may be assigned a weight of type double. Supply a method that returns a minimum weight spanning tree using Kruskal's Algorithm, and find the asymptotic time-complexity of this method. Show your work. You may add members to the Graph class, but do not add variables which are for local use only. Make sure to add a copy constructor, operator=, and destructor. WeightedGraph also needs these methods. Make sure that your class is foolproof against all possible uses; in particular, perform bounds checking before attempting to access an array element, and throw exceptions when appropriate. Make sure that your hasCycle method checks all components of the graph, in case the graph is not connected.