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

adsa3

The document contains C++ code for implementing Min Heap and Max Heap data structures, including methods for insertion, deletion, and displaying the heap. It also includes a Graph class that represents a graph using both adjacency matrix and adjacency list representations, with methods for adding edges and performing breadth-first and depth-first traversals. The main function demonstrates the usage of these data structures with example operations.

Uploaded by

codingclub52
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)
12 views

adsa3

The document contains C++ code for implementing Min Heap and Max Heap data structures, including methods for insertion, deletion, and displaying the heap. It also includes a Graph class that represents a graph using both adjacency matrix and adjacency list representations, with methods for adding edges and performing breadth-first and depth-first traversals. The main function demonstrates the usage of these data structures with example operations.

Uploaded by

codingclub52
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/ 13

EX-3

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

// Function to swap two elements in an array

void swap(int& a, int& b) {

int temp = a;

a = b;

b = temp;

// Min Heap

class MinHeap {

public:

vector<int> heap;

// Heapify up

void heapifyUp(int index) {

while (index > 0 && heap[(index - 1) / 2] > heap[index]) {

swap(heap[(index - 1) / 2], heap[index]);

index = (index - 1) / 2;

// Heapify down

void heapifyDown(int index) {

int smallest = index;

int left = 2 * index + 1;


int right = 2 * index + 2;

if (left < heap.size() && heap[left] < heap[smallest]) {

smallest = left;

if (right < heap.size() && heap[right] < heap[smallest]) {

smallest = right;

if (smallest != index) {

swap(heap[index], heap[smallest]);

heapifyDown(smallest);

void insert(int value) {

heap.push_back(value);

heapifyUp(heap.size() - 1);

void deleteElement(int value) {

int index = -1;

for(int i = 0; i < heap.size(); i++) {

if(heap[i] == value) {

index = i;

break;

if(index != -1) {

heap[index] = heap.back();

heap.pop_back();
heapifyDown(index);

} else {

cout << "Element not found in the heap." << endl;

void display() {

for (int i = 0; i < heap.size(); ++i) {

cout << heap[i] << " ";

cout << endl;

};

// Max Heap (similar structure as Min Heap)

class MaxHeap {

public:

vector<int> heap;

void heapifyUp(int index) {

while (index > 0 && heap[(index - 1) / 2] < heap[index]) {

swap(heap[(index - 1) / 2], heap[index]);

index = (index - 1) / 2;

void heapifyDown(int index) {

int largest = index;

int left = 2 * index + 1;

int right = 2 * index + 2;


if (left < heap.size() && heap[left] > heap[largest]) {

largest = left;

if (right < heap.size() && heap[right] > heap[largest]) {

largest = right;

if (largest != index) {

swap(heap[index], heap[largest]);

heapifyDown(largest);

void insert(int value) {

heap.push_back(value);

heapifyUp(heap.size() - 1);

void deleteElement(int value) {

int index = -1;

for(int i = 0; i < heap.size(); i++) {

if(heap[i] == value) {

index = i;

break;

if(index != -1) {

heap[index] = heap.back();

heap.pop_back();

heapifyDown(index);
} else {

cout << "Element not found in the heap." << endl;

void display() {

for (int i = 0; i < heap.size(); ++i) {

cout << heap[i] << " ";

cout << endl;

};

int main() {

MinHeap minHeap;

minHeap.insert(3);

minHeap.insert(1);

minHeap.insert(4);

minHeap.insert(1);

minHeap.insert(5);

minHeap.insert(9);

minHeap.insert(2);

minHeap.insert(6);

cout << "Min Heap: ";

minHeap.display();

minHeap.deleteElement(9);

cout << "Min Heap after deleting 9: ";

minHeap.display();
MaxHeap maxHeap;

maxHeap.insert(3);

maxHeap.insert(1);

maxHeap.insert(4);

maxHeap.insert(1);

maxHeap.insert(5);

maxHeap.insert(9);

maxHeap.insert(2);

maxHeap.insert(6);

cout << "Max Heap: ";

maxHeap.display();

maxHeap.deleteElement(9);

cout << "Max Heap after deleting 9: ";

maxHeap.display();

return 0;

}
Ex4a
#include <iostream>

#include <vector>

#include <queue>

#include <stack>

using namespace std;

class GraphMatrix

vector<vector<int>> adjMatrix;

int numVertices;

public:

GraphMatrix(int vertices);

void addEdge(int src, int dest);

void BFS(int start);

void DFS(int start);

private:

void DFSUtil(int vertex, vector<bool>& visited);

};

GraphMatrix::GraphMatrix(int vertices) : numVertices(vertices)

adjMatrix.resize(vertices, vector<int>(vertices, 0));

void GraphMatrix::addEdge(int src, int dest)

adjMatrix[src][dest] = 1;

adjMatrix[dest][src] = 1; // For undirected graph

}
void GraphMatrix::BFS(int start)

vector<bool> visited(numVertices, false);

queue<int> q;

visited[start] = true;

q.push(start);

cout << "BFS starting from vertex " << start << ": ";

while (!q.empty())

int vertex = q.front();

q.pop();

cout << vertex << " ";

for (int i = 0; i < numVertices; i++)

if (adjMatrix[vertex][i] == 1 && !visited[i])

visited[i] = true;

q.push(i);

cout << endl;

void GraphMatrix::DFS(int start)

vector<bool> visited(numVertices, false);

cout << "DFS starting from vertex " << start << ": ";

DFSUtil(start, visited);
cout << endl;

void GraphMatrix::DFSUtil(int vertex, vector<bool>& visited)

visited[vertex] = true;

cout << vertex << " ";

for (int i = 0; i < numVertices; i++)

if (adjMatrix[vertex][i] == 1 && !visited[i])

DFSUtil(i, visited);

int main()

cout << "Graph Representation using Adjacency Matrix:\n";

GraphMatrix graph(5); // Example with 5 vertices

graph.addEdge(0, 1);

graph.addEdge(0, 4);

graph.addEdge(1, 2);

graph.addEdge(1, 3);

graph.addEdge(2, 3);

graph.addEdge(3, 4);

graph.BFS(0); // Example BFS

graph.DFS(0); // Example DFS

return 0;

}
Ex4b
#include <iostream>

#include <vector>

#include <list>

#include <queue>

#include <stack>

#include <unordered_set>

using namespace std;

// Graph class using adjacency list representation

class Graph

private:

vector<list<int>>adjList; // adjacency list

int numVertices; // number of vertices

public:

// Constructor

Graph(int vertices) : numVertices(vertices)

adjList.resize(vertices);

// Add an edge to the graph

void addEdge(int u, int v)

adjList[u].push_back(v);

adjList[v].push_back(u); // For undirected graph

// Breadth-First Traversal
void breadthFirstTraversal(int start)

vector<bool>visited(numVertices, false);

queue<int> q;

visited[start] = true;

q.push(start);

while (!q.empty())

int node = q.front();

q.pop();

cout<< node << " ";

for (int neighbor :adjList[node])

if (!visited[neighbor])

visited[neighbor] = true;

q.push(neighbor);

cout<<endl;

// Depth-First Traversal

void depthFirstTraversal(int start)

vector<bool>visited(numVertices, false);

stack<int> s;

s.push(start);
while (!s.empty())

int node = s.top();

s.pop();

if (!visited[node])

visited[node] = true;

cout<< node << " ";

// Push all unvisited neighbors onto the stack

for (auto it = adjList[node].rbegin();

it != adjList[node].rend(); ++it)

if (!visited[*it])

s.push(*it);

cout<<endl;

};

int main()

// Create a graph with 4 vertices (0, 1, 2, 3)

Graph g(4);

// Add edges

g.addEdge(0, 1);

g.addEdge(0, 2);

g.addEdge(1, 2);

g.addEdge(1, 3);
g.addEdge(2, 3);

cout<< "Breadth-First Traversal (starting from vertex 0): ";

g.breadthFirstTraversal(0);

cout<< "Depth-First Traversal (starting from vertex 0): ";

g.depthFirstTraversal(0);

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy