A directed graph is graph ie a set of objects called vertice

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.

[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.

Solution

#include <iostream>
#include <cstdlib>
using namespace std;

// list node structure
struct AdjacentListNode
{
int destination;
struct AdjacentListNode* next;
};

// adjacency lists
struct AdjacencyList
{
struct AdjacentListNode *head;
};

// creating graph
class AdjacencyGraph
{
private:
int vertex;
struct AdjacencyList* arr;
public:
AdjacencyGraph(int vertex)
{
this->vertex = vertex;
arr = new AdjacencyList [vertex];
for (int i = 0; i < vertex; ++i)
arr[i].head = NULL;
}
// declaring new list instance
AdjacentListNode* newAdjListNode(int destination)
{
AdjacentListNode* tempNewNode = new AdjacentListNode;
tempNewNode->destination = destination;
tempNewNode->next = NULL;
return tempNewNode;
}
// function to add edges to graph
void addGraphEdge(int source, int destination)
{
AdjacentListNode* tempNewNode = newAdjListNode(destination);
tempNewNode->next = arr[source].head;
arr[source].head = tempNewNode;
tempNewNode = newAdjListNode(source);
tempNewNode->next = arr[destination].head;
arr[destination].head = tempNewNode;
}
       //operator overloading
       friend ostream &operator<<( ostream &output,
           const AdjacentListNode* temp ) {
           output<<\" --> \"<<temp->destination;
           return output;
}
// traversing graph and printing graph tree
void displayGraphTree()
{
for (int i = 0; i < vertex; ++i)
{
AdjacentListNode* temp = arr[i].head;
cout<<\"Vertex\"<<i<<endl;
while (temp)
{
cout<<\" --> \"<<temp
temp = temp->next;
}
cout<<endl;
}
}
};

// main
int main()
{
AdjacencyGraph obj(4);
obj.addGraphEdge(0, 1);
obj.addGraphEdge(1,2);
   obj.addGraphEdge(1, 4);
obj.addGraphEdge(1, 3);
obj.addGraphEdge(2, 3);

//calling methods
obj.displayGraphTree();

return 0;
}

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 an
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 an
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 an

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site