• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

Design and Analysis of Algorithms

N queen problem code in C Language

May 4, 2011 by ProjectsGeek Leave a Comment

N queen problem code in C Language

Write a program in C Language to Implement N queen problem using C Language and print the number of Solution to the problem as output.

Recursive backtracking algorithm, iterative backtracking method queens problem. Sum of subsets and Graph coloring, Hamiltonian cycle and Knapsack Problem etc.

N queen problem code

 

 #include  
 #include  
 #include  
 #include  
 int check(int *s,int k)  
 {  
   for(int i=0;i<k;++i) <br="">   {  
     if( (*(s+i)==*(s+k)) || ( abs(i-k)==abs(*(s+i)-*(s+k)) ) )  
       return 1;  
   }  
   return 0;  
 }  
 void display(int * s,int n)  
 {  
   printf("Solution\n");  
   for(int i=0;i<n;++i) <br="">   {  
     printf("\t%d",i);  
   }  
   printf("\n");  
   for(i=0;i<n;++i) <br="">   {  
     printf("%d\t",i);  
     for(int j=0;j<n;++j) <br="">     {  
       if(*(s+i)==j)  
         printf("x\t");  
       else  
         printf("\t");  
     }  
     printf("\n");  
   }  
 }  
 void queen(int *s,int n)  
 {  
   int k;  int count=0;  
   while(*(s+0) <= n-1)  
   {  
     k=1;  
     while(k>0)  
     {  
       while(*(s+k)         ++(*(s+k));  
       if(*(s+k)<n) <br="">       {  
         if(k==n-1)  
         {  
           count++;  
           display(s,n);  
           //if(count%3==0)  
             getch();  
           ++(*(s+k));  
         }  
         else  
           k++;  
       }  
       else  
       {  
         *(s+k)=0;  
         k--;  
         ++*(s+k);  
       }  
     }  
   }  
   printf("\n Total no of solutions are:%d",count);  
 }  
 void main()  
 {  
   int n;  
   clrscr();  
   printf("Enter the no of queens\n");  
   scanf("%d",&n);  
   int *s;  
   s=(int *)calloc(1,2*n);  
   queen(s,n);  
   getch();  
 }

 

Other Projects to Try:

  1. Hash table code in C Language
  2. Bankers Algorithm using C Language OS problem
  3. 8 Queen Problem
  4. Prims algorithm Code in C Language
  5. Krushkals algorithm Code in C Language

Filed Under: Design and Analysis of Algorithms

Krushkals algorithm Code in C Language

May 4, 2011 by ProjectsGeek Leave a Comment

Krushkals algorithm Code in C Language Code
 #include  
 #include  
 #define max 50  
 void create_heap(int arr[][3],int n)  
 {  
   int k,heap,j;  
   int temp[3];  
   for(int i=(n/2)-1;i>=0;--i)  
   {  
     k=i;  
     temp[0]=arr[k][0];  
     temp[1]=arr[k][1];  
     temp[2]=arr[k][2];  
     heap=0;  
     while( heap!=1 && ( (2*k +1)      {  
       j=2*k+1;  
       if(j<n-1) <br="">       if(arr[j][2]>arr[j+1][2])  
         j++;  
       if(arr[k][2]<arr[j][2]) <br="">         heap=1;  
       else  
       {  
         arr[k][0]=arr[j][0];  
         arr[k][1]=arr[j][1];  
         arr[k][2]=arr[j][2];  
         k=j;  
         heap=0;  
       }  
     }  
     arr[k][0]=temp[0];  
     arr[k][1]=temp[1];  
     arr[k][2]=temp[2];  
   }  
 }  
 void input(int arr[][3])  
 {  
   char ch;  
   int s,d,w;  
   int i=0;  
   while(1)  
   {  
     printf("Do you want to enter any edge?:");  
     flushall();  
     scanf("%c",&ch);  
     if(ch=='n'||ch=='N')  
       break;  
     printf("Source:");  
     flushall();  
     scanf("%d",&s);  
     printf("Destination:");  
     flushall();  
     scanf("%d",&d);  
     printf("Weight:");  
     flushall();  
     scanf("%d",&w);  
     arr[i][0]=s;  
     arr[i][1]=d;  
     arr[i++][2]=w;  
   }  
 }  
 void heap_delete(int arr[][3],int *n)  
 {  
   (*n)--;  
   arr[0][0]=arr[*n][0];  
   arr[0][1]=arr[*n][1];  
   arr[0][2]=arr[*n][2];  
   create_heap(arr,*n);  
 }  
 int find(int i,int parent[20])  
 {  
   while(parent[i]!=-1)  
     i=parent[i];  
   return i;  
 }  
 void main()  
 {  
   clrscr();  
   int n;  
   printf("Enter the no of veritces\n");  
   int edge[max][3],parent[max];  
   scanf("%d",&n);  
   input(edge);  
   int t[3];  
   //create_heap(edge,n);  
   create_heap(edge,n);  
   int u,v;  
   int n1=n;  
   int result[max][3];  
   for(int i=0;i<n;++i) <br="">     parent[i]=-1;  
   for(i=0;i<n1-1;++i) <br="">   {  
     t[0]=edge[0][0];  
     t[1]=edge[0][1];  
     t[2]=edge[0][2];  
     u=find(t[0],parent);  
     v=find(t[1],parent);  
     if(u!=v)  
     {  
       parent[u]=v;  
       result[i][0]=t[0];  
       result[i][1]=t[1];  
       result[i][2]=t[2];  
     }  
     heap_delete(edge,&n);  
   }  
   printf("S\tD\tW\n");  
   for(i=0;i<n1-1;++i) <br="">     printf("%d\t%d\t%d\n",result[i][0],result[i][1],result[i][2]);  
   getch();  
 }

 

 

Other Projects to Try:

  1. Hash table code in C Language
  2. Prims algorithm Code in C Language
  3. Hoffmans algorithm in C Language
  4. Dijkstra Algorithm in C Language
  5. BFS AND DFS Algorithm using C Language

Filed Under: Design and Analysis of Algorithms

  • « Go to Previous Page
  • Page 1
  • Page 2

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy