Search This Blog

Showing posts with label algorithm. Show all posts
Showing posts with label algorithm. Show all posts

Bresenham's Line Drawing Algorithm Implementation in C - Generalized Algorithm

Bresenham's line drawing algorithm is an efficient algorithm to draw any line. Unlike DDA algorithm, it uses only integer incremental calculations during the process. This saves much of the CPU time. I am posting a generalized Bresenham's line drawing algorithm (an algorithm that can draw any line in any quadrant with any slope). The main function in this program draws lines of almost all slopes by drawing radial lines for a circle. The entered endpoints of the line are marked in red color. The rest of the line is drawn in the default stroke color in BGI graphics. You can change it by calling setcolor() function.

Output:


Program is given below:

Algorithm and C Program to Find Candidate Key from Functional Dependencies

First of all, we will see the algorithm to find candidate keys from functional dependencies. The input is functional dependencies.

Algorithm:


1. Find the attributes that are neither on the left and right side
2. Find attributes that are only on the right side
3. Find attributes that are only on the left side
4. Combine the attributes on step 1 and 3
5. Test if the closures of attributes on step 4 constitutes all the attributes. If yes it is a candidate key.
6. If not, find the relation exteriors, that is the attributes not included in step 4 and step 2.
7. Now test the closures of attributes on step 4 + one attribute in step 6 one at a time. All those combinations are candidate keys if their closures constitute all the attributes.

C Program:



#include<stdio.h>
#include<string.h>
#include<conio.h>

struct fd
 {
   int left[8],right[8];
   int lcount,rcount;
   }f[10];

int attrcount,closcount=0,fdcount,closure[10];
char attr[10][25];
int nolnor[8],ronly[8],lonly[8],merg1n3[8],exteriors[8];

void getclosure();
void get_nolnor();
void get_ronly();
void get_lonly();
void get_merg1n3();
int iscomplete();

void getclosure()
{
int i,j,k,l=0,issubset,found;
do
 {
   for(i=0;i<=fdcount;i++)//Checking each functional dependancy
    {
      issubset=1;
      for(j=0;j<f[i].lcount;j++)//select each attr in leftside
   {
         found=0;
         for(k=0;k<closcount;k++) //checking with each element of 

closure
          

{
            if(closure[k]==f[i].left[j])
             

{
               found=1;
               break;
               }
            }
         if(found==0)
    {
          

issubset=0;
            break;
            }
   }
  if(issubset==1)
       {
         for(k=0;k<f[i].rcount;k++)
          

{
            found=0;
            for(j=0;j<closcount;j++)
             

{
               if(closure[j]==f[i].right[k])
                found=1;
               }
    if(found==0)
             

{
     closure[closcount]=f[i].right

[k];
             

closcount++;
               }
            }
         }
  }
   l++;
 }while(l<attrcount);
}


void get_nolnor()
 {
   int i,found,j,k,l=0;
   for(i=0;i<attrcount;i++)//take an attribute
    {
      found=0;
      for(j=0;j<=fdcount;j++) //take an fd
          {
          for(k=0;k<f[j].lcount;k++) //check in left
           

{
            if(i==f[j].left[k])
               {
             

found=1;
               break;
               }
            }
          if(found==1) //stop if found.
           

break;
          for(k=0;k<f[j].rcount;k++) //check in right
           

{
            if(i==f[j].right[k])
               {
             

found=1;
               break;
               }
            }
          if(found==1) //stop if found.
           

break;
          }
      if(found==0)
       {
         nolnor[l]=i;
         l++;
         }
      }
 nolnor[l]=222;
 }


void get_ronly()
 {
   int rpresent,lpresent,i,j,k,l=0;
   for(i=0;i<attrcount;i++)//take an atrribute
    {
      rpresent=0;
      for(j=0;j<=fdcount;j++)//take an fd
       {
         for(k=0;k<f[j].rcount;k++)
          

{
            if(i==f[j].right[k])
             

{
               rpresent=1;
               break;
               }
            }
         if(rpresent==1)
          

break;
         }
      lpresent=0;
      if(rpresent==1)
       {
         for(j=0;j<=fdcount;j++)//take an fd
        {
          

for(k=0;k<f[j].lcount;k++)
           

{
             

if(i==f[j].left[k])
             

 {
                lpresent=1;
                break;
                }
             

}
          

if(lpresent==1)
             

break;
          

}
         }
      if(lpresent==0&&rpresent==1)
       ronly[l++]=i;
  }
   ronly[l]=222;
   }

void get_lonly()
 {
   int rpresent,lpresent,i,j,k,l=0;
   for(i=0;i<attrcount;i++)//take an atrribute
    {
      lpresent=0;
      for(j=0;j<=fdcount;j++)//take an fd
       {
         for(k=0;k<f[j].lcount;k++) //looking in leftside
          

{
            if(i==f[j].left[k])
             

{
               lpresent=1;
               break;
               }
            }
         if(lpresent==1)
          

break;
         }
      rpresent=0;
      if(lpresent==1)
       {
         for(j=0;j<=fdcount;j++)//take an fd
        {
          

for(k=0;k<f[j].rcount;k++)//checking in right side
           

{
            

  if(i==f[j].right[k])
      {
                rpresent=1;
                break;
                }
             

}
          if

(rpresent==1)
             

break;
          

}
         }
      if(lpresent==1&&rpresent==0)
         lonly[l++]=i;
      }
   lonly[l]=222;
 }


void get_merg1n3()
 {
   /* combine lonly and nolnor */
   int i,j;
   for(i=0,j=0;lonly[j]!=222;i++,j++)
    merg1n3[i]=lonly[j];
   for(j=0;nolnor[j]!=222;i++,j++)
    merg1n3[i]=nolnor[j];

   merg1n3[i]=222;
   }

int compare(char temp[25])
 {
   int i;
   for(i=0;i<attrcount;i++)
    {
      if(strcmp(temp,attr[i])==0)
      return i;
      }
   return 0;
   }


int iscomplete()
 {
   if(closcount!=attrcount)
    return 0;
   else
    return 1;
   }

void main()
 {
   int i,j,k,attcode,found;
   char schema[100],temp[45],temp1[50];
   for(i=0;i<10;i++)
    {
      f[i].lcount=0;
      f[i].rcount=0;
      }
   printf("\nEnter the schema\n");
   scanf("%s",schema);
   attrcount=0;
   for(i=0;schema[i]!='(';i++);

   do
    {
    j=0;
      i++;
    while(schema[i]!

=','&&schema[i]!=')')
     {
       temp

[j]=schema[i];
       j++;
       i++;
       }
    temp[j]='\0';
    strcpy(attr

[attrcount],temp);
  attrcount++;
      }while(schema[i]==',');

   fdcount=-1;
   printf("\nEnter the functional dependancies\nEnter 0 to stop\n");
   for(i=0;i<10;i++)
    {
      scanf("%s",temp1);
      if(strcmp(temp1,"0")==0)
       break;
      fdcount++;
      j=0;
      if(temp1[0]=='{'||temp1[0]=='(')
       j++;
      do
       {
         if(temp1[j]==',')
             

j++;
         k=0;
         while(temp1[j]!=','&&temp1[j]!=')'&&temp1[j]!

='}'&&temp1[j]!='-')
           

{
            temp[k]=temp1[j];
            k++;
            j++;
            }
            temp[k]='\0';
            attcode=compare(temp);
            f[fdcount].left[f[fdcount].lcount]=attcode;
            f[fdcount].lcount++;
         }while(temp1[j]==',');
  if(temp1[j]==')'||temp1[j]=='}')
       j+=3;
      else if(temp1[j]=='-')
       j+=2;
      if(temp1[j]=='{'||temp1[j]=='(')
       j++;
      do
       {
         if(temp1[j]==',')
             

j++;
         k=0;
         while(temp1[j]!=','&&temp1[j]!=')'&&temp1[j]!

='}'&&temp1[j]!='\0')
           

{
            temp[k]=temp1[j];
            k++;
            j++;
            }
            temp[k]='\0';
            attcode=compare(temp);
            f[fdcount].right[f[fdcount].rcount]=attcode;
            f[fdcount].rcount++;
         }while(temp1[j]==',');

  }


/**************Step1**************
1. Find the attributes that are neither on the left and right side*/
 get_nolnor();

/**************Step2*************
2. Find attributes that are only on the right side*/
   get_ronly();

/**************Step3*************
3. Find attributes that are only on the left side*/
   get_lonly();

/**************Step4*************
4. Combine the attributes on step 1 and 3*/
 get_merg1n3();  //combine 

nolnor and lonly

/**************Step5*************
5. Test if the closures of attributes on step 4 are all the attributes*/
   closcount=0;
   for(i=0;merg1n3[i]!=222;i++)
    {
      closure[closcount++]=merg1n3[i];
      }
   getclosure();
 i=iscomplete();//check whether 

closure of merg1n3 is complete
   if(i==1)
    {
      printf("\nThe candidate key is:\n{");
      for(i=0;merg1n3[i]!=222;i++)
       {
         printf("%s,",attr[merg1n3[i]]);
         }
      printf("\b ");
      printf("}");
      }
   else
    {
/************** Step 6 **************
6. Find the relation exteriors, that is the attrbutes included not in 4 and not in 2
     included not in (ronly & merg1n3)
     */
      k=0;
      for(i=0;i<attrcount;i++)
       {
         found=0;
         for(j=0;ronly[j]!=222;j++)
          

{
            if(i==ronly[j])
             

{
               found=1;
               break;
               }
            }
         if(found==0)
          

{
            for(j=0;merg1n3[j]!=222;j++)
               {
             

if(i==merg1n3[j])
             

 {
                found=1;
                break;
                }
               }
            }
         if(found==0)
          

{
            exteriors[k++]=i;
            }
         }

      exteriors[k]=222;
      printf("Candidate Keys:");
/************** Step 6 **************
7. Now test the closures of attributes on step 4 + one attribute in step 6 one at a time.
*/
    for(k=0;exteriors

[k]!=222;k++)
     {
       closcount=0;
       for

(i=0;merg1n3[i]!=222;i++)
        {
        closure

[closcount++]=merg1n3[i];
          

}
       closure

[closcount++]=exteriors[k];
       getclosure();
   i=iscomplete();//check whether 

closure of this instance of this step is complete
     if(i==1)
      {
          

printf("\n{");
          

for(i=0;merg1n3[i]!=222;i++)
         {
           

printf("%s,",attr[merg1n3[i]]);
           

}
          

printf("%s}",attr[exteriors[k]]);
          

}
       }
      }
   getch();
   } 

Algorithm and C Program to Find Closure From Functional Dependencies

In this post we will see how to find closure of an attribute or a set of attributes. Before learning how to get closure, we should first know what is a closure. Closure of a given set C is the set of attributes that are functionally determined by the set C under the set of functional dependencies F. There can be closure for any set. Every attribute in the set whose closure is to be found out, will be a member of its closure set C+ also. Consider an example:

C Program to Simulate Round Robin CPU Sceduling Algorithm

The round robin CPU scheduling algorithm is simulated using C program. The time slice or time quantum is read from the user.

Program:

#include<stdio.h>
struct process{



int burst,wait,comp,f;
}p[20]={0,0};
int main(){
int
n,i,j,totalwait=0,totalturn=0,quantum,flag=1,
time=0;
printf("\nEnter The No Of Process
:");
scanf("%d",&n);
printf("\nEnter The Quantum time (in ms)
:");
scanf("%d",&quantum);
for(i=0;i<n;i++){
printf("Enter The Burst Time (in ms) For
Process #%2d :",i+1);
scanf("%d",&p[i].burst);
p[i].f=1;
}
printf("\nOrder Of Execution \n");
printf("\nProcess \tStarting\tEnding\tRemaining");
printf("\n\tTime \tTime\tTime");
while(flag==1){
flag=0;
for(i=0;i<n;i++){
if(p[i].f==1){
flag=1;
j=quantum;
if((p[i].burst-p[i].comp)>quantum){
p[i].comp+=quantum;
}
else{
p[i].wait=time-p[i].comp;
j=p[i].burst-p[i].comp;
p[i].comp=p[i].burst;
p[i].f=0;
}
printf("\nprocess # %-3d %-10d %-10d %-10d",i+1,time,time+j,p[i].burst-
p[i].comp);
time+=j;
}}}
printf("\n\n------------------");
printf("\nProcess \t Waiting Time  TurnAround Time ");
for(i=0;i<n;i++){
printf("\nProcess # %-12d%-15d%-15d",i+1,p[i].wait,p[i].wait+p[i].burst);
totalwait=totalwait+p[i].wait;
totalturn=totalturn+p[i].wait+p[i].burst;
}
printf("\n\nAverage\n------------------");
printf("\nWaiting Time : %f ms",totalwait/(float)n);
printf("\nTurnAround Time : %f ms\n\n",totalturn/(float)n);
return 0;
}


Round robin CPU scheduling algorithm simulation using C program. simulate CPU scheduling output

C Program to Simulate Priority Scheduling CPU scheduling Algorithm

The following C program implements Simulation of  priority scheduling (CPU Scheduling algorithm). Each process or job is given a priority. The priority is represented using a positive integer. The priority increases as the value of the integer decreases.

Program:

#include<stdio.h>
struct process{



int burst,wait,no,priority;
}p[20]={0,0};
int main(){
int n,i,j,totalwait=0,totalturn=0;
printf("\nEnter The No Of Process :");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter The Burst Time (in ms) For Process #%2d :",i+1);
scanf("%d",&p[i].burst);
printf("Enter The Priority For Process #%2d :",i+1);
scanf("%d",&p[i].priority);
p[i].no=i+1;
}
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(p[j].priority>p[j+1].priority)
{
p[j].burst^=p[j+1].burst^=p[j].burst^=p[j+1].burst;
p[j].no^=p[j+1].no^=p[j].no^=p[j+1].no;
//Simple way to swap 2 var’s
p[j].priority^=p[j+1].priority^=p[j].priority^=p[j+1].priority;
//printf("j");
}
printf("\nProcess \t Starting\tEnding\tWaiting\tTurnAround ");
printf("\n\t Time\tTime\tTime\tTime ");
for(i=0;i<n;i++)
{
printf("\nProcess # %-11d%-10d%-10d%-10d%10d",p[i].no,p[i].wait,p[i].wait+p[i].burst,p[i].wait,p[i].wait+p[i].burst);
p[i+1].wait=p[i].wait+p[i].burst;
totalwait=totalwait+p[i].wait;
totalturn=totalturn+p[i].wait+p[i].burst;
}
printf("\n\nAverage\n---------");
printf("\nWaiting Time : %f ms",totalwait/(float)n);
printf("\nTurnAround Time : %f ms\n\n",totalturn/(float)n);
return 0;
}

Output:
Enter The No Of Process :3
Enter The Burst Time (in ms) For Process #1 :30
Enter The Priority For Process # 1 :2
Enter The Burst Time (in ms) For Process #2 :20
Enter The Priority For Process # 2 :1
Enter The Burst Time (in ms) For Process #3 :40
Enter The Priority For Process # 3 :3

Process          Starting  Ending  Waiting  TurnAround
                          Time        Time      Time          Time
Process # 2        0             20           0                20
Process # 1       20            50          20              50
Process # 3       50            90          50              90

Average
---------
Waiting Time : 23.333333 ms
TurnAround Time : 53.333333 ms


Simulation of Shortest Job First (SJF) CPU scheduling algorithm in C Language

This is a C program to simulate Shortest Job First (SJF) CPU scheduling algorithm. In Shortest Job first CPU scheduling algorithm, the process with  shortest CPU burst time is executed first. The following program is to simulate non preemptive SJF scheduling. Preemptive SJF CPU scheduling algorithm will be discussed in another post.

#include<stdio.h>
struct process
{
int burst,wait,no;
}p[20]={0,0};

int main()
{
int n,i,j,totalwait=0,totalturn=0;
printf("\nEnter The No Of Process :");
scanf("%d",&n);
for(i=0;i<n;i++)
  {
  printf("Enter The Burst Time (in ms) For Process #%2d :",i+1);
  scanf("%d",&p[i].burst);
  p[i].no=i+1;
  }

for(i=0;i<n;i++)
  for(j=0;j<n-i-1;j++)
    if(p[j].burst>p[j+1].burst)
      {
      p[j].burst^=p[j+1].burst^=p[j].burst^=p[j+1].burst;
      p[j].no^=p[j+1].no^=p[j].no^=p[j+1].no;
      }

printf("\nProcess \t Waiting Time TurnAround Time ");
for(i=0;i<n;i++)
  {
  printf("\nProcess # %-12d%-15d%-15d",p[i].no,p[i].wait,p[i].wait+p[i].burst);
  p[i+1].wait=p[i].wait+p[i].burst;
  totalwait=totalwait+p[i].wait;
  totalturn=totalturn+p[i].wait+p[i].burst;
  }
printf("\n\nAverage\n---------");
printf("\nWaiting Time : %f ms",totalwait/(float)n);
printf("\nTurnAround Time : %f ms\n\n",totalturn/(float)n);
return 0;

}

Output:

Enter The No Of Process :3
Enter The Burst Time (in ms) For Process # 1 :20
Enter The Burst Time (in ms) For Process # 2 :30
Enter The Burst Time (in ms) For Process # 3 :10
Process          Waiting Time     TurnAround Time
Process # 3          0                           10
Process # 1        10                            30
Process # 2        30                            60

Average---------
Waiting Time : 13.333333 ms
TurnAround Time : 33.333333 ms

Related Posts:

C Program to Simulate ls Command in Linux
C Program to Simulate Round Robin CPU Scheduling Algorithm
C Program to Simulate Priority CPU Scheduling Algorithm
C Program to Simulate First Come First Serve (FCFS) CPU Scheduling Algorithm
C Program to Simulate rmdir Command or Delete Directory
C Program to Open, Read and Write Files
C Program to Create Process and Display Process ID - OS Lab Program
How to Use Fork and Exec System Calls
How to Use Exit System Call
C Program to make Parent Process Wait for Child to Terminate
C Program to Simulate GREP Command in Linux
C Program to make Child Process an Orphan Process
C Program to Show Process ID in Linux
C Program for Inter Process Communication using Pipes or Named Pipes (Chat Program)
C Program to Create a Process Using Fork System Call