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

t1 l14

The document contains a C++ program that constructs a graph using an adjacency matrix based on the sum of node indices being prime. It builds the graph for 7 nodes, prints the adjacency list representation, counts the total number of edges, and checks the existence of a specific edge between two nodes. The program utilizes a prime-checking function to determine connections between nodes.

Uploaded by

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

t1 l14

The document contains a C++ program that constructs a graph using an adjacency matrix based on the sum of node indices being prime. It builds the graph for 7 nodes, prints the adjacency list representation, counts the total number of edges, and checks the existence of a specific edge between two nodes. The program utilizes a prime-checking function to determine connections between nodes.

Uploaded by

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

#include <iostream>

#include <cmath>
using namespace std;

const int MAX = 100; // Max number of nodes (adjust as needed)

// Function to check if a number is prime


bool isPrime(int num) {
if (num <= 1) return false;
if (num == 2) return true;
for (int i = 2; i <= sqrt(num); ++i)
if (num % i == 0) return false;
return true;
}

int main() {
int N = 7;
int adjMatrix[MAX][MAX] = {0}; // Adjacency matrix

// Build the graph


for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
if (i != j && isPrime(i + j)) {
adjMatrix[i][j] = 1;
}
}
}

// Print the graph as adjacency list


cout << "Graph traversal:\n";
for (int i = 1; i <= N; ++i) {
cout << i << ": ";
bool first = true;
for (int j = 1; j <= N; ++j) {
if (adjMatrix[i][j] == 1) {
if (!first) cout << ",";
cout << j;
first = false;
}
}
cout << endl;
}

// Count total number of edges (undirected graph)


int totalEdges = 0;
for (int i = 1; i <= N; ++i) {
for (int j = i + 1; j <= N; ++j) {
if (adjMatrix[i][j] == 1)
totalEdges++;
}
}
cout << "\nTotal number of edges: " << totalEdges << endl;

// Check if edge exists between two nodes


int src = 4, dest = 7;
if (adjMatrix[src][dest] == 1)
cout << "\nEdge from " << src << " to " << dest << ": Exists\n";
else
cout << "\nEdge from " << src << " to " << dest << ": Does not exist\n";
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