Algorithm
Algorithm
Algorithm
No.
1 Breadth First Search ( BFS )
2 Quick Sort
3 Merge Sort
4 Binary Search
5 Prim Algorithm
6 Kruskal Algorithm
7 Dijkstra`s Algorithm
9 Knapsack 0 / 1
10 Adjacency Matrix
1
BFS (Breadth-First Search)
Algorithm :
Otherwise, enqueue any successors (nodes that are directly connected to the dequeued node) that
have not yet been visited.
4. If the queue is empty, every node on the graph has been visited, or there is no path from the initial
node to the goal node.
5. If the goal node was found, return the path that was followed.
Program Code :
#include <stdio.h>
#include <stdlib.h>
struct node {
int vertex;
};
struct adj_list {
};
struct graph {
int num_vertices;
int* visited;
};
new_node->vertex = vertex;
new_node->next = NULL;
2
return new_node;
graph->num_vertices = n;
int i;
graph->adj_lists[i].head = NULL;
graph->visited[i] = 0;
return graph;
new_node1->next = graph->adj_lists[src].head;
graph->adj_lists[src].head = new_node1;
new_node2->next = graph->adj_lists[dest].head;
graph->adj_lists[dest].head = new_node2;
int queue[1000];
graph->visited[v] = 1;
queue[++rear] = v;
3
struct node* temp = graph->adj_lists[current_vertex].head;
if (graph->visited[adj_vertex] == 0) {
graph->visited[adj_vertex] = 1;
queue[++rear] = adj_vertex;
temp = temp->next;
int main()
add_edge(graph, 0, 1);
add_edge(graph, 0, 2);
add_edge(graph, 1, 3);
add_edge(graph, 1, 4);
add_edge(graph, 2, 4);
add_edge(graph, 3, 4);
add_edge(graph, 3, 5);
add_edge(graph, 4,5);
bfs(graph, 0);
return 0;
4
Quick Sort
Algorithm :
2{
6}
1 pivot ? A[end]
2 i ? start-1
5 then i ? i + 1
7 }}
9 return i+1
Program Code:
#include <stdio.h>
int t = *a;
*a = *b;
*b = t;
5
int partition(int arr[], int low, int high) {
i++;
swap(&arr[i], &arr[j]);
return (i + 1);
quickSort(arr, pi + 1, high);
int i;
printf("\n");
int main() {
quickSort(arr, 0, n - 1);
6
printArray(arr, n);
return 0;
7
Merge Sort
Algorithm :
end of if
END MERGE_SORT
int i, j, k;
i = 0,
j = 0;
k = beg;
a[k] = LeftArray[i];
i++;
else
8
{
a[k] = RightArray[j];
j++;
k++;
while (i<n1)
a[k] = LeftArray[i];
i++;
k++;
while (j<n2)
a[k] = RightArray[j];
j++;
k++;
Program Code:
#include <stdio.h>
int i, j, k;
9
i = 0;
j = 0;
k = beg;
a[k] = LeftArray[i];
i++;
else
a[k] = RightArray[j];
j++;
k++;
while (i<n1)
a[k] = LeftArray[i];
i++;
k++;
while (j<n2)
a[k] = RightArray[j];
j++;
k++;
10
void mergeSort(int a[], int beg, int end)
int i;
printf("\n");
int main()
printArray(a, n);
mergeSort(a, 0, n - 1);
printArray(a, n);
return 0;
11
12
Prim Algorithm
Algorithm :
Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has minimum weight
Step 4: Add the selected edge and the vertex to the minimum spanning tree T
[END OF LOOP]
Step 5: EXIT
Program Code:
#include <stdio.h>
#include <limits.h>
#define vertices 5
return min;
int parent[vertices];
int k[vertices];
int mst[vertices];
k[i] = INT_MAX;
mst[i] = 0;
13
k[0] = 0;
parent[0] = -1;
mst[edge] = 1;
int main()
{0, 4, 2, 0, 1},
{0, 0, 6, 1, 0},
};
prim(g);
return 0;
14
15
Kruskal Algorithm
Algorithm :
2.Initialize an empty set for the minimum spanning tree and an empty disjoint-set data structure.
3.Add the edge to the tree and combine the two sets that the edge's endpoints belong to in the
disjoint-set data structure for each edge in the sorted order if doing so does not result in a cycle.
4.Until all edges have been taken into account or the smallest spanning tree has n-1 edges-where n is
the number of nodes in the graph-repeat step 3 as necessary.
5.At the end of the algorithm, the minimum spanning tree will be the set of edges added to the tree in
step 3. The temporal complexity of Kruskal's algorithm, where E is the number of edges and V is the
number of vertices in the graph, is either O(ElogE) or O(ElogV).
Program Code :
#include <stdio.h>
#include <stdlib.h>
} Edge;
int V, E;
Edge edges[MAX_EDGES];
} Graph;
} Subset;
graph->V = V;
graph->E = E;
return graph;
16
if (subsets[i].parent != i) {
return subsets[i].parent;
subsets[xroot].parent = yroot;
subsets[yroot].parent = xroot;
} else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
Edge mst[graph->V];
int e = 0, i = 0;
17
for (int v = 0; v < graph->V; ++v) {
subsets[v].parent = v;
subsets[v].rank = 0;
if (x != y) {
mst[e++] = next_edge;
Union(subsets, x, y);
int main() {
int V, E;
kruskalMST(graph);
return 0;
18
Output :
012
036
123
138
145
247
349
(0, 1) -> 2
(1, 2) -> 3
(1, 4) -> 5
(0, 3) -> 6
19
Dijkstra`s Algorithm
Algorithm :
Step 1: First, we will mark the source node with a current distance of 0 and set the rest of the nodes to
INFINITY.
Step 2: We will then set the unvisited node with the smallest current distance as the current node,
suppose X.
Step 3: For each neighbor N of the current node X: We will then add the current distance of X with the
weight of the edge joining X-N. If it is smaller than the current distance of N, set it as the new current
distance of N.
Step 5: We will repeat the process from 'Step 2' if there is any node unvisited left in the graph.
Program Code :
#include <stdio.h>
#define MAX 10
if (Graph[i][j] == 0)
cost[i][j] = INF;
else
cost[i][j] = Graph[i][j];
distance[i] = cost[start][i];
previous[i] = start;
visited_nodes[i] = 0;
20
distance[start] = 0;
visited_nodes[start] = 1;
counter = 1;
minimum_distance = INF;
minimum_distance = distance[i];
next_node = i;
visited_nodes[next_node] = 1;
if (!visited_nodes[i])
previous[i] = next_node;
counter++;
if (i != start) {
int main() {
size = 7;
Graph[0][0] = 0;
Graph[0][1] = 4;
Graph[0][2] = 0;
21
Graph[0][3] = 0;
Graph[0][4] = 0;
Graph[0][5] = 8;
Graph[0][6] = 0;
Graph[1][0] = 4;
Graph[1][1] = 0;
Graph[1][2] = 8;
Graph[1][3] = 0;
Graph[1][4] = 0;
Graph[1][5] = 11;
Graph[1][6] = 0;
Graph[2][0] = 0;
Graph[2][1] = 8;
Graph[2][2] = 0;
Graph[2][3] = 7;
Graph[2][4] = 0;
Graph[2][5] = 4;
Graph[2][6] = 0;
Graph[3][0] = 0;
Graph[3][1] = 0;
Graph[3][2] = 7;
Graph[3][3] = 0;
Graph[3][4] = 9;
Graph[3][5] = 14;
Graph[3][6] = 0;
Graph[4][0] = 0;
Graph[4][1] = 0;
22
Graph[4][2] = 0;
Graph[4][3] = 9;
Graph[4][4] = 0;
Graph[4][5] = 10;
Graph[4][6] = 2;
Graph[5][0] = 0;
Graph[5][1] = 0;
Graph[5][2] = 4;
Graph[5][3] = 14;
Graph[5][4] = 10;
Graph[5][5] = 0;
Graph[5][6] = 2;
Graph[6][0] = 0;
Graph[6][1] = 0;
Graph[6][2] = 0;
Graph[6][3] = 0;
Graph[6][4] = 2;
Graph[6][5] = 0;
Graph[6][6] = 1;
source = 0;
return 0;
23
Floyd Warshall Algorithm
Algorithm :
1.Initialize a distance matrix D wherein D[i][j] represents the shortest distance between vertex i and
vertex j.
2.Set the diagonal entries of the matrix to 0, and all other entries to infinity.
3.For every area (u,v) inside the graph, replace the gap matrix to mirror the weight of the brink: D[u][v]
= weight(u,v).
4.For every vertex okay in the graph, bear in mind all pairs of vertices (i,j) and check if the path from i
to j through k is shorter than the current best path. If it is, update the gap matrix: D[i][j] = min(D[i][j],
D[i][k] D[k][j]).
5.After all iterations, the matrix D will contain the shortest course distances between all pairs of
vertices.
Program Code :
#include <stdio.h>
int n = 4;
if (i == j)
D[i][j] = 0;
else if (A[i][j] == 0)
D[i][j] = INF;
else
D[i][j] = A[i][j];
fillDistanceMatrix(A, D);
24
if (D[i][k] < INF && D[k][j] < INF)
int main() {
{INF, 0, 3, INF},
int D[4][4];
floydWarshall(A, D);
if (D[i][j] == INF)
printf("%7s", "INF");
else
printf("%7d", D[i][j]);
printf("\n");
return 0;
25
26
Knapsack 0/1
Algorithm :
0'sFor w=0 to w {
B[0,w] = 0 }
0'sfor i = 1 to n {
B[1,0] = 0 }
For I =1 to n
for w = 0 to W
if w₁ <= w( //item i can be in the solution if v₁+ B[1-1,w-w₁] > B[i-1,w] B[1,w] = V + B[1-1,w- w₁]
else
B[i,w] B[i-1,w]}
Program Code :
#include<stdio.h>
if(a>b)
return a;
else
return b;
27
int knapsack(int W, int wt[], int val[], int n)
int i, w;
int knap[n+1][W+1];
if (i==0 || w==0)
knap[i][w] = 0;
else if
(wt[i-1] <= w)
else
knap[i][w] = knap[i-1][w];
return knap[n][W];
int main() {
int W = 50;
int n = sizeof(val)/sizeof(val[0]);
return 0;
OUTPUT :
The solution is : 65
28
Depth First Search ( DFS )
Algorithm :
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
Step 5: Push on the stack all the neighbors of N that are in the ready state (whose STATUS = 1) and set
their STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
Program Code :
#include<stdio.h>
void DFS(int);
int G[10][10],visited[10],n;
void main()
int i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
void DFS(int i)
int j; printf("\n%d",i);
visited[i]=1;
29
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1) DFS(j);
OUTPUT:
10000100
10000100
10000010
10000010
01100001
00011001
00000110
30
Adjacency Matrix
Algorithm:
1.Create a 2D array(say Adj[N+1][N+1]) of size NxN and initialise all value of this matrix to zero.
2.For each edge in arr[][](say X and Y), Update value at Adj[X][Y] and Adj[Y][X] to 1, denotes that there
is a edge between X and Y.
3.Display the Adjacency Matrix after the above operation for all the pairs in arr[][].
Program Code:
#include <stdio.h>
int N, M;
31
{
Adj[i][j] = 0;
int x = arr[i][0];
int y = arr[i][1];
Adj[x][y] = 1;
Adj[y][x] = 1;
printf("\n");
int main()
N = 5;
int arr[][2] = { { 1, 2 }, { 2, 3 } { 4, 5 }, { 1, 5 } };
M = sizeof(arr) / sizeof(arr[0]);
32
createAdjMatrix(Adj, arr);
printAdjMatrix(Adj);
return 0;
Output:
01001
10100
01000
00001
10010
33