0% found this document useful (0 votes)
8 views9 pages

CE_034_A2_LAB_9

The document contains code implementations for finding the shortest path using Dijkstra's algorithm and for insertion sort, both written in C++. It includes validation for the cost matrix in Dijkstra's algorithm and outputs the results of the sorting process. The CPP Check report indicates that the standard header file <bits/stdc++.h> is missing, but it does not affect the functionality of the code.

Uploaded by

23ceuog084
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)
8 views9 pages

CE_034_A2_LAB_9

The document contains code implementations for finding the shortest path using Dijkstra's algorithm and for insertion sort, both written in C++. It includes validation for the cost matrix in Dijkstra's algorithm and outputs the results of the sorting process. The CPP Check report indicates that the standard header file <bits/stdc++.h> is missing, but it does not affect the functionality of the code.

Uploaded by

23ceuog084
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/ 9

Name : Parekh Manthan H.

Roll No. : CE_108

Batch : B2

LAB - 9 : -

Note: CPP Check shows all header file as unfound files for all codes
so have skipped their errors

1. Finding shortest path from source :


Code :
#include<bits/stdc++.h>

using namespace std;

#define MAX_NODES 10

void validateCostMatrix(const vector<vector<int>>& costMatrix) {


int n = costMatrix.size();
for (int i = 0; i < n; ++i) {
if (costMatrix[i][i] != 0) {
throw string("Weight of the edge " + to_string(i + 1) + " - " + to_string(i +
1) + " must be 0");
}
for (int j = 0; j < n; ++j) {
if (costMatrix[i][j] < 0 && costMatrix[i][j] != -1) {
throw string("Weight of the edge " + to_string(i + 1) + " - " + to_string(j
+ 1) + " can not be negative");
}
if (i != j && costMatrix[i][j] == 0) {
throw string("Weight of the edge " + to_string(i + 1) + " - " + to_string(j
+ 1) + " can not be 0");
}
}
}
}

int minDistance(const vector<int>& dist, const vector<bool> &visited)


{
int minVal = INT_MAX;
int minIndex = -1;

for (int v = 0; v < int(dist.size()) ; ++v)


{
if (!visited[v] && dist[v] < minVal)
{
minVal = dist[v];
minIndex = v;
}
}

return minIndex;
}

void dijkstra(int graph[][MAX_NODES] , int n , int src){


vector<int> dist(n , INT_MAX);
vector<bool> visited(n , false);

vector<int> path[n];

dist[src] = 0;
path[src].push_back(src + 1);

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


int u = minDistance(dist , visited);
visited[u] = true;

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


if(!visited[v] && graph[u][v] != -1 && dist[u] != INT_MAX && dist[u]
+ graph[u][v] < dist[v]){
dist[v] = dist[u] + graph[u][v];
path[v] = path[u];
path[v].push_back(v + 1);
}
}
}

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


if (dist[i] == INT_MAX) {
cout << (i + 1) << "\t" << -1 << endl;
} else {
cout << (i + 1) << "\t" << dist[i] << "\t";
for (int j = 0; j < int(path[i].size()); j++) {
cout << path[i][j];
if (j < int(path[i].size()) - 1) cout << "->";
}
cout << endl;
}
}
}

int main() {
int n;
cin >> n;
int adj_mat[MAX_NODES][MAX_NODES];
vector<vector<int>> costMatrix(n , vector<int>(n));
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
cin >> adj_mat[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
costMatrix[i][j] = adj_mat[i][j];
}
}
try {
validateCostMatrix(costMatrix);
} catch (const string& str) {
cout << str << endl;
return 0;
}
int source;
cin >> source;

dijkstra(adj_mat , n ,source - 1);


/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}

CPP REPORT :
Id: missingIncludeSystem
Include file: <bits/stdc++.h> not found. Please note: Cppcheck does not need
standard library headers to get proper results.

Test cases :
2. Insertion Sort
Code :
#include<bits/stdc++.h>
using namespace std;

int main() {
int N;
cin >> N;
int arr[N];

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


cin >> arr[i];
}
for (int k = 0; k < N; k++) {

cout << arr[k] << " ";

cout << endl;


for (int i = 1; i < N; i++) {
int key = arr[i];
int j = i - 1;

while (j >= 0 && arr[j] > key) {


arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;

for (int k = 0; k < N; k++) {


cout << arr[k] << " ";
}
cout << endl;
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}

CPP REPORT :
Id: missingIncludeSystem
Include file: <bits/stdc++.h> not found. Please note: Cppcheck does not need standard
library headers to get proper results.

Test cases :

You might also like