Docsity

GRAPHS CODE IN DEV DSA, Lab Reports of Data Structures and Algorithms

Toggle sidebar
bg1
#include <iostream>
using namespace std;
class Graph {
private:
struct GNode {
char data;
bool visited;
GNode* ptr;
};
GNode* GList;
int vertexSize;
int noOfVertex;
public:
Graph();
void Create();
void InsertVertex(char data);
void InsertEdge(char src, char dest);
void DeleteVertex(char data);
void DeleteEdge(char src, char dest);
bool SearchVertex(char data);
bool SearchEdge(char src, char dest);
void Display();
void DFSTraversal(char startVertex);
void BFSTraversal(char startVertex);
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download GRAPHS CODE IN DEV DSA and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

#include using namespace std; class Graph { private: struct GNode { char data; bool visited; GNode* ptr; }; GNode* GList; int vertexSize; int noOfVertex; public: Graph(); void Create(); void InsertVertex(char data); void InsertEdge(char src, char dest); void DeleteVertex(char data); void DeleteEdge(char src, char dest); bool SearchVertex(char data); bool SearchEdge(char src, char dest); void Display(); void DFSTraversal(char startVertex); void BFSTraversal(char startVertex);

void resetVisited() { for (int i = 0; i < noOfVertex; i++) { GList[i].visited = false; } } GNode* getVertex(char vertex) { if (vertex != ' ') { for (int i = 0; i < noOfVertex; i++) { if (GList[i].data == vertex) { return &GList[i]; } } } return NULL; } }; class Queue { private: struct QNode { char vertex; QNode* next; }; QNode* front; QNode* rear;

front = front -> next; if (front == NULL) { rear = NULL; } }else { vertexReturn = ' '; } return vertexReturn; } bool isEmpty() { if (front == NULL) return true; else return false; } }; class Stack { private: struct SNode { char vertex; SNode* next; }; SNode* top; public:

Stack() { top = NULL; } void push(char vertex) { SNode* temp = new SNode; temp -> vertex = vertex; temp -> next = top; top = temp; } char pop() { char tempVertex = ' '; if (top != NULL) { tempVertex = top -> vertex; top = top -> next; } return tempVertex; } bool isEmpty() { if (top == NULL) return true;

void Graph::InsertEdge(char src, char dest) { bool srcFound = false; bool destFound = false; int i; int index; for (i = 0; i < noOfVertex; i++) { if (GList[i].data == src) { srcFound = true; index = i; } if (GList[i].data == dest) { destFound = true; } } if (srcFound && destFound) { GNode* temp = new GNode; temp -> data = dest; temp -> ptr = NULL; GNode* curr = GList[index].ptr; if (curr == NULL) { GList[index].ptr = temp; }else { while (curr -> ptr != NULL) {

//Already in List if (curr -> data == dest) { cout << "Edge Already Exist" << endl; return; } curr = curr -> ptr; } curr -> ptr = temp; } }else { if (!srcFound) cout << "src Not Found" << endl; if (!destFound) cout << "dest Not Found" << endl; } } void Graph::DeleteVertex(char data) { int i; for (i = 0; i < noOfVertex; i++) { if (GList[i].data == data) break; } while (i < noOfVertex - 1) {

void Graph::DeleteEdge(char src, char dest) { int i; bool srcFound = false; for (i = 0; i < noOfVertex; i++) { if (GList[i].data == src) { srcFound = true; break; } } if (srcFound) { GNode* curr = GList[i].ptr; if (curr -> data == dest) { GList[i].ptr = curr -> ptr; }else { GNode* prev = curr; curr = curr -> ptr; while (curr != NULL) { if (curr -> data == dest) { prev -> ptr = curr -> ptr; return; } prev = curr; curr = curr -> ptr; } //If destination not found cout << "Destination Vertex Not Found" << endl; }

}else { cout << "Source Vertex Not Found" << endl; } } bool Graph::SearchVertex(char data) { bool found = false; int i; for (i = 0; i < noOfVertex; i++) { if (GList[i].data == data) { found = true; break; } } if (found) cout << "Vertex " << data << " Found at " << i << endl; else cout << "Vertex Not Found" << endl; } bool Graph::SearchEdge(char src, char dest) { bool srcFound = false; int i;

for (int i = 0; i < noOfVertex; i++) { cout << "Vertex " << GList[i].data << ": "; GNode* curr = GList[i].ptr; while (curr != NULL) { cout << curr -> data << ", "; curr = curr -> ptr; } cout << endl; } } void Graph::BFSTraversal(char startVertex) { resetVisited(); Queue queue; //First Node to be Traverse GNode* curr = getVertex(startVertex); if (curr != NULL) { queue.Enqueue(curr->data); curr -> visited = true; cout << "\nBFS: "; while (!queue.isEmpty()) { char queueData = queue.Dequeue(); curr = getVertex(queueData);

cout << curr -> data << " "; curr = curr -> ptr; while (curr != NULL) { GNode* vertexPtr = getVertex(curr -> data); if (!(vertexPtr->visited)) { queue.Enqueue(curr->data); vertexPtr -> visited = true; } curr = curr -> ptr; } } cout << endl; } } void Graph::DFSTraversal(char startVertex) { resetVisited(); Stack stack; //First Node to be Traverse GNode* curr = getVertex(startVertex); if (curr != NULL) { cout << "DFS: "; stack.push(curr -> data);

graph.InsertVertex('A'); graph.InsertVertex('B'); graph.InsertVertex('C'); graph.InsertVertex('D'); graph.InsertVertex('E'); graph.InsertEdge('A', 'B'); graph.InsertEdge('A', 'C'); graph.InsertEdge('B', 'E'); graph.InsertEdge('C', 'B'); graph.InsertEdge('D', 'A'); graph.InsertEdge('B', 'D'); graph.InsertEdge('B', 'A'); graph.InsertEdge('D', 'C'); graph.InsertEdge('E', 'A'); graph.Display(); graph.BFSTraversal('A'); graph.DFSTraversal('A'); }