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

t2-14

The document contains a C++ implementation of a graph data structure using an adjacency list and a custom stack for depth-first search (DFS). It defines a Graph class with methods to add edges and perform DFS traversal starting from a specified vertex. The main function demonstrates the creation of a graph with 7 vertices and performs DFS starting from vertex 0.

Uploaded by

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

t2-14

The document contains a C++ implementation of a graph data structure using an adjacency list and a custom stack for depth-first search (DFS). It defines a Graph class with methods to add edges and perform DFS traversal starting from a specified vertex. The main function demonstrates the creation of a graph with 7 vertices and performs DFS starting from vertex 0.

Uploaded by

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

#include <iostream>

using namespace std;

// Node for adjacency list


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

// Stack node
struct StackNode {
int data;
StackNode* next;
};

// Custom Stack class (linked list implementation)


class Stack {
private:
StackNode* top;

public:
Stack() { top = nullptr; }

void push(int value) {


StackNode* newNode = new StackNode;
newNode->data = value;
newNode->next = top;
top = newNode;
}

bool isEmpty() {
return top == nullptr;
}

int pop() {
if (isEmpty()) {
cout << "Stack Underflow\n";
return -1;
}
StackNode* temp = top;
int value = top->data;
top = top->next;
delete temp;
return value;
}

int peek() {
if (!isEmpty())
return top->data;
return -1;
}

~Stack() {
while (!isEmpty())
pop();
}
};

// Graph class
class Graph {
private:
int numVertices;
Node** adjLists;
bool* visited;

public:
Graph(int vertices) {
numVertices = vertices;
adjLists = new Node*[numVertices];
visited = new bool[numVertices];

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


adjLists[i] = nullptr;
visited[i] = false;
}
}

// Add edge (undirected)


void addEdge(int src, int dest) {
Node* newNode = new Node{dest, adjLists[src]};
adjLists[src] = newNode;

newNode = new Node{src, adjLists[dest]};


adjLists[dest] = newNode;
}

// DFS using custom stack


void DFS(int startVertex) {
for (int i = 0; i < numVertices; i++)
visited[i] = false;

Stack stack;
stack.push(startVertex);
visited[startVertex] = true;

cout << "DFS traversal from vertex " << startVertex << ": ";

while (!stack.isEmpty()) {
int current = stack.pop();
cout << current << " ";

Node* temp = adjLists[current];


while (temp != nullptr) {
int adjVertex = temp->vertex;
if (!visited[adjVertex]) {
stack.push(adjVertex);
visited[adjVertex] = true;
}
temp = temp->next;
}
}

cout << endl;


}

~Graph() {
for (int i = 0; i < numVertices; i++) {
Node* temp = adjLists[i];
while (temp != nullptr) {
Node* toDelete = temp;
temp = temp->next;
delete toDelete;
}
}
delete[] adjLists;
delete[] visited;
}
};
int main() {
Graph g(7); // Vertices: 0 to 6

// Maze-style connections
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(2, 3);
g.addEdge(3, 4);
g.addEdge(4, 5);
g.addEdge(4, 6);

g.DFS(0); // DFS starting from vertex 0

return 0;
}

You might also like