0% found this document useful (0 votes)
3 views2 pages

PR 7

The document outlines a program that represents flight paths between cities as a graph using an adjacency matrix. It includes functions for depth-first search (DFS) and breadth-first search (BFS) to traverse the graph, allowing users to input city names and distances. The program also displays the adjacency matrix and allows the user to specify a starting vertex for traversal.

Uploaded by

Gaurav Patkar
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)
3 views2 pages

PR 7

The document outlines a program that represents flight paths between cities as a graph using an adjacency matrix. It includes functions for depth-first search (DFS) and breadth-first search (BFS) to traverse the graph, allowing users to input city names and distances. The program also displays the adjacency matrix and allows the user to specify a starting vertex for traversal.

Uploaded by

Gaurav Patkar
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/ 2

/*Gaurav Pravin Patkar

C-75
Prac cal no :-7
Problem Statement:- There are flight paths between ci es. If there is a flight between City A and City B then there is
an edge between the ci es. The cost of the edge can be the me that flight take to reach city B from A, or the
amount of fuel used for the journey. Represent this as a graph. The node can be represented by the airport name or
name of the city. Use adjacency list representa on of the graph or use adjacency matrix representa on of the graph.
*/

#include <iostream>
#include <queue>
using namespace std;

const int MAX_CITIES = 50;


int adj_mat[MAX_CITIES][MAX_CITIES] = {0}; // Adjacency matrix
bool visited[MAX_CITIES]; // Visited array for DFS
// DFS Func on
void dfs(int s, int n, string arr[]) {
visited[s] = true;
cout << arr[s] << " ";

for (int i = 0; i < n; i++) {


if (adj_mat[s][i] && !visited[i]) {
dfs(i, n, arr);
}
}
}
// BFS Func on
void bfs(int s, int n, string arr[]) {
bool visited[MAX_CITIES] = {false}; // Reset visited array for BFS
queue<int> bfsq;
visited[s] = true;
cout << arr[s] << " ";
bfsq.push(s);

while (!bfsq.empty()) {
int v = bfsq.front();
bfsq.pop();
for (int i = 0; i < n; i++) {
if (adj_mat[v][i] && !visited[i]) {
cout << arr[i] << " ";
visited[i] = true;
bfsq.push(i);
}
}
}
}

int main() {
int n, u;
cout << "Enter number of ci es: ";
cin >> n;
string ci es[MAX_CITIES];

// Input city names


for (int i = 0; i < n; i++) {
cout << "Enter city #" << i << " (Airport Code): ";
cin >> ci es[i];
}
// Display entered ci es
cout << "\nYour ci es are:\n";
for (int i = 0; i < n; i++) {
cout << "City #" << i << ": " << ci es[i] << endl;
}
// Input adjacency matrix (graph edges)
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
cout << "Enter distance between " << ci es[i] << " and " << ci es[j] << ": ";
cin >> adj_mat[i][j];
adj_mat[j][i] = adj_mat[i][j]; // Since it's an undirected graph
}
}
// Display adjacency matrix
cout << "\nAdjacency Matrix:\n\t";
for (int i = 0; i < n; i++) {
cout << ci es[i] << "\t";
}
cout << endl;
for (int i = 0; i < n; i++) {
cout << ci es[i] << "\t";
for (int j = 0; j < n; j++) {
cout << adj_mat[i][j] << "\t";
}
cout << endl;
}
// Input star ng vertex for traversal
cout << "Enter Star ng Vertex (index): ";
cin >> u;
// DFS and BFS Traversals
cout << "DFS: ";
fill(visited, visited + MAX_CITIES, false); // Reset global visited array
dfs(u, n, ci es);
cout << endl;
cout << "BFS: ";
bfs(u, n, ci es);
cout << endl;
return 0;
}

Pr-7

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