VELAGAPUDI RAMA KRISHNA

SIDDHARTHA ENGINEERING COLLEGE
Kanuru, VIJAYAWADA-520007

This is to Certify that this is the Bonafide work done by Mr./Miss. bearing ………………………………………………... B.Tech, for the

Roll No. ………………….. Of …………………. in ……………………………………… Laboratory course

………………………………………. during the Academic Year 200 …… - 2000 …… No. of Experiments Recorded …………………………. Marks Awarded : - …………………………. :-

Signature of the Staff Member In-Charge. Date: ………………

Signature of Head of the Dept. Date: ………………

2

INDEX
Program No : SERIAL NUMBER NAME OF THE EXPERIMENT DATE 1

SIGNATURE

3

: General principle of Divide & Conquer: If a problem is given we divide it into no. Otherwise we still divide the problem as we solve all individual sub-problems at last combine all these solution which gives solution of main problem. Algorithm: Step 1: if (low > high) then return -1 Step 2: if (low < high) the mid=(low + high)/2 Step 3: X be a key.Date Aim: To perform Binary Search Description: In Binary Search we use Divide & Conquer principle. If a[mid] = X then return mid Step 4: If a[mid] > X then search for X from a[low] to a[mid-1] Step 5: If a[mid] < X then search for X from a[mid + 1] to a[high] Source Code: 4 . of sub-problems if we get solution of each part then we stop at that point.

Program No : 5 2 . beg=0.mid).i<n.h> void main() { int a[10]. } if(a[mid]==target) { printf("\nThe number is found at position %2d".target. for(i=0. else beg=mid+1.i++) scanf("%d".h> #include<conio.n. mid=(beg+end)/2.end. } getch(). mid=(beg+end)/2. scanf("%d". while(beg<=end && a[mid]!=target) { if(target<a[mid]) end=mid-1.&target). } OutPut: Enter the total numbers:5 Enter the array elements:1 2 3 4 5 Enter the number to be searched: 5 The number is found at position 4 Result:Hence. clrscr(). printf("Enter the total numbers:"). } else { printf("\nThe number is not found"). printf("\nEnter the number to be searched:").temp.&n).i. int beg. end=n.#include<stdio. scanf("%d".j.&a[i]). printf("Enter the array elements:" ). BinarySearch is implemented.mid.

0. float x[20].i. A solution that can be maximized or minimized is called Optimal Solution. Algorithm: Algorithm GreedyKnapsack(m.m. int p[20].0.w[20].h> #include<conio. 6 . printf("Enter number of objects:"). 4) An object i is associated with profit Pi .0. for i :=1 to n do { if (w[i] > U) the break.Date Aim: To find Optimal solution for a Knap Sack Problem using Greedy Method Description: : Greedy method or technique is used to solve Optimization problems. x[i] := 1.h> main() { clrscr(). 5) when an object i is placed in knapsack we get profit Pi Xi . U := U – w[i].n) //p[1:n] and w[1:n] contain the profits and weights respectively //of the n objects ordered such that p[i] / w[i] > = p[i+1] / w[i+1] //m is the knapsack size and x[1:n] is the solution vector { for i := 1 to n do x[i] := 0.u. //Initialize x. float optimal=0. scanf("%d". 3) An object i is associated with profit Wi . Knapsack problem: In Knapsack problem we are given:1) n objects 2) Knapsack with capacity m.&n). } Source Code: #include<stdio. } if (i<=n) then x[i] := U/w[i]. int n. Here objects can be broken into pieces (Xi Values) The Objective of Knapsack problem is to maximize the profit . U :=m.

scanf("%d".printf("Enter capacity of KnapSack:"). for(i=1. for(i=1.0.optimal). printf("The x values are\n").i<=m. printf("\nOptimal Solution is %f". for(i=1.i<=n. u=u-w[i]. for(i=1.000000 0.i++) scanf("%d".000000 Result: The optimal solution for a Knapsack problem is obtained.i<=n. for(i=1.i<=n.i++) optimal=optimal+p[i]*x[i].x[i]).&m).i<=n. getch(). } OutPut: Enter the number of objects: 3 Enter the capacity of Knapscak:20 Enter profits in decreasing order of pi/wi : 16 15 14 Enter weights in decreasing order of pi/wi : 10 10 10 The x-values are : 1.&w[i]). for(i=1.i++) { if(w[i]>u) break. } if(i<=n) x[i]=(float)u/w[i]. printf("Enter profits in decreasing order of Pi/Wi:"). Program No : 7 3 .i++) x[i]=0.000000 1.i++) printf("%f\t".000000 Optimal Solution is : 31.0.i<=n. printf("Enter Weights in decreasing order of Pi/Wi:"). else x[i]=1.&p[i]).i++) scanf("%d". u=m.

G = <V. say a.E>: – Is the same as an undirected graph except E is a set of ordered pairs. <c. E) consists of a set of vertices V = {vi | i = 1. From the adjacency matrix. G = <V. 2. as the edge (i. v) is set to one. we can readily determine whether there is an edge connecting any two vertices i and j. <a. …. Source Code: 8 . y}. w}. j] =1 iff the edge (i. w}. {u. <b.In undirected graph if (u. with the property that a [i. E) be a graph with n vertices n>=1.j) (<i.E> is the pair: – V = set of distinct vertices – E = set of edges.If an edge is present between two vertices then that position is set to 1 in the matrix i. v) where u and v are the vertices being connected by e. y. {v. u. {v.For an undirected graph the degree of any vertex i is its row sum: For a directed graph the row sum is the out-degree. v) be the vertex then both the positions of (u. z}} • A directed graph. e>. b>.j>) for a directed graph)is in E(G). m} that connect the vertices.v) and (v. {x. <e. The space needed to represent a graph using its adjacency matrix is n^2 bits. and the column sum is the in-degree.Date Aim: To find Adjacency matrix for a given graph Algorithm: : Graph Representation: – A graph G = (V.u) is set to 1 but in the case of the directed graph only the position of the (u.e adjacency marix else zero. w. • An edge e may be represented by the pair (u.The adjacency matrix of G is a twodimensional n*n array. v}. each member is a set of 2 vertices Example: V = {t.The adjacency for a directed graph may not be symmetric. 2. c>. j) is in E (G). About half the space can be saved in the case of an undirected graph by storing only the upper or lower of the triangle of the matrix. v. n} and a set of edges E = {ej | j = 1. b>. z} E = {{u.j]=0 if there is no such edge in G. x. ….The adjacency matrix for an undirected graph is symmetric. Example: E = {<a. Directed and undirected graphs : • An undirected graph. b>} Description: Let G= (V.The element a[i.

} getch(). printf("Directed Graph\n"). if(beg==0 && des==0) { break. while(j<me) { printf("Enter the Edges:").h> #include<conio. } } printf("Adjacency Matrix is\n"). } if(beg>num || des>num || beg<=0 || des<=0) { printf("Invalid Edges").a)Directed #include<stdio. int adj[20][20]={0}. scanf("%d%d".k.&num).&des). scanf("%d". int j=0. j++. for(j=1.k++) { printf("%d\t". printf("Enter number of nodes:"). int me. int num.&beg.j++) { for(k=1. int beg.k<=num.j<=num.adj[j][k]). } printf("\n"). } else { adj[beg][des]=1. } OutPut: 9 .h> main() { clrscr(). me=num*(num-1)/2.des.

h> main() { clrscr(). } if(beg>num || des>num || beg<=0 || des<=0) { printf("Invalid Edges"). int num. while(j<me) { printf("Enter the Edges:"). scanf("%d%d". int j=0.&des). printf("Enter number of nodes:").des. adj[des][beg]=1. 10 . int adj[20][20]={0}. scanf("%d". int me. int beg.h> #include<conio.k. if(beg==0 && des==0) { break. } else { adj[beg][des]=1.Directed Graph Enter number of nodes : 4 Enter the edges : 1 2 Enter the edges : 2 3 Enter the edges : 3 4 Enter the edges : 4 1 Enter the edges : 0 0 Adjacency matrix is: 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0 b)Undirected #include<stdio. me=num*(num-1).&beg.&num).

k++) { printf("%d\t".adj[j][k]).k<=num.j++. } } printf("Adjacency Matrix is\n").j<=num. the adjacency matrix for a given graph is obtained. } OutPut: Enter number of nodes : 4 Enter the edges : 1 2 Enter the edges : 2 3 Enter the edges : 3 4 Enter the edges : 4 1 Enter the edges : 0 0 Adjacency matrix is: 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 Result: Therefore. } getch(). } printf("\n").j++) { for(k=1. for(j=1. Program No : 11 4 .

V2 . } } Source Code: 12 . 2. V3. V6 .Date Aim: To implement Depth First Search Description: : Graph Traversal :It is the visiting of nodes in a graph.Depth First Search (DFS).this algorithm visits all vertices //reachable from v. Example: V1. for each vertex w adjacent from v do { if ( visited[w] = 0) then DFS(w). { visited[v]:=1. V8 . G and visited[] are global. V4. V5. It is of two types: 1.E) with //n vertices and a n array visited[] initially set //to zero .V7.Breadth First Search (BFS) Depth First Search(DFS) :This method starts at any node in a graph as visited & recursively traverse remaining adjacent nodes. Algorithm: // Given an undirected(directed) graph G(V.

&n).j<=n. int graph[20][20].n.h> #include<conio.i++) { for(j=1.j++) { scanf("%d". void main() { clrscr().k++) { if(visited[k]==0 && graph[l][k]==1) { dfs(k).l). } visited[i]=0. for(i=1. } } } OutPut: 13 . printf("Enter the adjacency matrix\n"). } void dfs(int l) { visited[l]=1.k<=n. printf("Node visited is %d\n".h> int i.#include<stdio. void dfs(int). scanf("%d". int visited[20]. printf("Enter number of nodes:").j.i++) { if(visited[i]==0) { dfs(i).k.i<=n. } for(i=1. for(k=1.i<=n. } } getch().&graph[i][j]).

Enter number of nodes : 3 Enter the Adjacency matrix : 011 100 100 Node Visited is : 1 Node Visited is : 2 Node Visited is : 3 Result: Therefore. the Depth First Search is implemented. Program No : 14 5 .

For any node i visited[i]=1 if i has //already been visited. visited[] is initialized to zero.Date Aim: To implement Breadth First Search Description: : Breadth First Search(BFS) :BFS algorithm begins at root node & visits all the neighbouring nodes then for each of these nearest nodes it visits their un visited neighbour nodes . //q is a queue of unexplored vertices. V5. repeat { for all vertices w adjacent from u do { if(visited[w] = 0) then 15 . V3. { u:=v. We implement BFS by using queue data structure where as DFS by using stack Algorithm: // A breadth first search of G is carried out beginning //at vertex v. V2. The Graph G and array visited[] //are global. visited[v]:=1. This process terminates when all nodes visited. V6. V4. Example: V1.

adj[max][max].h> #define max 10 int n. void readmatrix(). for(i=1. }until(false). scanf("%d". } void readmatrix() { int i. getch().j<=n.i<=n. } Source Code: #include<stdio.i<=n. bfs(source).{ Add w to q.&adj[i][j]).i++) for(j=1.&n). printf("Enter number of nodes:"). clrscr(). } } if q is empty then return //no unexplored vertex Delete u from q.h> #include<conio. void bfs().visited[max]. main() { int source.j. printf("Enter adjacency matrix\n").j++) scanf("%d". scanf("%d". for(i=1. readmatrix(). //w is unexplored visited[w]:=1 . printf("Enter the source node:"). } void bfs(int source) 16 . //Get first unexplored vertex.&source).i++) visited[i]=0.

rear.i<=n.i++) if(adj[root][i] && ! visited[i]) { visited[i]=1. queue[rear++]=source. Program No : 17 6 . for(i=1. front=rear=0. printf("%d ". printf("%d ".i). visited[source]=1.root. } front++. queue[rear++]=i.source). while(front!=rear) { root=queue[front]. } } OutPut: Enter the Source node:1 Enter number of nodes : 6 Enter Adjacency matrix: 001011 001010 110111 001001 111000 101100 The order is 135624 Result: Therefore . printf("The Order is\n"). int i.front.{ int queue[max]. the Breadth First Search is implemented.

A Minimum cost spanning tree is a spanning tree with minimum weight.E1) is said to be a spanning tree of G if T is a tree. Prim’s method: The Greedy method to obtain MCST builds the tree edge by edge. Choose the edges with minimum cost Cycles should be avoided.E) be an undirected graph. Graph: TOTAL COST:118 Stages are: 18 . A sub-graph T=(V.Date Aim: To find minimum cost of spanning tree using Prims Algorithm Description: : Let G(V.

t[1. mincost :=mincost+cost[j. for k:=1 to n do //Update near[].n.near[k]]>cost[k. else near[i]:=k.1:2] .The final cost is returned.2] is an edge in the minimum cost spanning //tree .k]) then near[i] :=l. for i:=2 to n-1 do { // Find n-2 additional edges for t Let j be an index such that near[j]!=0and Cost[j.1]. { Let (k.j])) then near[k]:=j.1]:=k. for i:=1 to n do //initialize near if(cost[i.j) exists //a minimum spanning tree is computed and stored as a set of //edges in the array t[1:n-1.l]<cost[i.1:n] is the cost //adjacency matrix of an n vertex graph such that cost[i.2]:=l. } return mincost.near[j]].TOTAL COST :63 Algorithm: Prim(E. (t[i.near[j]] is minimum t[i.t[1. near[k]:=near [l]:=0.l) be an edge of minimum cost in Ei mincost :=cost[k.cost [1:n.cost. } Source Code: 19 .t) //E is the set of edges in G. if((near[k]!=0) and (cost[k.1]:=j.j] is //either a positive real number or infinity if no edge of (i.t[i.t[i.2]:=near[j]. near[j]:=0.l].

for(i=1. for(i=1.mincost=0.i<=v. } } void prim() { int current=1.&e). int i. creategraph().d[20]. visited[current]=1.min. d[current]=0.#include<stdio.i++) { printf("Enter edges a.&a.b.i++) 20 . scanf("%d%d%d".p[20]. void creategraph().h> #include<conio.j++) g[i][j]=0. d[i]=32767. visited[i]=0.&w).h> int g[20][20]. printf("Enter number of vertices:").i<=v. } void creategraph() { int a.j. printf("Enter number of edges:").i<=v.&v). getch().visited[20]. for(i=1.i<=e.j<=v. while(totalvisited!=v) { for(i=1. scanf("%d". g[a][b]=g[b][a]=w. prim().&b.e.i++) for(j=1.totalvisited=1. void prim().w. void main() { clrscr().i++) p[i]=0. scanf("%d".b and w:"). int v.

} for(i=1. p[i]=current. for(i=1.{ if(g[current][i]!=0) if(visited[i]==0) if(d[i]>g[current][i]) { d[i]=g[current][i]. b &w: 2 3 20 Enter edges a .i<=v.i<=v. } } visited[current]=1. b &w: 3 1 30 Minimum cost of the spanning tree is 30 Result: A minimum cost of spanning tree is obtained using Prims Algorithm Program No : 21 7 .i++) mincost=mincost+d[i]. b &w: 1 2 10 Enter edges a . printf("Minimum cost of the spanning tree is %d". } OutPut: Enter number of vertices : 3 Enter number of edges : 3 Enter edges a .mincost). totalvisited++. } } min=32767.i++) { if(visited[i]==0) if(d[i]<min) { min=d[i]. current=i.

We have to choose one vertex as source . This problem can be implemented by an algorithm called Dijkastra’s algorithm.Date Aim: To write a program to implement Dijkstras algorithm Description: : Let G (V. Algorithm: 22 .E) be a graph . V-Vertices & E-Edges . the problem aim is to find out minimum distance between source node and all remaining nodes. This problem is the case of ordered paradigm in Greedy method.

1<=j<=n denotes distance between V and j void main() { for i:=1 to n do { s[i]=false. //define distance } s[v]=true. s[u]=true. void path(int).0. printf("\nEnter number of nodes: ").&n).h> #define infinity 32767 int cost[20][20]. for(each w adjascent to u with s[w]=false) if(dist[w]>dist[u]+cost[u.i].a[20][20].dist[20]. for(i=1.&a[i][j]). void getdata().//G be a graph //Cost matrix [1:n. printf("Enter Adjacency Matrix: "). void setdata() { int i.j.j<=n.j++) { if(i==j) 23 .w]. // initialize s with n dist[i]=cost[v. for(i=1.i++) for(j=1.k. //update the distance } } Source Code: #include<stdio.i++) for(j=1. scanf("%d".s[20].n. //Distance between v and v is 0 for num:=2 to n-1 do { paths from v //choose u from among those vertices not in S such that dist[u]=min.i<=n. void setdata().w]) then dist[w]=dist[u]+cost[u.i<=n.j<=n. //put v in s dist[v]=0.1:n] for the graph G //S={set of vertices that path already generated} //Let V be source vertex //dist[j].j++) scanf("%d".h> #include<conio.

j).j++) if(s[j]==0 && dist[j]<min) u=j. dist[v]=0. scanf("%d". dist[i]=cost[v][i].j<=n. for(i=1.i++) if(dist[i]==32767) printf("not reachable").dist[i]).j<=n. for(j=1. } void path(int v) { int i. } else cost[i][j]=infinity. } } void getdata() { int i. } s[v]=1. for(j=1.i<=n.i. else printf(" %d". else if (a[i][j]!=0) { printf("\nEnter cost from %d to %d: ". for(i=2. for(i=1.i<=n.j++) if(s[j]==0 && a[u][j]==1) if(dist[j]>dist[u]+cost[u][j]) dist[j]=dist[u]+cost[u][j].cost[i][i]=0. } } void main() 24 . s[u]=1.j.&cost[i][j]).min.i++) { s[i]=0.i<=n.u.i++) { min=32767.

printf("\nShortest paths " ).{ int v. setdata(). scanf("%d". getdata(). Dijkstras algorithm is implemented Program No : 25 8 . getch(). path(v). clrscr().&v). printf("\nEnter the source vertex: "). } Output: Enter number of nodes: 6 Enter Adjacency Matrix: 0 1 1 1 0 0 001100 000010 100010 011000 000010 Enter cost from 1 to 2: 50 Enter cost from 1 to 3: 45 Enter cost from 1 to 4: 10 Enter cost from 2 to 3: 10 Enter cost from 2 to 4: 15 Enter cost from 3 to 5: 30 Enter cost from 4 to 1: 20 Enter cost from 4 to 5: 15 Enter cost from 5 to 2: 20 Enter cost from 5 to 3: 35 Enter cost from 6 to 5: 3 Enter the source vertex: 1 Shortest paths 0 45 45 10 25not reachable Result:Hence.

j).i)=0 cost(i.Ak-1(i.j is the shortest path from i to j cost(i.j> & cost(i.Date Aim: To write a program to perform All pairs shortest path problem Description: : Let G(V.j)=min{Ak-1(i.j)=infinity //no such edge exists A0 (i.E) be a directed graph .j)} Therefore Solution is : Algorithm: 26 .with V-Vertices & E-Edges The objective of all pairs shortest path problem is to find out matrix A(i.k)+Ak-1(k.j) = cost of edge <i.j) =cost(i.j) where i.j) If K is an intermediate vertex then Ak (i.

0 for 1<=i<=n.h> int cost[20][20].i++) { for(j=1.j<=n.j++) { printf("%d".j<=n. scanf("%d".j]:=min(A[i.j++) { scanf("%d".i<=n. { for i:= 1 to n do for j:= 1 to n do A[i.A[I. printf("\nEnter the number of nodes:"). void setdata() { int i.&cost[i][j]).n) //cost[1:n.n.j]).j] is the cost of a shortest path from vertex //i to vertex j. void path(). void setdata(). for(i=1.i++) { printf("\nEnter %d row\n". // Copy cost into A for k:= 1 to n do for i:= 1 to n do for j:= 1 to n do A[i. for(j=1.k.&n). void getdata(). } } } void getdata() { int i.j. printf("\nEnter cost matrix(32767 for infinity):").A.A[i.a[i][j]). 27 .i<=n. } Source Code: #include<stdio. for(i=1. printf("\n").k] + A[k.1:n] is the cost adjacency matrix of a graph with //n vertices.j.h> #include<conio.i] = 0.j]:=cost[i.AllPaths(cost.j].i).cost[i.a[20][20].j].

getdata().} } } void path() { int i. } Output: Enter the number of nodes:3 Enter cost matrix(32767 for infinity): Enter 1 row 0 4 11 Enter 2 row 602 Enter 3 row 370 Matrix with shortest path is:0 4 6 5 0 2 3 7 0 Result:Hence. } } void main() { clrscr(). for(i=1.i<=n.k<=n. getch().j<=n. a[i][j]=(a[i][j]>l)?l:a[i][j].j++) a[i][j]=cost[i][j].l. setdata(). for(k=1.i++) for(j=1.i++) for(j=1.j++) { l=a[i][k]+a[k][j].k.i<=n. printf("\nMatrix with shortest path is:").j. path().j<=n. All pairs shortest path problem is performed Program No : 28 9 .k++) for(i=1.

A=|1 |3 B=|2 |1 C=|4 |1 |1 2| 4| 2 1 1| 1| 1| 3| 7| 11| 27| 1| 1| (AB)C AB= |4 4 |10 10 (AB)C=|23 | 57 A(BC) BC==|11 5| | 6 3| A(BC) =|23 | 57 11| 27| PARANTHASIZATION (AB)C A(BC) COST CALCULATION (2*2*3)+(2*3*2) (2*3*2)+(2*2*2) TOTAL COST 24 20 Tree diagram for (AB)C 29 Tree diagram for A(BC) .Date Aim: To perform Matrix Chain Multiplication Description: : Let A.B &C are 3 matrices . since matrix multiplication is associative (AB)C=A(BC).The objective in matrix chain multiplication is to find out the paranthasization with less number of operations.

k] + m[k+1.Algorithm: // n is the number of matrices // l is chain length // Matrix Ai has the dimension p[i-1] x p[i] Matrix-Chain-Order(int p[]) { n = p.j]) { m[i. k<=j-1.h> #define MAX 15 #define INF 4294967295 30 . l++) { for (i=1.j] + p[i-1]*p[k]*p[j]. i++) { j = i+l-1. l<=n. i++) m[i. } } } } } Source Code: #include<stdio. for (l=2. i <= n.i] = 0.j] = MAXINT. i<=n-l+1. m[i. if (q < m[i.j] = k.h> #include<conio. k++) { q = m[i. s[i. for (k=i.j] = q.length .1. for (i = 1.

for(k=i. s[i][j]=k.k<j.l++) for(i=1. unsigned long m[MAX][MAX]={0}. m[i][j]=INF. num=1. } } } printf("Number of Multiplications are %d ". printf(" ) "). int s[MAX][MAX]={0}. } void setdata() 31 .int). void print(int s[MAX][MAX]. void printorder(). void print(int [][MAX]. int l.i.k.j).m[1][n]).i++) { j=i+l-1.j.k++) { q=m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j].n). printf(" x "). void setdata(). else { printf("(").i.s[i][j]).int num. printf("Order of Multiplication is: "). print(s.int.int i.num++).1.int j) { if(i==j) printf("A%d". } } void matrixchainorder() { unsigned int q. print(s.l<=n. if(q < m[i][j]) { m[i][j]=q. print(s.p[MAX+1]. void matrixchainorder(). for(l=2.i<=n-l+1.n.s[i][j]+1.

scanf("%d%d".i<=n.i). } Output: Enter number of matrices: 3 Enter 1 matrix size:2 2 Enter 2 matrix size:2 3 Enter 3 matrix size:3 2 Number of Multiplications are 20 Order of Multiplication is: (A1 x (A2 x A3 ) ) Result: Matrix Chain Multiplication is implemented.j. scanf("%d".&p[i]).&p[i-1]. printf("\nEnter number of matrices: ").&n). } void main() { setdata().i++) { printf("Enter %d matrix size:". Program No : 32 10 . for(i=1. } } void printorder() { int i.{ int i. printorder(). matrixchainorder().

X[2].X[k-1]) // criterian function do { if Bk(X[1]. int safe(int...….x2.x2.xn).. int y) { 33 . void putboard().….h> #include<conio.h> int row[8]. Algorithm: //Recursive algorithm for Backtracking //Let X[] be an array of objects . int safe(int x.X[k]) != 0 // bounding function then if(X[1].Explicit 2..X[2]..e) finding a vector that maximises (or) minimises a criterion function p(x1. Implicit constraints:we have to avoid multiple instances of same sub-set. n be the number of objects //Output is to find out solution vector { for each X[k] E p(X[1]. Explicit constraints:Each Xi should be taken from one of the Wi..X[2].…..X[k]) is in path then write X[1:k].s=0.Date Aim: : To write a program to solve Eight Queens problem using Back Tracking Technique Description: Many problems which deal with searching for a set of solutions or which ask for an optimal solution satisfying some constraints can be solved using the backtracking formulation.e (x1.xn) where xi are choosen from given a finite set of Si (i.int). Constraints in backtracking are of 2 types: 1. else Back Track } } Source Code: #include <stdio. void queen(int)..Implicit In backtracking desired solutions n tuple i.

} getch(). printf("|\n---------------------------------\n"). y++) { for (x=0.y-1) ) if (y<8) queen(y+1). queen(1).x++) { row[y-1]=x.i++) if( row[y-i]==x || row[y-i]==x-i || row[y-i]==x+i) return 0. } void queen(int y) { int x.x<8. if( safe(x.int i. else printf("| "). for(i=1.y. return 1.x<8. for(x=0. printf("\nSolution # %d". } void putboard() { int x. for(y=0.i<=y. } Output: 34 .x++) if(x==row[y]) printf("| Q ").++s). printf(":\n---------------------------------\n"). } } void main() { clrscr().y<8. else putboard().

35 .Result: Eight Queens problem using Back Tracking Technique is Solved.

Sign up to vote on this title
UsefulNot useful