-
Notifications
You must be signed in to change notification settings - Fork 53
/
graph.cpp
157 lines (124 loc) · 3.21 KB
/
graph.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include<iostream>
using namespace std;
int main()
{
int i,j;
int cost; //weight of edge
//defining number of vertices
int n;
cout<<"Enter the number of vertices you want in the graph ";
cin>>n;
//declaring an array that contain all the vertices
int vertices[n];
for(i=0;i<n;i++)
vertices[i]=i;
char cont;
int matrix[n][n]; //declaring adjacency matrix
//making the user to enter weight of the edges
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
cout<<"Does there exist any edge directed from vertix "<<vertices[i]<<" to "<<vertices[j]<<" Y/N?"<<endl;
cin>>cont;
if(cont=='Y'||cont=='y')
{
cout<<"Enter the weight of the edge ";
cin>>cost;
matrix[i][j]=cost;
}
else
{
matrix[i][j]=1000;
//here 1000 represents infinity i.e. the edges are not connected
}
}
else
{
matrix[i][j]=0;
}
}
}
//printing the adjacency matrix
cout<<endl<<"The adjacency matrix for the graph is: "<<endl;
for(i=0;i<n;i++)
{
cout<<endl;
for(j=0;j<n;j++)
{
cout<<"\t"<<matrix[i][j];
}
}
cout<<endl<<endl<<"Note: The value 1000 represents that there might be no edge between the"<<endl
<<"vertices or distance between them is infinity"<<endl;
//asking the user to enter the source node
int source;
cout<<"Enter the source node ";
cin>>source;
//Declaring the array that will contain the traversed nodes
int S[n];
//declaring array distance which contains the minimum distance of each vertex from the source node
int distance[n];
for(i=0;i<n;i++)
{
if(i!=source)
distance[i]=1000;
distance[source]=0;
}
//declaring the array which contains the predecessors of each vertex in shortest path
int pred[n];
for(i=0;i<n;i++)
{
if(i!=source)
pred[i]=-1;
pred[source]=source;
}
//declaring an another array Q which contains the vertices' distances yet to be traversed
int Q[n];
for(i=0;i<n;i++)
Q[i]=distance[i];
int k=0;
int pos;
S[n-1]=n;
int num=n;
while(S[n-1]==n) //Terminating condition....the loop continues till all the nodes are traversed
{
int min=1000;
for(i=0;i<num;i++) // extracting the minimum value out of Q and the correspoinding vertex
{
if(Q[i]<min)
{
min=Q[i];
pos=i;
}
}
for(i=0;i<n;i++)
{
if(matrix[pos][i]!=1000)
{
if(distance[i]>distance[pos]+matrix[pos][i])
{
distance[i]=distance[pos]+matrix[pos][i];
Q[i]=distance[i];
pred[i]=pos;
}
}
}
S[k]=pos;
k++;
num=num-1;
//Deleting the traversed node's distance
for(i=pos;i<num;i++)
{
Q[i]=Q[i+1];
}
}
cout<<"Hence the solution found using DIJKSTRA Algorithm is :"<<endl<<"Vertices\tMin. Distance\tPredecessor"<<endl;
for(i=0;i<n;i++)
{
cout<<vertices[i]<<"\t\t"<<distance[i]<<"\t\t"<<pred[i]<<endl;
}
return 0;
}