-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathGuess Number
55 lines (45 loc) · 1.11 KB
/
Guess Number
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
#include <iostream>
#include <vector>
using namespace std;
class Graph {
public:
Graph(int vertices);
void addEdge(int v, int w);
void printGraph();
private:
int numVertices;
vector<vector<int>> adjacencyList;
};
Graph::Graph(int vertices) {
numVertices = vertices;
adjacencyList.resize(vertices);
}
void Graph::addEdge(int v, int w) {
// Add edge between vertices v and w
adjacencyList[v].push_back(w);
adjacencyList[w].push_back(v); // For undirected graph
}
void Graph::printGraph() {
for (int v = 0; v < numVertices; ++v) {
cout << "Adjacency list of vertex " << v << ": ";
for (const auto &neighbor : adjacencyList[v]) {
cout << neighbor << " ";
}
cout << endl;
}
}
int main() {
// Create a graph with 5 vertices
Graph graph(5);
// Add edges to the graph
graph.addEdge(0, 1);
graph.addEdge(0, 4);
graph.addEdge(1, 2);
graph.addEdge(1, 3);
graph.addEdge(1, 4);
graph.addEdge(2, 3);
graph.addEdge(3, 4);
// Print the adjacency list of the graph
graph.printGraph();
return 0;
}