-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
209 lines (129 loc) · 5.09 KB
/
Program.cs
File metadata and controls
209 lines (129 loc) · 5.09 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
public class Graph
{
private Dictionary<string, List<(string to, double Distance)>> adjacencyList = new();
//List<(string to, double Distance) this is tuble
private void Add_Node(string place)
{
if(!adjacencyList.ContainsKey(place))
{
adjacencyList[place] = new List<(string to, double Distance)>();
}
// adjacencyList[place] the key of node
//new List<(string to, double Distance)>(); the value of the key
}
public void Add_Edge(string from , string to, double Distance)
{
Add_Node(from);
adjacencyList[from].Add((to, Distance));
}
public Dictionary<string, List<(string to, double Distance)>> GetadjacencyList()
{
return adjacencyList;
}
// abliy the encabsulation
};
public static class GraphLoader
{
public static Graph LoeadEdgsFromDatabase()
{
Graph graph = new Graph();
string connectionString = "Data Source=DESKTOP-JVK01UB;Initial Catalog=DBmaps;Integrated Security=True;";
using SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
string query = "select [FromGovernorate], [ToGovernorate] ,[DistanceKm] from [DirectGovernorateLinks]";
using SqlCommand command = new SqlCommand(query, connection);
using SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string from = reader.GetString(0);
string to = reader.GetString(1);
double distance = reader.GetDouble(2);
graph.Add_Edge(from, to, distance);
}
return graph;
}
};
public static class Dijkstra
{
public static (Dictionary<string, double> distance, Dictionary<string, string> previous) ComputeShortestPaths(Graph graph, string start)
{
var distance = new Dictionary<string, double>();
var previous = new Dictionary<string, string>();
var visited = new HashSet<string>();
var pirority = new PriorityQueue<string, double>();
foreach(var node in graph.GetadjacencyList().Keys)
{
distance[node] = double.PositiveInfinity;
previous[node] = null;
}
distance[start] = 0;
pirority.Enqueue(start, 0);
//this is handle the neighbors in the one node and get the piroity
while (pirority.Count > 0)
{
var currentNode = pirority.Dequeue();
if (visited.Contains(currentNode))
continue;
visited.Add(currentNode);
if(!graph.GetadjacencyList().ContainsKey(currentNode))
{
continue;
}
foreach(var (neighbor, distantofneighbor) in graph.GetadjacencyList()[currentNode])
{
double newdistanse = distance[currentNode] + distantofneighbor;
if(newdistanse < distance.GetValueOrDefault(neighbor,double.PositiveInfinity))//comber the newdistanc in the new path & the dist in the neighbor
{
distance[neighbor] = newdistanse;
previous[neighbor] = currentNode;//like this
//"Aswan": "Luxor",
//"Luxor": "Qena",
//"Qena": "Sohag",
//"Sohag": "Cairo";
pirority.Enqueue(neighbor, newdistanse);
}
}
}
return (distance, previous);
}
public static List<string> ReconstructPath(Dictionary<string, string> preivus, string end, string start)
{
string current = end;
List<string> path = new List<string>();
while (current != null)
{
path.Insert(0, current);
current = preivus[current];
}
if(path.Count == 0 || path[0] != start)
{
return new List<string>();
}
return path;
}
};
class Program
{
static void Main(string[] args)
{
Graph graph = new Graph();
graph = GraphLoader.LoeadEdgsFromDatabase();
string start = "Cairo";
string end = "Aswan";
//Tuple Deconstruction
var (distance,prevuse) = Dijkstra.ComputeShortestPaths(graph, start);
if(distance.ContainsKey(end))
{
Console.WriteLine($"the shortesh path from {start} to {end} = {distance[end]} km");
List<string> path = Dijkstra.ReconstructPath(prevuse,end,start);
Console.WriteLine($"the path from {start} to {end} is {string.Join("->", path)}");
}
else
{
Console.WriteLine($"No path found from {start} to {end}..");
}
}
}