Skip to content

Commit 41d44a8

Browse files
committed
2 parents dba0fd5 + aed1a6e commit 41d44a8

File tree

7 files changed

+33
-29
lines changed

7 files changed

+33
-29
lines changed

10.krushkal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void main()
2727
}
2828

2929
printf("\nThe edges of Minimum Cost Spanning Tree are\n");
30-
while (ne < n)
30+
while (ne < n) // n-1 edges
3131
{
3232
for (i = 1, min = 999; i <= n; i++)
3333
{

11.dijkstras.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#include <stdio.h>
2+
#include <stdlib.h>
23
#define INFINITY 9999
34
#define MAX 10
5+
46
void dijkstra(int G[MAX][MAX], int n, int startnode);
7+
58
int main()
69
{
710
int G[MAX][MAX], i, j, n, u;
@@ -16,10 +19,11 @@ int main()
1619
dijkstra(G, n, u);
1720
return 0;
1821
}
22+
1923
void dijkstra(int G[MAX][MAX], int n, int startnode)
2024
{
21-
int cost[MAX][MAX], distance[MAX], pred[MAX];
22-
int visited[MAX], count, mindistance, nextnode, i, j;
25+
int cost[MAX][MAX], distance[MAX], from[MAX];
26+
int visited[MAX], count, min_distance, nextnode, i, j;
2327
// pred[] stores the predecessor of each node
2428
// count gives the number of nodes seen so far
2529
// create the cost matrix
@@ -29,34 +33,34 @@ void dijkstra(int G[MAX][MAX], int n, int startnode)
2933
cost[i][j] = INFINITY;
3034
else
3135
cost[i][j] = G[i][j];
32-
// initialize pred[],distance[] and visited[]
36+
// initialise visited[], distance[] and from[]
37+
distance[startnode] = 0;
38+
visited[startnode] = 1;
3339
for (i = 0; i < n; i++)
3440
{
3541
distance[i] = cost[startnode][i];
36-
pred[i] = startnode;
42+
from[i] = startnode;
3743
visited[i] = 0;
3844
}
39-
distance[startnode] = 0;
40-
visited[startnode] = 1;
4145
count = 1;
4246
while (count < n - 1)
4347
{
44-
mindistance = INFINITY;
48+
min_distance = INFINITY;
4549
// nextnode gives the node at minimum distance
4650
for (i = 0; i < n; i++)
47-
if (distance[i] < mindistance && !visited[i])
51+
if (distance[i] < min_distance && !visited[i])
4852
{
49-
mindistance = distance[i];
5053
nextnode = i;
54+
min_distance = distance[i];
5155
}
5256
// check if a better path exists through nextnode
5357
visited[nextnode] = 1;
5458
for (i = 0; i < n; i++)
5559
if (!visited[i])
56-
if (mindistance + cost[nextnode][i] < distance[i])
60+
if (min_distance + cost[nextnode][i] < distance[i])
5761
{
58-
distance[i] = mindistance + cost[nextnode][i];
59-
pred[i] = nextnode;
62+
distance[i] = min_distance + cost[nextnode][i];
63+
from[i] = nextnode;
6064
}
6165
count++;
6266
}
@@ -69,7 +73,7 @@ void dijkstra(int G[MAX][MAX], int n, int startnode)
6973
j = i;
7074
do
7175
{
72-
j = pred[j];
76+
j = from[j];
7377
printf("<-%d", j);
7478
} while (j != startnode);
7579
}

3.Bubble_Insertion.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void insertionSort(int *array, int size)
2323
for (int i = 1; i < size; i++)
2424
{
2525
key = array[i]; // take value
26-
j = i;
26+
j = i; // take index
2727
while (j > 0 && array[j - 1] > key)
2828
{
2929
array[j] = array[j - 1];

6.BFS.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void bfs(int v)
2222
main()
2323
{
2424
int v, i, j;
25-
printf("\n Enter the number of vertices:");
25+
printf("\n Enter the number of vertices: ");
2626
scanf("%d", &n);
2727
for (i = 0; i < n; i++) // mark all the vertices as not visited
2828
{

9.prims.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3-
43
#define infinity 9999
54
#define MAX 20
65

7-
int G[MAX][MAX], spanning[MAX][MAX], n;
8-
9-
int prims();
6+
int prims(int G[MAX][MAX], int spanning[MAX][MAX], int n);
107

118
int main()
129
{
10+
int G[MAX][MAX], spanning[MAX][MAX], n;
1311
int i, j, total_cost;
1412
printf("Enter no. of vertices: ");
1513
scanf("%d", &n);
1614
printf("\nEnter the adjacency matrix: \n");
1715
for (i = 0; i < n; i++)
1816
for (j = 0; j < n; j++)
1917
scanf("%d", &G[i][j]);
20-
total_cost = prims();
18+
total_cost = prims(G, spanning, n);
2119
printf("\nspanning tree matrix:\n");
2220
for (i = 0; i < n; i++)
2321
{
@@ -29,7 +27,7 @@ int main()
2927
return 0;
3028
}
3129

32-
int prims()
30+
int prims(int G[MAX][MAX], int spanning[MAX][MAX], int n)
3331
{
3432
int cost[MAX][MAX];
3533
int u, v, min_distance, distance[MAX], from[MAX];
@@ -69,7 +67,7 @@ int prims()
6967
min_distance = distance[i];
7068
}
7169
u = from[v];
72-
70+
7371
// insert the edge in spanning tree
7472
spanning[u][v] = distance[v];
7573
spanning[v][u] = distance[v];

lab-manual/11.dijkstras.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ int main()
1919
void dijkstra(int G[MAX][MAX], int n, int startnode)
2020
{
2121
int cost[MAX][MAX], distance[MAX], pred[MAX];
22-
int visited[MAX], count, mindistance, nextnode, i, j;
22+
int visited[MAX], count, min_distance, nextnode, i, j;
2323
// pred[] stores the predecessor of each node
2424
// count gives the number of nodes seen so far
2525
// create the cost matrix
@@ -41,21 +41,21 @@ void dijkstra(int G[MAX][MAX], int n, int startnode)
4141
count = 1;
4242
while (count < n - 1)
4343
{
44-
mindistance = INFINITY;
44+
min_distance = INFINITY;
4545
// nextnode gives the node at minimum distance
4646
for (i = 0; i < n; i++)
47-
if (distance[i] < mindistance && !visited[i])
47+
if (distance[i] < min_distance && !visited[i])
4848
{
49-
mindistance = distance[i];
49+
min_distance = distance[i];
5050
nextnode = i;
5151
}
5252
// check if a better path exists through nextnode
5353
visited[nextnode] = 1;
5454
for (i = 0; i < n; i++)
5555
if (!visited[i])
56-
if (mindistance + cost[nextnode][i] < distance[i])
56+
if (min_distance + cost[nextnode][i] < distance[i])
5757
{
58-
distance[i] = mindistance + cost[nextnode][i];
58+
distance[i] = min_distance + cost[nextnode][i];
5959
pred[i] = nextnode;
6060
}
6161
count++;

lab-manual/2.Linear_Binary.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ int Linear_search()
1414
cout << "\n Enter a Number to Search: ";
1515
cin >> num;
1616
start = clock();
17+
int flag = 0;
1718
for (i = 0; i < n; i++)
1819
{
1920
if (arr[i] == num)
2021
{
2122
index = i;
23+
flag = 1;
2224
break;
2325
}
2426
}

0 commit comments

Comments
 (0)
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