Vous êtes sur la page 1sur 5

DFS :

1. Write a program in C language to implement Dijkstras algorithm.

2. IMPLEMENTATION OF DIJKSTRA ALGORITHM


3. To implement the Dijkstra algorithm for a weighted graph using C Language. Algorithm: Step 1: Start the process. Step 2: Get the number of vertices from the user. Step 3: Enter the values in the form of an adjacent matrix. Step 4: Get the source vertex and the algorithm displays the shortest path from the source vertex to all other vertices, with minimum weights. Step 5: Stop the process.

PROGRAM: #include #include void main() { int graph[15][15],s[15],pathestimate[15],mark[15]; int num_of_vertices,source,i,j,u,predecessor[15]; int count=0; int minimum(int a[],int m[],int k); void printpath(int,int,int[]); clrscr(); printf("\nEnter the no.of vertices\n"); scanf("%d",&num_of_vertices); if(num_of_vertices<=0) { printf("\nNo vertices present!!!\n"); exit(1); } printf("\nEnter the adjacent matrix\n"); for(i=1;i<=num_of_vertices;i++) { printf("\nEnter the elements of row %d\n",i); for(j=1;j<=num_of_vertices;j++) { scanf("%d",&graph[i][j]); } } printf("\nEnter the source vertex\n"); scanf("%d",&source); for(j=1;j<=num_of_vertices;j++) { mark[j]=0; pathestimate[j]=999; predecessor[j]=0;

} pathestimate[source]=0; while(count0) { if(mark[i]!=1) { if(pathestimate[i]>pathestimate[u]+graph[u][i]) { pathestimate[i]=pathestimate[u]+graph[u][i]; predecessor[i]=u; } } } } } for(i=1;i<=num_of_vertices;i++) { printpath(source,i,predecessor); if(pathestimate[i]!=999) { printf("->(%d)\n",pathestimate[i]); } } int minimum(int a[],int m[],int k) { int mi=999; int i,t; for(i=1;i<=k;i++) { if(m[i]!=1) { if(mi>=a[i]) { mi=a[i]; t=i; } }} return t; } void printpath(int x,int i,int p[]) { printf("\n"); if(i==x) { printf("%d",x); } else if(p[i]==0) printf("no path from %d to %d",x,i); else { printpath(x,p[i],p); printf("..%d",i); } }

2. Write a program in C language to implement Kruskals algorithm.

C PROGRAM TO FIND MINIMAL SPANNING TREE USING KRUSKAL'S ALGORITHM #include<stdio.h> #define INF 1000

char vertex[10]; int wght[10][10]; int span_wght[10][10]; int source; struct Sort { int v1,v2; int weight; }que[20]; int n,ed,f,r; int cycle(int s,int d) { int j,k; if(source==d) return 1; for(j=0;j<n;j++) if(span_wght[d][j]!=INF && s!=j) { if(cycle(d,j)) return 1; } return 0; } void build_tree() { int i,j,w,k,count=0; for(count=0;count<n;f++) { i=que[f].v1; j=que[f].v2; w=que[f].weight; span_wght[i][j]=span_wght[j][i]=w; source=i; k=cycle(i,j); if(k) span_wght[i][j]=span_wght[j][i]=INF; else count++; } } void swap(int *i,int *j) { int t; t=*i; *i=*j; *j=t; } void main() { int i,j,k=0,temp; int sum=0; clrscr(); printf("\n\n\tKRUSKAL'S ALGORITHM TO FIND SPANNING TREE\n\n"); printf("\n\tEnter the No. of Nodes : "); scanf("%d",&n);

for(i=0;i<n;i++) { printf("\n\tEnter %d value : ",i+1); fflush(stdin); scanf("%c",&vertex[i]); for(j=0;j<n;j++) { wght[i][j]=INF; span_wght[i][j]=INF; } } printf("\n\nGetting Weight\n"); for(i=0;i<n;i++) for(j=i+1;j<n;j++) { printf("\nEnter 0 if path Doesn't exist between %c to %c : ",vertex[i],vertex[j]); scanf("%d",&ed); if(ed>=1) { wght[i][j]=wght[j][i]=ed; que[r].v1=i; que[r].v2=j; que[r].weight=wght[i][j]; if(r) { for(k=0;k<r;k++) if(que[k].weight>que[r].weight) { swap(&que[k].weight,&que[r].weight); swap(&que[k].v1,&que[r].v1); swap(&que[k].v2,&que[r].v2); } } r++; } } clrscr(); printf("\n\tORIGINAL GRAPH WEIGHT MATRIX\n\n"); printf("\n\tweight matrix\n\n\t"); for(i=0;i<n;i++,printf("\n\t")) for(j=0;j<n;j++,printf("\t")) printf("%d",wght[i][j]); build_tree(); printf("\n\n\t\tMINIMUM SPANNING TREE\n\n"); printf("\n\t\tLIST OF EDGES\n\n"); for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(span_wght[i][j]!=INF) { printf("\n\t\t%c ------ %c = %d ",vertex[i],vertex[j],span_wght[i][j]); sum+=span_wght[i][j]; } printf("\n\n\t\tTotal Weight : %d ",sum); getch();

} Dbms
Session 1: In this session you need to create database for an Employee management system of an ABC organisation. The details about different tables are given below. According to that you can proceed further and create tables using MS-Access. Create the following tables with the specified constraints: Employee First name - Not NULL Middle initials Last name - Not NULL Employee-id - Primary Key Date of Birth Address Gender - M or F Salary - Range of 5000 to 25000 Date of Joining Department number - Refers to Department Number of Department table. Department Department name - Not NULL unique Department number - Primary Key Manager_id - Refers to employee-id of employee table. Manager date of joining - Not NULL. Department location Department number - Refers to Department number of department table. Department location - Not NULL. Department number & Department location are combined Primary Key. Project Project name - Not NULL. Project number - Primary Key. Project location - Not NULL. Department number - Refers to department number of Department table. Works-on Employee-id - Not NULL refers to employee-id of employee table. Project number - Not NULL refers to Project number of Project table. Hours - Not NULL. Employee-id & Project number are combined primary key. Dependent Employee-id - Refer to employee table employee id field Dependent name Gender - M or F Date of Birth - Not NULL Relationship - Not NULL Now enter a few sets of meaningful data and answer the following queries. 1. List the department wise details of all the employees. 2. Find out all those departments that are located in more than one location. 3. Find the list of projects. 4. Find out the list of employees working on a project. 5. List the dependents of the employee whose employee id is 001 JAVA Exercise 1: Write a program in Java to display the names and roll numbers of students. Initialize respective array variables for 10 students. Handle ArrayIndexOutOfBoundsExeption, so that any such problem doesnt cause illegal termination of program. Exercise 2: Write a Java program to enable the user to handle any chance of divide by zero exception.

Vous aimerez peut-être aussi