Sie sind auf Seite 1von 15

DIJKSTRAS ALGORITHM

#include<stdio.h>
#include<limits.h>
#define V 9
int minDistance(int dist[],bool sptSet)
{
int v;
int min= INT_MAX, min_index;
for(v=0;v<V;v++)
{
if(sptSet==false && dist[v]<min)
{min=dist[v];
min_index=v;}
}
return min_index;
}
void printSolution(int dist[])
{
printf("vertex distance from source");
for(int v=0;v<V;v++)
printf("%d\t\t%d",v,dist[v]);
}
void dijkstra(int graph[V][V], int src)
{
int v;
bool sptSet[V];
int dist[V];
for(v=0;v<V;v++)
{
dist[v]=INT_MAX;
sptSet[v]=false;
}
dist[src]=0;
for(int count=0;count<V-1;count++)
{
int u= minDistance(dist,sptSet);
sptSet[u]=true;
for(v=0;v<V-1;v++)
if(!sptSet[v] && graph[u][v] && dist[u]!=INT_MAX && dist[u]+graph[u]
[v]<dist[v])
dist[v]= dist[u]+ graph[u][v];
}
printSolution(dist);
}

int main()
{
int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 0, 10, 0, 2, 0, 0},
{0, 0, 0, 14, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
dijkstra(graph, 0);
return 0;
}

PRIMS ALGORITHM
#include<stdio.h>
#include<limits.h>
#define V 5
int minKey(int key[],bool mstSet[])
{
int v;
int min=INT_MAX,min_index;
for(v=0;v<V;v++)
if(mstSet[v]==false && key[v]<min)
{min=key[v];
min_index=v;}
return min_index;
}
void printMST(int graph[V][V], int n,int parent[])
{
int i;
printf("edge weight\n");
for(int i=1;i<V;i++)
printf("%d-%d %d\n",parent[i],i,graph[i][parent[i]]);
}
void primMST(int graph[V][V])
{
int parent[V];
bool mstSet[V];
int key[V];
for(int i=0;i<V;i++)
{
key[i]=INT_MAX;
mstSet[i]=false;
}
parent[0]=-1;
key[0]=0;
for(int count=0;count<V;count++)
{
int u= minKey(key,mstSet);
mstSet[u]=true;
for(int v=0;v<V;v++)
{
if(graph[u][v]&&mstSet[v]==false && graph[u][v]<key[v])
{
parent[v]=u;

key[v]=graph[u][v];
}
}
}
printMST(graph,V,parent);
}
int main()
{
int graph[V][V] = {{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0},
};
primMST(graph);
return 0;
}

HEAP SORT
#include<stdio.h>
#include<stdlib.h>
int arr[100]={0},n;
int location = -1;

int choicelist()
{
int choice,num;
printf("pleasse select an option\n 1. insert an element \n 2. delete an element \n
3.sort the heap \n 4.traversal of the heap \n 5. display \n 6. exit \n enter your
choice:");
scanf("%d",&choice);
return choice;
}
void swap(int *x,int *y)
{
int temp;
temp= *x;
*x = *y;
*y=temp;
}
void insert(int num)
{
location++;
int locationhere=location;
int parentindex;
while(locationhere>0)
{
parentindex= (locationhere-1)/2;
if(num<=arr[parentindex])
{
arr[locationhere]=num;
return;
}
arr[locationhere]=arr[parentindex];
locationhere=parentindex;
}
arr[0]=num;
}
void traverseheap(int src)
{
int right= 2*src+1;
int left = 2*src+2;

while(right<=location || left<=location)
{
if(arr[src]>=arr[right] && arr[src]>=arr[left])
break;
else if(arr[src]<arr[right])
{
swap(&arr[src],&arr[right]);
src=right;
traverseheap(src);
}
else if(arr[src]<arr[left])
{
swap(&arr[src],&arr[left]);
src=left;
traverseheap(src);
}
}
}
int deletee()
{
int root;
root= arr[0];
int last_element=arr[location];
root = arr[location];
location--;
traverseheap(0);
return root;
}
void sortheap()
{
for(int i=0;i<location;i++)
printf("%d\t",deletee());
}
void display()
{
for(int i=0;i<location;i++)
printf("%d ",&arr[i]);
}

void inordertraversal(int src)


{
if(src>location)
{
return;
}
else
{
inordertraversal(2*src+1);
printf("%d",arr[src]);
inordertraversal(2*src+2);
}
}
void postordertraversal(int src)
{
if(src>location)
{
return;
}
else
{
printf("%d",arr[src]);
postordertraversal(2*src+1);
postordertraversal(2*src+2);
}
}
void preordertraversal(int src)
{
if(src>location)
{
return;
}
else
{
preordertraversal(2*src+1);
preordertraversal(2*src+2);
printf("%d",arr[src]);
}
}
int main(int argc, char *argv[])
{
struct node *r=NULL;
int num=0,response;
if (argc != 2)
printf("usage: %s <filename>\n",argv[0]);

else {
FILE *file = fopen(argv[1],"r");
if (file ==0)
{
printf("could not open file\n");
}
else {
char x;
while ((x=fgetc(file))!=EOF)
{if(x != '\n')
{
num *= 10;
num += (x-48);
}
else
{
insert(num);
num=0;
}
}
}
do{
system("cls");
response = choicelist();
if(response == 1)
{
system("cls");
printf("enter the value to be inserted: ");
scanf("%d",&num);
insert(num);
}
else if(response == 2)
{
system("cls");
printf("value deleted: %d",deletee());
printf("\nPress 1 to continue, 6 to exit:");
scanf("%d",&response);
}
else if(response == 3)
{
system("cls");
sortheap();
printf("\nPress 1 to continue, 6 to exit:");
scanf("%d",&response);

}
else if(response == 4)
{
system("cls");
printf("1. Preorder\n2. Inorder\n3. Postorder\n");
scanf("%d",&num);
if(num == 1)
{
preordertraversal(0);
}
else if(num == 2)
{
inordertraversal(0);
}
else if(num == 3)
{
postordertraversal(0);
}
else
printf("Wrong Input");
printf("\nPress 1 to continue, 6 to exit:");
scanf("%d",&response);
}
else if(response == 5)
{
system("cls");
display();
printf("\nPress 1 to continue, 6 to exit:");
scanf("%d",&response);
}
}while(response != 6);
return 0;
}
}

GRAPH TRAVERSAL
#include<stdio.h>
#include<stack>
#include<queue>
#include<stdlib.h>
int matrix[100][100],n;
void dfs(int src)
{
bool visited[n];
for(int i=0;i<n;i++)
visited[i]=false;
int location = src;
visited[location]=true;
std::stack<int> dd;
dd.push(location);
while(!dd.empty())
{
location= dd.top();
dd.pop();
if(visited[location]== false)
{
visited[location]= true;
printf("%d ",location);
}
for(int j=0;j<n;j++)
{
if(matrix[location][j]==1 && visited[j]==false)
{
dd.push(j);
}
}
}
}
void bfs(int src)
{
bool visited[n];
std::queue<int> bb;
int location= src;
visited[location]=true;
bb.push(location);
while(!bb.empty())

{
int location=bb.front();
bb.pop();
printf("%d ",location);
for(int i=0;i<n;i++)
{
if(matrix[location][i]==1 && visited[i]==false)
{
visited[i]=true;
bb.push(i);
}
}
}
}
int showlist()
{
int n;
printf("1. View Matrix Representation \n2. BFS \n3. DFS \n4. Exit \n");
scanf("%d",&n);
return n;
}
int main ( int argc, char *argv[] )
{
int response;
if ( argc != 2 )
printf("usage: %s <filename>\n",argv[0]);
else {
FILE *the_file;
the_file = fopen(argv[1],"r");
if(the_file == NULL){
printf("FILE NOT FOUND");
return 0;
}
else {
char x;
bool new_line = true;
int currentnode,num=0;
fscanf(the_file, "%d", &n);
bool visited[n];

for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
matrix[i][j]=0;
while ( fscanf(the_file, "%c", &x)!=EOF )
if(x != ',' && x != '\n')
{
num *= 10;
num += (x-48);
}
else
{
if(new_line == true)
{
currentnode = num;
new_line = false;
}
else
{
matrix[currentnode][num] = 1;
matrix[num][currentnode] = 1;
if(x == '\n')
{
new_line = true;
}
}
num=0;
}
}
do{
system("cls");
response = showlist();
if(response == 1)
{
system("cls");
printf("\nMatrix Representation: \n");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++){
printf("%d ",matrix[i][j]);
}
printf("\n");
}

printf("\nIf you wish to exit program, input 4, else input 1: ");


scanf("%d",&response);
}
else if(response == 2)
{
system("cls");
printf("\nBFS Traversal: \n");
bfs(0);
printf("\n");
printf("\nIf you wish to exit program, input 4, else input 1: ");
scanf("%d",&response);
}
else if(response == 3)
{
system("cls");
printf("\nDFS Traversal: \n");
dfs(0);
printf("\n");
printf("\nIf you wish to exit program, input 4, else input 1: ");
scanf("%d",&response);
}
}while(response != 4);
}
return 0;
}

Das könnte Ihnen auch gefallen