0% found this document useful (0 votes)
10 views10 pages

Data Lab 13

The document discusses representations of graphs and algorithms to work with graphs. It includes implementations of adjacency list and adjacency matrix representations of graphs as well as algorithms for detecting cycles, breadth-first search and depth-first search on graphs.

Uploaded by

Rafay Naveed
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)
10 views10 pages

Data Lab 13

The document discusses representations of graphs and algorithms to work with graphs. It includes implementations of adjacency list and adjacency matrix representations of graphs as well as algorithms for detecting cycles, breadth-first search and depth-first search on graphs.

Uploaded by

Rafay Naveed
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/ 10

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);

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


{
g.addEdge(edges[i][0] - 1, edges[i][1] - 1);
}
g.printAdjList();

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());

bool* visited = new bool[numVertices];


for (int i = 0; i < numVertices; i++)
{
visited[i] = false;
}
visited[startId] = true;
while (!queue.empty())
{
Vertex currentVertex = getVertexById(queue.front());
queue.pop_front();

cout << "Visited: " << currentVertex.getName() << endl;


for (const Edge& edge : currentVertex.getEdges())
{
if (!visited[edge.getToVertex()])
{
visited[edge.getToVertex()] = true;
queue.push_back(edge.getToVertex());
}
}
}
delete[] visited;
}
void dfs(int startId)
{
if (!checkIfVertexExistsById(startId))
{
cout << "Vertex with id " << startId << " does not exist." << endl;
return;
}
Vertex startVertex = getVertexById(startId);
list<int> stack;
stack.push_back(startVertex.getId());
bool* visited = new bool[numVertices];
for (int i = 0; i < numVertices; i++)
{
visited[i] = false;
}
visited[startId] = true;

while (!stack.empty())
{
Vertex currentVertex = getVertexById(stack.back());
stack.pop_back();

cout << "Visited: " << currentVertex.getName() << endl;

for (const Edge& edge : currentVertex.getEdges())


{
if (!visited[edge.getToVertex()])
{
visited[edge.toVertex] = true;
stack.push_back(edge.getToVertex());
}
}
}
delete[] visited;
}
private:
int numVertices;
bool checkIfVertexExistsById(int id) const
{
for (const Vertex& vertex : vertices)
{
if (vertex.getId() == id)
{
return true;
}
}
return false;
}
};
int main()
{
Graph graph;
graph.addVertex(Vertex(0, "Islamabad"));
graph.addVertex(Vertex(1, "Karachi"));
graph.addVertex(Vertex(2, "Lahore"));
graph.addVertex(Vertex(3, "Peshawar"));
graph.addVertex(Vertex(4, "Quetta"));

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();

cout << "BFS:" << endl;


graph.bfs(0);

cout << "DFS:" << endl;


graph.dfs(0);
return 0;
}

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;

newNode = new Node(u);


newNode->next = adjList[v];
adjList[v] = newNode;
}
void detectCycles()
{
bool* visited = new bool[numVertices];
int* parent = new int[numVertices];
for (int i = 0; i < numVertices; i++)
{
visited[i] = false;
parent[i] = -1;
}
for (int i = 0; i < numVertices; i++)
{
if (!visited[i])
{
dfs(i, -1, visited, parent);
}
}
delete[] visited;
delete[] parent;
}
private:
void dfs(int node, int parentIndex, bool* visited, int* parent)
{
visited[node] = true;
parent[node] = parentIndex;

for (Node* neighbor = adjList[node]; neighbor != nullptr; neighbor = neighbor->next)


{
if (!visited[neighbor->vertex])
{
dfs(neighbor->vertex, node, visited, parent);
}
else if (parent[node] != neighbor->vertex)
{
cout << "Cycle detected: " << node << " -> " << neighbor->vertex << endl;
}
}
parent[node] = -1;
}
};
int main()
{
Graph g(6);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(3, 4);
g.addEdge(4, 3);
g.addEdge(2, 3);

g.detectCycles();

return 0;
}

You might also like