Question: Problem statement A directed graph is graph, i.e., a set of objects (called vertices or nodes) that are connected together, where all the edges are
Problem statement A directed graph is graph, i.e., a set of objects (called vertices or nodes) that are connected together, where all the edges are directed from one vertex to another. In contrast, a graph where the edges are bidirectional is called an undirected graph. A graph can be formally define as G=(N,E) consisting of the set N of nodes and the set E of edges, which are ordered pairs of elements of N. It is further assumed in this project specification that any graph is finite with no directed or undirected cycles. An example is given below for directed graph.
v={ 1,2,3,4,5,6}
E={(1,2),(1,4),(2,3),(2,4),(2,5),(3,4),(5,3),(6,3),(6,5)}
A graph has many useful applications in the real world, such as scheduling of tasks; network topology; family tree; data compression; literature citation, social connections and so on. This project requires you to develop object oriented programs of a graph that can achieve the following functions.
1. A graph can be empty with no vertex or edge. 2. A graph can be either a directed graph or an undirected graph.
3. A graph can be added in vertices and edges.
4. A vertex of a graph can contain values in theory, the values can be of any type.
5. A graph can be displayed by listing all the possible paths, each linking vertices.
6. A graph can be queried by given a starting vertex, listing the path this vertex leads.
7. A graph can be queried by given an edge, if this edge exists in the graph 8. A graph can be queried if a value is contained by any of its vertex.
8. A graph can be queried if a value is contained by any of its vertex.
Part 1:. [Totally 40 points]
1. Design and program necessary C++ Classes with data members and member functions for above functions.
2. For each Class designed, provide default constructor, copy constructor, and constructor with arguments.
3. A Driver application to test the graph and demonstrate its functions. 4. Apply at least three of the following techniques (any 3): inheritance; polymorphism; operator overloading; template; exception handling.
MY IMPLEMENTATION OF THE CODE NOT SURE IF IM RIGHT CAN SOMEONE PLEASE HELP ME!!!!!!
NODE.H///////
#ifndef NODE_H #define NODE_H
class Vertex { public: Vertex(); Vertex(int); ~Vertex(); int getvalue()const; // void addEdge(Vertex *v, int dist); private: int value; }; #endif
NODE.CPP///////
#include
Vertex::Vertex() { value = 0; }
Vertex::Vertex(int id) { value = id; }
int Vertex::getvalue()const { return value; }
Vertex::~Vertex(){}
EDGE.H//////
#ifndef EDGE_H #define EDGE_H
#include
class Edge { public: Edge(); Edge(Vertex org, Vertex dest); ~ Edge(); Edge(const Edge &);
Vertex getOrigin() const; Vertex getDestination() const; void addedge(Vertex , Vertex); //void seto(Vertex *);
protected: Vertex origin; Vertex destination; vector
EDGE.CPP///////
#include
Edge::Edge() { Vertex origin = 0; Vertex destination = 0; }
Edge::Edge(Vertex org, Vertex dest) { origin = org; destination = dest; }
Edge::Edge(const Edge &E) {
for(int i = 0; i<10; ++i) { origin[i] =E.origin[i]; destination[i] =E.destination[i]; } }
Vertex Edge:: getOrigin() const { return origin; }
Vertex Edge:: getDestination() const { return destination; } void Edge::addedge(Vertex n1, Vertex n2) { Edge newEdge(n1, n2); edges.push_back(newEdge); } Edge::~Edge() {}
GRAPH.H //////////
#ifndef GRAPH_H #define GRAPH_H #include
class Graph{
public: Graph(Vertex *, Edge *); Graph(); ~Graph();
void insert(Vertex *v, Edge *); virtual void printGraph()const;
void removeedge(Vertex *, Vertex *);
protected: //Vertex *listnode; // Edge *listedge; vector
GRAPH.CPP///
#include
Graph::Graph() { //Vertex *listnode = NULL; //Edge *listedge = NULL;
vector
} Graph::Graph(Vertex *ln, Edge *le) { // listnode = ln; //listedge = le; }
void Graph::insert(Vertex *v, Edge *e) { vertices.push_back(v); edges.push_back(e); }
void Graph::printGraph()const { for (int i = 0; i < vertices.size(); i++) {
cout< for (int i = 0; i < edges.size(); i++) { cout< } void Graph::removeedge(Vertex *n1, Vertex *n2){ vertices.erase(vertices.begin(),vertices.end(),n1); }; UNDIRECTED.H/////// #ifndef UNDIRECTED_H #define UNDIRECTED_H #include class Graph; class undirected : public Graph{ protected: Vertex *listnode; Edge *listedge; //int labeln; //int labele; vector undirected(Vertex *, Edge *); undirected(); void insert(Vertex *, Edge *); virtual void printGraph()const; }; #endif UNDIRECTED.CPP//// #include undirected::undirected() { Vertex *listnode = NULL; Edge *listedge = NULL; //labele = 0; //labeln = 0; } undirected::undirected( Vertex *ln, Edge *le) { listnode = new Vertex[10]; listedge = new Edge[10]; } undirected::undirected(const undirected &U) { for (int i = 0; i < 10; i++) { listnode[i] = U.listnode[i]; } for (int i = 0; i < 10; i++) { listedge[i] = U.listedge[i]; } } undirected::~undirected() {} void undirected::printGraph()const{ cout<<"Vertices: "; for(int i=0; i<10; ++i){ cout< for(int i=0; i<10; ++i){ cout< void undirected::insert( Vertex *v, Edge * e) { vertices.push_back(v); edges.push_back(e); } void removeeedge( Vertex n1, Vertex n2) { } DRIVER.CPP/// #include using namespace std; Vertex v(1); Vertex v2(2); Vertex v3(3); Edge e1; e1.addedge(v,v1); Graph g;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
