{"id":165,"date":"2020-12-28T10:02:46","date_gmt":"2020-12-28T10:02:46","guid":{"rendered":"https:\/\/awan.com.np\/?p=165"},"modified":"2023-02-23T10:30:24","modified_gmt":"2023-02-23T10:30:24","slug":"flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code","status":"publish","type":"post","link":"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/","title":{"rendered":"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm"},"content":{"rendered":"\n<p>For our Algorithm and Complexity Course here at Kathmandu University, I was given this mini project to work on Airline Flight Agenda Scheduling problem using shortest path algorithm.<\/p>\n\n\n\n<p>Here in this blog I am going to explain the implementation of Dijkstra\u2019s Algorithm for creating a flight scheduling algorithm and solving the problem below, along with the Python code.<\/p>\n\n\n\n<p>And, if you are in a hurry, here is the <a href=\"https:\/\/github.com\/awanshrestha\/flight-agenda-dijkstra\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub repo link of the project<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"problem-statement-airline-flight-scheduling-algorithm\">Problem Statement &#8211; Airline Flight Scheduling Algorithm<\/h2>\n\n\n\n<p><em>A travel agent requests software for making an agenda of flights for clients. The agent has access to a data base with all airports and flights. Besides the flight number, origin airport and destination, the flights have departure and arrival time. Specifically the agent wants to determine the earliest arrival time for the destination given an origin airport and start time.<\/em><\/p>\n\n\n\n<p>A few Google search and I found out, the problem was similar to the airline flight agenda scheduling problem example given here in this <a href=\"http:\/\/www.csl.mtu.edu\/cs2321\/www\/newLectures\/30_More_Dijkstra.htm\">page<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"problem-overview-shortest-path-algorithm\">Problem Overview &#8211; Shortest Path Algorithm<\/h2>\n\n\n\n<p>This is the shortest path algorithm problem as we need to calculate the earliest arrival time, with a given start time. So, we can take the problem as a di graph where all the airports are nodes and the flights are the di edges with weight of time interval i.e. the time difference between arrival time of destination airport and departure time of source airport.<\/p>\n\n\n\n<p>Here we need to take care that while going from origin airport to destination airport, and while taking flights from one airport to another, we can take only those flights which can be caught.<\/p>\n\n\n\n<p> So, time plays an important role in this problem as we can take only the flights whose departure time from the airport is later than our arrival time at the airport.<\/p>\n\n\n\n<p>So, to solve this we use Dijkstra\u2019s Algorithm with a little modification.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"dijkstra-s-algorithm\"><strong>Dijkstra\u2019s Algorithm:<\/strong><\/h3>\n\n\n\n<p>Dijkstra\u2019s Algorithm is a shortest path algorithm used to calculate the shortest path between nodes in a graph. This algorithm was created and published by computer scientist Dr. Edsger W. Dijkstra. <\/p>\n\n\n\n<p>The algorithm exists in many variants. Dijkstra&#8217;s original algorithm was to find the shortest path between two given nodes, but a more common variant fixes a single node as the &#8220;source&#8221; node and finds shortest paths from the source to all other nodes in the graph, producing a shortest-path tree.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"dijkstra-s-algorithm-priority-queue\"><strong>Dijkstra\u2019s Algorithm Priority Queue<\/strong><\/h4>\n\n\n\n<p>In Dijkstra\u2019s algorithm, we start from a source node and initialize its distance by zero. Next, we push the source node to a priority queue with a cost equal to zero. After that, we perform multiple steps. <\/p>\n\n\n\n<p>In each step<strong>, we extract the node with the lowest cost, update its neighbors\u2019 distances, and push them to the priority queue if needed.<\/strong> <\/p>\n\n\n\n<p>Each of the neighboring nodes is inserted with its respective new cost, which is equal to the cost of the extracted node plus the edge we just passed through. We continue to visit all nodes until there are no more nodes to extract from the priority queue. Then, we return the calculated distances.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"our-algorithm-explained-dijkstra-algorithm-example-explained\">Our Algorithm Explained &#8211; Dijkstra Algorithm Example Explained<\/h3>\n\n\n\n<p>The algorithm for this problem is the slight modification of Dijkstra\u2019s Algorithm. Here we have the database about airports and the flights. There is information about flight number, origin airport and destination, the flights have departure and arrival time. So, given origin and destination airports and start time we need to make our data structure properly to keep the data and find the shortest arrival time.<\/p>\n\n\n\n<p>Here the di graph is used where the airports are the vertices and flights are di-edges with weights: departure time and arrival time. The distance should be the function of earliest arrival times at airports. Then based upon those earliest arrival time, we use a minimum priority queue for airports. <\/p>\n\n\n\n<p>For the initial origin airport let the earliest arrival time be start time and for others it will be infinity in the beginning. Now, until the queue is not empty, adjacent loop is to be formed where we can only select the flights which can be caught, and we also want the flight with minimum arrival time. <\/p>\n\n\n\n<p>So, for that we create another priority queue of flights where its departure time should be later than arrival time of another flight at the airport. And we also use another variable time, which is used to update the flight priority queue. <\/p>\n\n\n\n<p>And finally, we perform relaxation, where if the above time is less than the earliest arrival time at adjacent airport, then we change the earliest arrival time of the airport to be the time and update in the airport queue accordingly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"lets-code-dijkstra-algorithm-python-code\">Lets Code &#8211; Dijkstra Algorithm Python Code<\/h2>\n\n\n\n<p>I use VS Code and its my favorite code editor. Which one is yours?<\/p>\n\n\n<div class='bootstrap-yop yop-poll-mc'>\n\t\t\t\t\t\t\t<div class=\"basic-yop-poll-container\" style=\"background-color:#555555; border:0px; border-style:solid; border-color:#555555; border-radius:7px; padding:10px 10px;\" data-id=\"1\" data-temp=\"basic\" data-skin=\"minimal\" data-cscheme=\"black\" data-cap=\"0\" data-access=\"guest\" data-tid=\"\" data-uid=\"b5b4b68d4897e0b252203b651bc8359a\" data-pid=\"165\" data-resdet=\"votes-number,percentages\" data-show-results-to=\"guest\" data-show-results-moment=\"after-vote\" data-show-results-only=\"false\" data-show-message=\"true\" data-show-results-as=\"bar\" data-sort-results-by=\"as-defined\" data-sort-results-rule=\"asc\"data-is-ended=\"0\" data-gdpr=\"no\" data-gdpr-sol=\"consent\" data-css=\".basic-yop-poll-container[data-uid] .basic-vote { text-align: center; }\" data-counter=\"0\" data-load-with=\"1\" data-notification-section=\"top\"><div class=\"row\"><div class=\"col-md-12\"><div class=\"basic-inner\"><div class=\"basic-message hide\" style=\"border-left: 10px solid #008000; padding: 0px 10px;\" data-error=\"#ff0000\" data-success=\"#008000\"><p class=\"basic-message-text\" style=\"color:#000000; font-size:14px; font-weight:normal;\"><\/p><\/div><div class=\"basic-overlay hide\"><div class=\"basic-vote-options\"><\/div><div class=\"basic-preloader\"><div class=\"basic-windows8\"><div class=\"basic-wBall basic-wBall_1\"><div class=\"basic-wInnerBall\"><\/div><\/div><div class=\"basic-wBall basic-wBall_2\"><div class=\"basic-wInnerBall\"><\/div><\/div><div class=\"basic-wBall basic-wBall_3\"><div class=\"basic-wInnerBall\"><\/div><\/div><div class=\"basic-wBall basic-wBall_4\"><div class=\"basic-wInnerBall\"><\/div><\/div><div class=\"basic-wBall basic-wBall_5\"><div class=\"basic-wInnerBall\"><\/div><\/div><\/div><\/div><\/div><form class=\"basic-form\"><input type=\"hidden\" name=\"_token\" value=\"2c7f0a3df2\" autocomplete=\"off\"><div class=\"basic-elements\"><div class=\"basic-element basic-question basic-question-text-vertical\" data-id=\"1\" data-uid=\"b632a5a92135b02a0836ee55717abc4b\" data-type=\"question\" data-question-type=\"text\" data-allow-multiple=\"no\" data-min=\"1\" data-max=\"1\" data-display=\"vertical\" data-colnum=\"\" data-display-others=\"no\" data-others-color=\"\" data-others=\"\"><div class=\"basic-question-title\"><h5 style=\"color:#ffffff; font-size:16px; font-weight:normal; text-align:center;\">Which is your favorite code editor?<\/h5><\/div><ul class=\"basic-answers\"><li class=\"basic-answer\" style=\"padding:0px 0px;\" data-id=\"1\" data-type=\"text\" data-vn=\"34\" data-color=\"#000000\" data-make-link=\"no\" data-link=\"\"><div class=\"basic-answer-content basic-text-vertical\"><label for=\"answer[1]\" class=\"basic-answer-label\"><input type=\"radio\" id=\"answer[1]\" name=\"answer[1]\" value=\"1\"  autocomplete=\"off\"><span class=\"basic-text\" style=\"color: #ffffff; font-size: 14px; font-weight: normal;\">VS Code<\/span><\/label><\/div><\/li><li class=\"basic-answer\" style=\"padding:0px 0px;\" data-id=\"2\" data-type=\"text\" data-vn=\"2\" data-color=\"#000000\" data-make-link=\"no\" data-link=\"\"><div class=\"basic-answer-content basic-text-vertical\"><label for=\"answer[2]\" class=\"basic-answer-label\"><input type=\"radio\" id=\"answer[2]\" name=\"answer[1]\" value=\"2\"  autocomplete=\"off\"><span class=\"basic-text\" style=\"color: #ffffff; font-size: 14px; font-weight: normal;\">Vim<\/span><\/label><\/div><\/li><li class=\"basic-answer\" style=\"padding:0px 0px;\" data-id=\"3\" data-type=\"text\" data-vn=\"0\" data-color=\"#000000\" data-make-link=\"no\" data-link=\"\"><div class=\"basic-answer-content basic-text-vertical\"><label for=\"answer[3]\" class=\"basic-answer-label\"><input type=\"radio\" id=\"answer[3]\" name=\"answer[1]\" value=\"3\"  autocomplete=\"off\"><span class=\"basic-text\" style=\"color: #ffffff; font-size: 14px; font-weight: normal;\">Atom<\/span><\/label><\/div><\/li><li class=\"basic-answer\" style=\"padding:0px 0px;\" data-id=\"4\" data-type=\"text\" data-vn=\"0\" data-color=\"#000000\" data-make-link=\"no\" data-link=\"\"><div class=\"basic-answer-content basic-text-vertical\"><label for=\"answer[4]\" class=\"basic-answer-label\"><input type=\"radio\" id=\"answer[4]\" name=\"answer[1]\" value=\"4\"  autocomplete=\"off\"><span class=\"basic-text\" style=\"color: #ffffff; font-size: 14px; font-weight: normal;\">Sublime Text<\/span><\/label><\/div><\/li><li class=\"basic-answer\" style=\"padding:0px 0px;\" data-id=\"5\" data-type=\"text\" data-vn=\"1\" data-color=\"#000000\" data-make-link=\"no\" data-link=\"\"><div class=\"basic-answer-content basic-text-vertical\"><label for=\"answer[5]\" class=\"basic-answer-label\"><input type=\"radio\" id=\"answer[5]\" name=\"answer[1]\" value=\"5\"  autocomplete=\"off\"><span class=\"basic-text\" style=\"color: #ffffff; font-size: 14px; font-weight: normal;\">Notepad++<\/span><\/label><\/div><\/li><li class=\"basic-answer\" style=\"padding:0px 0px;\" data-id=\"6\" data-type=\"text\" data-vn=\"1\" data-color=\"#000000\" data-make-link=\"no\" data-link=\"\"><div class=\"basic-answer-content basic-text-vertical\"><label for=\"answer[6]\" class=\"basic-answer-label\"><input type=\"radio\" id=\"answer[6]\" name=\"answer[1]\" value=\"6\"  autocomplete=\"off\"><span class=\"basic-text\" style=\"color: #ffffff; font-size: 14px; font-weight: normal;\">Others<\/span><\/label><\/div><\/li><\/ul><\/div><div class=\"clearfix\"><\/div><\/div><div class=\"basic-vote\"><a href=\"#\" class=\"button basic-vote-button\" style=\"background:#ffffff; border:1px; border-style: solid; border-color:#000000; border-radius:0px; padding:5px 10px; color:#000000; font-size:14px; font-weight:normal;\">Vote<\/a><\/div><\/form><\/div><\/div><\/div><\/div>\n\t\t\t\t\t\t<\/div>\n\n\n\n<p>Here we create a new file <strong>flight.py<\/strong>.<\/p>\n\n\n\n<p><strong>Flight.py<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import queue\n\nclass Flight:\n    def __init__(self, name, origin, dest, arrivalT, departT):\n        self.name = name\n        self.origin = origin\n        self.dest = dest\n        self.arrivalT = arrivalT\n        self.departT = departT\n        self.weight = departT - arrivalT\n\n    def __str__(self):\n        return str(self.origin) + \" - \" + str(self.dest)\n\n    def __repr__(self):\n        return self.__str__()\n\n    def __lt__(self, other):\n        return self.arrivalT &lt; other.arrivalT\n\n\nclass Graph:\n    def __init__(self, vertices):\n        self.vertices = vertices\n\nclass Schedule:\n    def __init__(self, vertex, time):\n        self.vertex = vertex\n        self.time = time\n\n    def __hash__(self):\n        return hash(str(self.vertex) + \":\" + str(self.time))\n\n    def __lt__(self, other):\n        return self.time &lt; other.time\n\n    def __str__(self):\n        return \"{\" + str(self.vertex) + \":\" + str(self.time) + \"}\"\n\n    def __repr__(self):\n        return self.__str__()\n\n    def __eq__(self, other):\n        if isinstance(other, Schedule):\n            return (self.vertex == other.vertex) and (self.time == other.time)\n        return False\n\nclass Vertex:\n\n    def __init__(self, name, adjacentVertices):\n        self.name = name\n        self.adjacentVertices = adjacentVertices\n\n    def __str__(self):\n        return self.name\n\n    def __repr__(self):\n        return self.__str__()\n\n    def __eq__(self, other):\n        if isinstance(other, Vertex):\n            return self.adjacentVertices == other.adjacentVertices\n        return False\n\n    def __hash__(self):\n        return hash(str(self.name))\n\n\ndef FlightAgency(F, G, s, d, startT):\n\n    T = {}\n    T&#091;s] = startT\n\n    # for all vertex, v != s do T&#091;v] \u2190 infinity\n    for vertex in G.vertices:\n        if(vertex is not s):\n            T&#091;vertex] = float(\"inf\")\n\n    # Priority Queue, q, of vertices keyed by T\n    q = queue.PriorityQueue()\n    for vertex in G.vertices:\n        q.put(Schedule(vertex, T&#091;vertex]))\n\n    while not q.empty():\n        v = q.get()\n\n        for adjacentVertex in v.vertex.adjacentVertices:\n            if Schedule(adjacentVertex, T&#091;adjacentVertex]) in q.queue:\n\n                p = queue.PriorityQueue()\n                for flight in F:\n                    if(flight.origin == v.vertex and \n                    flight.dest == adjacentVertex):\n                        if(flight.departT &gt;= T&#091;v.vertex]):\n                            p.put(flight)\n\n                t = float(\"inf\")\n\n                if not p.empty():\n                    t = p.queue&#091;0].arrivalT\n\n                # relaxation\n                if t &lt; T&#091;adjacentVertex]:\n                    T&#091;adjacentVertex] = t\n                    q.put(Schedule(adjacentVertex, t))\n\n            else:\n                break\n    \n    earliestTime = T&#091;d]\n    \n    return earliestTime\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"breaking-down-the-code\">Breaking Down the Code:<\/h3>\n\n\n\n<p>Here we create some classes. First there is the class Graph, which will have vertices. Then there is Vertex class for the airport which will have the airport details. Class flight will have the flight name, origin, destination, arrival time and departure time.<\/p>\n\n\n\n<p> As we have mentioned earlier, the flight difference between the arrival and departure time will be the weights. For listing the flight schedules, we have created a Schedule class.<\/p>\n\n\n\n<p>Then there is the main function FlightAgency(), which calculates the required earliest arrival time using the algorithm above.<\/p>\n\n\n\n<p>As for the main file here, we create a new file named <strong>main.py<\/strong>.<\/p>\n\n\n\n<p><strong>main.py<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from flight import Flight, Vertex, Graph, FlightAgency\n\n# The airports as vertices\nairportE = Vertex(\"E\", &#091;])\nairportD = Vertex(\"D\", &#091;airportE])\nairportB = Vertex(\"B\", &#091;airportD, airportE])\nairportC = Vertex(\"C\", &#091;airportB])\nairportA = Vertex(\"A\", &#091;airportC, airportB])\n\ndef run():\n\n    # List the flights for airports\n    flights = &#091;\n        Flight('FN-101', airportA, airportB,  6, 2),\n        Flight('FN-102', airportA, airportC,  8, 2),\n        Flight('FN-103', airportB, airportD,  13, 12),\n        Flight('FN-104', airportB, airportE,  17, 11),\n        Flight('FN-105', airportC, airportB,  10, 9),\n        Flight('FN-106', airportC, airportD,  10, 6,),\n        Flight('FN-107', airportD, airportE,  14, 13),\n    ]\n\n    # Graph with airports as vertices\n    graph = Graph(&#091;airportA, airportB, airportC, airportD, airportE])\n\n    # Origin Airport be airportA and destination be airportE\n    startVertex = airportA\n    endVertex = airportE\n    # Start time be 2 (Time in 24 hour format)\n    startT = 2\n\n    earliestArrivalTime = FlightAgency(flights, graph, startVertex, endVertex, startT)\n    if (earliestArrivalTime != float(\"inf\")):\n        print(f'The earliest arrival time for the airport {endVertex} from airport {startVertex} is {earliestArrivalTime}:00.')\n    else:\n        print(f'No flight from airport {startVertex} to airport {endVertex} after {startT}:00.')\n\nif __name__ == '__main__':\n    run()\n<\/code><\/pre>\n\n\n\n<p>So, for our main program we made following setup:<\/p>\n\n\n\n<ul>\n<li>Airport A, B, C, D, E as the vertices of the Graph G.<\/li>\n\n\n\n<li>The list of 7 flights:\n<ul>\n<li>FN-101: From airport A to B with departure time 02:00 and arrival time 06:00<\/li>\n\n\n\n<li>FN-102: From airport A to C with departure time 02:00 and arrival time 08:00<\/li>\n\n\n\n<li>FN-103: From airport B to D with departure time 12:00 and arrival time 13:00<\/li>\n\n\n\n<li>FN-104: From airport B to E with departure time 11:00 and arrival time 17:00<\/li>\n\n\n\n<li>FN-105: From airport C to B with departure time 09:00 and arrival time 10:00<\/li>\n\n\n\n<li>FN-106: From airport C to D with departure time 06:00 and arrival time 10:00<\/li>\n\n\n\n<li>FN-107: From airport D to E with departure time 13:00 and arrival time 14:00<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul>\n<li>The flights are the edges.<\/li>\n\n\n\n<li>Airport A as the origin airport.<\/li>\n\n\n\n<li>Airport E as the destination airport.<\/li>\n\n\n\n<li>02:00 as the start time.<\/li>\n\n\n\n<li>Finally, we call the FlightAgency function.<\/li>\n<\/ul>\n\n\n\n<p>We will get the result that,<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"69\" src=\"https:\/\/awan.com.np\/wp-content\/uploads\/2023\/02\/Picture1.jpg\" alt=\"Flight Application\" class=\"wp-image-167\" srcset=\"https:\/\/awan.com.np\/wp-content\/uploads\/2023\/02\/Picture1.jpg 500w, https:\/\/awan.com.np\/wp-content\/uploads\/2023\/02\/Picture1-300x41.jpg 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<p>So, starting at 02:00 from airport A the shortest path to reach airport E, the earliest arrival time is 14:00.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"unit-test-dijkstra-test-cases\">Unit Test &#8211; Dijkstra Test Cases<\/h3>\n\n\n\n<p>Here is a test case. Unittest library is used for test case. We use the function like test_flightagency to test the algorithms over test cases. For test case, we create a very simple graph and check for different cases like going to adjacent and non-adjacent airports, try to go to airport with flight missed, going to airport with no available flights, etc.<\/p>\n\n\n\n<p><strong>testFlight.py<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import unittest\nfrom flight import Flight, Vertex, Graph, FlightAgency\n\nclass TestFlight(unittest.TestCase):\n\n    def setUp(self):\n        self.airportE = Vertex(\"E\", &#091;])\n        self.airportD = Vertex(\"D\", &#091;self.airportE])\n        self.airportB = Vertex(\"B\", &#091;self.airportD])\n        self.airportC = Vertex(\"C\", &#091;self.airportB])\n        self.airportA = Vertex(\"A\", &#091;self.airportB, self.airportC, self.airportE])\n\n        self.flights = &#091;\n            Flight('1', self.airportA, self.airportB,  2, 1),\n            Flight('2', self.airportA, self.airportC,  4, 3),\n            Flight('3', self.airportA, self.airportE,  12, 1),\n            Flight('4', self.airportB, self.airportD,  6, 4),\n            Flight('5', self.airportC, self.airportB,  5, 3),\n            Flight('6', self.airportD, self.airportE,  8, 6),\n        ]\n\n        self.graph = Graph(&#091;self.airportA, self.airportB, self.airportC, self.airportD, self.airportE])\n\n    def test_flightagency(self):\n        # From A to B\n        self.assertEqual( FlightAgency(self.flights, self.graph, self.airportA, self.airportB, 1), 2 )\n        # From A to B after flight missed\n        self.assertEqual( FlightAgency(self.flights, self.graph, self.airportA, self.airportB, 5), float('inf') )\n        # From B to A\n        self.assertEqual( FlightAgency(self.flights, self.graph, self.airportB, self.airportA, 1), float('inf') )\n\n        # From A to C\n        self.assertEqual( FlightAgency(self.flights, self.graph, self.airportA, self.airportC, 1), 4 )\n\n        # To E\n        self.assertEqual( FlightAgency(self.flights, self.graph, self.airportA, self.airportE, 1), 8 )\n        self.assertEqual( FlightAgency(self.flights, self.graph, self.airportB, self.airportE, 1), 8 )\n\nif __name__ == \"__main__\":\n    unittest.main()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"analysis-dijkstra-algorithm-example\">Analysis &#8211;  Dijkstra Algorithm Example<\/h2>\n\n\n\n<p>Based upon the setup as in the main file, we would have got a graph like below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-rounded\"><img loading=\"lazy\" decoding=\"async\" width=\"582\" height=\"325\" src=\"https:\/\/awan.com.np\/wp-content\/uploads\/2023\/02\/image.png\" alt=\"airline flight agenda scheduling problem chart\" class=\"wp-image-168\" srcset=\"https:\/\/awan.com.np\/wp-content\/uploads\/2023\/02\/image.png 582w, https:\/\/awan.com.np\/wp-content\/uploads\/2023\/02\/image-300x168.png 300w\" sizes=\"(max-width: 582px) 100vw, 582px\" \/><\/figure>\n\n\n\n<p> <a href=\"https:\/\/awan.com.np\/easiest-way-for-javascript-network-graph-visualization-vis-js\/\">Easiest way for JavaScript Network Graph Visualization<\/a> <\/p>\n\n\n\n<p>In above graph, there is a flight that leaves from airport A at 2 and reaches C at 8 and another flight leaves airport A at 2 and reach airport B at 6 and so on.<\/p>\n\n\n\n<p>From airport A, we can go to airport B and C. The best possible arrival time for airport B and C would be 6 and 8 respectively. <\/p>\n\n\n\n<p>The flight from C to D departs at 6. So, starting at time 2 and reaching airport C at 8, there is no way we can catch that flight. So, from C, now we can only go to B. We can reach airport B either from A or from C, but the earliest arrival time would be from airport A at 6. <\/p>\n\n\n\n<p>Similarly, from B we can go to D or E and so on. So, finally using the algorithm the best possible shortest pathway we can reach airport E from airport A would be via route A \u2013 B \u2013 D \u2013 E.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-rounded\"><img loading=\"lazy\" decoding=\"async\" width=\"537\" height=\"342\" src=\"https:\/\/awan.com.np\/wp-content\/uploads\/2023\/02\/image-1.png\" alt=\"graph 2\" class=\"wp-image-169\" srcset=\"https:\/\/awan.com.np\/wp-content\/uploads\/2023\/02\/image-1.png 537w, https:\/\/awan.com.np\/wp-content\/uploads\/2023\/02\/image-1-300x191.png 300w\" sizes=\"(max-width: 537px) 100vw, 537px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/awan.com.np\/easiest-way-for-javascript-network-graph-visualization-vis-js\/\">Easiest way for JavaScript Network Graph Visualization<\/a><\/p>\n\n\n\n<p>And, using those flights, we would reach airport E at 14, which is the answer given by our program.<\/p>\n\n\n\n<p>In this way we have implemented the Dijkstra&#8217;s Algorithm for  airline flight agenda scheduling problem  .<\/p>\n\n\n\n<p><em>Due to the device width issue, for some devices the code lines above might have been altered and indentation might have been affected.<\/em><\/p>\n\n\n\n<p>In such a case, the code is available in my <a href=\"https:\/\/github.com\/awanshrestha\/flight-agenda-dijkstra\">GitHub<\/a>.<\/p>\n\n\n<div class='bootstrap-yop yop-poll-mc'>\n\t\t\t\t\t\t\t<div class=\"basic-yop-poll-container\" style=\"background-color:#555555; border:0px; border-style:solid; border-color:#555555; border-radius:7px; padding:10px 10px;\" data-id=\"2\" data-temp=\"basic\" data-skin=\"minimal\" data-cscheme=\"black\" data-cap=\"0\" data-access=\"guest\" data-tid=\"\" data-uid=\"b67e11b30f6e11ca372a0552078fdb8d\" data-pid=\"165\" data-resdet=\"votes-number,percentages\" data-show-results-to=\"guest\" data-show-results-moment=\"after-vote\" data-show-results-only=\"false\" data-show-message=\"true\" data-show-results-as=\"bar\" data-sort-results-by=\"as-defined\" data-sort-results-rule=\"asc\"data-is-ended=\"0\" data-gdpr=\"no\" data-gdpr-sol=\"consent\" data-css=\".basic-yop-poll-container[data-uid] .basic-vote { text-align: center; }\" data-counter=\"0\" data-load-with=\"1\" data-notification-section=\"top\"><div class=\"row\"><div class=\"col-md-12\"><div class=\"basic-inner\"><div class=\"basic-message hide\" style=\"border-left: 10px solid #008000; padding: 0px 10px;\" data-error=\"#ff0000\" data-success=\"#008000\"><p class=\"basic-message-text\" style=\"color:#000000; font-size:14px; font-weight:normal;\"><\/p><\/div><div class=\"basic-overlay hide\"><div class=\"basic-vote-options\"><\/div><div class=\"basic-preloader\"><div class=\"basic-windows8\"><div class=\"basic-wBall basic-wBall_1\"><div class=\"basic-wInnerBall\"><\/div><\/div><div class=\"basic-wBall basic-wBall_2\"><div class=\"basic-wInnerBall\"><\/div><\/div><div class=\"basic-wBall basic-wBall_3\"><div class=\"basic-wInnerBall\"><\/div><\/div><div class=\"basic-wBall basic-wBall_4\"><div class=\"basic-wInnerBall\"><\/div><\/div><div class=\"basic-wBall basic-wBall_5\"><div class=\"basic-wInnerBall\"><\/div><\/div><\/div><\/div><\/div><form class=\"basic-form\"><input type=\"hidden\" name=\"_token\" value=\"d7838230f0\" autocomplete=\"off\"><div class=\"basic-elements\"><div class=\"basic-element basic-question basic-question-text-vertical\" data-id=\"2\" data-uid=\"c21464b15ed54ddea344f3ab6e59eb61\" data-type=\"question\" data-question-type=\"text\" data-allow-multiple=\"no\" data-min=\"1\" data-max=\"1\" data-display=\"vertical\" data-colnum=\"\" data-display-others=\"no\" data-others-color=\"\" data-others=\"\"><div class=\"basic-question-title\"><h5 style=\"color:#ffffff; font-size:16px; font-weight:normal; text-align:center;\">Was this blog post helpful?<\/h5><\/div><ul class=\"basic-answers\"><li class=\"basic-answer\" style=\"padding:0px 0px;\" data-id=\"7\" data-type=\"text\" data-vn=\"18\" data-color=\"#000000\" data-make-link=\"no\" data-link=\"\"><div class=\"basic-answer-content basic-text-vertical\"><label for=\"answer[7]\" class=\"basic-answer-label\"><input type=\"radio\" id=\"answer[7]\" name=\"answer[2]\" value=\"7\"  autocomplete=\"off\"><span class=\"basic-text\" style=\"color: #ffffff; font-size: 14px; font-weight: normal;\">Yes<\/span><\/label><\/div><\/li><li class=\"basic-answer\" style=\"padding:0px 0px;\" data-id=\"8\" data-type=\"text\" data-vn=\"17\" data-color=\"#000000\" data-make-link=\"no\" data-link=\"\"><div class=\"basic-answer-content basic-text-vertical\"><label for=\"answer[8]\" class=\"basic-answer-label\"><input type=\"radio\" id=\"answer[8]\" name=\"answer[2]\" value=\"8\"  autocomplete=\"off\"><span class=\"basic-text\" style=\"color: #ffffff; font-size: 14px; font-weight: normal;\">No<\/span><\/label><\/div><\/li><li class=\"basic-answer\" style=\"padding:0px 0px;\" data-id=\"9\" data-type=\"text\" data-vn=\"16\" data-color=\"#000000\" data-make-link=\"no\" data-link=\"\"><div class=\"basic-answer-content basic-text-vertical\"><label for=\"answer[9]\" class=\"basic-answer-label\"><input type=\"radio\" id=\"answer[9]\" name=\"answer[2]\" value=\"9\"  autocomplete=\"off\"><span class=\"basic-text\" style=\"color: #ffffff; font-size: 14px; font-weight: normal;\">Still Working on it<\/span><\/label><\/div><\/li><\/ul><\/div><div class=\"clearfix\"><\/div><\/div><div class=\"basic-vote\"><a href=\"#\" class=\"button basic-vote-button\" style=\"background:#ffffff; border:1px; border-style: solid; border-color:#000000; border-radius:0px; padding:5px 10px; color:#000000; font-size:14px; font-weight:normal;\">Vote<\/a><\/div><\/form><\/div><\/div><\/div><\/div>\n\t\t\t\t\t\t<\/div>\n\n\n\n<p>If this was helpful, how about <a href=\"https:\/\/www.buymeacoffee.com\/awanshrestha\">buying me a coffee<\/a> ?<a href=\"https:\/\/www.buymeacoffee.com\/awanshrestha\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><a href=\"https:\/\/www.buymeacoffee.com\/awanshrestha\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/p>\n\n\n\n<a href=\"https:\/\/www.buymeacoffee.com\/awanshrestha\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/cdn.buymeacoffee.com\/buttons\/v2\/default-yellow.png\" alt=\"Buy Me A Coffee\" style=\"height: 60px !important;width: 217px !important;\"><\/a>\n","protected":false},"excerpt":{"rendered":"<p>This blog is about the Flight Scheduling Agenda Problem using Dijkstra\u2019s Algorithm to find out the earliest arrival time of a flight.<\/p>\n","protected":false},"author":1,"featured_media":166,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[11],"tags":[13,12],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm<\/title>\n<meta name=\"description\" content=\"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm to find earliest arrival time - Dijkstra Algorithm Example Python Code\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm\" \/>\n<meta property=\"og:description\" content=\"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm to find earliest arrival time - Dijkstra Algorithm Example Python Code\" \/>\n<meta property=\"og:url\" content=\"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/\" \/>\n<meta property=\"og:site_name\" content=\"Awan\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-28T10:02:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-23T10:30:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/awan.com.np\/wp-content\/uploads\/2023\/02\/Flight-problem-dijkstra.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"640\" \/>\n\t<meta property=\"og:image:height\" content=\"426\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Awan Shrestha\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Awan Shrestha\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/\"},\"author\":{\"name\":\"Awan Shrestha\",\"@id\":\"https:\/\/awan.com.np\/#\/schema\/person\/3b8b58d0614e69f915a6d64db44c1b73\"},\"headline\":\"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm\",\"datePublished\":\"2020-12-28T10:02:46+00:00\",\"dateModified\":\"2023-02-23T10:30:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/\"},\"wordCount\":1443,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/awan.com.np\/#\/schema\/person\/3b8b58d0614e69f915a6d64db44c1b73\"},\"keywords\":[\"Dijkstra&#039;s Algorithm\",\"Flight Simulation\"],\"articleSection\":[\"Algorithms\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/\",\"url\":\"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/\",\"name\":\"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm\",\"isPartOf\":{\"@id\":\"https:\/\/awan.com.np\/#website\"},\"datePublished\":\"2020-12-28T10:02:46+00:00\",\"dateModified\":\"2023-02-23T10:30:24+00:00\",\"description\":\"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm to find earliest arrival time - Dijkstra Algorithm Example Python Code\",\"breadcrumb\":{\"@id\":\"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/awan.com.np\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/awan.com.np\/#website\",\"url\":\"https:\/\/awan.com.np\/\",\"name\":\"Awan\",\"description\":\"Learn - Create - Contribute\",\"publisher\":{\"@id\":\"https:\/\/awan.com.np\/#\/schema\/person\/3b8b58d0614e69f915a6d64db44c1b73\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/awan.com.np\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/awan.com.np\/#\/schema\/person\/3b8b58d0614e69f915a6d64db44c1b73\",\"name\":\"Awan Shrestha\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/awan.com.np\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/awan.com.np\/wp-content\/uploads\/2023\/02\/Untitled-design.png\",\"contentUrl\":\"https:\/\/awan.com.np\/wp-content\/uploads\/2023\/02\/Untitled-design.png\",\"width\":366,\"height\":366,\"caption\":\"Awan Shrestha\"},\"logo\":{\"@id\":\"https:\/\/awan.com.np\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/awan.com.np\"],\"url\":\"https:\/\/awan.com.np\/author\/awan\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm","description":"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm to find earliest arrival time - Dijkstra Algorithm Example Python Code","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/","og_locale":"en_US","og_type":"article","og_title":"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm","og_description":"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm to find earliest arrival time - Dijkstra Algorithm Example Python Code","og_url":"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/","og_site_name":"Awan","article_published_time":"2020-12-28T10:02:46+00:00","article_modified_time":"2023-02-23T10:30:24+00:00","og_image":[{"width":640,"height":426,"url":"https:\/\/awan.com.np\/wp-content\/uploads\/2023\/02\/Flight-problem-dijkstra.jpg","type":"image\/jpeg"}],"author":"Awan Shrestha","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Awan Shrestha","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/#article","isPartOf":{"@id":"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/"},"author":{"name":"Awan Shrestha","@id":"https:\/\/awan.com.np\/#\/schema\/person\/3b8b58d0614e69f915a6d64db44c1b73"},"headline":"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm","datePublished":"2020-12-28T10:02:46+00:00","dateModified":"2023-02-23T10:30:24+00:00","mainEntityOfPage":{"@id":"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/"},"wordCount":1443,"commentCount":1,"publisher":{"@id":"https:\/\/awan.com.np\/#\/schema\/person\/3b8b58d0614e69f915a6d64db44c1b73"},"keywords":["Dijkstra&#039;s Algorithm","Flight Simulation"],"articleSection":["Algorithms"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/","url":"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/","name":"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm","isPartOf":{"@id":"https:\/\/awan.com.np\/#website"},"datePublished":"2020-12-28T10:02:46+00:00","dateModified":"2023-02-23T10:30:24+00:00","description":"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm to find earliest arrival time - Dijkstra Algorithm Example Python Code","breadcrumb":{"@id":"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/awan.com.np\/flight-scheduling-agenda-problem-using-dijkstras-algorithm-with-python-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/awan.com.np\/"},{"@type":"ListItem","position":2,"name":"Airline Flight Agenda Scheduling Problem using Dijkstra\u2019s Algorithm"}]},{"@type":"WebSite","@id":"https:\/\/awan.com.np\/#website","url":"https:\/\/awan.com.np\/","name":"Awan","description":"Learn - Create - Contribute","publisher":{"@id":"https:\/\/awan.com.np\/#\/schema\/person\/3b8b58d0614e69f915a6d64db44c1b73"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/awan.com.np\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/awan.com.np\/#\/schema\/person\/3b8b58d0614e69f915a6d64db44c1b73","name":"Awan Shrestha","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/awan.com.np\/#\/schema\/person\/image\/","url":"https:\/\/awan.com.np\/wp-content\/uploads\/2023\/02\/Untitled-design.png","contentUrl":"https:\/\/awan.com.np\/wp-content\/uploads\/2023\/02\/Untitled-design.png","width":366,"height":366,"caption":"Awan Shrestha"},"logo":{"@id":"https:\/\/awan.com.np\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/awan.com.np"],"url":"https:\/\/awan.com.np\/author\/awan\/"}]}},"_links":{"self":[{"href":"https:\/\/awan.com.np\/wp-json\/wp\/v2\/posts\/165"}],"collection":[{"href":"https:\/\/awan.com.np\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/awan.com.np\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/awan.com.np\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/awan.com.np\/wp-json\/wp\/v2\/comments?post=165"}],"version-history":[{"count":1,"href":"https:\/\/awan.com.np\/wp-json\/wp\/v2\/posts\/165\/revisions"}],"predecessor-version":[{"id":170,"href":"https:\/\/awan.com.np\/wp-json\/wp\/v2\/posts\/165\/revisions\/170"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/awan.com.np\/wp-json\/wp\/v2\/media\/166"}],"wp:attachment":[{"href":"https:\/\/awan.com.np\/wp-json\/wp\/v2\/media?parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/awan.com.np\/wp-json\/wp\/v2\/categories?post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/awan.com.np\/wp-json\/wp\/v2\/tags?post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}