DAA Lab(extras)
DAA Lab(extras)
PROBLEM STATEMENT :
Implement Hamiltonian problem. Mention the compiler used in your program.
Analyze the complexity value of your algorithm.
ALGORITHM :
Algorithm Hamiltonian(k)
repeat
else Hamiltonian(k+1);
} until (false);
AlgorithmNextValue(k)
repeat
if (G[x[k-1],x[k]] !=0)then
{ // Is there an edge?
then return;
} until(false);
PROGRAM CODE :
#include<stdio.h>
#define SIZE 10
int n, g[SIZE][SIZE],x[SIZE],i,j;
int hamiltonian(int);
int nextval(int);
int main()
int s;
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&g[i][j]);
scanf("%d",&s);
x[1]=s;
for(i=2;i<=n;i++)
x[i]=0;
hamiltonian(2);
printf("%d",s);
}
int hamiltonian(int k)
while(1)
nextval(k);
if(x[k]==0)
return;
if(k==n)
for(i=1;i<=n;i++)
printf("%d->",x[i]);
else
hamiltonian(k+1);
int nextval(int k)
while(1)
x[k]=(x[k]+1)%(n+1);
if(x[k]==0)
return;
if(g[x[k-1]][x[k]]!=0)
for(j=1;j<k;j++)
if(x[j]==x[k])
break;
if(j==k)
return;
01001
10111
01010
01101
11000
--------------------------------
COMPLEXITY ANALYSIS :
1. In this algorithm the function Hamiltonian() will be called for n times.
2. Now the function NextValue() checks for every vertex(except itself and
the vertex already traversed)
3. So, each next vertex has (n-1) choices
4. Thus the loop or the cycle goes on like, n(n-1)(n-2)(n-3)……….. = n!
ALGORITHM :
Algorithm bellman_ford(s)
dis[i]=INF
dis[s]=0
dis[j]=dis[i]+g[i][j]
for i=1 upto n do
If(dis[i]!=INF) then
print: s->i=dis[i]
Else
print: s->i=INF
PROGRAM CODE :
#include<stdio.h>
#define SIZE 10
int g[SIZE][SIZE],dis[SIZE],n,s;
void bellman_ford(int);
void main()
int i,j;
scanf("%d",&n);
scanf("%d",&s);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%d", &g[i][j]);
if(g[i][j]==0)
g[i][j]=INF;
bellman_ford(s);
void bellman_ford(int s)
int i,j,k;
for(i=1;i<=n;i++)
dis[i]=INF;
dis[s]=0;
for(k=1;k<=n-1;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
for(i=1;i<=n;i++)
if(dis[i]!=INF)
else
00323
01045
00000
0 0 0 -5 0
The path is: 1->1 = 0
--------------------------------
COMPLEXITY ANALYSIS :
As we can see there are three loops in a nested manner as:
So the total complexity of the algorithm is max(n, n^2, n^3) => O(n^3)
ASSIGNMENT NO :9
PROBLEM STATEMENT :
Implement MST Problem using Kruskal’s algorithm. Mention the Compiler used
in your program. Analyze the complexity value of your algorithm
ALGORITHM:
Algorithm Kruskals(s)
edge[i][2] = INF
edge[k+1][2] = edge[k][2]
edge[k][2] = cost[i][j]
edge[k+1][0] = edge[k][0]
edge[k+1][1] = edge[k][1]
edge[k][0] = i
edge[k][1] = j
count = count + 1
Print edge[i][2]
Return mincost
Algorithm set_union(u, v)
Declare variable i
For i from 1 to n
set[i] = set[u]
set[v] = set[u]
Print "Edge", edgeno, ": ", u, " --> ", v, " = ", edge[i][2]
edgeno = edgeno + 1
}
PROGRAM CODE:
#include<stdio.h>
#define MAX 10
int kruskals(int);
void set_union(int,int);
int cost[MAX][MAX],edge[2*MAX][3],set[10];
int n,count=1,edgeno=1;
void main()
int i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=INF;
}
set[i]=i;//making individual set of all the vertices....
int kruskals(int s)
int i,j,k,mincost=0;
for(i=1;i<MAX2;i++)
edge[i][2]=INF;
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
if(cost[i][j]!=INF)
for(k=count;k>=1;k--)
if(edge[k][2]>cost[i][j])
edge[k+1][2]=edge[k][2];
edge[k][2]=cost[i][j];
edge[k+1][0]=edge[k][0];
edge[k+1][1]=edge[k][1];
edge[k][0]=i;
edge[k][1]=j;
count++;
for(i=1;i<=count;i++)
if(set[edge[i][0]]!=set[edge[i][1]])
mincost+=edge[i][2];
return mincost;
int i;
set[i]=set[u];
set[v]=set[u];
}
RESULTS AND DISCUSSION:
Enter the number of vertices: 6
805030
150564
505002
036006
004260
Edge1: 1 --> 3 = 1
Edge2: 4 --> 6 = 2
Edge3: 2 --> 5 = 3
Edge4: 3 --> 6 = 4
Edge5: 2 --> 3 = 5
--------------------------------
COMPLEXITY ANALYSIS:
In this algorithm, if we ignore the sorting time needed to sort the edges,
then:
Now if the sorting time is considered, and if we use simple insertion sorting
to sort the edges:
ALGORITHM :
Function tsp(s) {
Curr s
min infinity
mindist[curr][i]
next i
visited[curr]=1
distance=distance + min
curr next
cities=cities+1
}
PROGRAM CODE :
#include<stdio.h>
#define MAX 10
int tsp(int);
int dist[MAX][MAX],visited[MAX],n;
void main()
int i,j,s;
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&dist[i][j]);
if(dist[i][j]==0)
dist[i][j]=INF;
}
visited[i]=0;
scanf("%d",&s);
int tsp(int s)
int i,min,next,curr=s,distance=0,cities=0;
while(cities<n-1)
min=INF;
for(i=1;i<=n;i++)
min=dist[curr][i];
next=i;
visited[curr]=1;
distance+=min;
cities++;
return distance+dist[curr][s];
50243000
02010000
64107000
03070064
40000030
00006302
70004020
--------------------------------
COMPLEXITY ANALYSIS :
In the tsp() function:
ALGORITHM :
1. Input the size of the puzzle grid (n) and the initial arrangement of the puzzle
(puzzle)
Else{
3. Function isSolvable():
Increment inversions count for each tile following it with a lower value
Return true if the sum of inversions count and the row index of the blank
tile is even
4. Function generateGoalState(n):
Create an empty 2D array goal with dimensions n x n
Initialize num to 1
If num is equal to n * n:
Else:
Increment num by 1
Copy the puzzle state from the puzzle parameter to the node's puzzle state
Swap the values of the blankPos and newBlankPos in the puzzle state
Initialize count to 0
If the cell value in initial array is not zero and different from the
corresponding cell in goal:
Increment count
Return count
7. Function isSafe(x, y):
Return true if x and y are within the bounds of the puzzle grid
9. Function pop(pq):
Print a newline
Return
PROGRAM CODE :
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct {
int x, y;
} Point;
int puzzle[MAX][MAX];
Point blankPos;
int cost;
int level;
} Node;
typedef struct {
int size;
} PriorityQueue;
int isSolvable();
void NPuzzle();
int pos_BTile();
int main() {
int i, j;
scanf("%d", &n);
scanf("%d", &puzzle[i][j]);
NPuzzle();
return 0;
}
void NPuzzle() {
if (isSolvable()) {
int i, j;
if (num == n * n) {
goal[i][j] = 0;
} else {
goal[i][j] = num++;
pos_BTile();
} else {
int isSolvable() {
flatten[ind++] = puzzle[i][j];
inv++;
if (n % 2 == 0) {
if ((inv + pos_BTile()) % 2 == 0)
return 0;
else
return 1;
} else {
if (inv % 2 == 0)
return 1;
else
return 0;
int pos_BTile() {
int i, j, pos = 0;
for (i = 1; i <= n; i++) {
pos++;
if (puzzle[i][j] == 0) {
row = i;
column = j;
return pos;
PriorityQueue pq = { .size = 0 };
push(&pq, root);
while (pq.size) {
if (min->cost == 0) {
printf("Solution found in %d moves:\n", min->level);
printPath(min); // This will print all states from the initial to the
final state
return;
int i;
if (isSafe(newX, newY)) {
push(&pq, child);
node->parent = parent;
node->level = level;
node->blankPos = newBlankPos;
int i, j;
node->puzzle[i][j] = puzzle[i][j];
node->puzzle[blankPos.x][blankPos.y] = node->puzzle[newBlankPos.x]
[newBlankPos.y];
node->puzzle[newBlankPos.x][newBlankPos.y] = temp;
return node;
int count = 0;
int i, j;
count++;
}
}
return count;
pq->nodes[pq->size++] = node;
int i = pq->size - 1;
pq->nodes[(i - 1) / 2] = temp;
i = (i - 1) / 2;
pq->nodes[0] = pq->nodes[--pq->size];
int i = 0;
int smallest = i;
smallest = 2 * i + 1;
}
if (2 * i + 2 < pq->size && (pq->nodes[2 * i + 2]->cost + pq->nodes[2 * i
+ 2]->level) < (pq->nodes[smallest]->cost + pq->nodes[smallest]->level)) {
smallest = 2 * i + 2;
if (smallest == i) break;
pq->nodes[i] = pq->nodes[smallest];
pq->nodes[smallest] = temp;
i = smallest;
return min;
int i, j;
printf("\n");
printf("\n");
printPuzzle(root->puzzle);
5608
9 10 7 11
13 14 15 12
1 2 3 4
5 6 0 8
9 10 7 11
13 14 15 12
1 2 3 4
5 6 7 8
9 10 0 11
13 14 15 12
1 2 3 4
5 6 7 8
9 10 11 0
13 14 15 12
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
COMPLEXITY ANALYSIS :
1. NPuzzle Function:
o isSolvable(): O(n^2) - The function computes the number of
inversions and checks if the puzzle is solvable based on its
dimensions and the number of inversions.
o pos_BTile(): O(n^2) - Determines the position of the blank tile
in the puzzle.
o solve(): O(b^d), where b is the branching factor (4 in this case)
and d is the depth of the solution.
o Creating the goal state: O(n^2)
2. Node Creation:
o newNode(): O(n^2) - Copies the puzzle state and swaps the
positions of tiles.
3. Priority Queue Operations:
o push(): O(log(N)) - Performs up-heap bubbling.
o pop(): O(log(N)) - Performs down-heap bubbling.
4. Printing:
o printPath(): O(d * n^2) - Prints the path from the initial state
to the final state.
5. Other Operations:
o calculateCost(): O(n^2) - Calculates the Manhattan distance
between the initial and goal states.
o isSafe(): O(1) - Checks if a given position is within the bounds
of the puzzle grid.
Overall, the time complexity is dominated by the solve() function, which has
a worst-case time complexity of O(b^d), where b is the branching factor (4 in
this case) and d is the depth of the solution. The space complexity mainly
depends on the priority queue size and the recursive depth of the solution path.