ADA Manual To Students
ADA Manual To Students
Index
Sl. No. Content Page
No.
Design and implement C/C++ Program to find Minimum Cost Spanning Tree of
1 a given connected undirected graph using Kruskal's algorithm.
2 Design and implement C/C++ Program to find Minimum Cost Spanning Tree of
a given connected undirected graph using Prim's algorithm.
3 a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths
problem using Floyd's algorithm.
3 b. Design and implement C/C++ Program to find the transitive closure using
Warshal's algorithm
Design and implement C/C++ Program to find shortest paths from a given
Design and implement C/C++ Program to solve 0/1 Knapsack problem using
6 Dynamic Programming method
Design and implement C/C++ Program to find a subset of a given set S = {sl ,
8 s2,.....,sn} of n positive integers whose sum is equal to a given positive integer
d.
Design and implement C/C++ Program to sort a given set of n integer elements
using Selection Sort method and compute its time complexity. Run the program
9 for varied values of n> 5000 and record the time taken to sort. Plot a graph of
the time taken versus n. The elements can be read from a file or can be
generated using the random number generator.
Design and implement C/C++ Program to sort a given set of n integer elements
using Merge Sort method and compute its time complexity. Run the program
11 for varied values of n> 5000, and record the time taken to sort. Plot a graph of
the time taken versus n. The elements can be read from a file or can be
generated using the random number generator.
PROGRAM OUTCOMES(POs)
Program Outcomes as defined by NBA (PO) Engineering Graduates will be able to:
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals,
and an engineering specialization to the solution of complex engineering problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences,
and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with an
understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering solutions
in societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of
the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
10. Communication: Communicate
effectively on complex engineering
activities with the engineering community
and with society at large, such as, being
able to comprehend and write effective
reports and design documentation, make
effective presentations, and give and
receive clear instructions.
11. Project management and finance:
Demonstrate knowledge and
understanding of the engineering and
management principles and apply these to
one’s own work, as a member and leader
in a team, to manage projects and in multidisciplinary environments.
Vision (College)
To nurture talent and enrich society through excellence in technical education, research and
innovation.
Mission (College)
1. To augment innovative Pedagogy, kindle quest for interdisciplinary learning & to enhance
conceptual understanding.
2. To build competence, professional ethics & develop entrepreneurial thinking.
3. To strengthen Industry Institute Partnership & explore global collaborations.
4. To inculcate culture of socially responsible citizenship.
5. To focus on Holistic & Sustainable development.
Vision (Dept)
Mission (Dept)
To train students with conceptual understanding through innovative pedagogies.
To imbibe professional, research, and entrepreneurial skills with a commitment to the
nation’s development at large.
To strengthen the industry-institute Interaction.
To promote life–long learning with a sense of societal & ethical responsibilities.
Subject with code: Analysis and Design of Algorithms Laboratory AY: 2024-25
th
Semester: 4
S.No. Description PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2
Develop programs to
solve computational
1 problems using suitable 3 1 1
algorithm design
strategy.
Compare algorithm
design strategies by
developing equivalent
2 programs and 3 2 1 1 1 1
observing running
times for analysis
(Empirical).
Make use of suitable
integrated development
3 3 2 1 1 1 1
tools to develop
programs
Choose appropriate
algorithm design
techniques to develop
4 3 2 1 1 1 1
solution to the
computational and
complex problems
Demonstrate and present
the development of
program, its execution
5 3 1 1 1 1 1
and running time(s) and
record the
results/inferences
Degree of compliance Low: 1 Medium: 2 High: 3
Evaluation: CIA
DESCRIPTION:
Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connectedweighted
graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total
weight of all the edges in the tree is minimized. If the graph is not connected, then it finds a minimum spanning
forest (a minimum spanning tree for each connected component). Kruskal'salgorithm is an example of a greedy
algorithm
ALGORITHM:
Let G = (V, E) be the given graph, with | V| = n
{
Start with a graph T = (V,$ \phi$) consisting of only the
vertices of G and no edges; /* This can be viewed as n connected components, each vertex being one
connected component */
Arrange E in the order of increasing costs;
for (i = 1, i$ \leq$n - 1, i + +)
{
Select the next smallest cost edge;
if (the edge connects two different connected components)
add the edge to T;
}
}
Program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{
clrscr();
printf("\n\tImplementation of Kruskal's algorithm\n");
Output:
Description:
The program uses prim's algorithm which is based on minimum spanning tree for a connected undirected
graph.A predefinined cost adjecency matrix is the input.To find the minimum spanning tree, we choose the
source node at random and in every step we find the node which is closest as well as having the least cost from
the previously selected node.And also the cost of selected edge is being added to variable sum.Based on the
value of sum, the presence of the minimum spanning tree is found.
Algorithm:
Input: A non-empty connected weighted graph with vertices V and edges E (the weights can be negative).
Initialize: Vnew = {x}, where x is an arbitrary node (starting point) from V, Enew = {}
Repeat until Vnew = V:
Choose an edge {u, v} with minimal weight such that u is in Vnew and v is not (if there are multiple edges
with the same weight, any of them may be picked)
Add v to Vnew, and {u, v} to Enew
Output: Vnew and Enew describe a minimal spanning tree
Program
#include<stdio.h>
#include<conio.h>
int n,cost[10][10],temp,nears[10];
void readv();
void primsalg();
void readv()
{
int i,j;
printf("\n Enter the No of nodes or vertices:");
scanf("%d",&n);
printf("\n Enter the Cost Adjacency matrix of the given graph:");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if((cost[i][j]==0) && (i!=j))
{
Output:
DESCRIPTION
The Floyd's algorithm is a graph analysis algorithm for finding shortest paths in a weighted graph with positive
or negative edge weights (but with no negative cycles, see below) and also for finding transitive closure of a
relation R. A single execution of the algorithm will find the lengths (summed weights) of the shortest paths
between all pairs of vertices, though it does not return details of the paths themselves.
ALGORITHM:
let dist be a |V| x |V| array of minimum distances initialized to infinity
for each vertex v
dist[v][v] <- 0
for each edge (u,v)
dist[u][v] <- w(u,v) // the weight of the edge (u,v)
for k from 1 to |V|
for i from 1 to |V|
for j from 1 to |V|
if dist[i][j] > dist[i][k] + dist[k][j]
dist[i][j] <- dist[i][k] + dist[k][j]
end if
Program
#include<stdio.h>
#include<conio.h>
void readf();
void amin();
int cost[20][20],a[20][20];
int i,j,k,n;
void readf()
{
printf("\n Enter the no of vertices:");
scanf("%d",&n);
printf("\n Enter the Cost of vertices:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0 && (i!=j))
Output:
DESCRIPTION :
Warshall's algorithm determines whether there is a path between any two nodes in the graph. It does not give
the number of the paths between two nodes. According to Warshall's algorith,a path exists between two
vertices i, j, iff there is a path from i to j or there is a path from i to j through 1,..,k intermadiate nodes.
ALGORITHM:
n = |V|
t(0) = the adjacency matrix for G
for i in 1..n do
t(0)[i,i] = True
end for
for k in 1..n do
for i in 1..n do
for j in 1..n do
t(k)[i,j] = t(k-1)[i,j] OR
(t(k-1)[i,k] AND t(k-1)[k,j])
end for
end for
end for
return t(n)
Program
#include<stdio.h>
const int MAX = 100;
void WarshallTransitiveClosure(int graph[MAX][MAX], int numVert);
int main(void)
{
int i, j, numVert;
int graph[MAX][MAX];
printf("Warshall's Transitive Closure\n");
printf("Enter the number of vertices : ");
scanf("%d",&numVert);
DESCRIPTION:
To find the shortest path between points, the weight or length of a path is calculated as the sum of the weights
of the edges in the path.
A path is a shortest path is there is no path from x to y with lower weight.
Dijkstra's algorithm finds the shortest path from x to y in order of increasing distance from x. That is, it chooses
the first minimum edge, stores this value and adds the next minimum value from the next edge it selects.
It starts out at one vertex and branches out by selecting certain edges that lead to new vertices.
It is similar to the minimum spanning tree algorithm, in that it is "greedy", always choosing the closest edge in
hopes of an optimal solution.
ALGORITHM:
function Dijkstra(Graph, source):
for each vertex v in Graph: // Initializations
dist[v] := infinity ; // Unknown distance function from
// source to v
previous[v] := undefined ; // Previous node in optimal path
end for // from source
dist[source] := 0 ; // Distance from source to source
Q := the set of all nodes in Graph ; // All nodes in the graph are
// unoptimized - thus are in Q
while Q is not empty: // The main loop
u := vertex in Q with smallest distance in dist[] ; // Source node in first case
remove u from Q ;
if dist[u] = infinity:
break ; // all remaining vertices are
end if // inaccessible from source
Program
#include<ostream>
#include<cstdio>
using namespace std;
void fnDijkstra(int [][MAXNODES], int [], int [], int[], int, int, int);
int main(void)
{
int n,cost[MAXNODES][MAXNODES],dist[MAXNODES],visited[MAXNODES],path[MAXNODES],i,j,source,dest;
if (dist[dest] == INF)
cout << dest << " not reachable" << endl;
else
{
cout << endl;
i = dest;
do
{
cout << i << "<--";
return 0;
}
void fnDijkstra(int c[MAXNODES][MAXNODES], int d[MAXNODES], int p[MAXNODES], int s[MAXNODES], int so,
int de, int n)
{
int i,j,a,b,min;
for (i=0;i<n;i++)
{
s[i] = 0;
d[i] = c[so][i];
p[i] = so;
}
s[so] = 1;
for (i=1;i<n;i++)
{
min = INF;
a = -1;
for (j=0;j<n;j++)
{
if (s[j] == 0)
{
if (d[j] < min)
{
min = d[j];
a = j;
}
}
}
if (a == -1) return;
s[a] = 1;
Dept. of CSE, KLS VDIT Haliyal. Page 23
Analysis & Design of Algorithms Lab BCSL404
if (a == de) return;
for (b=0;b<n;b++)
{
if (s[b] == 0)
{
if (d[a] + c[a][b] <d[b])
{
d[b] = d[a] + c[a][b];
p[b] = a;
}
}
}
}
}
Output
Enter the number of nodes 5 Enter the Cost Matrix
0 3 9999 7 9999 3 0 4 2 9999 9999 4 0 5 6 7 2 5 0 4 9999 9999 6 4 0
//For Source Vertex : 0 shortest path to other vertices//
0<--0 = 0
1<--0 = 3
2<--1<--0 = 7
3<--1<--0 = 5
4<--3<--1<--0 = 9 Press Enter to continue...
//For Source Vertex : 1 shortest path to other vertices//
0<--1 = 3
1<--1 = 0
2<--1 = 4
3<--1 = 2
4<--3<--1 = 6 Press Enter to continue...
//For Source Vertex : 2 shortest path to other vertices//
0<--1<--2 = 7
1<--2 = 4
2<--2 = 0
3<--2 = 5
4<--2 = 6 Press Enter to continue...
//For Source Vertex : 3 shortest path to other vertices//
0<--1<--3 = 5
1<--3 = 2
2<--3 = 5
3<--3 = 0
4<--3 = 4 Press Enter to continue...
DESCRIPTION :
Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed
edge uv, vertex u comes before v in the ordering.Topological Sorting for a graph is not possible if the graph is
not a DAG. Input parameters: int a[MAX][MAX] - adjacency matrix of the input graph int n - no of vertices in the
graph
ALGORITHM :
L Empty list that will contain the sorted elements S Set of all nodes with no incoming edges while S is non-empty
do remove a node n from S add n to tail of L for each node m with an edge e from n to m do remove edge e
from the graph if m has no other incoming edges then insert m into S if graph has edges then return error
(graph has at least one cycle) else return L (a topologically sorted order)
CODE :
#include <stdio.h>
const int MAX = 10;
void fnTopological(int a[MAX][MAX], int n);
int main(void)
{
int a[MAX][MAX],n;
int i,j;
fnTopological(a,n);
printf("\n");
return 0;
}
for (i=0;i<n;i++)
{
in[i] = 0;
for (j=0; j<n; j++)
if (a[j][i] == 1)
in[i]++;
}
while(1)
{
for (i=0;i<n;i++)
{
if (in[i] == 0)
{
stack[++top] = i;
in[i] = -1;
}
}
if (top == -1)
break;
out[k] = stack[top--];
for (i=0;i<n;i++)
{
if (a[out[k]][i] == 1)
in[i]--;
}
k++;
}
DESCRIPTION:
The Knapsack problem is probably one of the most interesting and most popular in computer science, especially
when we talk about dynamic programming. The knapsack problem is a problem in combinatorial optimization.
Given a set of items, each with a weight and a value, determine the number of each item to include in a
collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.
It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill
it with the most valuable items.
ALGORITHM:
Input:
Values (stored in array v or profit)
Weights (stored in array w or weight)
Number of distinct items (n)
Knapsack capacity (W)
for j from 0 to W do
m[0, j] = 0
end for
for i from 1 to n do
for j from 0 to W do
if w[i] <= j then
m[i, j] = max(m[i-1, j], m[i-1, j-w[i]] + v[i])
else
m[i, j] = m[i-1, j]
end if
end for
end for
Program
#include <iostream>
#include <cstdlib>
using namespace std;
const int MAX = 10;
inline int max(int a, int b);
void fnProfitTable(int w[MAX], int p[MAX], int n, int c, int t[MAX][MAX]);
void fnSelectItems(int n,int c, int t[MAX][MAX], int w[MAX], int l[MAX]);<\pre>
int main(void)
i = n;
j = c;
7. Design and implement C/C++ Program to solve discrete Knapsack and continuous Knapsack problems using
greedy approximation method.
Program
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *name;
int weight;
int value;
int count;
} item_t;
item_t items[] = {
{"map", 9, 150, 1},
{"compass", 13, 35, 1},
{"water", 153, 200, 2},
{"sandwich", 50, 60, 2},
{"glucose", 15, 60, 2},
{"tin", 68, 45, 3},
{"banana", 27, 60, 3},
{"apple", 39, 40, 3},
{"cheese", 23, 30, 1},
{"beer", 52, 10, 3},
{"suntan cream", 11, 70, 1},
{"camera", 32, 30, 1},
{"T-shirt", 24, 15, 2},
{"trousers", 48, 10, 2},
{"umbrella", 73, 40, 1},
{"waterproof trousers", 42, 70, 1},
{"waterproof overclothes", 43, 75, 1},
{"note-case", 22, 80, 1},
{"sunglasses", 7, 20, 1},
{"towel", 18, 12, 2},
{"socks", 4, 50, 1},
{"book", 30, 10, 2},
};
int main () {
int i, tc = 0, tw = 0, tv = 0, *s;
Desrcription:
In computer science, the subset sum problem is an important problem in complexity theory and cryptography.
The problem is this: Given a set of integers, is there a non-empty subset whose sum is zero? For example, given
the set {-7, -3, -2, 5, 8}, the answer is yes because the subset {-3, -2, 5} sums to zero. The problem is NP-
complete. Input: The number of elements. The Weights of each element. Total Required Weight.
Output:Subsets in which the sum of elements is equal to the given required weight(input).
Program
#include <iostream>
using namespace std;
// Constant definitions
const int MAX = 100;
// class definitions
class SubSet
{
int stk[MAX], set[MAX];
int size, top, count;
public:
SubSet()
{
top = -1;
count = 0;
}
void getInfo(void);
void push(int data);
void pop(void);
void display(void);
int fnFindSubset(int pos, int sum);
};
void SubSet :: getInfo(void)
{
int i;
cout << "Enter the maximum number of elements : ";
if (sum>0)
{
for (i=pos; i<=size; i++)
{
push(set[i]);
fnFindSubset(i+1, sum - set[i]);
pop();
}
}
if (sum == 0)
{
9. Design and implement C/C++ Program to sort a given set of n integer elements using Selection Sort method
and compute its time complexity. Run the program for varied values of n> 5000 and record the time taken to
sort. Plot a graph of the time taken versus n. The elements can be read from a file or can be generated using the
random number generator
Algorithm
SELECTION SORT(arr, n)
Step 1: Repeat Steps 2 and 3 for i = 0 to n-1
Step 2: CALL SMALLEST(arr, i, n, pos)
Step 3: SWAP arr[i] with arr[pos]
[END OF LOOP]
Step 4: EXIT
SMALLEST (arr, i, n, pos)
Step 1: [INITIALIZE] SET SMALL = arr[i]
Step 2: [INITIALIZE] SET pos = i
Step 3: Repeat for j = i+1 to n
if (SMALL > arr[j])
SET SMALL = arr[j]
SET pos = j
[END OF if]
[END OF LOOP]
Step 4: RETURN pos
Program
#include <stdio.h>
void selection_sort (int *a, int n) {
int i, j, m, t;
for (i = 0; i < n; i++) {
for (j = i, m = i; j < n; j++) {
if (a[j] < a[m]) {
m = j;
}
}
t = a[i];
a[i] = a[m];
a[m] = t;
}
int main () {
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
int n = sizeof a / sizeof a[0];
int i;
for (i = 0; i < n; i++)
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
selection_sort(a, n);
for (i = 0; i < n; i++)
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
return 0;
}
Output:
Input : 4 65 2 -31 0 99 2 83 782 1
Output: -31 0 1 2 2 4 65 83 99 782
10. Design and implement C/C++ Program to sort a given set of n integer elements using Quick Sort method and
compute its time complexity. Run the program for varied values of n> 5000 and record the time taken to sort.
Plot a graph of the time taken versus n. The elements can be read from a file or can be generated using the
random number generator.
Description:
The program is based on the Quicksort algorithm which is an instantiation of divide and conquer method of
solving the problem. Here the given array is partitioned every time and the sub-array is sorted. Dividing is based
on an element called pivot. A divide and conquer algorithm works by recursively breaking down a problem into
two or more sub-problems of the same (or related) type, until these become simple enough to be solved
directly. The solutions to the sub-problems are then combined to give a solution to the original problem.
Dept. of CSE, KLS VDIT Haliyal. Page 41
Analysis & Design of Algorithms Lab BCSL404
Program
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
FILE *fp;
struct timeval tv;
double dStart,dEnd;
int iaArr[500000],iNum,iPos,iKey,i,iChoice;
for(;;)
{
printf("\n1.Plot the Graph\n2.QuickSort\n3.Exit");
printf("\nEnter your choice\n");
scanf("%d",&iChoice);
switch(iChoice)
{
case 1:
fp = fopen("QuickPlot.dat","w");
for(i=100;i<100000;i+=100)
{
fnGenRandInput(iaArr,i);
gettimeofday(&tv,NULL);
dStart = tv.tv_sec + (tv.tv_usec/1000000.0);
fnQuickSort(iaArr,0,i-1);
gettimeofday(&tv,NULL);
dEnd = tv.tv_sec + (tv.tv_usec/1000000.0);
fprintf(fp,"%d\t%lf\n",i,dEnd-dStart);
}
fclose(fp);
printf("\nData File generated and stored in file < QuickPlot.dat >.\n Use a plotting utility\n");
break;
case 2:
printf("\nEnter the number of elements to sort\n");
scanf("%d",&iNum);
printf("\nUnsorted Array\n");
fnGenRandInput(iaArr,iNum);
fnDispArray(iaArr,iNum);
fnQuickSort(iaArr,0,iNum-1);
printf("\nSorted Array\n");
fnDispArray(iaArr,iNum);
break;
case 3:
exit(0);
}
}
return 0;
}
p = a[l];
i = l;
j = r+1;
do
{
Dept. of CSE, KLS VDIT Haliyal. Page 43
Analysis & Design of Algorithms Lab BCSL404
do { i++; } while (a[i] < p);
do { j--; } while (a[j] > p);
fnSwap(&a[i], &a[j]);
}
while (i<j);
fnSwap(&a[i], &a[j]);
fnSwap(&a[l], &a[j]);
return j;
}
if (l < r)
{
s = fnPartition(a, l, r);
fnQuickSort(a, l, s-1);
fnQuickSort(a, s+1, r);
}
}
srand(time(NULL));
for(i=0;i<n;i++)
{
X[i] = rand()%10000;
}
for(i=0;i<n;i++)
printf(" %5d \n",X[i]);
OUTPUT:
11. Design and implement C/C++ Program to sort a given set of n integer elements using Merge Sort method
and compute its time complexity. Run the program for varied values of n> 5000, and record the time taken to
sort. Plot a graph of the time taken versus n. The elements can be read from a file or can be generated using the
random number generator.
DESCRIPTION:
Merge sort is an O(n log n) comparison-based sorting algorithm. Most implementations produce a stable sort,
meaning that the implementation preserves the input order of equal elements in the sorted output. It is a divide
and conquer algorithm.
ALGORITHM:
Mergesort(A[O .. n - 1])
Sorts array A[O .. n - 1] by recursive mergesort
Input: An array A[O .. n - 1] of orderable elements
Output: Array A[O .. n - 1] sorted in nondecreasing order
Merge(B[O .. p- 1], C[O .. q -1], A[O.. p + q -1])
Merges two sorted arrays into one sorted array
Input: Arrays B[O .. p -1] and C[O .. q -1] both sorted
Output: Sorted array A[O .. p + q -1] of the elements of Band C
Program
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <omp.h>
int main()
{
FILE *fp;
int a[2000],i;
struct timeval tv;
double start, end, elapse;
fp=fopen("mergesort.txt","w");
for(i=10;i<=1000;i+=10)
{
getnumber(a,i);
gettimeofday(&tv,NULL);
start=tv.tv_sec+(tv.tv_usec/1000000.0);
merge(a,0,i-1);
gettimeofday(&tv,NULL);
end=tv.tv_sec+(tv.tv_usec/1000000.0);
elapse=end-start;
fprintf(fp,"%d\t%lf\n",i,elapse);
}
fclose(fp);
system("gnuplot");
return 0;
}
OUTPUT
#include<stdio.h>
#include<math.h>
int board[20],count;
int main()
{
int n,i,j;
void queen(int row,int n);
for(i=1;i<=n;++i)
printf("\t%d",i);
for(i=1;i<=n;++i)
{
printf("\n\n%d",i);
for(j=1;j<=n;++j) //for nxn board
{
if(board[i]==j)
printf("\tQ"); //queen at i,j position
else
printf("\t-"); //empty slot
}
}
}
0 0 1 0
1 0 0 0
0 0 0 1
0 1 0 0
Virtual Lab
Definition
A graph is a pictorial representation of a set of objects where some pairs of objects are connected by links. The
interconnected objects are represented by points termed as vertices, and the links that connect the vertices are
called edges.
Undirected Graph
Directed Graph
1. Study the basic concepts of Graphs and Queues and their representation.
2. Study the various Graph traversal algorithms and difference between them.
3. Understand the BFS Graph traversal using queues.
4. Demonstrate knowledge of time complexity and space complexity of performing a BFS on a given graph.
Definition
Breadth First Search (BFS) is a technique for traversing a finite graph. BFS visits the neighbour vertices before
visiting the child vertices, and a queue is used in the search process. This algorithm is often used to find the
shortest path from one vertex to another.
Here we start traversing from a selected node (source node) and traverse the graph layer/level wise thus
exploring the neighbour nodes (nodes which are directly connected to source node). We must move to the next
layer/level only after traversing the current layer/level completely.