-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_short_route.go
140 lines (120 loc) · 2.97 KB
/
find_short_route.go
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
// two cities in file(csv), with distance
// take a city name,
// []A , B , Distance
package main
import (
"fmt"
"os"
)
type Graph struct {
From string
TO string
Distance int
}
var GeoGraph []Graph
// check with map data
type GeoGraphMap map[string]map[string]int
func (g GeoGraphMap) set(city string, ip Graph) {
data, ok := g[city]
if !ok {
g[city] = map[string]int{
ip.TO: ip.Distance,
}
return
}
data[ip.TO] = ip.Distance
g[city] = data
}
func main() {
graphData := make(GeoGraphMap)
for _, d := range GeoGraph {
graphData.set(d.From, d)
}
// check is connected
// {mumbia: 550, ahjvs:22, as}
aruguments := os.Args
city1, city2 := aruguments[1], aruguments[2]
availablePaths, ok := graphData[city1]
if !ok {
panic("invalid city1")
}
// isDirectlyConnected := false
if distance, ok := availablePaths[city2]; ok {
fmt.Println(city1, " is connected directly to ", city2, " distance is: ", distance)
return
}
// loop over to all the possible paths untill to reach city2, recursive one!
// A ->
// 1. paths, a, b, T
// ------
// 2. paths, a, b, c,... T
isConnected := false
connectedPath := make(map[string]int)
for nextCity, distance := range availablePaths {
availablePaths, _ := graphData[nextCity]
// 1. check if target is connected directly
if dis, ok := availablePaths[city2]; ok {
isConnected = true
connectedPath[nextCity] = distance
connectedPath[city2] = dis
fmt.Println(city1, " is connected to ", city2, "via ", nextCity, " distance is: ", distance+dis)
return
}
}
if isConnected {
// calculate the distance...
return
}
// 2. check from the all the available paths
// TODO: to be checked/validate this logic
for nextCity, _ := range availablePaths {
nextCityAvailablePaths, _ := graphData[nextCity]
// A
for nextCity, distance := range nextCityAvailablePaths {
// somthing here...
// 1. check if target is connected directly
if dis, ok := availablePaths[city2]; ok {
isConnected = true
connectedPath[nextCity] = distance
connectedPath[city2] = dis
fmt.Println(city1, " is connected directly to ", city2, " distance is: ", distance)
return
}
}
}
}
//
// func isconnected(nextCity string) {
// for {
// isConnected = false && !isLast {
// isconnected(nextCityA)
// }
// // wif
// if isConnected{
// }
// }
// }
func init() {
GeoGraph = []Graph{
{"mumbai", "delhi", 1421},
{"mumbai", "bangalore", 982},
{"pune", "bangalore", 840},
{"pune", "guwahati", 2620},
{"bangalore", "panaji", 595},
{"bangalore", "jammu", 2761},
{"srinagar", "jammu", 264},
{"srinagar", "guwahati", 2747},
{"kolkata", "guwahati", 1046},
{"kolkata", "bangalore", 1881},
{"panaji", "kochi", 779},
{"kochi", "chennai", 692},
{"chennai", "srinagar", 3020},
{"srinagar", "leh", 418},
{"kolkata", "leh", 2492},
{"delhi", "chandigarh", 244},
{"chandigarh", "leh", 724},
{"panaji", "indore", 1026},
{"indore", "bhopal", 1510},
{"bhopal", "bangalore", 791},
}
}