Program to find correct order of visited cities in C++

Given a list of airline tickets represented by pairs of departure and arrival airports, we need to reconstruct the correct travel itinerary. All tickets belong to a traveler who starts from KLK airport, so the itinerary must begin there.

For example, if the input is [["MUC", "LHR"], ["KLK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]], the output should be ["KLK", "MUC", "LHR", "SFO", "SJC"].

Algorithm

We'll use a depth-first search (DFS) approach with the following steps ?

  • Build a graph where each airport maps to its destinations

  • Use DFS to traverse all paths starting from "KLK"

  • Add visited airports to result in reverse order

  • Return the reversed path as the correct itinerary

Implementation

#include <bits/stdc++.h>
using namespace std;

void print_vector(vector<string> v){
    cout << "[";
    for(int i = 0; i < v.size(); i++){
        cout << v[i];
        if(i < v.size() - 1) cout << ", ";
    }
    cout << "]" << endl;
}

class Solution {
public:
    vector<string> result;
    map<string, multiset<string>> graph;
    
    vector<string> findItinerary(vector<vector<string>>& tickets) {
        // Build the graph
        for(int i = 0; i < tickets.size(); i++){
            string departure = tickets[i][0];
            string arrival = tickets[i][1];
            graph[departure].insert(arrival);
        }
        
        // Start DFS from KLK
        visit("KLK");
        
        // Reverse to get correct order
        reverse(result.begin(), result.end());
        return result;
    }
    
    void visit(string airport){
        while(graph[airport].size() > 0){
            string next = *(graph[airport].begin());
            graph[airport].erase(graph[airport].begin());
            visit(next);
        }
        result.push_back(airport);
    }
};

int main(){
    Solution solution;
    vector<vector<string>> tickets = {
        {"MUC", "LHR"}, 
        {"KLK", "MUC"}, 
        {"SFO", "SJC"}, 
        {"LHR", "SFO"}
    };
    
    vector<string> itinerary = solution.findItinerary(tickets);
    print_vector(itinerary);
    
    return 0;
}

How It Works

The algorithm uses Hierholzer's algorithm for finding Eulerian paths ?

  • Graph Construction: Each departure airport maps to a multiset of arrival airports

  • DFS Traversal: Visit destinations recursively, removing used tickets

  • Backtracking: Add airports to result when all outgoing flights are exhausted

  • Path Reconstruction: Reverse the result to get the correct travel order

Output

[KLK, MUC, LHR, SFO, SJC]

Key Points

  • Uses multiset to handle multiple tickets between same airports

  • DFS ensures all tickets are used exactly once

  • Lexicographically smallest path is automatically chosen due to multiset ordering

  • Time complexity: O(E log E) where E is the number of tickets

Conclusion

This solution efficiently reconstructs flight itineraries using DFS and graph theory. The multiset ensures correct handling of duplicate routes while maintaining lexicographical order.

Updated on: 2026-03-25T11:08:57+05:30

253 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements