[solved]how to search for via routes when there are no direc

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: how to search for via routes when there are no direct ro

08 Apr 2015, 09:01

@smorgasbord: can you get it from http://ahk.uk.to/?p=d93913? it is the first time I use this method.

Hubert
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: how to search for via routes when there are no direct ro

08 Apr 2015, 09:30

@hubert

hd.png <- your code result
Epic coding, awesome.

but i guess it finds the longest possible route, where as i need the shortest.

Thanks.

level 4 is the last step.

see both images.
Attachments
hd.PNG
req.PNG
req.PNG (10.89 KiB) Viewed 4790 times
John ... you working ?
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: how to search for via routes when there are no direct ro

08 Apr 2015, 09:37

but i guess it finds the longest possible route, where as i need the shortest.
this is a new condition :!:
my code finds the first route, according to the alpha ordering of the locations

Hubert
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: how to search for via routes when there are no direct ro

08 Apr 2015, 09:40

hd0202 wrote:
but i guess it finds the longest possible route, where as i need the shortest.
this is a new condition :!:
Sorry, completely utterly absolutely my fault.
:( :( :(
John ... you working ?
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: how to search for via routes when there are no direct ro

09 Apr 2015, 03:07

Here is my new solution (you also find it on http://ahk.uk.to/?p=c59434):

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

test := ""
list_of_from_places := ""
list_of_to_places := ""
routes := ""

gosub, set_var	; for better editing of source code

array_from_places := {}
array := strsplit(var, "`n")

loop, % array.maxindex()
{
	lines := array[A_index]
	array_ := strsplit(lines, "`t")	
	array_from_places[array_[2], array_[3]] := [array_[4], array_[5] = "" ? 0 : array_[5], array_[6]]
	list_of_from_places .= array_[2] . "|"
	list_of_to_places .= array_[3] . "|"
} 
stringtrimright, list_of_from_places, list_of_from_places, 1
stringtrimright, list_of_to_places, list_of_to_places, 1
sort, list_of_from_places, D| U
sort, list_of_to_places, D| U

Gui, Add, Text,, From:
Gui, Add, ComboBox, vFrom ym gNew, %list_of_from_places%  ; The ym option starts a new column of controls.
Gui, Add, Text, ym, To:
Gui, Add, ComboBox, vto ym gNew, %list_of_to_places% 
Gui, Add, Button, gButtonOK vNew xm w80, % test
Gui, Add, Text, xm+30 vResult w1000 h300
Gui, Show,, Simple Input Example
return  ; End of auto-execute section. The script is idle until the user does something.

New:
GuiControl,, New, start search
GuiControl,, Result, 
return

GuiClose:
ButtonOK:
Gui, Submit, nohide  ; Save the input from the user to each control's associated variable.
GuiControl, disable, New
GuiControl,, New, searching
routes =
search(from, to)
test := routes "`n`n"
stringtrimright, routes, routes, 1
cheap = 99999999
cheapest =
low = 999
lowest =
loop, parse, routes, `n
{
	total = 0
	test =
	r := strsplit(a_loopfield, ",")
	for k, v in r
	{
		if (k = r.maxindex())
			break
		from := v
		to := r[k + 1]
		hltc := array_from_places[from, to, 1]
		dltc := array_from_places[from, to, 2]
		afc := array_from_places[from, to , 3]
		total += hltc
		total += afc
		test .= from " " to " " hltc " " dltc " " afc "`tTotal: " total "`n"
	}
	if (total < cheap)
	{
		cheap := total
		cheapest := test
		if (r.maxindex() <= low)
		{
			low := r.maxindex()
			lowest := test
		}
	}
	if (r.maxindex() < low)
	{
		low := r.maxindex()
		lowest := test
	}
}
if !routes
	test = no connection found
else
{
	test := "cheapest:`n"cheapest "`n" "lowest:`n" lowest
}
GuiControl, enable, New
GuiControl,, New, start search
GuiControl,, Result, % test
return	

search(from, to, route := "")
{
	global array_from_places, used, result, found, routes
	if !from
		return
	route .= from ","
	for key, value in array_from_places[from]
	{
		if !key
			return
		if key in %route%
			continue
		if (key = to)
			routes .= route to "`n"
		search(key, to, route)
	}
return
}

set_var:
; separator = tab
var =
( C
; for testing purposes
0	a	b	1	1	1
0	a	c	1	1	1
0	a	g	20	0	20
0	b	c	2	2	2
0	b	g	8	8	8
0	c	d	3	3	3
0	c	g	1	1	1
0	d	e	4	4	4
0	e	f	5	5	5
0	f	g	6	6	6
;
1	Agartala	Guwahati	4831	10140	1850
2	Agartala	Kolkata	3891	8506	1850
3	Agartala	Silchar	4401		1850
4	Agatti	Bengaluru	10691		1850
5	Agatti	Chennai	10646		2150
6	Agatti	Kochi	8571		1850
7	Agatti	Kozhikode	7471		1850
8	Agra	Delhi	5681	15327	1850
9	Agra	Khajuraho	3891	8506	1850
10	Agra	Varanasi	4946	12951	1850
11	Agra	Mumbai	8571		2950
12	Ahmedabad	Chennai	7426	26186	2950
13	Ahmedabad	Delhi	6000	15502	2150
14	Ahmedabad	Hyderabad	7076	19532	2150
15	Ahmedabad	Mumbai	4351	13425	1850
16	Aizawl	Guwahati	5351		1850
17	Aizawl	Imphal	4466	7337	1850
18	Aizawl	Kolkata	4626	11108	1850
19	Allahabad	Delhi	6541		1850
20	Allahabad	Kanpur	4751		1850
21	Allahabad	Mumbai	9401		2950
22	Amritsar	Delhi	4366	12436	1850
23	Aurangabad	Delhi	8801	21747	2150
24	Aurangabad	Mumbai	4451	10133	1850
25	Bagdogra	Delhi	9716	21024	2950
26	Bagdogra	Guwahati	4536	8398	1850
27	Bagdogra	Kolkata	5436	13188	1850
28	Bengaluru	Chennai	4701	10691	1850
29	Bengaluru	Delhi	9901	28985	3650
30	Bengaluru	Goa	5151	13297	1850
31	Bengaluru	Hyderabad	5451	14823	1850
32	Bengaluru	Kochi	4501	10907	1850
33	Bengaluru	Kolkata	9701	30799	3650
34	Bengaluru	Mangalore	4936		1850
35	Bengaluru	Mumbai	6851	16356	2150
36	Bengaluru	Pune	6184	16506	1850
37	Bengaluru	Tirupati	5221		1850
38	Bengaluru	Trivandrum	5451	12473	1850
39	Bhopal	Delhi	4801	14856	1850
40	Bhopal	Indore	4281	8625	1850
41	Bhopal	Mumbai	5356	15560	1850
42	Bhubaneshwar	Chennai	8291	17520	2950
43	Bhubaneshwar	Delhi	9001	27424	2950
44	Bhubaneshwar	Kolkata	5406	11764	1850
45	Bhubaneshwar	Mumbai	9401	29518	2950
46	Bhubaneshwar	Port Blair	11216		2950
47	Bhuj	Mumbai	6541		1850
48	Chandigarh	Delhi	4301	10534	1850
49	Chandigarh	Mumbai	8836	26295	2950
50	Chennai	Coimbatore	4251	12166	1850
51	Chennai	Delhi	8456	28474	3650
52	Chennai	Goa	6311	16217	1850
53	Chennai	Hyderabad	4451	13732	1850
54	Chennai	Kochi	5251	14630	1850
55	Chennai	Kolkata	8566	27543	2950
56	Chennai	Madurai	4251	11801	1850
57	Chennai	Mumbai	8571	18623	2950
58	Chennai	Pune	7851	21017	2150
59	Chennai	Portblair	9411	24229	2950
60	Chennai	Trivandrum	5601	14637	1850
61	Chennai	Vishakhapatnam	4801	14673	1850
62	Coimbatore	Delhi	9751	32763	3650
63	Coimbatore	Kozhikode	4451	6739	1850
64	Coimbatore	Mumbai	8001	18331	2950
65	Dehradun	Delhi	5221	13980	1850
66	Dehli	Dharamsala	4821		1850
67	Delhi	Gaya	6851	19532	2150
68	Delhi	Goa	8821	25860	3650
69	Delhi	Guwahati	9811	24988	3650
70	Delhi	Gwalior	5051	9977	1850
71	Delhi	Hyderabad	8401	25748	2950
72	Delhi	Imphal	9081	27248	3650
73	Delhi	Indore	4851	15867	1850
74	Delhi	Jabalpur	6401		1850
75	Delhi	Jaipur	3551	8815	1850
76	Delhi	Jammu	4401	13181	1850
77	Delhi	Jodhpur	5706	14290	1850
78	Delhi	Kanpur	5701		1850
79	Delhi	Khajuraho	5651	15108	1850
80	Delhi	Kochi	12351	37731	3650
81	Delhi	Kolkata	9061	24251	2950
82	Delhi	Kozhikode	10051	32763	3650
83	Delhi	Kullu	6301		1850
84	Delhi	Leh	5501	15141	1850
85	Delhi	Lucknow	4821	12505	1850
86	Delhi	Ludhiana	4351		1850
87	Delhi	Mangalore	9901	29248	3650
88	Delhi	Mumbai	8951	22740	2950
89	Delhi	Pantnagar	4301		1850
90	Delhi	Nagpur	7171	17336	2150
91	Delhi	Pathankot	5101		1850
92	Delhi	Patna	7151	17265	2150
93	Delhi	Port Blair	21516		3650
94	Delhi	Pune	9401	28208	2950
95	Delhi	Raipur	7851	22112	2150
96	Delhi	Rajkot	9101		2150
97	Delhi	Ranchi	8811	20732	2950
98	Delhi	Srinagar	6201	13370	1850
99	Delhi	Surat	9101	20819	2150
100	Delhi	Tirupati	9016	23756	3650
101	Delhi	Trivandrum	12156	37731	3650
102	Delhi	Udaipur	5786	15382	1850
103	Delhi	Vadodra	7051	19853	2150
104	Delhi	Varanasi	5681	15327	1850
105	Delhi	Vijayawada	8566	26202	2950
106	Delhi	Vishakhapatnam	10401	30218	2950
107	Dibrugarh	Dimapur	3251	5106	1850
108	Dibrugarh	Guwahati	4801		1850
109	Dibrugarh	Kolkata	7401	14776	2150
110	Dibrugarh	Lilabari	4051		1850
111	Dimapur	Guwahati	4701		1850
112	Dimapur	Imphal	4401		1850
113	Dimapur	Kolkata	6101	13822	1850
114	Dimapur	Shillong	4101		1850
115	Gaya	Kolkata	4501	11744	1850
116	Gaya	Varanasi	4851	9518	1850
117	Goa	Kochi	5001	15159	1850
118	Goa	Hyderabad	5251	13657	1850
119	Goa	Mumbai	5321	11232	1850
120	Goa	Pune	4536	8874	1850
121	Goa	Srinagar	12351	38431	3650
122	Guwahati	Imphal	4901	9498	1850
123	Guwahati	Jorhat	3736		1850
124	Guwahati	Kolkata	5076	11465	1850
125	Guwahati	Lilabari	5151		1850
126	Guwahati	Silchar	5251		1850
127	Guwahati	Tezpur	4436		1850
128	Gwalior	Mumbai	8401	19551	2150
129	Hyderabad	Kolkata	9696	24985	2950
130	Hyderabad	Mumbai	5251	14980	1850
131	Hyderabad	Pune	5231	14265	1850
132	Hyderabad	Tirupati	4656	12571	1850
133	Hyderabad	Varanasi	8811	21806	2950
134	Hyderabad	Vijayawada	5051	10655	1850
135	Hyderabad	Vishakhapatnam	4946	12951	1850
136	Imphal	Kolkata	4281	11680	1850
137	Imphal	Silchar	4601		1850
138	Indore	Mumbai	4481	12637	1850
139	Jaipur	Mumbai	7851	18794	2150
140	Jammu	Leh	4886	9069	1850
141	Jammu	Srinagar	4403	6998	1850
142	Jamnagar	Mumbai	5181	12400	1850
143	Jodhpur	Mumbai	7686	18670	2150
144	Jodhpur	Udaipur	4231	8724	1850
145	Jorhat	Kolkata	4976		1850
146	Jorhat	Tezpur	4136		1850
147	Kanpur	Kolkata	7401		2150
148	Khajuraho	Varanasi	4936	12681	1850
149	Kochi	Kozhikode	3501	7283	1850
150	Kochi	Madurai	4301		1850
151	Kochi	Mumbai	8401	21634	2950
152	Kochi	Trivandrum	4301	8326	1850
153	Kolkata	Kochi	10051		3650
154	Kolkata	Lilabari	7800		2150
155	Kolkata	Mumbai	8486	23558	3650
156	Kolkata	Patna	5706		1850
157	Kolkata	Port Blair	11071	26781	2950
158	Kolkata	Ranchi	4536		1850
159	Kolkata	Shillong	5481		1850
160	Kolkata	Silchar	5001	11085	1850
161	Kolkata	Tezpur	5151		1850
162	Kozhikode	Chennai	5151		1850
163	Kozhikode	Kolkata	8456		3650
164	Kozhikode	Mumbai	9100	16513	2150
165	Kozhikode	Trivandrum	4391		1850
166	Kullu	Pathankot	4001		1850
167	Leh	Srinagar	4603	8283	1850
168	Lilabari	Tezpur	3881		1850
169	Lucknow	Mumbai	8051	24985	2950
170	Lucknow	Varanasi	4626	9607	1850
171	Ludhiana	Pathankot	4201		1850
172	Madurai	Mumbai	7851	23657	2950
173	Mangalore	Mumbai	6086	15761	1850
174	Mumbai	Nagpur	5001	15159	1850
175	Mumbai	Raipur	9500	20692	2150
176	Mumbai	Rajkot	5281	12473	1850
177	Mumbai	Ranchi	9751	22813	2950
178	Mumbai	Srinagar	9016	23756	3650
179	Mumbai	Trivandrum	10201	23901	2950
180	Mumbai	Udaipur	4786	15407	1850
181	Mumbai	Varanasi	9696	24511	2950
182	Mumbai	Vishakhapatnam	9101	24872	2950
183	Patna	Ranchi	4603		1850
184	PortBlair	Vishakhapatnam	9696	24511	2950
185	Raipur	Bhubaneshwar	4281	9977	1850
186	Raipur	Nagpur	5181	12660	1850
187	Raipur	Vishakhapatnam	4251	11606	1850
188	Shillong	Jorhat	4551		1850
189	Silchar	Tezpur	4101		1850
190	Tirupati	Vijayawada	5406		1850
191	Vishakhapatnam	Bhubaneshwar	5321	10518	1850
)
return

esc::ExitApp
Btw, are you located in India?

Hubert
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: how to search for via routes when there are no direct ro

09 Apr 2015, 03:20

@Hubert awesome! no words to thank you.
Yes.

you?

thanks again. will ask for more things if any in here. :) really really nice.

Can i get results instantly?
without pressing the button?
like in my script?
John ... you working ?
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: how to search for via routes when there are no direct ro

09 Apr 2015, 03:21

Tab separated Var lines, your code. :)

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

test := ""
list_of_from_places := ""
list_of_to_places := ""
routes := ""

gosub, set_var   ; for better editing of source code

array_from_places := {}
array := strsplit(var, "`n")

loop, % array.maxindex()
{
   lines := array[A_index]
   array_ := strsplit(lines, "`t")   
   array_from_places[array_[2], array_[3]] := [array_[4], array_[5] = "" ? 0 : array_[5], array_[6]]
   list_of_from_places .= array_[2] . "|"
   list_of_to_places .= array_[3] . "|"
} 
stringtrimright, list_of_from_places, list_of_from_places, 1
stringtrimright, list_of_to_places, list_of_to_places, 1
sort, list_of_from_places, D| U
sort, list_of_to_places, D| U

Gui, Add, Text,, From:
Gui, Add, ComboBox, vFrom ym gNew, %list_of_from_places%  ; The ym option starts a new column of controls.
Gui, Add, Text, ym, To:
Gui, Add, ComboBox, vto ym gNew, %list_of_to_places% 
Gui, Add, Button, gButtonOK vNew xm w80, % test
Gui, Add, Text, xm+30 vResult w1000 h300
Gui, Show,, Simple Input Example
return  ; End of auto-execute section. The script is idle until the user does something.

New:
GuiControl,, New, start search
GuiControl,, Result, 
return

GuiClose:
ButtonOK:
Gui, Submit, nohide  ; Save the input from the user to each control's associated variable.
GuiControl, disable, New
GuiControl,, New, searching
routes =
search(from, to)
test := routes "`n`n"
stringtrimright, routes, routes, 1
cheap = 99999999
cheapest =
low = 999
lowest =
loop, parse, routes, `n
{
   total = 0
   test =
   r := strsplit(a_loopfield, ",")
   for k, v in r
   {
      if (k = r.maxindex())
         break
      from := v
      to := r[k + 1]
      hltc := array_from_places[from, to, 1]
      dltc := array_from_places[from, to, 2]
      afc := array_from_places[from, to , 3]
      total += hltc
      total += afc
      test .= from " " to " " hltc " " dltc " " afc "`tTotal: " total "`n"
   }
   if (total < cheap)
   {
      cheap := total
      cheapest := test
      if (r.maxindex() <= low)
      {
         low := r.maxindex()
         lowest := test
      }
   }
   if (r.maxindex() < low)
   {
      low := r.maxindex()
      lowest := test
   }
}
if !routes
   test = no connection found
else
{
   test := "cheapest:`n"cheapest "`n" "lowest:`n" lowest
}
GuiControl, enable, New
GuiControl,, New, start search
GuiControl,, Result, % test
return   

search(from, to, route := "")
{
   global array_from_places, used, result, found, routes
   if !from
      return
   route .= from ","
   for key, value in array_from_places[from]
   {
      if !key
         return
      if key in %route%
         continue
      if (key = to)
         routes .= route to "`n"
      search(key, to, route)
   }
return
}

set_var:
; separator = tab
var =
( 
1	Agartala	Guwahati	4831	10140	1850
2	Agartala	Kolkata	3891	8506	1850
3	Agartala	Silchar	4401		1850
4	Agatti	Bengaluru	10691		1850
5	Agatti	Chennai	10646		2150
6	Agatti	Kochi	8571		1850
7	Agatti	Kozhikode	7471		1850
8	Agra	Delhi	5681	15327	1850
9	Agra	Khajuraho	3891	8506	1850
10	Agra	Varanasi	4946	12951	1850
11	Agra	Mumbai	8571		2950
12	Ahmedabad	Chennai	7426	26186	2950
13	Ahmedabad	Delhi	6000	15502	2150
14	Ahmedabad	Hyderabad	7076	19532	2150
15	Ahmedabad	Mumbai	4351	13425	1850
16	Aizawl	Guwahati	5351		1850
17	Aizawl	Imphal	4466	7337	1850
18	Aizawl	Kolkata	4626	11108	1850
19	Allahabad	Delhi	6541		1850
20	Allahabad	Kanpur	4751		1850
21	Allahabad	Mumbai	9401		2950
22	Amritsar	Delhi	4366	12436	1850
23	Aurangabad	Delhi	8801	21747	2150
24	Aurangabad	Mumbai	4451	10133	1850
25	Bagdogra	Delhi	9716	21024	2950
26	Bagdogra	Guwahati	4536	8398	1850
27	Bagdogra	Kolkata	5436	13188	1850
28	Bengaluru	Chennai	4701	10691	1850
29	Bengaluru	Delhi	9901	28985	3650
30	Bengaluru	Goa	5151	13297	1850
31	Bengaluru	Hyderabad	5451	14823	1850
32	Bengaluru	Kochi	4501	10907	1850
33	Bengaluru	Kolkata	9701	30799	3650
34	Bengaluru	Mangalore	4936		1850
35	Bengaluru	Mumbai	6851	16356	2150
36	Bengaluru	Pune	6184	16506	1850
37	Bengaluru	Tirupati	5221		1850
38	Bengaluru	Trivandrum	5451	12473	1850
39	Bhopal	Delhi	4801	14856	1850
40	Bhopal	Indore	4281	8625	1850
41	Bhopal	Mumbai	5356	15560	1850
42	Bhubaneshwar	Chennai	8291	17520	2950
43	Bhubaneshwar	Delhi	9001	27424	2950
44	Bhubaneshwar	Kolkata	5406	11764	1850
45	Bhubaneshwar	Mumbai	9401	29518	2950
46	Bhubaneshwar	Port Blair	11216		2950
47	Bhuj	Mumbai	6541		1850
48	Chandigarh	Delhi	4301	10534	1850
49	Chandigarh	Mumbai	8836	26295	2950
50	Chennai	Coimbatore	4251	12166	1850
51	Chennai	Delhi	8456	28474	3650
52	Chennai	Goa	6311	16217	1850
53	Chennai	Hyderabad	4451	13732	1850
54	Chennai	Kochi	5251	14630	1850
55	Chennai	Kolkata	8566	27543	2950
56	Chennai	Madurai	4251	11801	1850
57	Chennai	Mumbai	8571	18623	2950
58	Chennai	Pune	7851	21017	2150
59	Chennai	Portblair	9411	24229	2950
60	Chennai	Trivandrum	5601	14637	1850
61	Chennai	Vishakhapatnam	4801	14673	1850
62	Coimbatore	Delhi	9751	32763	3650
63	Coimbatore	Kozhikode	4451	6739	1850
64	Coimbatore	Mumbai	8001	18331	2950
65	Dehradun	Delhi	5221	13980	1850
66	Dehli	Dharamsala	4821		1850
67	Delhi	Gaya	6851	19532	2150
68	Delhi	Goa	8821	25860	3650
69	Delhi	Guwahati	9811	24988	3650
70	Delhi	Gwalior	5051	9977	1850
71	Delhi	Hyderabad	8401	25748	2950
72	Delhi	Imphal	9081	27248	3650
73	Delhi	Indore	4851	15867	1850
74	Delhi	Jabalpur	6401		1850
75	Delhi	Jaipur	3551	8815	1850
76	Delhi	Jammu	4401	13181	1850
77	Delhi	Jodhpur	5706	14290	1850
78	Delhi	Kanpur	5701		1850
79	Delhi	Khajuraho	5651	15108	1850
80	Delhi	Kochi	12351	37731	3650
81	Delhi	Kolkata	9061	24251	2950
82	Delhi	Kozhikode	10051	32763	3650
83	Delhi	Kullu	6301		1850
84	Delhi	Leh	5501	15141	1850
85	Delhi	Lucknow	4821	12505	1850
86	Delhi	Ludhiana	4351		1850
87	Delhi	Mangalore	9901	29248	3650
88	Delhi	Mumbai	8951	22740	2950
89	Delhi	Pantnagar	4301		1850
90	Delhi	Nagpur	7171	17336	2150
91	Delhi	Pathankot	5101		1850
92	Delhi	Patna	7151	17265	2150
93	Delhi	Port Blair	21516		3650
94	Delhi	Pune	9401	28208	2950
95	Delhi	Raipur	7851	22112	2150
96	Delhi	Rajkot	9101		2150
97	Delhi	Ranchi	8811	20732	2950
98	Delhi	Srinagar	6201	13370	1850
99	Delhi	Surat	9101	20819	2150
100	Delhi	Tirupati	9016	23756	3650
101	Delhi	Trivandrum	12156	37731	3650
102	Delhi	Udaipur	5786	15382	1850
103	Delhi	Vadodra	7051	19853	2150
104	Delhi	Varanasi	5681	15327	1850
105	Delhi	Vijayawada	8566	26202	2950
106	Delhi	Vishakhapatnam	10401	30218	2950
107	Dibrugarh	Dimapur	3251	5106	1850
108	Dibrugarh	Guwahati	4801		1850
109	Dibrugarh	Kolkata	7401	14776	2150
110	Dibrugarh	Lilabari	4051		1850
111	Dimapur	Guwahati	4701		1850
112	Dimapur	Imphal	4401		1850
113	Dimapur	Kolkata	6101	13822	1850
114	Dimapur	Shillong	4101		1850
115	Gaya	Kolkata	4501	11744	1850
116	Gaya	Varanasi	4851	9518	1850
117	Goa	Kochi	5001	15159	1850
118	Goa	Hyderabad	5251	13657	1850
119	Goa	Mumbai	5321	11232	1850
120	Goa	Pune	4536	8874	1850
121	Goa	Srinagar	12351	38431	3650
122	Guwahati	Imphal	4901	9498	1850
123	Guwahati	Jorhat	3736		1850
124	Guwahati	Kolkata	5076	11465	1850
125	Guwahati	Lilabari	5151		1850
126	Guwahati	Silchar	5251		1850
127	Guwahati	Tezpur	4436		1850
128	Gwalior	Mumbai	8401	19551	2150
129	Hyderabad	Kolkata	9696	24985	2950
130	Hyderabad	Mumbai	5251	14980	1850
131	Hyderabad	Pune	5231	14265	1850
132	Hyderabad	Tirupati	4656	12571	1850
133	Hyderabad	Varanasi	8811	21806	2950
134	Hyderabad	Vijayawada	5051	10655	1850
135	Hyderabad	Vishakhapatnam	4946	12951	1850
136	Imphal	Kolkata	4281	11680	1850
137	Imphal	Silchar	4601		1850
138	Indore	Mumbai	4481	12637	1850
139	Jaipur	Mumbai	7851	18794	2150
140	Jammu	Leh	4886	9069	1850
141	Jammu	Srinagar	4403	6998	1850
142	Jamnagar	Mumbai	5181	12400	1850
143	Jodhpur	Mumbai	7686	18670	2150
144	Jodhpur	Udaipur	4231	8724	1850
145	Jorhat	Kolkata	4976		1850
146	Jorhat	Tezpur	4136		1850
147	Kanpur	Kolkata	7401		2150
148	Khajuraho	Varanasi	4936	12681	1850
149	Kochi	Kozhikode	3501	7283	1850
150	Kochi	Madurai	4301		1850
151	Kochi	Mumbai	8401	21634	2950
152	Kochi	Trivandrum	4301	8326	1850
153	Kolkata	Kochi	10051		3650
154	Kolkata	Lilabari	7800		2150
155	Kolkata	Mumbai	8486	23558	3650
156	Kolkata	Patna	5706		1850
157	Kolkata	Port Blair	11071	26781	2950
158	Kolkata	Ranchi	4536		1850
159	Kolkata	Shillong	5481		1850
160	Kolkata	Silchar	5001	11085	1850
161	Kolkata	Tezpur	5151		1850
162	Kozhikode	Chennai	5151		1850
163	Kozhikode	Kolkata	8456		3650
164	Kozhikode	Mumbai	9100	16513	2150
165	Kozhikode	Trivandrum	4391		1850
166	Kullu	Pathankot	4001		1850
167	Leh	Srinagar	4603	8283	1850
168	Lilabari	Tezpur	3881		1850
169	Lucknow	Mumbai	8051	24985	2950
170	Lucknow	Varanasi	4626	9607	1850
171	Ludhiana	Pathankot	4201		1850
172	Madurai	Mumbai	7851	23657	2950
173	Mangalore	Mumbai	6086	15761	1850
174	Mumbai	Nagpur	5001	15159	1850
175	Mumbai	Raipur	9500	20692	2150
176	Mumbai	Rajkot	5281	12473	1850
177	Mumbai	Ranchi	9751	22813	2950
178	Mumbai	Srinagar	9016	23756	3650
179	Mumbai	Trivandrum	10201	23901	2950
180	Mumbai	Udaipur	4786	15407	1850
181	Mumbai	Varanasi	9696	24511	2950
182	Mumbai	Vishakhapatnam	9101	24872	2950
183	Patna	Ranchi	4603		1850
184	PortBlair	Vishakhapatnam	9696	24511	2950
185	Raipur	Bhubaneshwar	4281	9977	1850
186	Raipur	Nagpur	5181	12660	1850
187	Raipur	Vishakhapatnam	4251	11606	1850
188	Shillong	Jorhat	4551		1850
189	Silchar	Tezpur	4101		1850
190	Tirupati	Vijayawada	5406		1850
191	Vishakhapatnam	Bhubaneshwar	5321	10518	1850
)
return

esc::ExitApp
John ... you working ?
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: how to search for via routes when there are no direct ro

09 Apr 2015, 09:47

@smorgasbord: It's very simple --> replace this code block in your script:

Code: Select all

Gui, Add, Text,, From:
Gui, Add, ComboBox, vFrom ym gButtonOK, %list_of_from_places%
Gui, Add, Text, ym, To:
Gui, Add, ComboBox, vto ym gButtonOK, %list_of_to_places% 
;Gui, Add, Button, gButtonOK vNew xm w80, % test
Gui, Add, Text, xm+30 vResult w1000 h300
Gui, Show,, Simple Input Example
return  ; End of auto-execute section. The script is idle until the user does something.

;New:
;GuiControl,, New, start search
;GuiControl,, Result, 
;return

GuiClose:
ButtonOK:
Gui, Submit, nohide  ; Save the input from the user to each control's associated variable.
;GuiControl, disable, New
;GuiControl,, New, searching
guicontrol,, result, searching
routes =
Hubert
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: how to search for via routes when there are no direct ro

09 Apr 2015, 10:16

@Hubert
Epic!!

just remembered one thing. sorry it was not supposed to be there. dont know how i missed it.

The route for A to B would also work for B to A is it possible to do that as well?
the from to places could be reversed. eg Delhi to Bhubaneshwar is as good as Bhuwaneshawar to Delhi

Can the output show the reverse routes as well? that is if we enter one out of the above ( now the from and to list contains all the stations ) i.e Delhi to Bhubaneshwar ( or vice versa ) we see all the routes for both Delhi to Bhubaneshwar and vice versa ??
Edited : all the shortest/cheapest routes. like it already does.
thanks a ton again.
John ... you working ?
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: how to search for via routes when there are no direct ro

09 Apr 2015, 21:27

@Hubert
Added lines 15 to 30, variable changed form var to var_, but it goes kaput, the end goal is as sought in last post. thanks

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
var_R =
test := ""
list_of_from_places := ""
list_of_to_places := ""
routes := ""


array_from_places := {}
; Appending reverse routes
gosub, set_var
;------------------------------------------------------------------------------------------------------------------------------------------------------------
array_ := strsplit(var, ["`n"])


array := strsplit(var, ["`n", "`t"])

loop, % array.maxindex()//6
{
   m := (A_index-1 ) * 6 + 1
  var_R .= "`n" . array[m]+array_.maxindex() . "`t" . array[m+2]  . "`t" . array[m+1]  . "`t" . array[m+3]  . "`t" . array[m+4]  . "`t" . array[m+5]
}
;~ MsgBox % var_R
;~ FileAppend, % var, test.txt
;================
var_ := var . var_R
FileAppend, % var, test.txt
;-----------------------------------------------------------------------------------------------------------------------------------------------------------------

;~ gosub, set_var   ; for better editing of source code
array := strsplit(var_, "`n")

loop, % array.maxindex()
{
   lines := array[A_index]
   array_ := strsplit(lines, "`t")   
   array_from_places[array_[2], array_[3]] := [array_[4], array_[5] = "" ? 0 : array_[5], array_[6]]
   list_of_from_places .= array_[2] . "|"
   list_of_to_places .= array_[3] . "|"
} 
stringtrimright, list_of_from_places, list_of_from_places, 1

stringtrimright, list_of_to_places, list_of_to_places, 1
sort, list_of_from_places, D| U
MsgBox % list_of_from_places
sort, list_of_to_places, D| U
MsgBox % list_of_to_places

Gui, Add, Text,, From:
Gui, Add, ComboBox, vFrom ym gButtonOK, %list_of_from_places%
Gui, Add, Text, ym, To:
Gui, Add, ComboBox, vto ym gButtonOK, %list_of_to_places% 
;Gui, Add, Button, gButtonOK vNew xm w80, % test
Gui, Add, Text, xm+30 vResult w1000 h300
Gui, Show,, Simple Input Example
return  ; End of auto-execute section. The script is idle until the user does something.

;New:
;GuiControl,, New, start search
;GuiControl,, Result, 
;return

GuiClose:
ButtonOK:
Gui, Submit, nohide  ; Save the input from the user to each control's associated variable.
;GuiControl, disable, New
;GuiControl,, New, searching
guicontrol,, result, searching
routes =
search(from, to)
test := routes "`n`n"
stringtrimright, routes, routes, 1
cheap = 99999999
cheapest =
low = 999
lowest =
loop, parse, routes, `n
{
   total = 0
   test =
   r := strsplit(a_loopfield, ",")
   MsgBox % r
   for k, v in r
   {
      if (k = r.maxindex())
         break
      from := v
      to := r[k + 1]
      hltc := array_from_places[from, to, 1]
      dltc := array_from_places[from, to, 2]
      afc := array_from_places[from, to , 3]
      total += hltc
      total += afc
      test .= from " " to " " hltc " " dltc " " afc "`tTotal: " total "`n"
   }
   if (total < cheap)
   {
      cheap := total
      cheapest := test
      if (r.maxindex() <= low)
      {
         low := r.maxindex()
         lowest := test
      }
   }
   if (r.maxindex() < low)
   {
      low := r.maxindex()
      lowest := test
   }
}
if !routes
   test = no connection found
else
{
   test := "cheapest:`n"cheapest "`n" "lowest:`n" lowest
}
GuiControl, enable, New
GuiControl,, New, start search
GuiControl,, Result, % test
return   

search(from, to, route := "")
{
   global array_from_places, used, result, found, routes
   if !from
      return
   route .= from ","
   for key, value in array_from_places[from]
   {
      if !key
         return
      if key in %route%
         continue
      if (key = to)
         routes .= route to "`n"
      search(key, to, route)
   }
return
}

set_var:
; separator = tab
var =
( 
1	Agartala	Guwahati	4831	10140	1850
2	Agartala	Kolkata	3891	8506	1850
3	Agartala	Silchar	4401		1850
4	Agatti	Bengaluru	10691		1850
5	Agatti	Chennai	10646		2150
6	Agatti	Kochi	8571		1850
7	Agatti	Kozhikode	7471		1850
8	Agra	Delhi	5681	15327	1850
9	Agra	Khajuraho	3891	8506	1850
10	Agra	Varanasi	4946	12951	1850
11	Agra	Mumbai	8571		2950
12	Ahmedabad	Chennai	7426	26186	2950
13	Ahmedabad	Delhi	6000	15502	2150
14	Ahmedabad	Hyderabad	7076	19532	2150
15	Ahmedabad	Mumbai	4351	13425	1850
16	Aizawl	Guwahati	5351		1850
17	Aizawl	Imphal	4466	7337	1850
18	Aizawl	Kolkata	4626	11108	1850
19	Allahabad	Delhi	6541		1850
20	Allahabad	Kanpur	4751		1850
21	Allahabad	Mumbai	9401		2950
22	Amritsar	Delhi	4366	12436	1850
23	Aurangabad	Delhi	8801	21747	2150
24	Aurangabad	Mumbai	4451	10133	1850
25	Bagdogra	Delhi	9716	21024	2950
26	Bagdogra	Guwahati	4536	8398	1850
27	Bagdogra	Kolkata	5436	13188	1850
28	Bengaluru	Chennai	4701	10691	1850
29	Bengaluru	Delhi	9901	28985	3650
30	Bengaluru	Goa	5151	13297	1850
31	Bengaluru	Hyderabad	5451	14823	1850
32	Bengaluru	Kochi	4501	10907	1850
33	Bengaluru	Kolkata	9701	30799	3650
34	Bengaluru	Mangalore	4936		1850
35	Bengaluru	Mumbai	6851	16356	2150
36	Bengaluru	Pune	6184	16506	1850
37	Bengaluru	Tirupati	5221		1850
38	Bengaluru	Trivandrum	5451	12473	1850
39	Bhopal	Delhi	4801	14856	1850
40	Bhopal	Indore	4281	8625	1850
41	Bhopal	Mumbai	5356	15560	1850
42	Bhubaneshwar	Chennai	8291	17520	2950
43	Bhubaneshwar	Delhi	9001	27424	2950
44	Bhubaneshwar	Kolkata	5406	11764	1850
45	Bhubaneshwar	Mumbai	9401	29518	2950
46	Bhubaneshwar	Port Blair	11216		2950
47	Bhuj	Mumbai	6541		1850
48	Chandigarh	Delhi	4301	10534	1850
49	Chandigarh	Mumbai	8836	26295	2950
50	Chennai	Coimbatore	4251	12166	1850
51	Chennai	Delhi	8456	28474	3650
52	Chennai	Goa	6311	16217	1850
53	Chennai	Hyderabad	4451	13732	1850
54	Chennai	Kochi	5251	14630	1850
55	Chennai	Kolkata	8566	27543	2950
56	Chennai	Madurai	4251	11801	1850
57	Chennai	Mumbai	8571	18623	2950
58	Chennai	Pune	7851	21017	2150
59	Chennai	Portblair	9411	24229	2950
60	Chennai	Trivandrum	5601	14637	1850
61	Chennai	Vishakhapatnam	4801	14673	1850
62	Coimbatore	Delhi	9751	32763	3650
63	Coimbatore	Kozhikode	4451	6739	1850
64	Coimbatore	Mumbai	8001	18331	2950
65	Dehradun	Delhi	5221	13980	1850
66	Dehli	Dharamsala	4821		1850
67	Delhi	Gaya	6851	19532	2150
68	Delhi	Goa	8821	25860	3650
69	Delhi	Guwahati	9811	24988	3650
70	Delhi	Gwalior	5051	9977	1850
71	Delhi	Hyderabad	8401	25748	2950
72	Delhi	Imphal	9081	27248	3650
73	Delhi	Indore	4851	15867	1850
74	Delhi	Jabalpur	6401		1850
75	Delhi	Jaipur	3551	8815	1850
76	Delhi	Jammu	4401	13181	1850
77	Delhi	Jodhpur	5706	14290	1850
78	Delhi	Kanpur	5701		1850
79	Delhi	Khajuraho	5651	15108	1850
80	Delhi	Kochi	12351	37731	3650
81	Delhi	Kolkata	9061	24251	2950
82	Delhi	Kozhikode	10051	32763	3650
83	Delhi	Kullu	6301		1850
84	Delhi	Leh	5501	15141	1850
85	Delhi	Lucknow	4821	12505	1850
86	Delhi	Ludhiana	4351		1850
87	Delhi	Mangalore	9901	29248	3650
88	Delhi	Mumbai	8951	22740	2950
89	Delhi	Pantnagar	4301		1850
90	Delhi	Nagpur	7171	17336	2150
91	Delhi	Pathankot	5101		1850
92	Delhi	Patna	7151	17265	2150
93	Delhi	Port Blair	21516		3650
94	Delhi	Pune	9401	28208	2950
95	Delhi	Raipur	7851	22112	2150
96	Delhi	Rajkot	9101		2150
97	Delhi	Ranchi	8811	20732	2950
98	Delhi	Srinagar	6201	13370	1850
99	Delhi	Surat	9101	20819	2150
100	Delhi	Tirupati	9016	23756	3650
101	Delhi	Trivandrum	12156	37731	3650
102	Delhi	Udaipur	5786	15382	1850
103	Delhi	Vadodra	7051	19853	2150
104	Delhi	Varanasi	5681	15327	1850
105	Delhi	Vijayawada	8566	26202	2950
106	Delhi	Vishakhapatnam	10401	30218	2950
107	Dibrugarh	Dimapur	3251	5106	1850
108	Dibrugarh	Guwahati	4801		1850
109	Dibrugarh	Kolkata	7401	14776	2150
110	Dibrugarh	Lilabari	4051		1850
111	Dimapur	Guwahati	4701		1850
112	Dimapur	Imphal	4401		1850
113	Dimapur	Kolkata	6101	13822	1850
114	Dimapur	Shillong	4101		1850
115	Gaya	Kolkata	4501	11744	1850
116	Gaya	Varanasi	4851	9518	1850
117	Goa	Kochi	5001	15159	1850
118	Goa	Hyderabad	5251	13657	1850
119	Goa	Mumbai	5321	11232	1850
120	Goa	Pune	4536	8874	1850
121	Goa	Srinagar	12351	38431	3650
122	Guwahati	Imphal	4901	9498	1850
123	Guwahati	Jorhat	3736		1850
124	Guwahati	Kolkata	5076	11465	1850
125	Guwahati	Lilabari	5151		1850
126	Guwahati	Silchar	5251		1850
127	Guwahati	Tezpur	4436		1850
128	Gwalior	Mumbai	8401	19551	2150
129	Hyderabad	Kolkata	9696	24985	2950
130	Hyderabad	Mumbai	5251	14980	1850
131	Hyderabad	Pune	5231	14265	1850
132	Hyderabad	Tirupati	4656	12571	1850
133	Hyderabad	Varanasi	8811	21806	2950
134	Hyderabad	Vijayawada	5051	10655	1850
135	Hyderabad	Vishakhapatnam	4946	12951	1850
136	Imphal	Kolkata	4281	11680	1850
137	Imphal	Silchar	4601		1850
138	Indore	Mumbai	4481	12637	1850
139	Jaipur	Mumbai	7851	18794	2150
140	Jammu	Leh	4886	9069	1850
141	Jammu	Srinagar	4403	6998	1850
142	Jamnagar	Mumbai	5181	12400	1850
143	Jodhpur	Mumbai	7686	18670	2150
144	Jodhpur	Udaipur	4231	8724	1850
145	Jorhat	Kolkata	4976		1850
146	Jorhat	Tezpur	4136		1850
147	Kanpur	Kolkata	7401		2150
148	Khajuraho	Varanasi	4936	12681	1850
149	Kochi	Kozhikode	3501	7283	1850
150	Kochi	Madurai	4301		1850
151	Kochi	Mumbai	8401	21634	2950
152	Kochi	Trivandrum	4301	8326	1850
153	Kolkata	Kochi	10051		3650
154	Kolkata	Lilabari	7800		2150
155	Kolkata	Mumbai	8486	23558	3650
156	Kolkata	Patna	5706		1850
157	Kolkata	Port Blair	11071	26781	2950
158	Kolkata	Ranchi	4536		1850
159	Kolkata	Shillong	5481		1850
160	Kolkata	Silchar	5001	11085	1850
161	Kolkata	Tezpur	5151		1850
162	Kozhikode	Chennai	5151		1850
163	Kozhikode	Kolkata	8456		3650
164	Kozhikode	Mumbai	9100	16513	2150
165	Kozhikode	Trivandrum	4391		1850
166	Kullu	Pathankot	4001		1850
167	Leh	Srinagar	4603	8283	1850
168	Lilabari	Tezpur	3881		1850
169	Lucknow	Mumbai	8051	24985	2950
170	Lucknow	Varanasi	4626	9607	1850
171	Ludhiana	Pathankot	4201		1850
172	Madurai	Mumbai	7851	23657	2950
173	Mangalore	Mumbai	6086	15761	1850
174	Mumbai	Nagpur	5001	15159	1850
175	Mumbai	Raipur	9500	20692	2150
176	Mumbai	Rajkot	5281	12473	1850
177	Mumbai	Ranchi	9751	22813	2950
178	Mumbai	Srinagar	9016	23756	3650
179	Mumbai	Trivandrum	10201	23901	2950
180	Mumbai	Udaipur	4786	15407	1850
181	Mumbai	Varanasi	9696	24511	2950
182	Mumbai	Vishakhapatnam	9101	24872	2950
183	Patna	Ranchi	4603		1850
184	PortBlair	Vishakhapatnam	9696	24511	2950
185	Raipur	Bhubaneshwar	4281	9977	1850
186	Raipur	Nagpur	5181	12660	1850
187	Raipur	Vishakhapatnam	4251	11606	1850
188	Shillong	Jorhat	4551		1850
189	Silchar	Tezpur	4101		1850
190	Tirupati	Vijayawada	5406		1850
191	Vishakhapatnam	Bhubaneshwar	5321	10518	1850
)
return

esc::ExitApp
John ... you working ?
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: how to search for via routes when there are no direct ro

09 Apr 2015, 21:38

based on Dijkstra's algorithm
http://ahk.uk.to/?p=466676
Spoiler
Last edited by AlphaBravo on 09 Apr 2015, 22:58, edited 1 time in total.
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: how to search for via routes when there are no direct ro

09 Apr 2015, 22:23

@AlphaBravo

Code: Select all

data =
(
A   B   7
A   C   9
A   F   14
B   C   10
B   D   15
C   D   11
C   F   2
D   E   6
E   F   9
)
; --- mask following for simple data above
gosub, setup
loop, parse, data, `n, `r
    x := StrSplit(A_LoopField, "`t"), total := (x.4?x.4:0) + (x.5?x.5:0) + (x.6?x.6:0), res .= x.2 "`t" x.3 "`t" total "`n" 
data := RegExReplace( res, "`am)^\D+$\R")       ; remove lines with no connectios
; --- end mask

nodes:=[], Distance := []
for each, line in StrSplit(data, "`n" , "`r")
    field := StrSplit(line,"`t"), nodes[field.1] := 1, nodes[field.2] := 1
    , Distance[field.1,field.2] := field.3, Distance[field.2,field.1] := field.3

for node, v in nodes
    nodeList .= (nodeList?"|":"") node (A_Index=1?"|":"")

Gui, add, Text,, From:
Gui, add, Text, x200 yp, To:
Gui, add, DDL, xs vFrom gSubmit, % nodeList
Gui, add, DDL, x200 yp vTo gSubmit, % nodeList
Gui, add, Edit, xs w340 r10
Gui, show

Submit:
Gui, Submit, NoHide
res := Dijkstra(data, From)
Route := Done := "" , origin := to
while (!Done)
    for xTo, xFrom in res.2
    {
        if (xTo <> To)
            continue
        else if !(xFrom)
            done := true
        else
            route := xFrom "`t>`t" xTo "`t" Distance[xFrom , xTo] "`n" route,   To := xFrom
    }
GuiControl, , Edit1, % "From`t>`tTo`tLength`n" . route . "`nTotal distance = " res.1[origin]
return

esc::
GuiClose:
ExitApp
return

;-----------------------------------------------
Dijkstra(data, start){
    nodes := [], dist := [], Distance := [], dist := [], prev := [], Q := [], min := "x"
    for each, line in StrSplit(data, "`n" , "`r")
        field := StrSplit(line,"`t"), nodes[field.1] := 1, nodes[field.2] := 1
        , Distance[field.1,field.2] := field.3, Distance[field.2,field.1] := field.3
    dist[start] := 0, prev[start] := ""

    for node in nodes 
    {
        if (node <> start)
            dist[node] := "x"
            , prev[node] := ""
        Q[node] := 1
    }
    
    while % ObjCount(Q)
    {
        u := MinDist(Q, dist).2
        for node, val in Q
            if (node = u) 
            {
                q.Remove(node)
                break
            }
            
        for v, length in Distance[u] 
        {
            alt := dist[u] + length
            if (alt < dist[v])
                dist[v] := alt  
                , prev[v] := u
        }
    }   
    return [dist, prev]
}
;-----------------------------------------------
MinDist(Q, dist){
    for node , val in Q
        if A_Index=1
            min := dist[node], minNode := node
        else
            min := min < dist[node] ? min : dist[node]  , minNode := min < dist[node] ? minNode : node      
    return [min,minNode]
}
ObjCount(Obj){
    for key, val in Obj
        count := A_Index
    return count
}
;-----------------------------------------------

setup:
data =
(
1	Agartala	Guwahati	4831	10140	1850
2	Agartala	Kolkata	3891	8506	1850
3	Agartala	Silchar	4401		1850
4	Agatti	Bengaluru	10691		1850
5	Agatti	Chennai	10646		2150
6	Agatti	Kochi	8571		1850
7	Agatti	Kozhikode	7471		1850
8	Agra	Delhi	5681	15327	1850
9	Agra	Khajuraho	3891	8506	1850
10	Agra	Varanasi	4946	12951	1850
11	Agra	Mumbai	8571		2950
12	Ahmedabad	Chennai	7426	26186	2950
13	Ahmedabad	Delhi	6000	15502	2150
14	Ahmedabad	Hyderabad	7076	19532	2150
15	Ahmedabad	Mumbai	4351	13425	1850
16	Aizawl	Guwahati	5351		1850
17	Aizawl	Imphal	4466	7337	1850
18	Aizawl	Kolkata	4626	11108	1850
19	Allahabad	Delhi	6541		1850
20	Allahabad	Kanpur	4751		1850
21	Allahabad	Mumbai	9401		2950
22	Amritsar	Delhi	4366	12436	1850
23	Aurangabad	Delhi	8801	21747	2150
24	Aurangabad	Mumbai	4451	10133	1850
25	Bagdogra	Delhi	9716	21024	2950
26	Bagdogra	Guwahati	4536	8398	1850
27	Bagdogra	Kolkata	5436	13188	1850
28	Bengaluru	Chennai	4701	10691	1850
29	Bengaluru	Delhi	9901	28985	3650
30	Bengaluru	Goa	5151	13297	1850
31	Bengaluru	Hyderabad	5451	14823	1850
32	Bengaluru	Kochi	4501	10907	1850
33	Bengaluru	Kolkata	9701	30799	3650
34	Bengaluru	Mangalore	4936		1850
35	Bengaluru	Mumbai	6851	16356	2150
36	Bengaluru	Pune	6184	16506	1850
37	Bengaluru	Tirupati	5221		1850
38	Bengaluru	Trivandrum	5451	12473	1850
39	Bhopal	Delhi	4801	14856	1850
40	Bhopal	Indore	4281	8625	1850
41	Bhopal	Mumbai	5356	15560	1850
42	Bhubaneshwar	Chennai	8291	17520	2950
43	Bhubaneshwar	Delhi	9001	27424	2950
44	Bhubaneshwar	Kolkata	5406	11764	1850
45	Bhubaneshwar	Mumbai	9401	29518	2950
46	Bhubaneshwar	Port Blair	11216		2950
47	Bhuj	Mumbai	6541		1850
48	Chandigarh	Delhi	4301	10534	1850
49	Chandigarh	Mumbai	8836	26295	2950
50	Chennai	Coimbatore	4251	12166	1850
51	Chennai	Delhi	8456	28474	3650
52	Chennai	Goa	6311	16217	1850
53	Chennai	Hyderabad	4451	13732	1850
54	Chennai	Kochi	5251	14630	1850
55	Chennai	Kolkata	8566	27543	2950
56	Chennai	Madurai	4251	11801	1850
57	Chennai	Mumbai	8571	18623	2950
58	Chennai	Pune	7851	21017	2150
59	Chennai	Portblair	9411	24229	2950
60	Chennai	Trivandrum	5601	14637	1850
61	Chennai	Vishakhapatnam	4801	14673	1850
62	Coimbatore	Delhi	9751	32763	3650
63	Coimbatore	Kozhikode	4451	6739	1850
64	Coimbatore	Mumbai	8001	18331	2950
65	Dehradun	Delhi	5221	13980	1850
66	Dehli	Dharamsala	4821		1850
67	Delhi	Gaya	6851	19532	2150
68	Delhi	Goa	8821	25860	3650
69	Delhi	Guwahati	9811	24988	3650
70	Delhi	Gwalior	5051	9977	1850
71	Delhi	Hyderabad	8401	25748	2950
72	Delhi	Imphal	9081	27248	3650
73	Delhi	Indore	4851	15867	1850
74	Delhi	Jabalpur	6401		1850
75	Delhi	Jaipur	3551	8815	1850
76	Delhi	Jammu	4401	13181	1850
77	Delhi	Jodhpur	5706	14290	1850
78	Delhi	Kanpur	5701		1850
79	Delhi	Khajuraho	5651	15108	1850
80	Delhi	Kochi	12351	37731	3650
81	Delhi	Kolkata	9061	24251	2950
82	Delhi	Kozhikode	10051	32763	3650
83	Delhi	Kullu	6301		1850
84	Delhi	Leh	5501	15141	1850
85	Delhi	Lucknow	4821	12505	1850
86	Delhi	Ludhiana	4351		1850
87	Delhi	Mangalore	9901	29248	3650
88	Delhi	Mumbai	8951	22740	2950
89	Delhi	Pantnagar	4301		1850
90	Delhi	Nagpur	7171	17336	2150
91	Delhi	Pathankot	5101		1850
92	Delhi	Patna	7151	17265	2150
93	Delhi	Port Blair	21516		3650
94	Delhi	Pune	9401	28208	2950
95	Delhi	Raipur	7851	22112	2150
96	Delhi	Rajkot	9101		2150
97	Delhi	Ranchi	8811	20732	2950
98	Delhi	Srinagar	6201	13370	1850
99	Delhi	Surat	9101	20819	2150
100	Delhi	Tirupati	9016	23756	3650
101	Delhi	Trivandrum	12156	37731	3650
102	Delhi	Udaipur	5786	15382	1850
103	Delhi	Vadodra	7051	19853	2150
104	Delhi	Varanasi	5681	15327	1850
105	Delhi	Vijayawada	8566	26202	2950
106	Delhi	Vishakhapatnam	10401	30218	2950
107	Dibrugarh	Dimapur	3251	5106	1850
108	Dibrugarh	Guwahati	4801		1850
109	Dibrugarh	Kolkata	7401	14776	2150
110	Dibrugarh	Lilabari	4051		1850
111	Dimapur	Guwahati	4701		1850
112	Dimapur	Imphal	4401		1850
113	Dimapur	Kolkata	6101	13822	1850
114	Dimapur	Shillong	4101		1850
115	Gaya	Kolkata	4501	11744	1850
116	Gaya	Varanasi	4851	9518	1850
117	Goa	Kochi	5001	15159	1850
118	Goa	Hyderabad	5251	13657	1850
119	Goa	Mumbai	5321	11232	1850
120	Goa	Pune	4536	8874	1850
121	Goa	Srinagar	12351	38431	3650
122	Guwahati	Imphal	4901	9498	1850
123	Guwahati	Jorhat	3736		1850
124	Guwahati	Kolkata	5076	11465	1850
125	Guwahati	Lilabari	5151		1850
126	Guwahati	Silchar	5251		1850
127	Guwahati	Tezpur	4436		1850
128	Gwalior	Mumbai	8401	19551	2150
129	Hyderabad	Kolkata	9696	24985	2950
130	Hyderabad	Mumbai	5251	14980	1850
131	Hyderabad	Pune	5231	14265	1850
132	Hyderabad	Tirupati	4656	12571	1850
133	Hyderabad	Varanasi	8811	21806	2950
134	Hyderabad	Vijayawada	5051	10655	1850
135	Hyderabad	Vishakhapatnam	4946	12951	1850
136	Imphal	Kolkata	4281	11680	1850
137	Imphal	Silchar	4601		1850
138	Indore	Mumbai	4481	12637	1850
139	Jaipur	Mumbai	7851	18794	2150
140	Jammu	Leh	4886	9069	1850
141	Jammu	Srinagar	4403	6998	1850
142	Jamnagar	Mumbai	5181	12400	1850
143	Jodhpur	Mumbai	7686	18670	2150
144	Jodhpur	Udaipur	4231	8724	1850
145	Jorhat	Kolkata	4976		1850
146	Jorhat	Tezpur	4136		1850
147	Kanpur	Kolkata	7401		2150
148	Khajuraho	Varanasi	4936	12681	1850
149	Kochi	Kozhikode	3501	7283	1850
150	Kochi	Madurai	4301		1850
151	Kochi	Mumbai	8401	21634	2950
152	Kochi	Trivandrum	4301	8326	1850
153	Kolkata	Kochi	10051		3650
154	Kolkata	Lilabari	7800		2150
155	Kolkata	Mumbai	8486	23558	3650
156	Kolkata	Patna	5706		1850
157	Kolkata	Port Blair	11071	26781	2950
158	Kolkata	Ranchi	4536		1850
159	Kolkata	Shillong	5481		1850
160	Kolkata	Silchar	5001	11085	1850
161	Kolkata	Tezpur	5151		1850
162	Kozhikode	Chennai	5151		1850
163	Kozhikode	Kolkata	8456		3650
164	Kozhikode	Mumbai	9100	16513	2150
165	Kozhikode	Trivandrum	4391		1850
166	Kullu	Pathankot	4001		1850
167	Leh	Srinagar	4603	8283	1850
168	Lilabari	Tezpur	3881		1850
169	Lucknow	Mumbai	8051	24985	2950
170	Lucknow	Varanasi	4626	9607	1850
171	Ludhiana	Pathankot	4201		1850
172	Madurai	Mumbai	7851	23657	2950
173	Mangalore	Mumbai	6086	15761	1850
174	Mumbai	Nagpur	5001	15159	1850
175	Mumbai	Raipur	9500	20692	2150
176	Mumbai	Rajkot	5281	12473	1850
177	Mumbai	Ranchi	9751	22813	2950
178	Mumbai	Srinagar	9016	23756	3650
179	Mumbai	Trivandrum	10201	23901	2950
180	Mumbai	Udaipur	4786	15407	1850
181	Mumbai	Varanasi	9696	24511	2950
182	Mumbai	Vishakhapatnam	9101	24872	2950
183	Patna	Ranchi	4603		1850
184	PortBlair	Vishakhapatnam	9696	24511	2950
185	Raipur	Bhubaneshwar	4281	9977	1850
186	Raipur	Nagpur	5181	12660	1850
187	Raipur	Vishakhapatnam	4251	11606	1850
188	Shillong	Jorhat	4551		1850
189	Silchar	Tezpur	4101		1850
190	Tirupati	Vijayawada	5406		1850
191	Vishakhapatnam	Bhubaneshwar	5321	10518	1850
)
return
Agartala to Guwahati there is direct flight
so the same can be used for Guwahati Agartala


where as when Guwahati to Agartala is the input the route has guwahati to silchar to Agartala

can it show both ways ???
John ... you working ?
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: how to search for via routes when there are no direct ro

09 Apr 2015, 22:50

updated subroutine "submit"

Code: Select all

Submit:
Gui, Submit, NoHide
res := Dijkstra(data, From)
Route := Done := "" , origin := to

if Distance[From, To]		; direct flight
	route := "`nDirect Flight`n" From "`t>`t" To "`t" Distance[xFrom , xTo] "`n" 

while (!Done)
	for xTo, xFrom in res.2
	{
		if (xTo <> To)
			continue
		else if !(xFrom)
			done := true
		else
			route := xFrom "`t>`t" xTo "`t" Distance[xFrom , xTo] "`n" route,	To := xFrom
	}
GuiControl, , Edit1, % "From`t>`tTo`tLength`n" . route . "`nTotal distance = " res.1[origin]
return
would you explain the 3 digits in each line?, some lines don't have all three!

Edited to show both options!
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: how to search for via routes when there are no direct ro

09 Apr 2015, 22:56

First number := HLTC fares ( required )
Second number := DLTC fares ( not required ), sometimes not there as well. so i make it 0
third := Air Fuel charges ( required )

we need to Add First and Third, and that gives the Fare from A to B ( same as B to A )
John ... you working ?
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: how to search for via routes when there are no direct ro

09 Apr 2015, 22:58

I edited my original answer to remove the DLTC from total!
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: how to search for via routes when there are no direct ro

10 Apr 2015, 00:18

@AlphaBravo.
please edit this. also Route A to B fare as well as the route could be used for reverse route.

eg: Agartala Guwahati ------yy-------
means there is also an entry
Guwahti Agartala ------yy-------

thanks

Code: Select all

data =
(
A   B   7
A   C   9
A   F   14
B   C   10
B   D   15
C   D   11
C   F   2
D   E   6
E   F   9
)
; --- mask following for simple data above
gosub, setup
loop, parse, data, `n, `r
    x := StrSplit(A_LoopField, "`t"), total := (x.4?x.4:0) + (x.5?x.5:0) + (x.6?x.6:0), res .= x.2 "`t" x.3 "`t" total "`n" 
data := RegExReplace( res, "`am)^\D+$\R")       ; remove lines with no connectios
; --- end mask

nodes:=[], Distance := []
for each, line in StrSplit(data, "`n" , "`r")
    field := StrSplit(line,"`t"), nodes[field.1] := 1, nodes[field.2] := 1
    , Distance[field.1,field.2] := field.3, Distance[field.2,field.1] := field.3

for node, v in nodes
    nodeList .= (nodeList?"|":"") node (A_Index=1?"|":"")

Gui, add, Text,, From:
Gui, add, Text, x200 yp, To:
Gui, add, DDL, xs vFrom gSubmit, % nodeList
Gui, add, DDL, x200 yp vTo gSubmit, % nodeList
Gui, add, Edit, xs w340 r10
Gui, show

Submit:
Gui, Submit, NoHide
res := Dijkstra(data, From)
Route := Done := "" , origin := to

if Distance[From, To]       ; direct flight
{
    route := From "`t>`t" To "`t" Distance[xFrom , xTo] "`n" 
    GuiControl, , Edit1, % "From`t>`tTo`tLength`n" . route . "`nTotal distance = " res.1[origin]
    return
}

while (!Done)
    for xTo, xFrom in res.2
    {
        if (xTo <> To)
            continue
        else if !(xFrom)
            done := true
        else
            route := xFrom "`t>`t" xTo "`t" Distance[xFrom , xTo] "`n" route,   To := xFrom
    }
GuiControl, , Edit1, % "From`t>`tTo`tLength`n" . route . "`nTotal distance = " res.1[origin]
return

esc::
GuiClose:
ExitApp
return

;-----------------------------------------------
Dijkstra(data, start){
    nodes := [], dist := [], Distance := [], dist := [], prev := [], Q := [], min := "x"
    for each, line in StrSplit(data, "`n" , "`r")
        field := StrSplit(line,"`t"), nodes[field.1] := 1, nodes[field.2] := 1
        , Distance[field.1,field.2] := field.3, Distance[field.2,field.1] := field.3
    dist[start] := 0, prev[start] := ""

    for node in nodes 
    {
        if (node <> start)
            dist[node] := "x"
            , prev[node] := ""
        Q[node] := 1
    }
    
    while % ObjCount(Q)
    {
        u := MinDist(Q, dist).2
        for node, val in Q
            if (node = u) 
            {
                q.Remove(node)
                break
            }
            
        for v, length in Distance[u] 
        {
            alt := dist[u] + length
            if (alt < dist[v])
                dist[v] := alt  
                , prev[v] := u
        }
    }   
    return [dist, prev]
}
;-----------------------------------------------
MinDist(Q, dist){
    for node , val in Q
        if A_Index=1
            min := dist[node], minNode := node
        else
            min := min < dist[node] ? min : dist[node]  , minNode := min < dist[node] ? minNode : node      
    return [min,minNode]
}
ObjCount(Obj){
    for key, val in Obj
        count := A_Index
    return count
}
;-----------------------------------------------

setup:
data =
(
1	Agartala	Guwahati	4831	10140	1850
2	Agartala	Kolkata	3891	8506	1850
3	Agartala	Silchar	4401		1850
4	Agatti	Bengaluru	10691		1850
5	Agatti	Chennai	10646		2150
6	Agatti	Kochi	8571		1850
7	Agatti	Kozhikode	7471		1850
8	Agra	Delhi	5681	15327	1850
9	Agra	Khajuraho	3891	8506	1850
10	Agra	Varanasi	4946	12951	1850
11	Agra	Mumbai	8571		2950
12	Ahmedabad	Chennai	7426	26186	2950
13	Ahmedabad	Delhi	6000	15502	2150
14	Ahmedabad	Hyderabad	7076	19532	2150
15	Ahmedabad	Mumbai	4351	13425	1850
16	Aizawl	Guwahati	5351		1850
17	Aizawl	Imphal	4466	7337	1850
18	Aizawl	Kolkata	4626	11108	1850
19	Allahabad	Delhi	6541		1850
20	Allahabad	Kanpur	4751		1850
21	Allahabad	Mumbai	9401		2950
22	Amritsar	Delhi	4366	12436	1850
23	Aurangabad	Delhi	8801	21747	2150
24	Aurangabad	Mumbai	4451	10133	1850
25	Bagdogra	Delhi	9716	21024	2950
26	Bagdogra	Guwahati	4536	8398	1850
27	Bagdogra	Kolkata	5436	13188	1850
28	Bengaluru	Chennai	4701	10691	1850
29	Bengaluru	Delhi	9901	28985	3650
30	Bengaluru	Goa	5151	13297	1850
31	Bengaluru	Hyderabad	5451	14823	1850
32	Bengaluru	Kochi	4501	10907	1850
33	Bengaluru	Kolkata	9701	30799	3650
34	Bengaluru	Mangalore	4936		1850
35	Bengaluru	Mumbai	6851	16356	2150
36	Bengaluru	Pune	6184	16506	1850
37	Bengaluru	Tirupati	5221		1850
38	Bengaluru	Trivandrum	5451	12473	1850
39	Bhopal	Delhi	4801	14856	1850
40	Bhopal	Indore	4281	8625	1850
41	Bhopal	Mumbai	5356	15560	1850
42	Bhubaneshwar	Chennai	8291	17520	2950
43	Bhubaneshwar	Delhi	9001	27424	2950
44	Bhubaneshwar	Kolkata	5406	11764	1850
45	Bhubaneshwar	Mumbai	9401	29518	2950
46	Bhubaneshwar	Port Blair	11216		2950
47	Bhuj	Mumbai	6541		1850
48	Chandigarh	Delhi	4301	10534	1850
49	Chandigarh	Mumbai	8836	26295	2950
50	Chennai	Coimbatore	4251	12166	1850
51	Chennai	Delhi	8456	28474	3650
52	Chennai	Goa	6311	16217	1850
53	Chennai	Hyderabad	4451	13732	1850
54	Chennai	Kochi	5251	14630	1850
55	Chennai	Kolkata	8566	27543	2950
56	Chennai	Madurai	4251	11801	1850
57	Chennai	Mumbai	8571	18623	2950
58	Chennai	Pune	7851	21017	2150
59	Chennai	Portblair	9411	24229	2950
60	Chennai	Trivandrum	5601	14637	1850
61	Chennai	Vishakhapatnam	4801	14673	1850
62	Coimbatore	Delhi	9751	32763	3650
63	Coimbatore	Kozhikode	4451	6739	1850
64	Coimbatore	Mumbai	8001	18331	2950
65	Dehradun	Delhi	5221	13980	1850
66	Dehli	Dharamsala	4821		1850
67	Delhi	Gaya	6851	19532	2150
68	Delhi	Goa	8821	25860	3650
69	Delhi	Guwahati	9811	24988	3650
70	Delhi	Gwalior	5051	9977	1850
71	Delhi	Hyderabad	8401	25748	2950
72	Delhi	Imphal	9081	27248	3650
73	Delhi	Indore	4851	15867	1850
74	Delhi	Jabalpur	6401		1850
75	Delhi	Jaipur	3551	8815	1850
76	Delhi	Jammu	4401	13181	1850
77	Delhi	Jodhpur	5706	14290	1850
78	Delhi	Kanpur	5701		1850
79	Delhi	Khajuraho	5651	15108	1850
80	Delhi	Kochi	12351	37731	3650
81	Delhi	Kolkata	9061	24251	2950
82	Delhi	Kozhikode	10051	32763	3650
83	Delhi	Kullu	6301		1850
84	Delhi	Leh	5501	15141	1850
85	Delhi	Lucknow	4821	12505	1850
86	Delhi	Ludhiana	4351		1850
87	Delhi	Mangalore	9901	29248	3650
88	Delhi	Mumbai	8951	22740	2950
89	Delhi	Pantnagar	4301		1850
90	Delhi	Nagpur	7171	17336	2150
91	Delhi	Pathankot	5101		1850
92	Delhi	Patna	7151	17265	2150
93	Delhi	Port Blair	21516		3650
94	Delhi	Pune	9401	28208	2950
95	Delhi	Raipur	7851	22112	2150
96	Delhi	Rajkot	9101		2150
97	Delhi	Ranchi	8811	20732	2950
98	Delhi	Srinagar	6201	13370	1850
99	Delhi	Surat	9101	20819	2150
100	Delhi	Tirupati	9016	23756	3650
101	Delhi	Trivandrum	12156	37731	3650
102	Delhi	Udaipur	5786	15382	1850
103	Delhi	Vadodra	7051	19853	2150
104	Delhi	Varanasi	5681	15327	1850
105	Delhi	Vijayawada	8566	26202	2950
106	Delhi	Vishakhapatnam	10401	30218	2950
107	Dibrugarh	Dimapur	3251	5106	1850
108	Dibrugarh	Guwahati	4801		1850
109	Dibrugarh	Kolkata	7401	14776	2150
110	Dibrugarh	Lilabari	4051		1850
111	Dimapur	Guwahati	4701		1850
112	Dimapur	Imphal	4401		1850
113	Dimapur	Kolkata	6101	13822	1850
114	Dimapur	Shillong	4101		1850
115	Gaya	Kolkata	4501	11744	1850
116	Gaya	Varanasi	4851	9518	1850
117	Goa	Kochi	5001	15159	1850
118	Goa	Hyderabad	5251	13657	1850
119	Goa	Mumbai	5321	11232	1850
120	Goa	Pune	4536	8874	1850
121	Goa	Srinagar	12351	38431	3650
122	Guwahati	Imphal	4901	9498	1850
123	Guwahati	Jorhat	3736		1850
124	Guwahati	Kolkata	5076	11465	1850
125	Guwahati	Lilabari	5151		1850
126	Guwahati	Silchar	5251		1850
127	Guwahati	Tezpur	4436		1850
128	Gwalior	Mumbai	8401	19551	2150
129	Hyderabad	Kolkata	9696	24985	2950
130	Hyderabad	Mumbai	5251	14980	1850
131	Hyderabad	Pune	5231	14265	1850
132	Hyderabad	Tirupati	4656	12571	1850
133	Hyderabad	Varanasi	8811	21806	2950
134	Hyderabad	Vijayawada	5051	10655	1850
135	Hyderabad	Vishakhapatnam	4946	12951	1850
136	Imphal	Kolkata	4281	11680	1850
137	Imphal	Silchar	4601		1850
138	Indore	Mumbai	4481	12637	1850
139	Jaipur	Mumbai	7851	18794	2150
140	Jammu	Leh	4886	9069	1850
141	Jammu	Srinagar	4403	6998	1850
142	Jamnagar	Mumbai	5181	12400	1850
143	Jodhpur	Mumbai	7686	18670	2150
144	Jodhpur	Udaipur	4231	8724	1850
145	Jorhat	Kolkata	4976		1850
146	Jorhat	Tezpur	4136		1850
147	Kanpur	Kolkata	7401		2150
148	Khajuraho	Varanasi	4936	12681	1850
149	Kochi	Kozhikode	3501	7283	1850
150	Kochi	Madurai	4301		1850
151	Kochi	Mumbai	8401	21634	2950
152	Kochi	Trivandrum	4301	8326	1850
153	Kolkata	Kochi	10051		3650
154	Kolkata	Lilabari	7800		2150
155	Kolkata	Mumbai	8486	23558	3650
156	Kolkata	Patna	5706		1850
157	Kolkata	Port Blair	11071	26781	2950
158	Kolkata	Ranchi	4536		1850
159	Kolkata	Shillong	5481		1850
160	Kolkata	Silchar	5001	11085	1850
161	Kolkata	Tezpur	5151		1850
162	Kozhikode	Chennai	5151		1850
163	Kozhikode	Kolkata	8456		3650
164	Kozhikode	Mumbai	9100	16513	2150
165	Kozhikode	Trivandrum	4391		1850
166	Kullu	Pathankot	4001		1850
167	Leh	Srinagar	4603	8283	1850
168	Lilabari	Tezpur	3881		1850
169	Lucknow	Mumbai	8051	24985	2950
170	Lucknow	Varanasi	4626	9607	1850
171	Ludhiana	Pathankot	4201		1850
172	Madurai	Mumbai	7851	23657	2950
173	Mangalore	Mumbai	6086	15761	1850
174	Mumbai	Nagpur	5001	15159	1850
175	Mumbai	Raipur	9500	20692	2150
176	Mumbai	Rajkot	5281	12473	1850
177	Mumbai	Ranchi	9751	22813	2950
178	Mumbai	Srinagar	9016	23756	3650
179	Mumbai	Trivandrum	10201	23901	2950
180	Mumbai	Udaipur	4786	15407	1850
181	Mumbai	Varanasi	9696	24511	2950
182	Mumbai	Vishakhapatnam	9101	24872	2950
183	Patna	Ranchi	4603		1850
184	PortBlair	Vishakhapatnam	9696	24511	2950
185	Raipur	Bhubaneshwar	4281	9977	1850
186	Raipur	Nagpur	5181	12660	1850
187	Raipur	Vishakhapatnam	4251	11606	1850
188	Shillong	Jorhat	4551		1850
189	Silchar	Tezpur	4101		1850
190	Tirupati	Vijayawada	5406		1850
191	Vishakhapatnam	Bhubaneshwar	5321	10518	1850
)
return
John ... you working ?
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: how to search for via routes when there are no direct ro

10 Apr 2015, 00:34

smorgasbord wrote:@AlphaBravo.
please edit this. also Route A to B fare as well as the route could be used for reverse route.

eg: Agartala Guwahati ------yy-------
means there is also an entry
Guwahti Agartala ------yy-------

thanks
I am not sure I understand what you're asking!!
Guwahti to Agartala and vise versa worked fine.
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: how to search for via routes when there are no direct ro

10 Apr 2015, 01:02

@AlphaBravo
;=============================================
Edit: Dibrugarh to Kolkata must be

Dibrugarh Kolkata ( 7401147762150 ) = 7401+2150

whereas it shows something different.
;=============================================

awesome!
will do more testing.
:) so far works like a charm

Code: Select all

data =
(
A   B   7
A   C   9
A   F   14
B   C   10
B   D   15
C   D   11
C   F   2
D   E   6
E   F   9
)
; --- mask following for simple data above
gosub, setup
loop, parse, data, `n, `r
    x := StrSplit(A_LoopField, "`t"), total := (x.4?x.4:0) + (x.5?x.5:0) + (x.6?x.6:0), res .= x.2 "`t" x.3 "`t" total "`n" 
data := RegExReplace( res, "`am)^\D+$\R")       ; remove lines with no connectios
; --- end mask

nodes:=[], Distance := []
for each, line in StrSplit(data, "`n" , "`r")
    field := StrSplit(line,"`t"), nodes[field.1] := 1, nodes[field.2] := 1
    , Distance[field.1,field.2] := field.3, Distance[field.2,field.1] := field.3

for node, v in nodes
    nodeList .= (nodeList?"|":"") node (A_Index=1?"|":"")

Gui, add, Text,, From:
Gui, add, Text, x200 yp, To:
Gui, add, DDL, xs vFrom gSubmit, % nodeList
Gui, add, DDL, x200 yp vTo gSubmit, % nodeList
Gui, add, Edit, xs w340 r10
Gui, show

Submit:
Gui, Submit, NoHide
res := Dijkstra(data, From)
Route := Done := "" , origin := to

if Distance[From, To]       ; direct flight
{
    route := From "`t>`t" To "`t" Distance[xFrom , xTo] "`n" 
    GuiControl, , Edit1, % "From`t>`tTo`tLength`n" . route . "`nTotal distance = " res.1[origin]
    return
}

while (!Done)
    for xTo, xFrom in res.2
    {
        if (xTo <> To)
            continue
        else if !(xFrom)
            done := true
        else
            route := xFrom "`t>`t" xTo "`t" Distance[xFrom , xTo] "`n" route,   To := xFrom
    }
GuiControl, , Edit1, % "From`t>`tTo`tLength`n" . route . "`nTotal distance = " res.1[origin]
return

esc::
GuiClose:
ExitApp
return

;-----------------------------------------------
Dijkstra(data, start){
    nodes := [], dist := [], Distance := [], dist := [], prev := [], Q := [], min := "x"
    for each, line in StrSplit(data, "`n" , "`r")
        field := StrSplit(line,"`t"), nodes[field.1] := 1, nodes[field.2] := 1
        , Distance[field.1,field.2] := field.3, Distance[field.2,field.1] := field.3
    dist[start] := 0, prev[start] := ""

    for node in nodes 
    {
        if (node <> start)
            dist[node] := "x"
            , prev[node] := ""
        Q[node] := 1
    }
    
    while % ObjCount(Q)
    {
        u := MinDist(Q, dist).2
        for node, val in Q
            if (node = u) 
            {
                q.Remove(node)
                break
            }
            
        for v, length in Distance[u] 
        {
            alt := dist[u] + length
            if (alt < dist[v])
                dist[v] := alt  
                , prev[v] := u
        }
    }   
    return [dist, prev]
}
;-----------------------------------------------
MinDist(Q, dist){
    for node , val in Q
        if A_Index=1
            min := dist[node], minNode := node
        else
            min := min < dist[node] ? min : dist[node]  , minNode := min < dist[node] ? minNode : node      
    return [min,minNode]
}
ObjCount(Obj){
    for key, val in Obj
        count := A_Index
    return count
}
;-----------------------------------------------

setup:
data =
(
1	Agartala	Guwahati	4831	10140	1850
2	Agartala	Kolkata	3891	8506	1850
3	Agartala	Silchar	4401		1850
4	Agatti	Bengaluru	10691		1850
5	Agatti	Chennai	10646		2150
6	Agatti	Kochi	8571		1850
7	Agatti	Kozhikode	7471		1850
8	Agra	Delhi	5681	15327	1850
9	Agra	Khajuraho	3891	8506	1850
10	Agra	Varanasi	4946	12951	1850
11	Agra	Mumbai	8571		2950
12	Ahmedabad	Chennai	7426	26186	2950
13	Ahmedabad	Delhi	6000	15502	2150
14	Ahmedabad	Hyderabad	7076	19532	2150
15	Ahmedabad	Mumbai	4351	13425	1850
16	Aizawl	Guwahati	5351		1850
17	Aizawl	Imphal	4466	7337	1850
18	Aizawl	Kolkata	4626	11108	1850
19	Allahabad	Delhi	6541		1850
20	Allahabad	Kanpur	4751		1850
21	Allahabad	Mumbai	9401		2950
22	Amritsar	Delhi	4366	12436	1850
23	Aurangabad	Delhi	8801	21747	2150
24	Aurangabad	Mumbai	4451	10133	1850
25	Bagdogra	Delhi	9716	21024	2950
26	Bagdogra	Guwahati	4536	8398	1850
27	Bagdogra	Kolkata	5436	13188	1850
28	Bengaluru	Chennai	4701	10691	1850
29	Bengaluru	Delhi	9901	28985	3650
30	Bengaluru	Goa	5151	13297	1850
31	Bengaluru	Hyderabad	5451	14823	1850
32	Bengaluru	Kochi	4501	10907	1850
33	Bengaluru	Kolkata	9701	30799	3650
34	Bengaluru	Mangalore	4936		1850
35	Bengaluru	Mumbai	6851	16356	2150
36	Bengaluru	Pune	6184	16506	1850
37	Bengaluru	Tirupati	5221		1850
38	Bengaluru	Trivandrum	5451	12473	1850
39	Bhopal	Delhi	4801	14856	1850
40	Bhopal	Indore	4281	8625	1850
41	Bhopal	Mumbai	5356	15560	1850
42	Bhubaneshwar	Chennai	8291	17520	2950
43	Bhubaneshwar	Delhi	9001	27424	2950
44	Bhubaneshwar	Kolkata	5406	11764	1850
45	Bhubaneshwar	Mumbai	9401	29518	2950
46	Bhubaneshwar	Port Blair	11216		2950
47	Bhuj	Mumbai	6541		1850
48	Chandigarh	Delhi	4301	10534	1850
49	Chandigarh	Mumbai	8836	26295	2950
50	Chennai	Coimbatore	4251	12166	1850
51	Chennai	Delhi	8456	28474	3650
52	Chennai	Goa	6311	16217	1850
53	Chennai	Hyderabad	4451	13732	1850
54	Chennai	Kochi	5251	14630	1850
55	Chennai	Kolkata	8566	27543	2950
56	Chennai	Madurai	4251	11801	1850
57	Chennai	Mumbai	8571	18623	2950
58	Chennai	Pune	7851	21017	2150
59	Chennai	Portblair	9411	24229	2950
60	Chennai	Trivandrum	5601	14637	1850
61	Chennai	Vishakhapatnam	4801	14673	1850
62	Coimbatore	Delhi	9751	32763	3650
63	Coimbatore	Kozhikode	4451	6739	1850
64	Coimbatore	Mumbai	8001	18331	2950
65	Dehradun	Delhi	5221	13980	1850
66	Dehli	Dharamsala	4821		1850
67	Delhi	Gaya	6851	19532	2150
68	Delhi	Goa	8821	25860	3650
69	Delhi	Guwahati	9811	24988	3650
70	Delhi	Gwalior	5051	9977	1850
71	Delhi	Hyderabad	8401	25748	2950
72	Delhi	Imphal	9081	27248	3650
73	Delhi	Indore	4851	15867	1850
74	Delhi	Jabalpur	6401		1850
75	Delhi	Jaipur	3551	8815	1850
76	Delhi	Jammu	4401	13181	1850
77	Delhi	Jodhpur	5706	14290	1850
78	Delhi	Kanpur	5701		1850
79	Delhi	Khajuraho	5651	15108	1850
80	Delhi	Kochi	12351	37731	3650
81	Delhi	Kolkata	9061	24251	2950
82	Delhi	Kozhikode	10051	32763	3650
83	Delhi	Kullu	6301		1850
84	Delhi	Leh	5501	15141	1850
85	Delhi	Lucknow	4821	12505	1850
86	Delhi	Ludhiana	4351		1850
87	Delhi	Mangalore	9901	29248	3650
88	Delhi	Mumbai	8951	22740	2950
89	Delhi	Pantnagar	4301		1850
90	Delhi	Nagpur	7171	17336	2150
91	Delhi	Pathankot	5101		1850
92	Delhi	Patna	7151	17265	2150
93	Delhi	Port Blair	21516		3650
94	Delhi	Pune	9401	28208	2950
95	Delhi	Raipur	7851	22112	2150
96	Delhi	Rajkot	9101		2150
97	Delhi	Ranchi	8811	20732	2950
98	Delhi	Srinagar	6201	13370	1850
99	Delhi	Surat	9101	20819	2150
100	Delhi	Tirupati	9016	23756	3650
101	Delhi	Trivandrum	12156	37731	3650
102	Delhi	Udaipur	5786	15382	1850
103	Delhi	Vadodra	7051	19853	2150
104	Delhi	Varanasi	5681	15327	1850
105	Delhi	Vijayawada	8566	26202	2950
106	Delhi	Vishakhapatnam	10401	30218	2950
107	Dibrugarh	Dimapur	3251	5106	1850
108	Dibrugarh	Guwahati	4801		1850
109	Dibrugarh	Kolkata	7401	14776	2150
110	Dibrugarh	Lilabari	4051		1850
111	Dimapur	Guwahati	4701		1850
112	Dimapur	Imphal	4401		1850
113	Dimapur	Kolkata	6101	13822	1850
114	Dimapur	Shillong	4101		1850
115	Gaya	Kolkata	4501	11744	1850
116	Gaya	Varanasi	4851	9518	1850
117	Goa	Kochi	5001	15159	1850
118	Goa	Hyderabad	5251	13657	1850
119	Goa	Mumbai	5321	11232	1850
120	Goa	Pune	4536	8874	1850
121	Goa	Srinagar	12351	38431	3650
122	Guwahati	Imphal	4901	9498	1850
123	Guwahati	Jorhat	3736		1850
124	Guwahati	Kolkata	5076	11465	1850
125	Guwahati	Lilabari	5151		1850
126	Guwahati	Silchar	5251		1850
127	Guwahati	Tezpur	4436		1850
128	Gwalior	Mumbai	8401	19551	2150
129	Hyderabad	Kolkata	9696	24985	2950
130	Hyderabad	Mumbai	5251	14980	1850
131	Hyderabad	Pune	5231	14265	1850
132	Hyderabad	Tirupati	4656	12571	1850
133	Hyderabad	Varanasi	8811	21806	2950
134	Hyderabad	Vijayawada	5051	10655	1850
135	Hyderabad	Vishakhapatnam	4946	12951	1850
136	Imphal	Kolkata	4281	11680	1850
137	Imphal	Silchar	4601		1850
138	Indore	Mumbai	4481	12637	1850
139	Jaipur	Mumbai	7851	18794	2150
140	Jammu	Leh	4886	9069	1850
141	Jammu	Srinagar	4403	6998	1850
142	Jamnagar	Mumbai	5181	12400	1850
143	Jodhpur	Mumbai	7686	18670	2150
144	Jodhpur	Udaipur	4231	8724	1850
145	Jorhat	Kolkata	4976		1850
146	Jorhat	Tezpur	4136		1850
147	Kanpur	Kolkata	7401		2150
148	Khajuraho	Varanasi	4936	12681	1850
149	Kochi	Kozhikode	3501	7283	1850
150	Kochi	Madurai	4301		1850
151	Kochi	Mumbai	8401	21634	2950
152	Kochi	Trivandrum	4301	8326	1850
153	Kolkata	Kochi	10051		3650
154	Kolkata	Lilabari	7800		2150
155	Kolkata	Mumbai	8486	23558	3650
156	Kolkata	Patna	5706		1850
157	Kolkata	Port Blair	11071	26781	2950
158	Kolkata	Ranchi	4536		1850
159	Kolkata	Shillong	5481		1850
160	Kolkata	Silchar	5001	11085	1850
161	Kolkata	Tezpur	5151		1850
162	Kozhikode	Chennai	5151		1850
163	Kozhikode	Kolkata	8456		3650
164	Kozhikode	Mumbai	9100	16513	2150
165	Kozhikode	Trivandrum	4391		1850
166	Kullu	Pathankot	4001		1850
167	Leh	Srinagar	4603	8283	1850
168	Lilabari	Tezpur	3881		1850
169	Lucknow	Mumbai	8051	24985	2950
170	Lucknow	Varanasi	4626	9607	1850
171	Ludhiana	Pathankot	4201		1850
172	Madurai	Mumbai	7851	23657	2950
173	Mangalore	Mumbai	6086	15761	1850
174	Mumbai	Nagpur	5001	15159	1850
175	Mumbai	Raipur	9500	20692	2150
176	Mumbai	Rajkot	5281	12473	1850
177	Mumbai	Ranchi	9751	22813	2950
178	Mumbai	Srinagar	9016	23756	3650
179	Mumbai	Trivandrum	10201	23901	2950
180	Mumbai	Udaipur	4786	15407	1850
181	Mumbai	Varanasi	9696	24511	2950
182	Mumbai	Vishakhapatnam	9101	24872	2950
183	Patna	Ranchi	4603		1850
184	PortBlair	Vishakhapatnam	9696	24511	2950
185	Raipur	Bhubaneshwar	4281	9977	1850
186	Raipur	Nagpur	5181	12660	1850
187	Raipur	Vishakhapatnam	4251	11606	1850
188	Shillong	Jorhat	4551		1850
189	Silchar	Tezpur	4101		1850
190	Tirupati	Vijayawada	5406		1850
191	Vishakhapatnam	Bhubaneshwar	5321	10518	1850
)
return
 
John ... you working ?
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: how to search for via routes when there are no direct ro

10 Apr 2015, 09:39

Still waiting for @hubert or @alphabravo
John ... you working ?
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: how to search for via routes when there are no direct ro

10 Apr 2015, 09:46

smorgasbord wrote:@AlphaBravo
;=============================================
Edit: Dibrugarh to Kolkata must be

Dibrugarh Kolkata ( 7401147762150 ) = 7401+2150

whereas it shows something different.
AlphaBravo wrote:I edited my original answer to remove the DLTC from total!
remove (x.5?x.5:0)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], CodeKiller, just me, rc76, yxldh and 206 guests