-
Notifications
You must be signed in to change notification settings - Fork 0
/
Finder.aspx.cs
203 lines (190 loc) · 7.33 KB
/
Finder.aspx.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
namespace DisconnectedExample
{
public partial class Finder : System.Web.UI.Page
{
private List<Pair>[] adjList;
private int[] parents;
private int[] costs;
private Comparator comp = new Comparator();
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["DBPath"].ConnectionString;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select max(DistrictId) as DistrictId from Districts";
cmd.Connection = conn;
SqlDataReader reader = cmd.ExecuteReader();
int max = 0;
while (reader.Read())
{
max = (int)reader["DistrictId"];
}
adjList = new List<Pair>[max + 5];
parents = new int[max + 5];
costs = new int[max + 5];
for (int i = 0; i < max + 5; i++)
costs[i] = int.MaxValue;
cmd.CommandText = "select Source, Destination, Cost from Routes";
reader.Close();
reader = cmd.ExecuteReader();
while (reader.Read())
{
int source, destination, cost;
source = (int)reader["Source"];
destination = (int)reader["Destination"];
cost = (int)reader["Cost"];
if (adjList[source] == null)
adjList[source] = new List<Pair>();
if (adjList[destination] == null)
adjList[destination] = new List<Pair>();
adjList[source].Add(new Pair(destination, cost));
adjList[destination].Add(new Pair(source, cost));
}
int s = (int)Session["Source"];
int d = (int)Session["Destination"];
int ans = UCS(s, d);
if (ans == 0)
{
Label1.Text = "No Path Found";
}
else
{
Stack<int> st = new Stack<int>();
int i = d;
while (i != 0)
{
//Response.Write("ok");
st.Push(i);
i = parents[i];
}
Response.Write("<center><span style='color:Blue;'><h1>Least Cost Path From " + GetDistrictName(s) + " To " + GetDistrictName(d));
Response.Write("</h1></span><hr/><table border='1'> <th>Source</th> <th>Destination</th> <th>Cost</th>");
while (st.Count != 1)
{
Response.Write("<tr><td>");
int rScource, rDestination;
rScource = st.Peek();
st.Pop();
rDestination = st.Peek();
Response.Write(GetDistrictName(rScource) + "</td><td>");
Response.Write(GetDistrictName(rDestination));
Response.Write("</td><td>");
Response.Write(FindCost(rScource, rDestination));
Response.Write("</td></tr>");
}
Response.Write("</table>");
Response.Write("<span style='color:Green;font-size:40px;'>Total Cost: " + ans + "</span>");
Response.Write("</table><hr></center>");
}
conn.Dispose();
}
public int FindCost(int u, int v)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["DBPath"].ConnectionString;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = string.Format("select min(Cost) as cost from Routes where Source = {0} and Destination = {1}", u, v);
//Response.Write(cmd.CommandText);
cmd.Connection = conn;
SqlDataReader reader = cmd.ExecuteReader();
int cost = 0;
while (reader.Read())
{
cost = Convert.ToInt32(reader["cost"]);
}
return cost;
}
public string GetDistrictName(int id)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["DBPath"].ConnectionString;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = string.Format("select DistrictName from Districts where DistrictId = {0}", id);
cmd.Connection = conn;
SqlDataReader reader = cmd.ExecuteReader();
string name = "";
while (reader.Read())
{
name = (string)reader["DistrictName"];
}
return name;
}
public int UCS(int u, int v)
{
//Response.Write(GetDistrictName(u) + " " + GetDistrictName(v));
ArrayList list = new ArrayList();
int node, cost, node2, cost2;
list.Add(new Node(u, 0, 0));
//costs[u] = 0;
while (list.Count != 0)
{
node = ((Node)list[0]).Id;
cost = ((Node)list[0]).Cost;
parents[node] = ((Node)list[0]).Parent;
//costs[node] = ((Node)list[0]).Cost;
if (node == v)
return cost;
list.RemoveAt(0);
//len = adjList[node].Count;
foreach (Pair temp in adjList[node])
{
node2 = temp.First;
cost2 = temp.Second;
//Response.Write("Node: " + GetDistrictName(node2) + " Cost: " + (cost + cost2) + " Parent: " + GetDistrictName(node) + "<br />");
//if (parents[node2] != node && costs[node2] > cost + cost2)
//{
if (parents[node] != node2)
{
list.Add(new Node(node2, cost + cost2, node));
list.Sort(comp);
}
//parents[node2] = node;
//costs[node2] = cost + cost2;
//}
}
}
return 0;
}
}
public struct Pair
{
public int First { get; set; }
public int Second { get; set; }
public Pair(int first, int second)
{
this.First = first;
this.Second = second;
}
}
public class Node
{
public int Id { get; set; }
public int Cost { get; set; }
public int Parent { get; set; }
public Node(int id, int cost, int parent)
{
this.Id = id;
this.Cost = cost;
this.Parent = parent;
}
}
public class Comparator : IComparer
{
public int Compare(object x, object y)
{
int val1 = ((Node)x).Cost;
int val2 = ((Node)y).Cost;
if (val1 >= val2)
return 1;
return 0;
}
}
}