Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find out the length between two cities in shortcuts in Python
Finding the shortest path using shortcuts between cities is a graph theory problem. We have highways connecting some cities, and shortcuts exist between cities that are not directly connected by highways. Our goal is to find the minimum distances using only shortcuts from a starting city to all other cities.
Problem Understanding
Given a graph where edges represent highways, shortcuts exist between vertices that are not connected by highways. We need to find shortest paths using only these shortcuts ?
In this example, there's a shortcut between cities 3 and 4 since they're not connected by a highway.
Algorithm Steps
We use a BFS-like approach to find shortest distances using shortcuts ?
- Create an adjacency list for the highway graph
- Initialize distance array and visited sets
- For each level, find cities reachable via shortcuts
- Continue until all reachable cities are processed
Implementation
def solve(n, edges, start):
# Create adjacency list for highways
graph = [set() for i in range(n)]
for x, y in edges:
x -= 1 # Convert to 0-indexed
y -= 1
graph[x].add(y)
graph[y].add(x)
# Initialize distances array
distances = [-1] * n
current_level = {start - 1} # Start city (0-indexed)
remaining = set(range(n)) - current_level
distance = 0
while current_level:
# Mark current level cities with current distance
for city in current_level:
distances[city] = distance
# Find next level cities (shortcuts from current level)
next_level = set()
for city in remaining:
# City has shortcut if not connected to any city in current level
if not any(city in graph[curr] for curr in current_level):
next_level.add(city)
# Update for next iteration
remaining -= next_level
current_level = next_level
distance += 1
# Return distances for cities other than start city
result = [str(distances[i]) for i in range(n) if distances[i] > 0]
return ' '.join(result)
# Test the function
n = 4
edges = [(1, 2), (2, 3), (1, 4)]
start = 1
print(solve(n, edges, start))
The output shows the minimum shortcut distances from city 1 to other cities ?
3 1 2
How It Works
The algorithm processes cities level by level ?
| Level | Cities | Distance | Explanation |
|---|---|---|---|
| 0 | 1 | 0 | Starting city |
| 1 | 3 | 1 | Shortcut from 1 (not connected by highway) |
| 2 | 4 | 2 | Shortcut from 3 (not connected by highway) |
| 3 | 2 | 3 | Shortcut from 4 (not connected by highway) |
Path Analysis
From the example, the shortest shortcut paths are ?
- 1 ? 3: Direct shortcut (distance 1)
- 1 ? 4: Via shortcut 1?3?4 (distance 2)
- 1 ? 2: Via shortcuts 1?3?4?2 (distance 3)
Conclusion
This algorithm finds minimum distances using shortcuts by treating non-highway connections as edges in a complement graph. The BFS approach ensures we find the shortest paths efficiently in O(n²) time complexity.
