0% found this document useful (0 votes)
36 views

Menu Driven Program For DSA CPP

The document describes an implementation of a menu-driven graph data structure in C++. It includes functions to create directed and undirected graphs, add edges, print the graph and vertices, and check properties like degrees, isolated/pendant vertices, and universal sources/sinks.

Uploaded by

janhaviyp0304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Menu Driven Program For DSA CPP

The document describes an implementation of a menu-driven graph data structure in C++. It includes functions to create directed and undirected graphs, add edges, print the graph and vertices, and check properties like degrees, isolated/pendant vertices, and universal sources/sinks.

Uploaded by

janhaviyp0304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

DES PUNE UNIVERSITY

Computer Engineering and Technology


Program: B.Tech. Computer Science and Engineering
Academic Year: 2023-24 Year: Second Year Term: II
Roll No.: 32 Name: Ayuti Raj Pardeshi
Subject: Data Structures and Algorithm
Assignment No.: 3 Title: Menu Based Graph Implementation
Date: 21 / 04 / 24

Code for Graph

#include <iostream>
using namespace std;

#define MAX 100

struct Node {
int vertex;
Node* next;
};

class Graph
{
private:
int n; //num of vertex
int m;// num of edges
bool directed;
Node* adjlist[MAX];

public:
void createUnDirGraph(int num_v)
{
directed= false;
n=num_v;
m=0;
for(int i = 0; i<n; i++)
{
adjlist[i]=NULL;
}
}

void createDirGraph(int num_v)


{
directed= true;
n=num_v;
m=0;
for(int i = 0; i<n; i++)
{
adjlist[i]=NULL;

Assignment By: Ayuti Raj Pardeshi Page 1 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering
}
}

void addEdge(int v1, int v2) {


Node* v2node = new Node();
v2node->vertex = v2;
v2node->next = NULL;

if (adjlist[v1] == NULL) {
adjlist[v1] = v2node;
} else {
Node* temp = adjlist[v1];
while (temp->next != NULL)
temp = temp->next;
temp->next = v2node;
}

if (!directed) {
Node* v1node = new Node();
v1node->vertex = v1;
v1node->next = NULL;

if (adjlist[v2] == NULL) {
adjlist[v2] = v1node;
} else {
Node* temp = adjlist[v2];
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = v1node;
}
}
}

void printGraph() {
for (int i = 0; i < n; i++) {
cout << i << " -> ";
Node* temp = adjlist[i];
while (temp != NULL) {
cout << temp->vertex << " ";
temp = temp->next;
}
cout << endl;
}
}

Assignment By: Ayuti Raj Pardeshi Page 2 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

void printNeighbours(int vertex) {

cout << "Neighbours of vertex " << vertex << ": ";
Node* temp = adjlist[vertex]; //0
while (temp != NULL) {
cout << temp->vertex << " ";
temp = temp->next;
}
cout << endl;
}

void printIncomingNeighbours(int vertex) //0


{
cout << "Incoming neighbours of vertex " << vertex << ": ";
for (int i = 0; i < n; i++) {
Node* temp = adjlist[i]; //0
while (temp != NULL) {
if (temp->vertex == vertex) {
cout << i << " ";
break;
}
temp = temp->next;
}
}
cout << endl;
}

void printOutgoingNeighbours(int vertex)


{
printNeighbours(vertex);
}

int degree(int vertex) //1


//1->2 3
{
int deg = 0;
Node* temp = adjlist[vertex];
while ( temp!= NULL) {
deg++;
temp = temp->next;
}
return deg;
}

int inDegree(int vertex) {


int in_deg = 0;

Assignment By: Ayuti Raj Pardeshi Page 3 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering
for (int i = 0; i < n; i++) {
Node* temp = adjlist[i];
while (temp != NULL) {
if (temp->vertex == vertex) {
in_deg++;
break;
}
temp = temp->next;
}
}
return in_deg;
}

int outDegree(int vertex)


{
return degree(vertex);
}

void checkIsolated(int vertex) //1 ->2 3 =2


{
if (degree(vertex) == 0)
{
cout << "Vertex " << vertex << " is isolated." << endl;
} else {
cout << "Vertex " << vertex << " is not isolated." << endl;
}
}

void checkPendant(int vertex)


{
if (degree(vertex) == 1) {
cout << "Vertex " << vertex << " is pendant." << endl;
} else {
cout << "Vertex " << vertex << " is not pendant." << endl;
}
}

void checkUniversalSink()
{
for (int i = 0; i < n; i++) {
if (inDegree(i) == n - 1 && outDegree(i) == 0) {
cout << "Vertex " << i << " is a universal sink." << endl;
return;
}
}
cout << "There is no universal sink." << endl;
}

Assignment By: Ayuti Raj Pardeshi Page 4 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

void checkUniversalSource() {
for (int i = 0; i < n; i++) {
if (outDegree(i) == n - 1 && inDegree(i) == 0) {
cout << "Vertex " << i << " is a universal source." << endl;
return;
}
}
cout << "There is no universal source." << endl;
}

};

int main() {
int choice, num_v;
Graph g;

do {
cout << "1. Create an empty undirected graph" << endl;
cout << "2. Create an empty directed graph" << endl;
cout << "3. Insert an edge" << endl;
cout << "4. Print graph" << endl;
cout << "5. Find neighbours of a vertex" << endl;
cout << "6. Find incoming neighbours of a vertex" << endl;
cout << "7. Find outgoing neighbours of a vertex" << endl;
cout << "8. Find degree of a vertex" << endl;
cout << "9. Find in-degree of a vertex" << endl;
cout << "10. Find out-degree of a vertex" << endl;
cout << "11. Check if a vertex is isolated" << endl;
cout << "12. Check if a vertex is pendant" << endl;
cout << "13. Check if there is a universal sink" << endl;
cout << "14. Check if there is a universal source" << endl;
cout << "-1 to exit" << endl;
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter number of vertices: ";
cin >> num_v;
g.createUnDirGraph(num_v);
cout << "Undirected graph created with " << num_v << " vertices." << endl;
break;
case 2:
cout << "Enter number of vertices: ";
cin >> num_v;
g.createDirGraph(num_v);

Assignment By: Ayuti Raj Pardeshi Page 5 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering
cout << "Directed graph created with " << num_v << " vertices." << endl;
break;
case 3:
int v1, v2;
cout << "Enter vertices v1 and v2 to add an edge (v1 v2): ";
cin >> v1 >> v2;
g.addEdge(v1, v2);
break;
case 4:
cout << "Graph:" << endl;
g.printGraph();
break;
case 5:
cout << "Enter vertex to find its neighbours: ";
cin >> num_v;
g.printNeighbours(num_v);
break;
case 6:
cout << "Enter vertex to find its incoming neighbours: ";
cin >> num_v;
g.printIncomingNeighbours(num_v);
break;
case 7:
cout << "Enter vertex to find its outgoing neighbours: ";
cin >> num_v;
g.printOutgoingNeighbours(num_v);
break;
case 8:
cout << "Enter vertex to find its degree: ";
cin >> num_v;
cout << "Degree of vertex " << num_v << ": " << g.degree(num_v) << endl;
break;
case 9:
cout << "Enter vertex to find its in-degree: ";
cin >> num_v;
cout << "In-degree of vertex " << num_v << ": " << g.inDegree(num_v) << endl;
break;
case 10:
cout << "Enter vertex to find its out-degree: ";
cin >> num_v;
cout << "Out-degree of vertex " << num_v << ": " << g.outDegree(num_v) << endl;
break;
case 11:
cout << "Enter vertex to check if it is isolated: ";
cin >> num_v;
g.checkIsolated(num_v);
break;

Assignment By: Ayuti Raj Pardeshi Page 6 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering
case 12:
cout << "Enter vertex to check if it is pendant: ";
cin >> num_v;
g.checkPendant(num_v);
break;
case 13:
g.checkUniversalSink();
break;
case 14:
g.checkUniversalSource();
break;
case -1:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice! Please enter a valid option." << endl;
}
} while (choice != -1);

return 0;
}

Pseudo Code

Assignment By: Ayuti Raj Pardeshi Page 7 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering
Create a struct for node with the data as Vertex , and a pointer to next , class for Graph and add all
the necessary code for class as well as functions , and main to showcase menu and use switch case to
call all functions.

 Pseudo code for Struct node


struct Node
{
int vertex;
Node* next;
};

 Pseudo code for Graph Class


create class Graph {private: numofvertex, numofedges,bool-directed,adjlist[MAX]}
public:
createUnDirGraph()
directed=false;
n=numofvertex;
for i<n ;
adjlist[i]=NULL;

createDirGraph()
directed=true;
n=numofvertex;
for i<n ;
adjlist[i]=NULL;

 Pseudo code for addEdge in Graph class


addEdge()
input vertex1
input vertex2

// Create new node for vertex2 and link it to vertex1's adjacency list
newNode1 = new Node
newNode1->vertex = vertex2
newNode1->next = adjlist[vertex1]
adjlist[vertex1] = newNode1

// If the graph is undirected, create an edge in the opposite direction


if !directed
newNode2 = new Node
newNode2->vertex = vertex1
newNode2->next = adjlist[vertex2]
adjlist[vertex2] = newNode2

Assignment By: Ayuti Raj Pardeshi Page 8 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering
 Pseudo code for printGraph in Graph class
printgraph()
for i < n;
print i;
Node* temp= adjlist[i];
while( temp != NULL)
print temp->vertex;
temp=temp->next;

 Pseudo code for printNeighbour in Graph class


printNeighbour(int vertex)
{
print vertex;
Node* temp = adjlist[vertex];
while( temp != NULL)
print temp->vertex;
temp=temp->next;
}

 Pseudo code for printIncomingneigbour in Graph class


printIncomingneigbour(int vertex)
print vertex;
for i<n;
Node* temp=adjlist[i];
while( temp != NULL)
if temp->vertex==vertex
print i;
break;
temp=temp->next;

 Pseudo code for printOutNeighbour in Graph class


printOutNeighbour()
//call printneighbours function;

 Pseudo code for degree in Graph class


int degree(int vertex)
int deg=0;
Node* temp = adjlist[vertex];
while temp to not NULL
deg++;
temp= temp->next;
return deg;

Assignment By: Ayuti Raj Pardeshi Page 9 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering
 Pseudo code for indegree in Graph class
int inDegree (int vertex)
in_deg=0;
for i=0 to i<n
Node* temp = adjlist[i];
while !=NULL
if (temp->vertex)==vertx
in-deg++;
break;
temp->next;
return in_deg;

 Pseudo code for outdegree in Graph class


int outDegree()
return degree(vertex);//call above function

 Pseudo code for checkisolated in Graph class


method checkisolated(int vertex)
if degree(vertex)==0
print vertex
else
print vertex not isolated

 Pseudo code for checkpendant in Graph class


method checkpendant()
if degree(vertex)==1
print vertex
else
print vertex not pendant

 Pseudo code for checkuniversalsink in Graph class


method checkuniversalsink()
for i=0 to i<n
if(indegree(i)==n-1 && outdegree(i)==0)
print i
return;

 Pseudo code for checkuniversalsource in Graph class


method checkuniversalsource()
for i=0 to i<n
if(outdegree(i)==n-1 && intdegree(i)==0)
print i
return;

Assignment By: Ayuti Raj Pardeshi Page 10 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering
 Pseudo code for Main
main function
print menu
switch case:
call function;

Assignment By: Ayuti Raj Pardeshi Page 11 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

Outputs

TestCase 1

Assignment By: Ayuti Raj Pardeshi Page 12 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

TESTCASE 2

Assignment By: Ayuti Raj Pardeshi Page 13 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

Assignment By: Ayuti Raj Pardeshi Page 14 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

Assignment By: Ayuti Raj Pardeshi Page 15 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

TESTCASE 3

Assignment By: Ayuti Raj Pardeshi Page 16 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

Assignment By: Ayuti Raj Pardeshi Page 17 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

Assignment By: Ayuti Raj Pardeshi Page 18 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

Assignment By: Ayuti Raj Pardeshi Page 19 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

TESTCASE 4

Assignment By: Ayuti Raj Pardeshi Page 20 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

Assignment By: Ayuti Raj Pardeshi Page 21 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

Assignment By: Ayuti Raj Pardeshi Page 22 of 23


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

Assignment By: Ayuti Raj Pardeshi Page 23 of 23

You might also like