C Kruskal’s algorithm - Reference

ImageDescription
Kruskal Algorithm 1.svgAD and CE are the shortest arcs, with length 5, and AD has been arbitrarily chosen, so it is highlighted.
Kruskal Algorithm 2.svgCE is now the shortest arc that does not form a cycle, with length 5, so it is highlighted as the second arc.
Kruskal Algorithm 3.svgThe next arc, DF with length 6, is highlighted using much the same method.
Kruskal Algorithm 4.svgThe next-shortest arcs are AB and BE, both with length 7. AB is chosen arbitrarily, and is highlighted. The arc BD has been highlighted in red, because there already exists a path (in green) between B and D, so it would form a cycle (ABD) if it were chosen.
Kruskal Algorithm 5.svgThe process continues to highlight the next-smallest arc, BE with length 7. Many more arcs are highlighted in red at this stage: BC because it would form the loop BCE, DE because it would form the loop DEBA, and FE because it would form FEBAD.
Kruskal Algorithm 6.svgFinally, the process finishes with the arc EG of length 9, and the minimum spanning tree is found.

C Program FIFO Page Replacement Policy : Explanation

Program : [ Click Here ]



Step 1 : Accept Given Data .
  1. Frame Number  : fno
  2. Number of Pages  : count
  3. Actual Pages : arr[100]
void accept()
{
int i;
printf("nEnter the number of Frames : ");
scanf("%d",&fno);

printf("nEnter the number of Pages : ");
scanf("%d",&count);

printf("nEnter the Page No : ");
for(i=0;i< count;i++)
scanf("%d",&arr[i]);
}

Step 2 :
  1. Now we have Pages and Frame Size.
  2. Total Number of Pages is Given by Variable : “Count“.

Logic :

  • Now We are Creating Circular Linked List Whose Size is equal to “Frame Size
  • Initialize Each Node’s Data = -99.
  • -99 Tells us That Place is Empty
  • Call Create Function.
Size of Linked List = Frame Size

Step 3 :
  • After Creating Initial Setup Call Function : fifo().
  • While Inserting Element in Frame [i.e in Linked List ] We have to Check Whether -
    • Page number is Already Present or Not.
    • There is Empty Location or not.
temp = start;  //Temporary Purpose
last = start;  //Replacement Purpose
curr = start;  //Current Purpose ->-99

We have To insert Total Number of Pages = “Value Specified in Count” so we requires 1 for loop.

for(i=0;i< count;i++)
  • Check Whether First Page is already Present or not.
res = search(arr[i]);
  • If Page is Present then Don’t Take any Action , and Read Next Page.
  • But If Page is Present then we have to insert Page in Linked List

Step 4 :
if(res == 0)
{
fault++;
if(curr->data == -99)
{
curr->data = arr[i];
curr = curr->next;
}
else
{
last ->data = arr[i];
last = last->next;
}
}//end outer if
  • If Linked List have Blank Space then First “If Part” gets Executed.
  • If Linked List doesn’t have Blank Space then Overwrite on the Oldest Node. [ “last”  is used for Keeping Track of Oldest Node , After Overwriting Move “last” one position ahead. ]

C Program FIFO Page Replacement Algorithm

Program for FIFO Page Replacement : Explanation [Click Here]

/*---------------------------------------------
Title : Page Replacement Algorithms
Program Developed By : Pritesh Abhiman Taral
----------------------------------------------*/

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

struct node
{
int data;
struct node *next;
}*curr,*new_node,*current,*start=NULL,*temp,*last;

int arr[100],count,fault,fno;
//--------------------------------
void creat()
{
int i;

for(i=0;i< fno;i++)
{
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data = -99; // -99 = NULL or Empty Characcter
new_node->next=NULL;

if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
}
current->next = start;
}
//--------------------------------
void display()
{
struct node *temp;
temp=start;
do
{
if(temp->data == -99)
printf("-t",temp->data);
else
printf("%dt",temp->data);
temp=temp->next;
}while(temp!=start);
}
//--------------------------------
int search(int number)
{
int flag;
struct node *temp;
temp=start;
do
{
if(temp->data==number)
return(1);
else
flag = 0;
temp=temp->next;
}while(temp!=start);

if(flag ==0)
return(0);
}
//--------------------------------
void fifo()
{
int res,i;
temp = start; //Temp Purpose
last = start; //Repl Purpose
curr = start; //Curr Purpose ->-99

for(i=0;i< count;i++)
{
res = search(arr[i]);

if(res == 0)
{
fault++;
if(curr->data == -99)
{
curr->data = arr[i];
curr = curr->next;
}
else
{
last ->data = arr[i];
last = last->next;
}
}//end outer if

printf("nnAfter Inserting (%d) :: ",arr[i]);
display();
printf(" Fault : %d",fault);
}//end for

printf("n--------------------------------------------n");
printf(" ¯ Total Number of Faults = %d",fault);
printf("n--------------------------------------------n");

}
//--------------------------------
void accept()
{
int i;
printf("nEnter the number of Frames : ");
scanf("%d",&fno);

printf("nEnter the number of Pages : ");
scanf("%d",&count);

printf("nEnter the Page No : ");
for(i=0;i< count;i++)
scanf("%d",&arr[i]);
}
//------------------------------------------------
void main()
{
char ch;
clrscr();
printf("nAccept Frame Number and Pages");
printf("n");

accept();
creat();
fifo();

getch();
}

Explanation :  [Click Here]


Output :

Accept Frame Number and Pages

Enter the number of Frames : 3

Enter the number of Pages : 7

Enter the Page No : 1 3 2 1 3 4 2


After Inserting (1) :: 1 - - Fault : 1

After Inserting (3) :: 1 3 - Fault : 2

After Inserting (2) :: 1 3 2 Fault : 3

After Inserting (1) :: 1 3 2 Fault : 3

After Inserting (3) :: 1 3 2 Fault : 3

After Inserting (4) :: 4 3 2 Fault : 4

After Inserting (2) :: 4 3 2 Fault : 4
═══════════════════════════════════════════════
» Total Number of Faults = 4
═══════════════════════════════════════════════

Explanation :  [Click Here]

C Program First in First Out [FIFO] Page Replacement Algorithm in c

Program for FIFO Page Replacement : Explanation [Click Here]

/*---------------------------------------------
Title : Page Replacement Algorithms
Program Developed By : Pritesh Abhiman Taral
----------------------------------------------*/

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

struct node
{
int data;
struct node *next;
}*curr,*new_node,*current,*start=NULL,*temp,*last;

int arr[100],count,fault,fno;
//--------------------------------
void creat()
{
int i;

for(i=0;i< fno;i++)
{
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data = -99; // -99 = NULL or Empty Characcter
new_node->next=NULL;

if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
}
current->next = start;
}
//--------------------------------
void display()
{
struct node *temp;
temp=start;
do
{
if(temp->data == -99)
printf("-t",temp->data);
else
printf("%dt",temp->data);
temp=temp->next;
}while(temp!=start);
}
//--------------------------------
int search(int number)
{
int flag;
struct node *temp;
temp=start;
do
{
if(temp->data==number)
return(1);
else
flag = 0;
temp=temp->next;
}while(temp!=start);

if(flag ==0)
return(0);
}
//--------------------------------
void fifo()
{
int res,i;
temp = start; //Temp Purpose
last = start; //Repl Purpose
curr = start; //Curr Purpose ->-99

for(i=0;i< count;i++)
{
res = search(arr[i]);

if(res == 0)
{
fault++;
if(curr->data == -99)
{
curr->data = arr[i];
curr = curr->next;
}
else
{
last ->data = arr[i];
last = last->next;
}
}//end outer if

printf("nnAfter Inserting (%d) :: ",arr[i]);
display();
printf(" Fault : %d",fault);
}//end for

printf("n--------------------------------------------n");
printf(" ¯ Total Number of Faults = %d",fault);
printf("n--------------------------------------------n");

}
//--------------------------------
void accept()
{
int i;
printf("nEnter the number of Frames : ");
scanf("%d",&fno);

printf("nEnter the number of Pages : ");
scanf("%d",&count);

printf("nEnter the Page No : ");
for(i=0;i< count;i++)
scanf("%d",&arr[i]);
}
//------------------------------------------------
void main()
{
char ch;
clrscr();
printf("nAccept Frame Number and Pages");
printf("n");

accept();
creat();
fifo();

getch();
}

Explanation :  [Click Here]


Output :

Accept Frame Number and Pages

Enter the number of Frames : 3

Enter the number of Pages : 7

Enter the Page No : 1 3 2 1 3 4 2


After Inserting (1) :: 1 - - Fault : 1

After Inserting (3) :: 1 3 - Fault : 2

After Inserting (2) :: 1 3 2 Fault : 3

After Inserting (1) :: 1 3 2 Fault : 3

After Inserting (3) :: 1 3 2 Fault : 3

After Inserting (4) :: 4 3 2 Fault : 4

After Inserting (2) :: 4 3 2 Fault : 4
═══════════════════════════════════════════════
» Total Number of Faults = 4
═══════════════════════════════════════════════

Explanation :  [Click Here]