Friday 26 December 2014

data structure

Evaluation of Postfix Expression

Algorithm to find the Value of an arithmetic expression P Written in Postfix
[1] Add a right parenthesis ‘)” at the end of P. [This act as delimiter]
[2] Scan P from left to right and repeat Steps 3 and 4 for each element of P until the delimiter “)” is encountered 
[3] If an operand is encountered, put it on STACK
[4] If an operator     is encountered, then
  (a) Remove the two top elements of STACK, where A is the top element and B is the next-to-top element
  (b) Evaluate B    A
  (c ) Place the result of (b) on STACK
[5] Set Value equal to the top element of STACK
[6] Exit 
example

Evaluation of Prefix Expression

Algorithm to find the Value of an arithmetic expression P Written in Prefix
[1] Scan P from right to left and repeat Steps 2 and 3 for each element of P 
[2] If an operand is encountered, put it on STACK
[3] If an operator  is encountered, then
  (a) Remove the two top elements of STACK, where A is the top element and B is the next-to-top element
  (b) Evaluate A OP B
  (c ) Place the result of (b) on STACK
[4] Set Value equal to the top element of STACK
[5] Exit 

                 Infix to Postfix

Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix notation
[1] Push “(“ onto STACK and “)” to the end of Q
      [2] Scan Q from Left to Right and Repeat Steps 3 to 6         for each element of Q until the STACK is empty
[3] If an operand is encountered, add it to P
[4] If a left parenthesis is encountered, push it onto STACK
[5] If an operator     is encountered, then:
  (a) Repeatedly pop from STACK and to P each operator (on the top of STACK) which has same precedence as or higher precedence than    .
  (b)  Add     to STACK
[6] If  a right parenthesis is encountered, then
  (a) Repeatedly pop from the STACK and add to P each operator (on top of STACK) until a left parenthesis is encountered.
  (b)  Remove the left parenthesis. [Do not add it to P]
     [7] Exit 


                           Infix to Prefix 

Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent prefix notation
[1] Push “)“ onto STACK and “(” to the begin of Q
      [2] Scan Q from right to left and Repeat Steps 3 to 6         for each element of Q until the STACK is empty
[3] If an operand is encountered, add it to P
[4] If a right parenthesis is encountered, push it onto STACK
[5] If an operator     is encountered, then:
  (a) Repeatedly pop from STACK and to P each operator (on the top of STACK) which has same precedence as or higher precedence than    .
  (b)  Add     to STACK
[6] If  a left parenthesis is encountered, then
  (a) Repeatedly pop from the STACK and add to P each operator (on top of STACK) until a right parenthesis is encountered.
  (b)  Remove the right parenthesis. [Do not add it to P]
     [7] now reverse the expression P we get the prefix expression

Sortest Path Algorithm
Given a graph and a source vertex in graph, find shortest paths from source to all vertices in the given graph.
Dijkstra’s algorithm is very similar to Prim’s algorithm for minimum spanning tree. Like Prim’s MST, we generate a SPT (shortest path tree) with given source as root. We maintain two sets, one set contains vertices included in shortest path tree, other set includes vertices not yet included in shortest path tree. At every step of the algorithm, we find a vertex which is in the other set (set of not yet included) and has minimum distance from source.
Below are the detailed steps used in Dijkstra’s algorithm to find the shortest path from a single source vertex to all other vertices in the given graph.
Algorithm
1) Create a set sptSet (shortest path tree set) that keeps track of vertices included in shortest path tree, i.e., whose minimum distance from source is calculated and finalized. Initially, this set is empty.
2) Assign a distance value to all vertices in the input graph. Initialize all distance values as INFINITE. Assign distance value as 0 for the source vertex so that it is picked first.
3) While sptSet doesn’t include all vertices
….a) Pick a vertex u which is not there in sptSetand has minimum distance value.
….b) Include u to sptSet.
….c) Update distance value of all adjacent vertices of u. To update the distance values, iterate through all adjacent vertices. For every adjacent vertex v, if sum of distance value of u (from source) and weight of edge u-v, is less than the distance value of v, then update the distance value of v.
Let us understand with the following example:
The set sptSetis initially empty and distances assigned to vertices are {0, INF, INF, INF, INF, INF, INF, INF} where INF indicates infinite. Now pick the vertex with minimum distance value. The vertex 0 is picked, include it in sptSet. So sptSet becomes {0}. After including 0 to sptSet, update distance values of its adjacent vertices. Adjacent vertices of 0 are 1 and 7. The distance values of 1 and 7 are updated as 4 and 8. Following subgraph shows vertices and their distance values, only the vertices with finite distance values are shown. The vertices included in SPT are shown in green color.
Pick the vertex with minimum distance value and not already included in SPT (not in sptSET). The vertex 1 is picked and added to sptSet. So sptSet now becomes {0, 1}. Update the distance values of adjacent vertices of 1. The distance value of vertex 2 becomes 12.
Pick the vertex with minimum distance value and not already included in SPT (not in sptSET). Vertex 7 is picked. So sptSet now becomes {0, 1, 7}. Update the distance values of adjacent vertices of 7. The distance value of vertex 6 and 8 becomes finite (15 and 9 respectively).
Pick the vertex with minimum distance value and not already included in SPT (not in sptSET). Vertex 6 is picked. So sptSet now becomes {0, 1, 7, 6}. Update the distance values of adjacent vertices of 6. The distance value of vertex 5 and 8 are updated.
We repeat the above steps until sptSet doesn’t include all vertices of given graph. Finally, we get the following Shortest Path Tree (SPT).

C PROGRAM FOR SORTEST PATH ALGORITHM

 #include<stdio.h>
 #define infinity 1000 
 int minreturn(int a[],int n,int include[])
 {
  int i,min=infinity,temp=infinity;
  for(i=0;i<n;i++)
  {
  if(temp>a[i] && include[i]==0)
  {
  min=i;
  temp=a[i];
  }
  }printf("\nmin is %d   $ ",min);
  return min;
}
void pathprint(int pred[],int s)
{
if(s==0)
{
printf("%d - > ",s);
return;
}
   pathprint(pred,pred[s]);
      printf("%d - > ",s);
     
 }
 int main() 
 { 
     int n,cur,s;
     printf("Enter matrix size : ");
     scanf("%d",&n);
     int a[n][n],include[n],i,j,temp,pred[n],dist[n];
     for(i=0;i<n;i++)
     for(j=0;j<n;j++)
     {
  scanf("%d",&a[i][j]);
  dist[i]=infinity;
  include[i]=0;
     }
     dist[0]=0;
     while(1)
     {
      cur=minreturn(dist,n,include);
      if(cur==infinity)
      break;
      include[cur]=1;
      for(j=0;j<n;j++)
      if(a[cur][j]!=0 && dist[cur]+a[cur][j]<dist[j] && include[j]==0 && cur!=j)
      {
      dist[j]=a[cur][j]+dist[cur];
      pred[j]=cur;
      }
     }
     printf("\nvertex\tpredecessor");
     for(j=0;j<n;j++)
     {
  printf("\n%d\t\t%d",j+1,pred[j]);
     }
     printf("\n");
     printf("vertex\tsortest distance");
      for(j=0;j<n;j++)
     {
  printf("\n%d\t\t%d",j+1,dist[j]);
     }
     printf("\nEnter the vertex : ");
     scanf("%d",&s);
     pathprint(pred,s);
}




No comments:

Post a Comment