Wednesday, 7 October 2015

shortest path

#include<stdio.h>
#include<conio.h>
#define MAX 1000



void dijkstra(int n,int v,int cost[10][10],int dist[10]);

int main()
{
 int n,v,i,j,cost[10][10],dist[10];
 clrscr();
 printf("\n Enter the number of Nodes: ");
 scanf("%d",&n);
 printf("\n Enter the Weight Matrix:\n");
 printf("\nEnter 1000 to denote Infinity\n");
 for(i=0;i<n;i++)
 {
   for(j=0;j<n;j++)
   {
     scanf("%d",&cost[i][j]);
   }
 }
 printf("\n Enter the Source Node:");
 scanf("%d",&v);
 dijkstra(n,v-1,cost,dist);
 printf("\n   Shortest Path from Node : %d",v);
 printf("\n#################################\n\n");
 for(i=0;i<n;i++)
 {
    printf("Distance to Node: %d is %d\n",i+1,dist[i]);
 }
 return 0;
}

void dijkstra(int n,int v,int cost[10][10],int dist[10])
{
  int i,u,count,w,flag[10],min;

  for(i=0;i<n;i++)
  {
    flag[i]=0;
    dist[i]=cost[v][i];
  }
  count=1;
  while(count<n)
  {
    min=MAX;
    for(w=0;w<n;w++)
    {
       if(dist[w]<min && !flag[w])
       {
    min=dist[w];
    u=w;
       }
    }
    flag[u]=1;
    count++;
    for(w=0;w<n;w++)
    {
       if((dist[u]+cost[u][w]<dist[w])&&!flag[w])
       {
   dist[w]=dist[u]+cost[u][w];
       }
    }
 }

}

news paper using kruskal alog

#include<iostream.h>
#include<stdio.h>
int parent[10]; //initial value 0 due to global initialization

class kruskal
{
 int a,b,u,v,i,j,n,noofedges;
 int visited[10],min,mincost,cost[10][10];
public:
 kruskal()
{
 noofedges=1;
 mincost=0;
}
 void read();
 void kruskals(int cost[][10],int n);

};
void kruskal::read()
{
 cout<<"Enter the no. of vertices";
 cin>>n;
 cout<<"Enter the adjacency matrices";
 for(i=1;i<=n;i++)
 for(j=1;j<=n;j++)
 {
 cin>>cost[i][j];
 if(cost[i][j]==0)
 cost[i][j]=999;
 }
 kruskals(cost,n);
}

void kruskal::kruskals(int cost[][10],int n)
{
 cout<<"The minimum cost edges are \n";
 while(noofedges<n)
 {
 min=999;
 for(i=1;i<=n;i++)
 for(j=1;j<=n;j++)
 if(cost[i][j]<min)
 {
 min=cost[i][j];
 a=u=i;
 b=v=j;
 }
 while(parent[u])
 u=parent[u];
 while(parent[v])
 v=parent[v];
 if(u!=v)
 {
 noofedges++;
 cout<<"\n Edge ( "<<a<<"->"<<b<<")"<<min;
 mincost+=min;
 parent[v]=u;
 }
 cost[a][b]=cost[b][a]=999;
 }
 cout<<"\n \n Minimum cost \t"<<mincost<<" ";

}
  main()

java program for queue

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Graphics;
public class queue extends Applet implements ActionListener {
 TextField t1, t2;
 Button b1, b2, b3, b4;
 TextArea T;
 static int n;
 int i, front=0, rear=0, item, count=0;
 String s1, s2;
 int queue[];
 public void init() {
 t1 = new TextField(10);
 add(t1);
 t1.setText("");
 Button b4 =  new Button("queue Limit");
 add(b4);
 b4.addActionListener(this);
 t2 = new TextField(10);
 add(t2);
 t2.setText("");
 Button b1 =  new Button("Push");
 add(b1);
 b1.addActionListener(this);
 Button b2 = new Button("Pop");
 add(b2);
 b2.addActionListener(this);
 Button b3 = new Button("Display");
 add(b3);
 b3.addActionListener(this);
 T = new TextArea(20, 30);
 add(T);
 }

 public void actionPerformed(ActionEvent e){

 Button source = (Button)e.getSource();
 if(source.getLabel() == "queue Limit"){
 try {
 s1 = t1.getText();
      n = Integer.parseInt(s1);
    queue = new int[n];
    }  
   catch(Exception e1) {
    System.out.println(e1.getMessage());
    }
 }
       if(source.getLabel() == "Push"){
 try {
    if(count < n) {
 s2 = t2.getText();
     item = Integer.parseInt(s2);
     queue[rear]=item;
 t2.setText("");
      rear++;
     count++;
     }
    else{
 T.setText("");
     T.setText("queue IS FULL\n");
 t2.setText("");
 }
    }
   catch(Exception e1) {
    System.out.println(e1.getMessage());
    }
   }
 if (source.getLabel() == "Pop") {
 if(count!=0) {
 T.setText("");
     T.setText("The item deleted is: "+queue[front]+"\n");
     front++;
     count--;
     }
    else{
 T.setText("");
     T.setText("queue IS EMPTY\n");
 }
   if(rear==n)
    rear=0;
 }
 if (source.getLabel() == "Display") {
 int m=0;
 T.setText("");
    if(count==0) {
 T.setText("");
    T.setText("queue IS EMPTY\n");
 }
    else {
    for(i=front;m<count;i++,m++)
    T.append(" "+queue[i]);
    }
   }
 }
}

dictionary

#include<iostream.h>
#include<conio.h>
#include<string.h>
struct dict
{
 char key[20];
 char mean[20];

};

void main()
{
int n,i,f=0;
char k[20];


clrscr();
cout<<"\t\t\t\tDictionary Dpcoe\n\n\n\n" ;

//insert operation

struct dict d[30];
cout<<"how many key u want to enter\n";
cin>>n;
//insret
for(i=1;i<=n;i++)
{
 cout<<"enter key:\t";
 cin>>d[i].key;
 cout<<"enter meaning\t" ;
 cin>>d[i].mean;
}
 //diplay

cout<<"key      \t      mean \n" ;
//display
for(i=1;i<=n;i++)
{
cout<<d[i].key<<"\t";
cout<<d[i].mean<<"\n";

}


cout<<"enetr key to serch\n" ;
cin>>k;
// logic for search oprration

for(i=1;i<=n;i++)
{
if(strcmp(d[i].key,k)==0)
{
cout<<"meaninig is \t";
cout<<d[i].mean;
f=1;

}
}

if(f==0)
{  cout<<"update key\n" ;
}

//update

cout<<"enetr key to be updated\n" ;
cin>>k;
for(i=1;i<=n;i++)
{
if(strcmp(d[i].key,k)==0)
{
cout<<"meaninig to be updated \t";
cin>>d[i].mean;
}
}

//diply updated:

cout<<"updated dictionary is\n" ;

for(i=1;i<=n;i++)
{
cout<<d[i].key<<"\t";
cout<<d[i].mean<<"\n";

}
getch();
}

bubble sort

# Program Name: Bubble Sort using python

#array[] contains the element that we want to sort
array = [2,5,4,1,7]

#BUBBLE SORT LOGIC

swapped = True
while (swapped):
     swapped = False
     for i in range(0,len(array)-1):
         if (array[i] > array[i+1]):
             temp = array[i]
             array[i] = array[i+1]
             array[i+1] = temp
             swapped = True

# PRINT THE SORTED ARRAY

print (array)

python program

def bubbleSort(alist):
    for passnum in range(len(alist)-1,0,-1):
        for i in range(passnum):
            if alist[i]>alist[i+1]:
                temp = alist[i]
                alist[i] = alist[i+1]
                alist[i+1] = temp

alist = [54,26,93,17,77,31,44,55,20]
bubbleSort(alist)
print(alist)


<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>

def insertionSort(alist):
   for index in range(1,len(alist)):

     currentvalue = alist[index]
     position = index

     while position>0 and alist[position-1]>currentvalue:
         alist[position]=alist[position-1]
         position = position-1

     alist[position]=currentvalue

alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)


<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

def mergeSort(alist):
    print("Splitting ",alist)
    if len(alist)>1:
        mid = len(alist)//2
        lefthalf = alist[:mid]
        righthalf = alist[mid:]

        mergeSort(lefthalf)
        mergeSort(righthalf)

        i=0
        j=0
        k=0
        while i<len(lefthalf) and j<len(righthalf):
            if lefthalf[i]<righthalf[j]:
                alist[k]=lefthalf[i]
                i=i+1
            else:
                alist[k]=righthalf[j]
                j=j+1
            k=k+1

        while i<len(lefthalf):
            alist[k]=lefthalf[i]
            i=i+1
            k=k+1

        while j<len(righthalf):
            alist[k]=righthalf[j]
            j=j+1
            k=k+1
    print("Merging ",alist)

alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)



<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

def quickSort(alist):
   quickSortHelper(alist,0,len(alist)-1)

def quickSortHelper(alist,first,last):
   if first<last:

       splitpoint = partition(alist,first,last)

       quickSortHelper(alist,first,splitpoint-1)
       quickSortHelper(alist,splitpoint+1,last)


def partition(alist,first,last):
   pivotvalue = alist[first]