-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdjikstra.cpp
More file actions
109 lines (99 loc) · 1.92 KB
/
djikstra.cpp
File metadata and controls
109 lines (99 loc) · 1.92 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
#include<iostream>
using namespace std ;
class c{
private:
int n ; //the number of computers ;
int d[10][10] ; //the distance array
int check[10] ; // to check activity
int dst[10] ; //to check current distance
int ans[10][10] ; // the final answer
int source ;
int e ; //edge
public:
void setter() ; //to take in the input
void mi() ; // the overall ittreation
void reset() ; //resetting the check marker
void dj() ; // the algorithm's magic
int minimum() ; //finding the minimum unmarked node
void display() ; // the view the final result
} ;
void c::setter(){
cout<<"Enter the number of computers\n" ;
cin>>n ;
cout<<"Enter the distance between respective edges\n" ;
for(int i=0 ; i<n ; i++){
for(int j=0 ; j<n ; j++){
if(i <j){
cout<<"Distance between "<<i<<" and "<<j<<" : " ;
cin>>d[i][j] ;
} else if(i>j){
d[i][j] = d[j][i] ;
}
}
}
e = (n*(n-1))/2 ;
}
void c::mi(){
for(int i=0 ; i<n ; i++){
source = i ;
reset() ;
check[source] = 1 ;
dst[i] = 0 ;
dj() ;
for(int i=0 ; i<n ; i++){
if(i != source){
ans[source][i] = dst[i] ;
} else {
ans[source][source] = 0 ;
}
}
}
}
void c::reset(){
for(int i=0 ; i<n ; i++){
check[i] = 0 ;
dst[i] = 999 ;
}
}
void c::dj(){
for(int j=0 ; j<n ; j++){
int u = minimum() ;
check[u] = 1 ;
for(int i=0 ; i<n ; i++){
if(check[i] == 0 && dst[i] > dst[u] + d[u][i]){
dst[i] = dst[u] + d[u][i] ;
}
}
}
}
int c::minimum(){
int v = -1 ;
int min = 999 ;
for(int i=0 ; i<n ; i++){
if(check[i] == 0 && dst[i] !=999 && dst[i]<=min){
min = dst[i] ;
v = i ;
}
}
if(v == -1){
v = source ;
}
return v ;
}
void c::display(){
for(int i=0 ; i<n ; i++){
cout<<"Taking the source as "<<(i)<<" :\n" ;
for(int j=0 ; j<n ; j++){
if(i != j){
cout<<"\t The distance to "<<(j)<<" is "<<ans[i][j]<<"\n" ;
}
}
}
}
int main(){
c apple ;
apple.setter() ;
apple.mi();
apple.display() ;
return 0 ;
}