-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
143 lines (135 loc) · 3.53 KB
/
Dijkstra.cpp
File metadata and controls
143 lines (135 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include<iostream>
#include<queue>
#include<malloc.h>
#include<climits>
using namespace std;
struct Graph
{
int V;
int E;
int **Adj;
};
struct Graph *createMatrix();
int getMinDistance(int *,bool *,int);
void dijsktra(struct Graph *,int);
int main()
{
struct Graph *G=createMatrix();
dijsktra(G,0);
return 0;
}
int getMinDistance(int *distance,bool *visited,int vertices)
{
int min=-1;
for(int i=0;i<vertices;i++)
{
if(!visited[i] && (min==-1 || distance[i]<distance[min]))
{
min=i;
}
}
return min;
}//function ends here....
void dijsktra(struct Graph *G,int source)
{
int * distance=(int *) malloc (G->V * sizeof(int));
bool * visited=(bool *) malloc (G->V * sizeof(bool));
//Set initial values in array's
for(int i=0;i<G->V;i++)
{
distance[i]=INT_MAX;
visited[i]=false;
}
distance[source]=0;
for(int i=0;i<G->V;i++)
{
int min=getMinDistance(distance,visited,G->V);
visited[min]=true;
//Get the neighbour's
for(int j=0;j<G->V;j++)
{
if(G->Adj[min][j]!=0 && !visited[j])
{
//This will calculate the min distance from minVertex to its neighbour;
//In short - we are figuring out min. distance to reach to minVertex's neightbour;
int dist=G->Adj[min][j]+distance[min];
if(dist<distance[j])
{
distance[j]=dist;
}
}
}
}
cout<<"-----------Displaying result !--------------"<<endl;
cout<<"\tVertex\t\tDistance"<<endl;
for(int i=0;i<G->V;i++)
{
char str=i+65;
cout<<"\t"<<str<<"\t\t"<<distance[i]<<endl;
}
}//function ends here....
struct Graph * createMatrix()
{
int vertices,edges;
int i;
struct Graph *G=(struct Graph *) malloc (sizeof(struct Graph));
if(!G) return NULL;
cout<<"Enter the no. of vertices & edges: ";
cin>>vertices>>edges;
G->Adj=(int **) malloc (vertices * sizeof(int *));
G->V=vertices;
G->E=edges;
for(i=0;i<vertices;i++)
{
G->Adj[i]=(int *) malloc (vertices * sizeof(int));
}
for(i=0;i<vertices;i++)
{
for(int j=0;j<vertices;j++)
{
G->Adj[i][j]=0;
}
}
cout<<"Reading edge data....."<<endl;
int u,v,x;
for(i=0;i<edges;i++)
{
cin>>u>>v>>x;
G->Adj[u][v]=x;
}
cout<<"Printing graph matrix....."<<endl;
for(i=0;i<vertices;i++)
{
cout<<i<<" -> ";
for(int j=0;j<vertices;j++)
{
cout<<G->Adj[i][j]<<" ";
}
cout<<endl;
}
return G;
}//function ends here....
----------------
Steps :-
Steps :-
1. Code for creating graph & adjacency matrix.
2. Process for dijkstra starts -
- We need two array's of size - no. of vertices i.e - {visited[vertices] , distance[vertices]}
- Initially, put values in both the array's i.e - visited will contain all fields as false
And distance will contain all fields as INT_MAX;
- Traverse the adjacent vertices of source vertex and calculate 'minDistance' to select a vertex.
- To do so , we need a function - getMinDistance() which will calculate min. distance...
- Take a variable min=-1.
- traverse all the vertices
- in every cycle - check if i'th element is not visited and has distance[i]<distance[min] || min==-1.
- if the condition fulfills then update the min. with i'th.
- after the completion , return the 'min'.
- set min vertex as visited.
- And start checking out minVertex's neighbour vertices.
- On each cycle - Check if the minVertex's neighbour vertex has an edge from source vertex.
- and check also it is not visited.
- If so, then calculate 'dist' by adding source to minVertex distance i.e -> min + minVertex's neighbour distance
i.e distance[min];
- Check if that dist. is less than distance[j];
- if so then update the distance of j'th vertex;
3. Algorithm finished....