Data Lab 13
Data Lab 13
Abdur Rafay
[Company name] [Company address]
Q1
#include <iostream>
using namespace std;
class Graph
{
public:
struct Node
{
int vertex;
Node* next;
Node(int vertex) : vertex(vertex), next(nullptr) {}
};
Node** adjList;
int numVertices;
Graph(int numVertices) : numVertices(numVertices)
{
adjList = new Node * [numVertices];
for (int i = 0; i < numVertices; i++)
{
adjList[i] = nullptr;
}
}
void addEdge(int u, int v)
{
Node* newNode = new Node(v);
newNode->next = adjList[u];
adjList[u] = newNode;
newNode = new Node(u);
newNode->next = adjList[v];
adjList[v] = newNode;
}
void printAdjList()
{
cout << "Adjacency List:" << endl;
for (int i = 0; i < numVertices; i++)
{
cout << i << ": ";
for (Node* neighbor = adjList[i]; neighbor != nullptr; neighbor = neighbor->next)
{
cout << neighbor->vertex << " ";
}
cout << endl;
}
}
};
int main()
{
int N = 8;
int M = 7;
int edges[][2] = {{1, 2}, {2, 3},{4, 5},{1, 5},{6, 1},{7, 4},{3, 8}};
Graph g(N);
return 0;
}
Q2
#include <iostream>
using namespace std;
class Graph
{
int numVertices;
int** adjMatrix;
public:
Graph(int V);
void addEdge(int src, int dest);
void printGraph();
};
Graph::Graph(int V)
{
numVertices = V;
adjMatrix = new int* [V];
for (int i = 0; i < V; i++)
{
adjMatrix[i] = new int[V];
for (int j = 0; j < V; j++)
{
adjMatrix[i][j] = 0;
}
}
}
void Graph::addEdge(int src, int dest)
{
adjMatrix[src][dest] = 1;
adjMatrix[dest][src] = 1;
}
void Graph::printGraph()
{
for (int i = 0; i < numVertices; i++)
{
cout << "Vertex " << i << ": ";
for (int j = 0; j < numVertices; j++)
{
if (adjMatrix[i][j] == 1)
{
cout << j << " ";
}
}
cout << endl;
}
}
int main()
{
int V = 5;
Graph graph(V);
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(0, 3);
graph.addEdge(1, 2);
graph.addEdge(1, 3);
graph.printGraph();
return 0;
}
Q3
#include <iostream>
#include <list>
#include <string>
using namespace std;
class Edge
{
public:
int toVertex;
double weight;
Edge(int toVertex, double weight) : toVertex(toVertex), weight(weight) {}
int getToVertex() const
{
return toVertex;
}
double getWeight() const
{
return weight;
}
};
class Vertex
{
public:
int id;
string name;
list<Edge> edges;
Vertex(int id, string name) : id(id), name(name) {}
void addEdge(const Edge& edge)
{
edges.push_back(edge);
}
void updateVertexName(const string& newName)
{
name = newName;
}
int getId() const
{
return id;
}
const string& getName() const
{
return name;
}
const list<Edge>& getEdges() const
{
return edges;
}
int getNumEdges() const
{
return edges.size();
}
};
class Graph
{
public:
list<Vertex> vertices;
void addVertex(const Vertex& newVertex)
{
vertices.push_back(newVertex);
}
const Vertex& getVertexById(int id) const
{
for (const Vertex& vertex : vertices)
{
if (vertex.getId() == id) {
return vertex;
}
}
throw runtime_error("Vertex not found");
}
void addEdgeById(int fromVertex, int toVertex, double weight)
{
for (Vertex& vertex : vertices)
{
if (vertex.getId() == fromVertex)
{
vertex.addEdge(Edge(toVertex, weight));
break;
}
}
}
void printGraph() const
{
for (const Vertex& vertex : vertices)
{
cout << "Vertex " << vertex.getId() << ": " << vertex.getName() << endl;
for (const Edge& edge : vertex.getEdges())
{
cout << " -> " << getVertexById(edge.getToVertex()).getName() << " (" << edge.getWeight() << ")" <<
endl;
}
cout << endl;
}
}
void bfs(int startId)
{
if (!checkIfVertexExistsById(startId))
{
cout << "Vertex with id " << startId << " does not exist." << endl;
return;
}
Vertex startVertex = getVertexById(startId);
list<int> queue;
queue.push_back(startVertex.getId());
while (!stack.empty())
{
Vertex currentVertex = getVertexById(stack.back());
stack.pop_back();
graph.addEdgeById(0, 1, 1300);
graph.addEdgeById(0, 2, 350);
graph.addEdgeById(0, 3, 1600);
graph.addEdgeById(1, 2, 550);
graph.addEdgeById(1, 4, 1200);
graph.addEdgeById(2, 3, 350);
graph.addEdgeById(3, 4, 1200);
graph.printGraph();
Q4
#include <iostream>
using namespace std;
class Graph
{
public:
struct Node
{
int vertex;
Node* next;
Node(int vertex) : vertex(vertex), next(nullptr) {}
};
Node** adjList;
int numVertices;
Graph(int numVertices) : numVertices(numVertices)
{
adjList = new Node * [numVertices];
for (int i = 0; i < numVertices; i++)
{
adjList[i] = nullptr;
}
}
void addEdge(int u, int v)
{
Node* newNode = new Node(v);
newNode->next = adjList[u];
adjList[u] = newNode;
g.detectCycles();
return 0;
}