-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
114 lines (98 loc) · 3.31 KB
/
Graph.cpp
File metadata and controls
114 lines (98 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
using namespace std;
// Structure to represent a node in the linked list
struct Node {
int vertex;
Node* next;
};
// Function to create a new node in the linked list
Node* newNode(int vertex) {
Node* temp = new Node;
temp->vertex = vertex;
temp->next = nullptr;
return temp;
}
// Function to add an edge to the adjacency list represented by a linked list
void addEdge(Node* adjList[], int source, int destination) {
Node* newnode = newNode(destination);
newnode->next = adjList[source]; // Add the new node to the head of the list for source vertex
adjList[source] = newnode;
}
// Function to perform DFS traversal of a graph
void DFS(Node* adjList[], int startVertex, bool visited[]) {
visited[startVertex] = true;
cout << startVertex << " ";
// Recur for all the adjacent vertices of the current vertex
for (Node* neighbor = adjList[startVertex]; neighbor != nullptr; neighbor = neighbor->next) {
if (!visited[neighbor->vertex]) {
DFS(adjList, neighbor->vertex, visited);
}
}
}
// Function to display the menu
void displayMenu() {
cout << "\nMenu:\n";
cout << "1. Add an edge\n";
cout << "2. Perform DFS traversal\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
}
int main() {
int numVertices;
cout << "Enter the number of vertices: ";
cin >> numVertices;
// Create an array of pointers to Node to represent adjacency list
Node* adjList[numVertices];
for (int i = 0; i < numVertices; ++i) {
adjList[i] = nullptr;
}
int choice;
do {
displayMenu();
cin >> choice;
switch (choice) {
case 1: {
int source, destination;
cout << "Enter the source vertex: ";
cin >> source;
cout << "Enter the destination vertex: ";
cin >> destination;
addEdge(adjList, source, destination);
cout << "Edge added from " << source << " to " << destination << ".\n";
break;
}
case 2: {
int startVertex;
cout << "Enter the starting vertex for DFS traversal: ";
cin >> startVertex;
// Create a visited array to keep track of visited vertices
bool visited[numVertices];
for (int i = 0; i < numVertices; ++i) {
visited[i] = false;
}
cout << "DFS Traversal (starting from vertex " << startVertex << "): ";
DFS(adjList, startVertex, visited);
cout << endl;
break;
}
case 3: {
cout << "Exiting...\n";
break;
}
default: {
cout << "Invalid choice. Please try again.\n";
break;
}
}
} while (choice != 3);
// Clean up dynamically allocated memory
for (int i = 0; i < numVertices; ++i) {
Node* current = adjList[i];
while (current != nullptr) {
Node* temp = current;
current = current->next;
delete temp;
}
}
return 0;
}