Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include<stdbool.h>
- #define inf 9999
- int n;
- void diji( int x[n][n] ,int s , int des )
- {
- int a[n][n] , d[n], p[n];
- bool visited [n];
- int mind=0 , next =0 , i,j;
- for (i=0;i<n;i++)
- { d[i]=0;
- for (j=0;j<n;j++)
- {
- if (x[i][j]==0)
- {
- a[i][j]=inf;
- }else{a[i][j]=x[i][j];}}}
- for (i=0;i<n;i++)
- {
- d[i] = a[s][i];
- p[i]=s;
- visited[i]=false;
- }
- d[s]=0;
- visited[s]=true;
- int ned = 1;
- while (ned < n-1 )
- {mind = inf;
- for(i=0;i<n;i++)
- {if (d[i]<mind&&visited[i]!=true){
- mind = d[i];
- next=i;
- }
- }visited[next]=true;
- for(i=0;i<n;i++)
- {
- if (visited[i]==false)
- {
- if(mind+a[i][j]<d[i])
- {
- d[i]= mind +a[next][i];
- p[i]=next;
- }
- }
- }
- ned++;
- }
- for(i=0;i < n;i++){
- if(i== des)
- {
- printf("\n min distance from %d to %d = %d ",s, i, d[i]);
- printf("\nPath = %d", i);
- j=i;
- do
- {
- j=p[j];
- printf(" <- %d", j);
- }
- while(j!=s);
- }
- }
- }
- int main()
- {
- int i,j;
- printf("insert the number of ver :");
- scanf("%d",&n);
- int x[n][n];
- for (i=0;i<n;i++)
- {
- for (j=0;j<n;j++)
- {
- if(i==j)
- {
- x[i][j]=0;
- }else
- {
- printf("\n enter the weight of the edge from %d to %d \n ",i,j);
- scanf("%d",&x[i][j]);
- }
- }
- }
- for (i=0;i<n;i++)
- {
- for (j=0;j<n;j++)
- {
- printf("%d \t",x[i][j]);
- }
- printf("\n");
- }int s,des;
- printf("\n enter the starting node :\n");
- scanf("%d",&s);
- printf("\nenter the des node :\n");
- scanf("%d",&des);
- diji(x,s,des);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement