Viloyat tumanlari va shaharlari
|
Samarkand sh.
|
Bulung’ur
|
Jomboy
|
Ishtixon
|
Kattako’rg’an
|
Qo’shrabot
|
Narpay
|
Nurobod
|
Oqdaryo
|
PayariQ
|
Pastdarg’om
|
Paxtachi
|
Samarqand
|
Tayloq
|
Urgut
|
1
|
Samarkand sh.
|
|
37
|
19
|
61
|
70
|
95
|
104
|
75
|
35
|
34
|
29
|
124
|
12
|
15
|
43
|
2
|
Bulung’ur
|
37
|
|
23
|
92
|
106
|
110
|
145
|
11
|
65
|
47
|
66
|
166
|
43
|
38
|
56
|
3
|
Jomboy
|
19
|
23
|
|
74
|
88
|
115
|
128
|
94
|
49
|
43
|
48
|
146
|
30
|
19
|
46
|
4
|
Ishtixon
|
61
|
92
|
74
|
|
23
|
40
|
54
|
57
|
25
|
37
|
47
|
74
|
72
|
75
|
101
|
5
|
Kattaqo’rgan
|
70
|
106
|
88
|
23
|
|
65
|
34
|
57
|
48
|
61
|
44
|
55
|
90
|
93
|
117
|
6
|
Qushrabot
|
95
|
110
|
115
|
40
|
65
|
|
95
|
170
|
65
|
38
|
115
|
118
|
107
|
110
|
135
|
7
|
Narpay
|
104
|
145
|
128
|
54
|
34
|
95
|
|
86
|
79
|
91
|
78
|
27
|
122
|
125
|
150
|
8
|
Nurobod
|
75
|
111
|
94
|
57
|
53
|
170
|
86
|
|
70
|
95
|
55
|
107
|
87
|
90
|
107
|
9
|
Oqkdaryo
|
35
|
65
|
49
|
25
|
48
|
65
|
79
|
70
|
|
40
|
47
|
99
|
47
|
50
|
75
|
10
|
Payariq
|
34
|
47
|
43
|
37
|
61
|
38
|
91
|
95
|
40
|
|
50
|
112
|
52
|
55
|
74
|
11
|
Pastdarg’om
|
29
|
66
|
48
|
47
|
44
|
115
|
78
|
55
|
47
|
50
|
|
99
|
32
|
35
|
76
|
12
|
Paxtachi
|
124
|
166
|
146
|
74
|
55
|
118
|
27
|
107
|
99
|
112
|
99
|
|
147
|
150
|
166
|
13
|
Samarqand
|
12
|
43
|
30
|
72
|
90
|
107
|
122
|
87
|
47
|
52
|
32
|
147
|
|
18
|
52
|
14
|
Tayloq
|
15
|
38
|
19
|
75
|
73
|
110
|
125
|
90
|
50
|
55
|
35
|
150
|
18
|
|
27
|
15
|
Urgut
|
43
|
56
|
46
|
101
|
117
|
135
|
150
|
107
|
75
|
74
|
76
|
166
|
52
|
27
|
|
2. O’zbekiston Respublikasi viloyatlarini bog’lovchi transport harakati mavjud (1.3-jadval). 1.3-jadvaldan foydalanib viloyatlarni bog’lovchi transport harakati marshrutini kengligi va chuqurligi bo’yicha izlash algoritmi yordamida toping va graf ko’rinishda tasvirlang.
Dastur kodi:
from collections import defaultdict
class Region():
def __init__(self):
self.edges = defaultdict(list)
self.weights = {}
def add_edge(self, from_node, to_node, weight):
# Note: assumes edges are bi-directional
self.edges[from_node].append(to_node)
self.edges[to_node].append(from_node)
self.weights[(from_node, to_node)] = weight
self.weights[(to_node, from_node)] = weight
graph = Region()
edges = [
('Kitob', 'Chiroqchi', 42),
('Kitob', 'Shaxrisabz', 23),
('Kitob', 'Yakkabog', 36),
('Shaxrisabz', 'Muborak', 45),
('Shaxrisabz', 'Qamashi', 37),
('Muborak', 'Guzor', 46),
('Muborak', 'Qamashi', 50),
('Nishon', 'Dehqonobod', 80),
('Nishon', 'Qarshi', 42),
('Qarshi', 'kukdala', 46),
('Qarshi', 'Guzor', 89),
('Guzor', 'Nishon', 67),
('Guzor', 'Muborak', 25),
('Dehqonobod', 'Chiroqchi', 19),
('Dehqonobod', 'Guzor', 46),
('Kukdala', 'Kitob', 39),
('Kasbi', 'Yakkabog', 41),
('Kasbi', 'Kitob', 57),
]
for edge in edges:
graph.add_edge(*edge)
def dijsktra(graph, initial, end):
# shortest paths is a dict of nodes
# whose value is a tuple of (previous node, weight)
shortest_paths = {initial: (None, 0)}
current_node = initial
visited = set()
while current_node != end:
visited.add(current_node)
destinations = graph.edges[current_node]
weight_to_current_node = shortest_paths[current_node][1]
for next_node in destinations:
weight = graph.weights[(current_node, next_node)] + weight_to_current_node
if next_node not in shortest_paths:
shortest_paths[next_node] = (current_node, weight)
else:
current_shortest_weight = shortest_paths[next_node][1]
if current_shortest_weight > weight:
shortest_paths[next_node] = (current_node, weight)
next_destinations = {node: shortest_paths[node] for node in shortest_paths if node not in visited}
if not next_destinations:
return "Route Not Possible"
# next node is the destination with the lowest weight
current_node = min(next_destinations, key=lambda k: next_destinations[k][1])
# Work back through destinations in shortest path
path = []
while current_node is not None:
path.append(current_node)
next_node = shortest_paths[current_node][0]
current_node = next_node
# Reverse path
path = path[::-1]
return path
print(dijsktra(graph, input(""), input("")))
Do'stlaringiz bilan baham: |