0% found this document useful (0 votes)
13 views8 pages

Lab 7 Ds

Uploaded by

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

Lab 7 Ds

Uploaded by

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

Adjacency Matrix Representation of Graph Data Structure

#include<iostream>

#include<vector>

using namespace std;

void addEdge(vector <vector<int> > &mat, int i, int j)

mat[i][j] = 1;

mat[j][i] = 1; // Since the graph is undirected

void displayMatrix(vector <vector<int> > &mat)

int V = mat.size();

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

for (int j = 0; j < V; j++)

cout << mat[i][j] << " ";

cout << endl;

}
int main()

// Create a graph with 4 vertices and no edges

// Note that all values are initialized as 0

int V = 4;

vector<vector <int> > mat(V, vector<int>(V, 0));

// Now add edges one by one

addEdge(mat, 0, 1);

addEdge(mat, 0, 2);

addEdge(mat, 1, 2);

addEdge(mat, 2, 3);

cout << "Adjacency Matrix Representation" << endl;

displayMatrix(mat);

return 0;
}

Adjacency List Representation of Graph:

#include <iostream>

#include <vector>

using namespace std;

void displayAdjList(const vector<vector<int> >& adj) {

for (size_t i = 0; i < adj.size(); i++) {

cout << i << ": "; // Print the vertex

for (size_t j = 0; j < adj[i].size(); j++) {

cout << adj[i][j] << " "; // Print its adjacent vertices

cout << endl;

int main() {

vector<vector<int> > adj(4); // Create a vector with 4 empty vectors

adj[0].push_back(1);
adj[0].push_back(2);

adj[1].push_back(0);

adj[1].push_back(2);

adj[2].push_back(0);

adj[2].push_back(1);

adj[2].push_back(3);

adj[3].push_back(2);

displayAdjList(adj);

return 0;

Traversing a graph

#include <iostream>

#include <vector>

#include <queue>

#include <stack>

#include <map>
using namespace std;

// Depth-First Search (DFS) using a stack

void DFS(int start, const vector<vector<int> >& adj) {

vector<bool> visited(adj.size(), false);

stack<int> s;

s.push(start);

cout << "DFS Traversal starting from node " << start << ": ";

while (!s.empty()) {

int node = s.top();

s.pop();

if (!visited[node]) {

cout << node << " ";

visited[node] = true;

// Push all unvisited neighbors to the stack

for (int i = adj[node].size() - 1; i >= 0; i--) {

int neighbor = adj[node][i];

if (!visited[neighbor]) {
s.push(neighbor);

cout << endl;

// Breadth-First Search (BFS) using a queue

void BFS(int start, const vector<vector<int> >& adj) {

vector<bool> visited(adj.size(), false);

queue<int> q;

q.push(start);

visited[start] = true;

cout << "BFS Traversal starting from node " << start << ": ";

while (!q.empty()) {

int node = q.front();

q.pop();

cout << node << " ";

// Enqueue all unvisited neighbors

for (size_t i = 0; i < adj[node].size(); i++) {


int neighbor = adj[node][i];

if (!visited[neighbor]) {

visited[neighbor] = true;

q.push(neighbor);

cout << endl;

int main() {

// Create a graph with 5 vertices (C++98-compatible initialization)

vector<vector<int> > adj(5);

// Add edges (undirected graph)

adj[0].push_back(1);

adj[0].push_back(2);

adj[1].push_back(0);

adj[1].push_back(3);

adj[2].push_back(0);

adj[2].push_back(4);

adj[3].push_back(1);

adj[4].push_back(2);
// Display adjacency list (optional)

cout << "Adjacency List:\n";

for (size_t i = 0; i < adj.size(); i++) {

cout << i << ": ";

for (size_t j = 0; j < adj[i].size(); j++) {

cout << adj[i][j] << " ";

cout << endl;

// Perform DFS and BFS starting from node 0

DFS(0, adj);

BFS(0, adj);

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy